Playing cards: Difference between revisions

Content added Content deleted
m (Moved explanation for PARI/GP into correct section)
m (Restored alphabetical order of languages)
Line 729: Line 729:
(declare (ignore x y))
(declare (ignore x y))
(zerop (random 2)))))</lang>
(zerop (random 2)))))</lang>

=={{header|Delphi}}==
<lang d>
program Cards;

{$APPTYPE CONSOLE}

uses
SysUtils, Classes;

type
TPip = (pTwo, pThree, pFour, pFive, pSix, pSeven, pEight, pNine, pTen, pJack, pQueen, pKing, pAce);
TSuite = (sDiamonds, sSpades, sHearts, sClubs);

const
cPipNames: array[TPip] of string = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A');
cSuiteNames: array[TSuite] of string = ('Diamonds', 'Spades', 'Hearts', 'Clubs');

type
TCard = class
private
FSuite: TSuite;
FPip: TPip;
public
constructor Create(aSuite: TSuite; aPip: TPip);
function ToString: string; override;

property Pip: TPip read FPip;
property Suite: TSuite read FSuite;
end;

TDeck = class
private
FCards: TList;
public
constructor Create;
destructor Destroy; override;
procedure Shuffle;
function Deal: TCard;
function ToString: string; override;
end;

{ TCard }

constructor TCard.Create(aSuite: TSuite; aPip: TPip);
begin
FSuite := aSuite;
FPip := aPip;
end;

function TCard.ToString: string;
begin
Result := Format('%s of %s', [cPipNames[Pip], cSuiteNames[Suite]])
end;

{ TDeck }

constructor TDeck.Create;
var
pip: TPip;
suite: TSuite;
begin
FCards := TList.Create;
for suite := Low(TSuite) to High(TSuite) do
for pip := Low(TPip) to High(TPip) do
FCards.Add(TCard.Create(suite, pip));
end;

function TDeck.Deal: TCard;
begin
Result := FCards[0];
FCards.Delete(0);
end;

destructor TDeck.Destroy;
var
i: Integer;
c: TCard;
begin
for i := FCards.Count - 1 downto 0 do begin
c := FCards[i];
FCards.Delete(i);
c.Free;
end;
FCards.Free;
inherited;
end;

procedure TDeck.Shuffle;
var
i, j: Integer;
temp: TCard;
begin
Randomize;
for i := FCards.Count - 1 downto 0 do begin
j := Random(FCards.Count);
temp := FCards[j];
FCards.Delete(j);
FCards.Add(temp);
end;
end;

function TDeck.ToString: string;
var
i: Integer;
begin
for i := 0 to FCards.Count - 1 do
Writeln(TCard(FCards[i]).ToString);
end;

begin
with TDeck.Create do
try
Shuffle;
ToString;
Writeln;
with Deal do
try
Writeln(ToString);
finally
Free;
end;
finally
Free;
end;

Readln;
end.

</lang>


=={{header|D}}==
=={{header|D}}==
Line 1,148: Line 1,018:
5 of Club 4 of Club 3 of Spade 2 of Diamond
5 of Club 4 of Club 3 of Spade 2 of Diamond
2 of Heart </pre>
2 of Heart </pre>

=={{header|Delphi}}==
<lang delphi>program Cards;

{$APPTYPE CONSOLE}

uses
SysUtils, Classes;

type
TPip = (pTwo, pThree, pFour, pFive, pSix, pSeven, pEight, pNine, pTen, pJack, pQueen, pKing, pAce);
TSuite = (sDiamonds, sSpades, sHearts, sClubs);

const
cPipNames: array[TPip] of string = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A');
cSuiteNames: array[TSuite] of string = ('Diamonds', 'Spades', 'Hearts', 'Clubs');

type
TCard = class
private
FSuite: TSuite;
FPip: TPip;
public
constructor Create(aSuite: TSuite; aPip: TPip);
function ToString: string; override;

property Pip: TPip read FPip;
property Suite: TSuite read FSuite;
end;

TDeck = class
private
FCards: TList;
public
constructor Create;
destructor Destroy; override;
procedure Shuffle;
function Deal: TCard;
function ToString: string; override;
end;

{ TCard }

constructor TCard.Create(aSuite: TSuite; aPip: TPip);
begin
FSuite := aSuite;
FPip := aPip;
end;

function TCard.ToString: string;
begin
Result := Format('%s of %s', [cPipNames[Pip], cSuiteNames[Suite]])
end;

{ TDeck }

constructor TDeck.Create;
var
pip: TPip;
suite: TSuite;
begin
FCards := TList.Create;
for suite := Low(TSuite) to High(TSuite) do
for pip := Low(TPip) to High(TPip) do
FCards.Add(TCard.Create(suite, pip));
end;

function TDeck.Deal: TCard;
begin
Result := FCards[0];
FCards.Delete(0);
end;

destructor TDeck.Destroy;
var
i: Integer;
c: TCard;
begin
for i := FCards.Count - 1 downto 0 do begin
c := FCards[i];
FCards.Delete(i);
c.Free;
end;
FCards.Free;
inherited;
end;

procedure TDeck.Shuffle;
var
i, j: Integer;
temp: TCard;
begin
Randomize;
for i := FCards.Count - 1 downto 0 do begin
j := Random(FCards.Count);
temp := FCards[j];
FCards.Delete(j);
FCards.Add(temp);
end;
end;

function TDeck.ToString: string;
var
i: Integer;
begin
for i := 0 to FCards.Count - 1 do
Writeln(TCard(FCards[i]).ToString);
end;

begin
with TDeck.Create do
try
Shuffle;
ToString;
Writeln;
with Deal do
try
Writeln(ToString);
finally
Free;
end;
finally
Free;
end;

Readln;
end.</lang>


=={{header|E}}==
=={{header|E}}==
Line 1,204: Line 1,201:
{card,"10","Hearts"} {card,"2","Hearts"} {card,"6","Diamonds"}
{card,"10","Hearts"} {card,"2","Hearts"} {card,"6","Diamonds"}
</pre>
</pre>

=={{header|Forth}}==
{{works with|GNU Forth}}
<lang forth>require random.fs \ RANDOM ( n -- 0..n-1 ) is called CHOOSE in other Forths

create pips s" A23456789TJQK" mem,
create suits s" DHCS" mem, \ diamonds, hearts, clubs, spades
: .card ( c -- )
13 /mod swap
pips + c@ emit
suits + c@ emit ;

create deck 52 allot
variable dealt

: new-deck
52 0 do i deck i + c! loop 0 dealt ! ;
: .deck
52 dealt @ ?do deck i + c@ .card space loop cr ;
: shuffle
51 0 do
52 i - random i + ( rand-index ) deck +
deck i + c@ over c@
deck i + c! swap c!
loop ;
: cards-left ( -- n ) 52 dealt @ - ;
: deal-card ( -- c )
cards-left 0= abort" Deck empty!"
deck dealt @ + c@ 1 dealt +! ;
: .hand ( n -- )
0 do deal-card .card space loop cr ;

new-deck shuffle .deck
5 .hand
cards-left . \ 47</lang>


=={{header|Fantom}}==
=={{header|Fantom}}==
Line 1,309: Line 1,271:
}
}
</lang>
</lang>

=={{header|Forth}}==
{{works with|GNU Forth}}
<lang forth>require random.fs \ RANDOM ( n -- 0..n-1 ) is called CHOOSE in other Forths

create pips s" A23456789TJQK" mem,
create suits s" DHCS" mem, \ diamonds, hearts, clubs, spades
: .card ( c -- )
13 /mod swap
pips + c@ emit
suits + c@ emit ;

create deck 52 allot
variable dealt

: new-deck
52 0 do i deck i + c! loop 0 dealt ! ;
: .deck
52 dealt @ ?do deck i + c@ .card space loop cr ;
: shuffle
51 0 do
52 i - random i + ( rand-index ) deck +
deck i + c@ over c@
deck i + c! swap c!
loop ;
: cards-left ( -- n ) 52 dealt @ - ;
: deal-card ( -- c )
cards-left 0= abort" Deck empty!"
deck dealt @ + c@ 1 dealt +! ;
: .hand ( n -- )
0 do deal-card .card space loop cr ;

new-deck shuffle .deck
5 .hand
cards-left . \ 47</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==