Sei sulla pagina 1di 5

unit Main; { Article: Your First Delphi Game: Tic Tac Toe http://delphi.about.com/library/library/weekly/aa021803a.

htm A Beginner s Guide to Delphi Programming: Chapter 10. Designing and developing a real game using Delphi: Tic Tac Toe. } interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, StrUtils; type TXOPosArray = array [1..3, 1..3] of Integer; type TfrMain = class(TForm) lblCell0: TLabel; lblCell1: TLabel; lblCell2: TLabel; lblCell3: TLabel; lblCell4: TLabel; lblCell5: TLabel; lblCell6: TLabel; lblCell7: TLabel; lblCell8: TLabel; gbScoreBoard: TGroupBox; rgPlayFirst: TRadioGroup; lblX: TLabel; lblMinus: TLabel; Label1: TLabel; lblXScore: TLabel; lblColon: TLabel; lblOScore: TLabel; btnNewGame: TButton; btnResetScore: TButton; procedure procedure procedure procedure FormCreate(Sender: TObject); lblCell0Click(Sender: TObject); btnNewGameClick(Sender: TObject); btnResetScoreClick(Sender: TObject);

private procedure InitPlayGround; function GamePlay(xo_Move : Integer) : integer; function CheckWin(iPos : TXOPosArray) : integer; public

{ Public declarations } end; var frMain: TfrMain; iXPos : TXOPosArray; iOPos : TXOPosArray; sPlaySign : String; bGameOver : Boolean; iMove : Integer; iXScore : Integer; iOScore : Integer; implementation {$R *.dfm} procedure TfrMain.InitPlayGround; var i, j, k: integer; begin for i := 1 to 3 do begin for j := 1 To 3 do begin k:= (i - 1) * 3 + j - 1; // 0 .. 8 TLabel(FindComponent('lblCell' + IntToStr(k))).Caption := ''; iXPos[i, j] := 0; iOPos[i][j] := 0; end; end; if rgPlayFirst.ItemIndex = 0 then sPlaySign := 'X'; if rgPlayFirst.ItemIndex = 1 then sPlaySign := 'O'; bGameOver := False; iMove := 0; end; procedure TfrMain.FormCreate(Sender: TObject); begin iXScore := 0; iOScore := 0; InitPlayGround; end; function TfrMain.CheckWin(iPos : TXOPosArray) : Integer; var iScore : Integer; i : Integer; j : Integer; begin Result := -1; //in rows? iScore := 0; for i := 1 to 3 do

begin iScore := 0; Inc(Result); for j := 1 To 3 do Inc(iScore, iPos[i,j]); if iScore = 3 Then Exit end;//for i //top-left bottom-right diagonal? iScore := 0; Inc(Result); for i := 1 to 3 do Inc(iScore, iPos[i,i]); if iScore = 3 then Exit; //top-right bottom-left diagonal? iScore := 0; Inc(Result); for i := 1 to 3 do Inc(iScore, iPos[i,4-i]); if iScore = 3 then Exit; //columns? for i := 1 to 3 do begin iScore := 0; Inc(Result); for j := 1 to 3 do Inc(iScore, iPos[j,i]); if iScore = 3 then Exit; end;//for i Result := -1; end; function TfrMain.GamePlay(xo_Move : Integer):integer; var x, y : 1..3; iWin : integer; begin Result := -1; Inc(iMove); x := (xo_Move Div 3) + 1; y := (xo_Move Mod 3) + 1; if sPlaySign = 'O' then begin iOPos[x,y] := 1; iWin := CheckWin(iOPos); end else begin iXPos[x,y] := 1; iWin := CheckWin(iXPos); end; TLabel(FindComponent('lblCell' + IntToStr(xo_Move))).Caption := sPlaySign; Result := iWin; if iWin >= 0 then begin bGameOver := True;

//mark victory if sPlaySign = 'X' then begin iXScore := iXScore + 1; lblXScore.Caption := IntToStr(iXScore); end else begin iOScore := iOScore + 1; lblOScore.Caption := IntToStr(iOScore); end; ShowMessage(sPlaySign + ' - Wins!'); end; if (iMove = 9) AND (bGameOver = False) Then begin ShowMessage('It''s a Draw!'); bGameOver := True end; if sPlaySign = 'O' Then sPlaySign := 'X' else sPlaySign := 'O'; end; procedure TfrMain.lblCell0Click(Sender: TObject); var iWin : integer; CellIndex : 0..8; begin if bGameOver = True Then Exit; if TLabel(Sender).Caption <> '' then begin ShowMessage('Cell ocupied!'); Exit; end; CellIndex := StrToInt(RightStr(TLabel(Sender).Name,1)); iWin := GamePlay(CellIndex); end; procedure TfrMain.btnNewGameClick(Sender: TObject); begin if bGameOver = False then begin if MessageDlg('End the current game?', mtConfirmation, mbOKCancel,0) = mrCance l then Exit; end; InitPlayGround; end; procedure TfrMain.btnResetScoreClick(Sender: TObject); begin if MessageDlg( 'Reset the scores?', mtConfirmation,

mbOKCancel,0) = mrCancel then Exit; iXScore := 0; iOScore := 0; lblXScore.Caption := IntToStr(iXScore); lblOScore.Caption := IntToStr(iOScore); end; end.

Potrebbero piacerti anche