Deal cards for FreeCell: Difference between revisions

From Rosetta Code
Content added Content deleted
(Save the page again.)
No edit summary
Line 27: Line 27:


Deals can be checked against [http://freecellgamesolutions.com/ FreeCell solutions to 1000000 games]. (Summon a video solution, and it displays the initial deal.)
Deals can be checked against [http://freecellgamesolutions.com/ FreeCell solutions to 1000000 games]. (Summon a video solution, and it displays the initial deal.)

=={{header|C}}==
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

wchar_t s_suits[] = L"♣♦♥♠", s_nums[] = L"A23456789TJQK";

#define RMAX32 ((1U << 31) - 1)
#define RMAX ((1U << 15) - 1)
static int seed = 1;
int rnd(void) { return (seed = (seed * 214013 + 2531011) & RMAX32) >> 16; }
void srnd(int x) { seed = x; }

int card[52];
void show(void)
{
int i;
for (i = 0; i < 52; i ++)
printf("%lc%lc%s",
s_suits[card[i] % 4], s_nums[card[i] / 4],
((i + 1) % 8) ? " " : "\n");
putchar('\n');
}

void deal(int s)
{
int i, j, n, t[52];
for (i = 0; i < 52; i++) t[i] = i;

srnd(s);
for (j = 0, n = 52; n; ) {
i = rnd() % n--;
card[j++] = t[i];
t[i] = t[n];
}
printf("Deal %d\n", s);
show();
}

int main(int c, char **v)
{
int s;

setlocale(LC_ALL, "");

if (c < 2 || (s = atoi(v[1])) <= 0) s = 11982;

deal(s);
return 0;
}</lang>

Revision as of 00:52, 19 September 2011

Deal cards for FreeCell is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Free Cell is the solitaire card game that Paul Alfille introduced to the PLATO system in 1978. Jim Horne, at Microsoft, changed the name to FreeCell and reimplemented the game for DOS, then Windows. This version introduced 32000 numbered deals. Later versions have 1 million deals, numbered 1 to 1000000. (The Freecell FAQ tells this history.)

As the game became popular, Jim Horne disclosed the algorithm, and other implementations of FreeCell began to reproduce the Microsoft deals. These deals are numbered from 1 to 32000.

The algorithm uses this linear congruential generator from Microsoft C:

  • is in range 0 to 32767.
  • Rosetta Code has another task, linear congruential generator, with code for this RNG in several languages.

The algorithm follows:

  1. Seed the RNG with the number of the deal.
  2. Create an array of 52 cards: Ace of Clubs, Ace of Diamonds, Ace of Hearts, Ace of Spades, 2 of Clubs, 2 of Diamonds, and so on through the ranks: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King. The array indexes are 0 to 51, with Ace of Clubs at 0, and King of Spades at 51.
  3. Perform a shuffle (MISSING instructions)
  4. Deal all 52 cards, face up, across 8 columns. The first 8 cards go in 8 columns, the next 8 cards go on the first 8 cards, and so on.
 1  2  3  4  5  6  7  8
 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52

Deals can be checked against FreeCell solutions to 1000000 games. (Summon a video solution, and it displays the initial deal.)

C

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <locale.h>

wchar_t s_suits[] = L"♣♦♥♠", s_nums[] = L"A23456789TJQK";

  1. define RMAX32 ((1U << 31) - 1)
  2. define RMAX ((1U << 15) - 1)

static int seed = 1; int rnd(void) { return (seed = (seed * 214013 + 2531011) & RMAX32) >> 16; } void srnd(int x) { seed = x; }

int card[52]; void show(void) { int i; for (i = 0; i < 52; i ++) printf("%lc%lc%s", s_suits[card[i] % 4], s_nums[card[i] / 4], ((i + 1) % 8) ? " " : "\n"); putchar('\n'); }

void deal(int s) { int i, j, n, t[52]; for (i = 0; i < 52; i++) t[i] = i;

srnd(s); for (j = 0, n = 52; n; ) { i = rnd() % n--; card[j++] = t[i]; t[i] = t[n]; } printf("Deal %d\n", s); show(); }

int main(int c, char **v) { int s;

setlocale(LC_ALL, "");

if (c < 2 || (s = atoi(v[1])) <= 0) s = 11982;

deal(s); return 0; }</lang>