unit maingame; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Menus, ExtCtrls; type Tgame = class(TForm) p11: TPanel; p12: TPanel; p13: TPanel; p21: TPanel; p22: TPanel; p23: TPanel; p31: TPanel; p32: TPanel; p33: TPanel; wonkymenu: TMainMenu; Game1: TMenuItem; new1: TMenuItem; quit1: TMenuItem; About1: TMenuItem; display: TLabel; procedure quit1Click(Sender: TObject); procedure clobbergrid; procedure drawgrid; procedure initialize; procedure claimclick(Sender: TObject); procedure new1Click(Sender: TObject); function winner:boolean; function tie:boolean; procedure FormActivate(Sender: TObject); private { Private declarations } public { Public declarations } end; var game: Tgame; implementation {$R *.DFM} var grid : array[1..3,1..3] of char; player1 : boolean; procedure Tgame.quit1Click(Sender: TObject); begin close end; procedure Tgame.clobbergrid; {1} var row, col : byte; begin {initialize array} for row := 1 to 3 do for col := 1 to 3 do grid[row,col] := '-'; end; procedure Tgame.drawgrid; {2} begin p11.caption := grid[1,1]; p12.caption := grid[1,2]; p13.caption := grid[1,3]; p21.caption := grid[2,1]; p22.caption := grid[2,2]; p23.caption := grid[2,3]; p31.caption := grid[3,1]; p32.caption := grid[3,2]; p33.caption := grid[3,3]; end; procedure Tgame.initialize; begin clobbergrid; drawgrid; player1 := true; end; procedure Tgame.new1Click(Sender: TObject); begin initialize; end; procedure Tgame.claimclick(sender: TObject); var row, col : byte; thepanel: TPanel; temp : string; begin if (sender is TPanel) and not(winner) then begin thepanel := sender as TPanel; row := strtoint(thepanel.name[2]); col := strtoint(thepanel.name[3]); if (grid[row,col] <> '-') then showmessage('Space Claimed, try again') else begin if player1 then grid[row,col] := 'O' else grid[row,col] := 'X'; if winner then begin drawgrid; if player1 then temp := 'O Wins' else temp := 'X Wins'; showmessage(temp) end else player1 := not(player1); end end; {else ignore it} drawgrid; end; function Tgame.tie:boolean; var temp:boolean; row, col:byte; begin {is the game a tie?} temp := true; for row := 1 to 3 do for col := 1 to 3 do if grid[row,col] = '-' then temp := false; tie := temp end; function Tgame.winner:boolean; var temp : boolean; row, col : byte; begin temp := false; {is there a winner?} {check row win} for row := 1 to 3 do if (grid[row,1]=grid[row,2]) and (grid[row,2]=grid[row,3]) and (grid[row,1] <> '-') then temp := true; {check col win} for col := 1 to 3 do if (grid[1,col]=grid[2,col]) and (grid[2,col]=grid[3,col]) and (grid[1,col] <> '-') then temp := true; {checkdiag} if (grid[1,1]=grid[2,2]) and (grid[2,2]=grid[3,3]) and (grid[1,1] <> '-') then temp := true; if (grid[1,3]=grid[2,2]) and (grid[2,2]=grid[3,1]) and (grid[1,3] <> '-') then temp := true; winner := temp; end; procedure Tgame.FormActivate(Sender: TObject); begin initialize; end; end.