所以我在這個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.
我組命中,新和站按鈕執行過程「大一」
難道我犯了一個錯誤?我剛開始學習delphi,所以我希望你們能理解我做了些什麼「愚蠢」的事情。
您的「DoIt」事件處理程序查詢按鈕的標籤。在設計表單時,您是否爲這些標籤分配了一個編號?如果不是,那麼標籤爲0,在「DoIt」中什麼都不會發生,因爲它預計標籤爲1,2等。 –
您是否已經完成了任何調試?如果你不知道如何調試,那麼你問的是錯誤的問題。如果你不努力學習調試技能,那麼繼續努力是毫無意義的。 –
我想知道如何在Delphi中編譯'{$ mode objfpc}'。所以我認爲這是在拉撒路而不是在德爾福? –