Search code examples
lazarusfreepascal

Why does my button click event handler not do what I expect?


so I found a blackjack source code on this forum, but I have a problem to make it work. I had made the form myself for the code and I think that is the problem. The game should start when I click the "new button", but nothing happen when I click on it. Here is the source code:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Buttons;

type

  { TForm1 }

  TForm1 = class(TForm)
    BetCount: TLabel; //not used
    MoneyEdit: TEdit; //not used
    BetEdit: TEdit; //not used
    HitBtn: TButton;
    MoneyCountLbl: TLabel; //not used
    NewBtn: TButton;
    StandBtn: TButton;
    PlayerEdit: TEdit;
    DealerEdit: TEdit;
    MemoDealer: TMemo;
    MemoPlayer: TMemo;
    procedure PickASuit;
    procedure PickACard;
    procedure CardName;
    procedure LookAtHands;
    procedure newDeal;
    procedure DoIt(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public

    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}  
var
suitNum, cardNum, current, total1, total2 : Integer;
suitStr, cardStr : String[8];

procedure TForm1.PickASuit;
begin
suitNum := random(4)+1;
Case suitNum of
1 : suitStr := 'Spades';
2 : suitStr := 'Clubs';
3 : suitStr := 'Diamonds';
4 : suitStr := 'Hearts';
end;
end;



procedure TForm1.CardName;
begin
Case cardNum of
1 : cardStr := 'Ace';
2 : cardStr := 'Two';
3 : cardStr := 'Three';
4 : cardStr := 'Four';
5 : cardStr := 'Five';
6 : cardStr := 'Six';
7 : cardStr := 'Seven';
8 : cardStr := 'Eight';
9 : cardStr := 'Nine';
10 : cardStr := 'Ten';
11 : cardStr := 'Jack';
12 : cardStr := 'Queen';
13 : cardStr := 'King';
end;

Case cardNum of
1 : cardNum := 11;
10..13 : cardNum := 10;
end;

end;

procedure TForm1.PickACard;
begin
cardNum := random(13)+1;
PickASuit; {runs pickasuit procedure}
CardName; {runs cardnume procedure}

Case current of {tells the program what its doing}
  1 : begin
  MemoPlayer.Lines.Add(cardStr + ' of ' + suitStr );
  total1 := total1 + cardNum;
  PlayerEdit.Text := IntToStr(total1);
  end;

  2 : begin
  MemoDealer.Lines.Add(cardStr + ' of ' + suitStr );
  total2 := total2 + cardNum;
  DealerEdit.Text := IntToStr(total2);
  end;

end;
end;

procedure TForm1.LookAtHands;
Begin
If total2 > 21 then ShowMessage('House Busted')
Else if total1 > total2 then ShowMessage('You win')
Else if total1 = total2 then ShowMessage('Draw')
Else ShowMessage('You lose');
newDeal;
End;

procedure TForm1.newDeal;
Begin
MemoDealer.Clear;
MemoPlayer.Clear;
total1 := 0;
total2 := 0;
current := 1;
PickACard;
current := 2;
PickACard;
end;

procedure TForm1.DoIt(Sender: TObject);
begin
current := (Sender as TButton).Tag;
Case current of
1 : Begin
PickACard;
If total1 > 21 then
begin ShowMessage('Busted');
newDeal;
end;
end;

2 : begin While total2 < 17 do PickACard;
LookAtHands;
end;

3 : newDeal;
end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
   Randomize;
end;
end.

I set hit, new, and stand buttons to execute the procedure 'DoIt'

enter image description here

Do I make a mistake ? I'm just starting to learn delphi, so I hope you guys can understand if I do something "stupid" .


Solution

  • You probably did not fill the tag properties of the Buttons on the form. Look in the list of properties of each button for the Tag property. The HitBtn should have a Tag of 1. There's at least one other button that should have a Tag of 2.