⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playcard.pas

📁 《Delphi开发人员指南》配书原码
💻 PAS
字号:
unit PlayCard;

interface

uses SysUtils, Cards;

type
  ECardError = class(Exception);  // generic card exception

  TPlayingCard = record           // represents one card
    Face: TCardValue;             // card face value
    Suit: TCardSuit;              // card suit value
  end;

  { an array of 52 cards representing one deck }
  TCardArray = array[1..52] of TPlayingCard;

  { Object which represents a deck of 52 UNIQUE cards. }
  { This is a scrambled deck of 52 cards, and the      }
  { object keeps track of how far throughtout the deck    }
  { the user has picked. }
  TCardDeck = class
  private
    FCardArray: TCardArray;
    FTop: integer;
    procedure InitCards;
    function GetCount: integer;
  public
    property Count: integer read GetCount;
    constructor Create; virtual;
    procedure Shuffle;
    function Draw: TPlayingCard;
  end;

{ GetCardValue returns the numeric value of any card }
function GetCardValue(C: TPlayingCard): Integer;

implementation

function GetCardValue(C: TPlayingCard): Integer;
{ returns a card's numeric value }
begin
  Result := Ord(C.Face) + 1;
  if Result > 10 then Result := 10;
end;

procedure TCardDeck.InitCards;
{ initializes the deck by assigning a unique value/suit combination }
{ to each card. }
var
  i: integer;
  AFace: TCardValue;
  ASuit: TCardSuit;
begin
  AFace := cvAce;                        // start with ace
  ASuit := csClub;                       // start with clubs
  for i := 1 to 52 do                    // for each card in deck...
  begin
    FCardArray[i].Face := AFace;         // assign face
    FCardArray[i].Suit := ASuit;         // assign suit
    if (i mod 4 = 0) and (i <> 52) then  // every four cards...
      inc(AFace);                        // increment the face
    if ASuit <> High(TCardSuit) then     // always increment the suit
      inc(ASuit)
    else
      ASuit := Low(TCardSuit);
  end;
end;

constructor TCardDeck.Create;
{ constructor for TCardDeck object. }
begin
  inherited Create;
  InitCards;
  Shuffle;
end;

function TCardDeck.GetCount: integer;
{ Returns a count of unused cards }
begin
  Result := 52 - FTop;
end;

procedure TCardDeck.Shuffle;
{ Re-mixes cards and sets top card to 0. }
var
  i: integer;
  RandCard: TPlayingCard;
  RandNum: integer;
begin
  for i := 1 to 52 do
  begin
    RandNum := Random(51) + 1;            // pick random number
    RandCard := FCardArray[RandNum];      // swap next card with
    FCardArray[RandNum] := FCardArray[i]; // random card in deck
    FCardArray[i] := RandCard;
  end;
  FTop := 0;
end;

function TCardDeck.Draw: TPlayingCard;
{ Picks the next card from the deck. }
begin
  inc(FTop);
  if FTop = 53 then
    raise ECardError.Create('Deck is empty');
  Result := FCardArray[FTop];
end;

initialization
  Randomize;                   // must seed random number generator
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -