为什么我的按钮点击事件处理程序没有按照我的预期执行?

时间:2017-01-02 10:21:54

标签: lazarus freepascal

所以我在这个forum上找到了一个二十一点的源代码,但我有一个问题要让它工作。我自己为代码制作了表单,我认为这就是问题所在。当我点击“新按钮”时游戏应该开始,但是当我点击它时没有任何反应。 这是源代码:

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.

我设置了hit,new和stand按钮来执行'DoIt'程序

enter image description here

我犯了错误吗?我刚刚开始学习德尔福,所以我希望你们能理解我是否做了“愚蠢”的事情。

1 个答案:

答案 0 :(得分:1)

您可能没有在表单上填充按钮的标记属性。查看Tag属性的每个按钮的属性列表。 HitBtn的标记应为1.至少有一个其他按钮的Tag为{。}