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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Deal cards for FreeCell step by step in the Delphi 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 Delphi programming language

Source code in the delphi programming language

program Deal_cards_for_FreeCell;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

type
  TRandom = record
    Seed: Int64;
    function Next: Integer;
  end;

  TCard = record
    const
      kSuits = '♣♦♥♠';
      kValues = 'A23456789TJQK';
    var
      Value: Integer;
      Suit: Integer;
    procedure Create(rawvalue: Integer); overload;
    procedure Create(value, suit: Integer); overload;
    procedure Assign(other: TCard);
    function ToString: string;
  end;

  TDeck = record
    Cards: TArray;
    procedure Create(Seed: Integer);
    function ToString: string;
  end;

{ TRandom }

function TRandom.Next: Integer;
begin
  Seed := ((Seed * 214013 + 2531011) and Integer.MaxValue);
  Result := Seed shr 16;
end;

{ TCard }

procedure TCard.Create(rawvalue: Integer);
begin
  Create(rawvalue div 4, rawvalue mod 4);
end;

procedure TCard.Assign(other: TCard);
begin
  Create(other.Value, other.Suit);
end;

procedure TCard.Create(value, suit: Integer);
begin
  self.Value := value;
  self.Suit := suit;
end;

function TCard.ToString: string;
begin
  result := format('%s%s', [kValues[value + 1], kSuits[suit + 1]]);
end;

{ TDeck }

procedure TDeck.Create(Seed: Integer);
var
  r: TRandom;
  i, j: integer;
  tmp: Tcard;
begin
  r.Seed := Seed;
  SetLength(Cards, 52);
  for i := 0 to 51 do
    Cards[i].Create(51 - i);
  for i := 0 to 50 do
  begin
    j := 51 - (r.Next mod (52 - i));
    tmp.Assign(Cards[i]);
    Cards[i].Assign(Cards[j]);
    Cards[j].Assign(tmp);
  end;
end;

function TDeck.ToString: string;
var
  i: Integer;
begin
  Result := '';
  for i := 0 to length(Cards) - 1 do
  begin
    Result := Result + Cards[i].ToString;
    if i mod 8 = 7 then
      Result := Result + #10
    else
      Result := Result + ' ';
  end;
end;

var
  Deck: TDeck;

begin
  Deck.Create(1);
  Writeln('Deck 1'#10, Deck.ToString, #10);
  Deck.Create(617);
  Writeln('Deck 617'#10, Deck.ToString);
  readln;
end.


  

You may also check:How to resolve the algorithm Array concatenation step by step in the Elixir programming language
You may also check:How to resolve the algorithm Cantor set step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Unix/ls step by step in the Python programming language
You may also check:How to resolve the algorithm Five weekends step by step in the D programming language
You may also check:How to resolve the algorithm Factorial step by step in the AmigaE programming language