The GUI design part of the Tic Tac Toe game is complete, we move on to game logic and initialization...
Initializing the Tic Tac Toe Game
Before we start coding we have to decide how to represent the playfield (label/cell grid) in the computer memory.
Each cell can be treated individually, we can place all cells in one-dimensional array, or we can make two-dimensional array. Since we want to automate our program as much as possible we will represent cells as a two dimensional array. The top-left "corner" will have an index of [1, 1], the top-right corner will be [1, 3], the bottom-right [3, 3], etc. Two playfield variables will be created for playfield data, as the code will be more readable if we store each player moves separately.
Beside playfield variables we need to declare variables that will tell us how many moves are played (iMove), who is on the move (sPlaySign), is the game in progress or is the game over (bGameOver), and two variables that will hold the number of victories for each player (iXScore and iOScore). Place the following code above the implementation keyword inside the unit Main.pas, just below the frMain variable:
...
var
frMain: TfrMain; //added by Delphi - do NOT delete
iXPos : TXOPosArray;
iOPos : TXOPosArray;
sPlaySign : String;
bGameOver : Boolean;
iMove : Integer;
iXScore : Integer;
iOScore : Integer;
implementation
...
|
Note: the TXOPosArray (two-dimensional integer array - holding 9 elements) type is declared above the form type declaration. Since we'll need to send an array as a parameter to a procedure, we have to define our "array type", like:
type TXOPosArray = array [1..3, 1..3] of Integer;
|
Now, inside the FormCreate event handler (double click a form to create one) we add the following code:
procedure TfrMain.FormCreate(Sender: TObject);
begin
iXScore := 0;
iOScore := 0;
InitPlayGround;
end;
|
The iXScore and iOScore variables are initialized when the form is created. This is because we don't want to change their values until user presses the btnResetScore button.
All other variables have to be initialized after the end of each game - inside the InitPlayGround procedure.
Playfield Initialization
During a playfield initialization we will clear the cells (that is, Captions of all Labels with lblCellX names) by setting the Caption property to '' (empty string). It is necessary to set iXPos and iOPos arrays to zero. Beside that, we have to check the value of the rgPlayFirst radio group ItemIndex property to determine which player will play first in the next game.
Also, we have to reset the number of moves to zero (iMove), and set the bGameOver variable to False.
To create the InitPlayGround procedure, go to the Code Editor window and below the implementation keyword, add the next code:
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;
|
Note that you will also need to add this procedure's header in the private section of the form declaration (interface part of the unit):
...
private
procedure InitPlayGround;
...
|
Stop for the moment, and consider the next two lines in the above code:
k:= (i - 1) * 3 + j - 1; // 0 .. 8
TLabel(FindComponent('lblCell' + IntToStr(k))).Caption := '';
What we want to do in the InitPlayGround procedure is to set the caption to all lblCellX label components to an empty string. One way to achieve this is to use 9 lines of code:
lblCell0.Caption:='';
lblCell1.Caption:='';
lblCell2.Caption:='';
...
lblCell7.Caption:='';
lblCell8.Caption:='';
|
Bad practice! The above code "tip", searches for a component on a form by its name (FindComponent), casts it to TLabel and assigns an empty string to the Caption Property. FindComponent returns the component in the forms Components property array with the name that matches the string in the only parameter required.
Next page > Your Turn! > Page 1, 2, 3, 4
A Beginner's Guide to Delphi Programming: Next Chapter >>
>>
Your First MDI Delphi Project