unit Question4_U;

//Enter your student number here


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons,pngimage;

type
  TfrmQuestion4 = class(TForm)
    btnQ4_2: TButton;
    redQ4: TRichEdit;
    grpQ1_2: TGroupBox;
    Img1: TImage;
    img2: TImage;
    img3: TImage;
    img4: TImage;
    img5: TImage;
    img6: TImage;
    img7: TImage;
    img8: TImage;
    img9: TImage;
    img10: TImage;
    lblDeck: TLabel;
    btnQ4_1: TBitBtn;
    GroupBox1: TGroupBox;
    img11: TImage;
    img12: TImage;
    img13: TImage;
    img14: TImage;
    img15: TImage;
    img16: TImage;
    img17: TImage;
    img18: TImage;
    img19: TImage;
    img20: TImage;
    procedure btnQ4_2Click(Sender: TObject);
    procedure btnQ4_1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
  Procedure initializeDeck;
  procedure ShuffleDeck;
  procedure DealInitialCards;
  procedure CalculateTotal;
  function CardValue(Card: string): Integer;
  procedure DisplayHands(ShowAllCards: Boolean);
  procedure PlayerTurn;
  procedure DealerTurn;
  procedure DetermineWinner;
  function HandSize(PlayerIndex: Integer): Integer;
  Procedure ReMoveFromDeck;
  procedure LoadImages;
    { Public declarations }
  end;

const

  Suits: array[1..4] of string = ('Hearts', 'Diamonds', 'Clubs', 'Spades');
  Ranks: array[1..13] of string = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace');




 var
  arrDeck: array[1..52] of string;
  arrPlayers: array[1..2,1..10] of string;
  arrPlayerTotal: array[1..2] of Integer;
  arrPlayerNames: array[1..2] of string;
  frmQuestion4:TfrmQuestion4;
  iNumDeckCards : Integer = 52;
  arrCards : array[1..10] of tImage;

{ TfrmQuestion4 }
implementation
 {$R *.dfm}



procedure TfrmQuestion4.btnQ4_1Click(Sender: TObject ) ;
 
begin

 //Provided code
  InitializeDeck;
  ShuffleDeck;
   redQ4.Clear;

   //===================

  //Question 4.1
 



end;

procedure TfrmQuestion4.ShuffleDeck;      
 
begin

  //Question 4.2

 

end;

procedure TfrmQuestion4.CalculateTotal;      

begin
//Question 4.3
 
 
 
end;


procedure TfrmQuestion4.ReMoveFromDeck;        
 var i : integer;
begin
//Question 4.4
 

 
end;

function TfrmQuestion4.HandSize(PlayerIndex: Integer): Integer;     
var i : integer;
begin
//Question 4.5



end;

procedure TfrmQuestion4.DetermineWinner;      
begin
   //Question 4.6

 


end;


 {$region 'BlackJack game methods - DO NOT CHANGE'}
//Provided code   - do not change


procedure TfrmQuestion4.LoadImages;
var
 I ,j: Integer;
begin

for I := 1 to 10 do
  if arrPlayers[2,I] <> '' then
   arrCards[I].Picture.LoadFromFile('Cards\'+trim(arrPlayers[2,I])+'.png');


for I := 11 to 20 do
  if arrPlayers[1,I-10] <> '' then
   arrCards[I].Picture.LoadFromFile('Cards\'+trim(arrPlayers[1,I-10])+'.png');
end;


procedure TfrmQuestion4.initializeDeck;

var
  CardIndex, SuitIndex, RankIndex: Integer;

begin
  CardIndex := 1;

  for SuitIndex := 1 to 4 do
    for RankIndex := 1 to 13 do
    begin
      arrDeck[CardIndex] := Ranks[RankIndex] + ' of ' + Suits[SuitIndex];
      Inc(CardIndex);
    end;
end;

procedure TfrmQuestion4.DealInitialCards;
var
  i, j: Integer;
begin
   lblDeck.caption := 'Playing...';
  for j := 1 to 2 do
    for i := 1 to 2 do
    begin
      arrPlayers[j,i] := arrDeck[1];
      ReMoveFromDeck;
      CalculateTotal;
    end;
end;



function TfrmQuestion4.CardValue(Card: string): Integer;
var
  Rank: string;
begin
  Rank := Copy(Card, 1, Pos(' ', Card) - 1);
  case Rank[1] of
    '2', '3', '4', '5', '6', '7', '8', '9': Result := StrToInt(Rank);
    '1','J', 'Q', 'K': Result := 10;
    'A': Result := 11;
  else
    Result := 0;
  end;
end;

procedure TfrmQuestion4.DisplayHands(ShowAllCards: Boolean);
var
  i, j: Integer;
begin
redQ4.Clear;
  for j := 1 to 2 do
  begin
    redQ4.Lines.Add(arrPlayerNames[j]+ '`s Hand:');
    for i := 1 to 10 do
    begin
      if (arrPlayers[j,i] <> '') then
        redQ4.Lines.Add('  '+ arrPlayers[j,i]);
    end;
    LoadImages;
    redQ4.Lines.Add('Total Points: '+ inttostr(arrPlayerTotal[j]));
   redQ4.Lines.Add('');
  end;
end;

procedure TfrmQuestion4.FormActivate(Sender: TObject);
var
i ,icount: integer;
Control : tControl;

begin
  icount:= 1;
  for i := 0 to frmQuestion4.ComponentCount - 1 do
  begin
    if frmQuestion4.Components[i] is TImage then
    begin
      arrCards[icount] := TImage(frmQuestion4.Components[i]);
      Inc(icount);
    end;

  end;

redQ4.Paragraph.TabCount := 3;
redQ4.Paragraph.Tab[0] := 120;
redQ4.Paragraph.Tab[1] := 270;
redQ4.Paragraph.Tab[2] := 390;
arrPlayerTotal[1] := 0;
arrPlayerTotal[2] := 0;
end;

procedure TfrmQuestion4.PlayerTurn;
var
  cMove: Char;
begin
  repeat
    DisplayHands(False);
             arrPlayers[2,HandSize(2) + 1] := arrDeck[1];
             ReMoveFromDeck;
             CalculateTotal;
     cMove := uppercase(inputbox('Choose your move: (H)it or (S)tand','H','H'))[1];
  until (cMove = 'S') or (arrPlayerTotal[2] >= 21);
end;


procedure TfrmQuestion4.DealerTurn;
begin
  while (arrPlayerTotal[1] < 17) and (arrPlayerTotal[1] < 21) do
  begin
    arrPlayers[1,HandSize(1) + 1] := arrDeck[1];
    ReMoveFromDeck;
    CalculateTotal
  end;
end;



procedure TfrmQuestion4.btnQ4_2Click(Sender: TObject);
var
iplayer : Integer   ;

  begin



    arrPlayerNames[1] := 'Dealer'  ;

    arrPlayerNames[2] := inputbox( 'Enter the name for Player '+ inttostr(iPlayer)
               + ': ','','Peter');

  DealInitialCards;

  PlayerTurn;

  if arrPlayerTotal[1] <= 21 then
    DealerTurn;


  DisplayHands(True);
   lblDeck.caption := 'Final Results:';
  DetermineWinner;

end;

 {$endregion}
end.
