为什么我的按钮单击事件处理程序没有按照我的预期进行?
Why does my button click event handler not do what I expect?
所以我在这个 forum 上找到了一个 blackjack 源代码,但是我在让它工作时遇到了问题。我自己为代码制作了表格,我认为这就是问题所在。当我点击“新建按钮”时游戏应该开始,但是当我点击它时没有任何反应。
这是源代码:
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'
我做错了吗?我才刚刚开始学习delphi,所以如果我做了一些“愚蠢”的事情,希望你们能理解。
您可能没有填写表单上按钮的标记属性。在每个按钮的属性列表中查找 Tag
属性。 HitBtn
的标签应为 1。至少还有一个其他按钮的 Tag
应为 2。
所以我在这个 forum 上找到了一个 blackjack 源代码,但是我在让它工作时遇到了问题。我自己为代码制作了表格,我认为这就是问题所在。当我点击“新建按钮”时游戏应该开始,但是当我点击它时没有任何反应。 这是源代码:
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'
我做错了吗?我才刚刚开始学习delphi,所以如果我做了一些“愚蠢”的事情,希望你们能理解。
您可能没有填写表单上按钮的标记属性。在每个按钮的属性列表中查找 Tag
属性。 HitBtn
的标签应为 1。至少还有一个其他按钮的 Tag
应为 2。