How to resolve the algorithm Deal cards for FreeCell step by step in the Ceylon programming language
How to resolve the algorithm Deal cards for FreeCell step by step in the Ceylon 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 Ceylon programming language
Source code in the ceylon programming language
shared void freeCellDeal() {
//a function that returns a random number generating function
function createRNG(variable Integer state) =>
() => (state = (214_013 * state + 2_531_011) % 2^31) / 2^16;
void deal(Integer num) {
// create an array with a list comprehension
variable value deck = Array {
for(rank in "A23456789TJQK")
for(suit in "CDHS")
"``rank````suit``"
};
value rng = createRNG(num);
for(i in 1..52) {
value index = rng() % deck.size;
assert(exists lastIndex = deck.lastIndex);
//swap the random card with the last one
deck.swap(index, lastIndex);
//print the last one
process.write("``deck.last else "missing card"`` " );
if(i % 8 == 0) {
print("");
}
//and shrink the array to remove the last card
deck = deck[...lastIndex - 1];
}
}
deal(1);
print("\n");
deal(617);
}
You may also check:How to resolve the algorithm Search a list of records step by step in the Maple programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Anti-primes step by step in the Rust programming language
You may also check:How to resolve the algorithm Terminal control/Hiding the cursor step by step in the Arturo programming language
You may also check:How to resolve the algorithm Sort stability step by step in the PARI/GP programming language