How to resolve the algorithm Deal cards for FreeCell step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Deal cards for FreeCell step by step in the XPL0 programming language

Table of Contents

Problem Statement

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. (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. Newer versions from Microsoft have 1 million deals, numbered from 1 to 1000000; some implementations allow numbers outside that range. The algorithm uses this linear congruential generator from Microsoft C:

The algorithm follows: Deals can also be checked against FreeCell solutions to 1000000 games. (Summon a video solution, and it displays the initial deal.) Write a program to take a deal number and deal cards in the same order as this algorithm. The program may display the cards with ASCII, with Unicode, by drawing graphics, or any other way. Related tasks:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Deal cards for FreeCell step by step in the XPL0 programming language

Source code in the xpl0 programming language

include c:\cxpl\codes;                  \intrinsic 'code' declarations
string 0;                               \use zero-terminated string convention
int  RandState;

func Rand;                              \Random number in range 0 to 32767
[RandState:= (214013*RandState + 2531011) & $7FFF_FFFF;
return RandState >> 16;
];

int  Card, Deck(52), Size;
char Suit, Rank;
[RandState:= IntIn(8);                  \seed RNG with number from command line
for Card:= 0 to 52-1 do Deck(Card):= Card; \create array of 52 cards
Rank:= "A23456789TJQK";
Suit:= "CDHS";
Size:= 52;
repeat  Card:= rem(Rand/Size);          \choose a random card
        ChOut(0, Rank(Deck(Card)/4));   \deal it by showing it
        ChOut(0, Suit(rem(0)));
        if rem(Size/8)=5 then CrLf(0) else ChOut(0, ^ );
        Size:= Size-1;                  \one less card in deck
        Deck(Card):= Deck(Size);        \replace dealt card with last card
until   Size = 0;                       \all cards have been dealt
]

  

You may also check:How to resolve the algorithm Abstract type step by step in the Caché ObjectScript programming language
You may also check:How to resolve the algorithm Empty string step by step in the VBScript programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Draco programming language