home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
komix
/
DATA.Z
/
TTicTacToe_GUIUnit.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-08-20
|
9KB
|
390 lines
//
// File : TTicTacToe_GUIUnit.pas
//
// Project : PCProject
// Configuration : PCConfig 1
// Phase : Implementation 1
// System : TicTacToe 1
//
unit TTicTacToe_GUIUnit;
interface
uses
// Start user include section
// End user include section
Menus,
ExtCtrls,
StdCtrls,
Forms,
Classes,
TicTacToe_GameUnit,
TicTacToe_ComputerUnit;
type
// Graphical User Interface of the game.
TTicTacToe_GUI = class(TForm)
// Visual Components
CellButton3: TButton (* Component:TicTacToe_CellButton *);
CellButton8: TButton (* Component:TicTacToe_CellButton *);
Board: TPanel (* Component:TicTacToe_Board *);
New: TMenuItem (* Component:TicTacToe_New *);
CellButton5: TButton (* Component:TicTacToe_CellButton *);
Result: TLabel (* Component:TicTacToe_Result *);
ComputerX: TMenuItem (* Component:TicTacToe_Opponent *);
CellButton2: TButton (* Component:TicTacToe_CellButton *);
CellButton7: TButton (* Component:TicTacToe_CellButton *);
MainMenu: TMainMenu (* Component:TicTacToe_MainMenu *);
GameMenu: TMenuItem (* Component:TicTacToe_GameMenu *);
OpponentMenu: TMenuItem (* Component:TicTacToe_OpponentMenu *);
CellButton4: TButton (* Component:TicTacToe_CellButton *);
CellButton9: TButton (* Component:TicTacToe_CellButton *);
Human: TMenuItem (* Component:TicTacToe_Opponent *);
CellButton1: TButton (* Component:TicTacToe_CellButton *);
ComputerO: TMenuItem (* Component:TicTacToe_Opponent *);
CellButton6: TButton (* Component:TicTacToe_CellButton *);
// Events methods
procedure CellButton1Click(Sender: TObject);
procedure CellButton3Click(Sender: TObject);
procedure HumanClick(Sender: TObject);
procedure CellButton4Click(Sender: TObject);
procedure CellButton5Click(Sender: TObject);
procedure CellButton6Click(Sender: TObject);
procedure CellButton7Click(Sender: TObject);
procedure CellButton8Click(Sender: TObject);
procedure CellButton9Click(Sender: TObject);
procedure NewClick(Sender: TObject);
procedure ComputerOClick(Sender: TObject);
procedure ComputerXClick(Sender: TObject);
procedure CellButton2Click(Sender: TObject);
private
// User defined attributes
CellButtons: array [1..9] of TButton;
// Association attributes
computerRef: TicTacToe_Computer;
gameRef: TicTacToe_Game;
// User defined methods
// Properties
protected
// User defined attributes
// User defined methods
// Properties
public
// User defined attributes
// Default constructor/destructor
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// User defined methods
procedure enableBoard;
procedure displayResult(text: String);
procedure disableBoard;
procedure cellButtonClick(index: Integer);
procedure clearBoard;
// Association methods
procedure setGame(newTicTacToe_Game: TicTacToe_Game);
function getGame: TicTacToe_Game;
procedure removeComputer;
function getComputer: TicTacToe_Computer;
procedure setComputer(newTicTacToe_Computer: TicTacToe_Computer);
procedure removeGame;
// Properties
published
// User defined attributes
// User defined methods
// Properties
end;
// Class feature attributes
var
TicTacToe_GUI: TTicTacToe_GUI (* Form attribute *);
implementation
// Start user include section
// End user include section
{$R *.DFM}
constructor TTicTacToe_GUI.Create(AOwner: TComponent);
// Start user section
// End user section
begin
inherited Create(AOwner);
// Start user section
setGame(TicTacToe_Game.Create);
setComputer(TicTacToe_Computer.Create(getGame()));
CellButtons[1] := CellButton1;
CellButtons[2] := CellButton2;
CellButtons[3] := CellButton3;
CellButtons[4] := CellButton4;
CellButtons[5] := CellButton5;
CellButtons[6] := CellButton6;
CellButtons[7] := CellButton7;
CellButtons[8] := CellButton8;
CellButtons[9] := CellButton9;
TicTacToe_Game(getGame()).startGame;
// End user section
end;
destructor TTicTacToe_GUI.Destroy;
// Start user section
// End user section
begin
// Start user section
// End user section
removeComputer;
removeGame;
inherited Destroy;
end;
// Enables all the buttons of the board.
procedure TTicTacToe_GUI.enableBoard;
var
i: Integer;
begin
for i := 1 to 9 do
if CellButtons[i].Caption = ' ' then
CellButtons[i].Enabled := True;
end;
// Displays the specified result.
procedure TTicTacToe_GUI.displayResult(text: String);
begin
Result.Caption := text;
end;
// Disables all the buttons of the board.
procedure TTicTacToe_GUI.disableBoard;
var
i: Integer;
begin
for i := 1 to 9 do
CellButtons[i].Enabled := False;
end;
// Checks if cell is empty and sets it.
procedure TTicTacToe_GUI.cellButtonClick(index: Integer);
begin
if CellButtons[index].Enabled then
begin
CellButtons[index].Enabled := False;
CellButtons[index].Caption := gameRef.getActivePlayer();
gameRef.selectCell(index);
computerRef.checkTurn();
end;
end;
// Clears the contents of all the buttons of the board.
procedure TTicTacToe_GUI.clearBoard;
var
i: Integer;
begin
for i := 1 to 9 do
CellButtons[i].Caption := ' ';
end;
procedure TTicTacToe_GUI.CellButton1Click(Sender: TObject);
begin
cellButtonClick(1);
end;
procedure TTicTacToe_GUI.CellButton3Click(Sender: TObject);
begin
cellButtonClick(3);
end;
// Sets opponent.
procedure TTicTacToe_GUI.HumanClick(Sender: TObject);
begin
Human.Checked := True;
computerRef.setId('');
computerRef.checkTurn;
end;
procedure TTicTacToe_GUI.CellButton4Click(Sender: TObject);
begin
cellButtonClick(4);
end;
procedure TTicTacToe_GUI.CellButton5Click(Sender: TObject);
begin
cellButtonClick(5);
end;
procedure TTicTacToe_GUI.CellButton6Click(Sender: TObject);
begin
cellButtonClick(6);
end;
procedure TTicTacToe_GUI.CellButton7Click(Sender: TObject);
begin
cellButtonClick(7);
end;
procedure TTicTacToe_GUI.CellButton8Click(Sender: TObject);
begin
cellButtonClick(8);
end;
procedure TTicTacToe_GUI.CellButton9Click(Sender: TObject);
begin
cellButtonClick(9);
end;
// Starts new game.
procedure TTicTacToe_GUI.NewClick(Sender: TObject);
begin
gameRef.startGame;
computerRef.checkTurn;
end;
// Sets opponent.
procedure TTicTacToe_GUI.ComputerOClick(Sender: TObject);
begin
ComputerO.Checked := True;
computerRef.setId('O');
computerRef.checkTurn;
end;
// Sets opponent.
procedure TTicTacToe_GUI.ComputerXClick(Sender: TObject);
begin
ComputerX.Checked := True;
computerRef.setId('X');
computerRef.checkTurn;
end;
procedure TTicTacToe_GUI.CellButton2Click(Sender: TObject);
begin
cellButtonClick(2);
end;
// Do not delete this line -- regeneration marker
procedure TTicTacToe_GUI.setGame(newTicTacToe_Game: TicTacToe_Game);
begin
if (newTicTacToe_Game <> NIL) then
begin
if (newTicTacToe_Game <> gameRef) then
begin
if (gameRef <> NIL) then
begin
gameRef.removeGui;
end;
gameRef := newTicTacToe_Game;
newTicTacToe_Game.setGui(SELF);
end;
end
else
begin
removeGame;
end;
end;
function TTicTacToe_GUI.getGame: TicTacToe_Game;
begin
getGame := gameRef;
end;
procedure TTicTacToe_GUI.removeComputer;
var
oldTicTacToe_Computer: TicTacToe_Computer;
begin
if (computerRef <> NIL) then
begin
oldTicTacToe_Computer := computerRef;
computerRef := NIL;
oldTicTacToe_Computer.removeGui();
end;
end;
function TTicTacToe_GUI.getComputer: TicTacToe_Computer;
begin
getComputer := computerRef;
end;
procedure TTicTacToe_GUI.setComputer(newTicTacToe_Computer: TicTacToe_Computer);
begin
if (newTicTacToe_Computer <> NIL) then
begin
if (newTicTacToe_Computer <> computerRef) then
begin
if (computerRef <> NIL) then
begin
computerRef.removeGui;
end;
computerRef := newTicTacToe_Computer;
newTicTacToe_Computer.setGui(SELF);
end;
end
else
begin
removeComputer;
end;
end;
procedure TTicTacToe_GUI.removeGame;
var
oldTicTacToe_Game: TicTacToe_Game;
begin
if (gameRef <> NIL) then
begin
oldTicTacToe_Game := gameRef;
gameRef := NIL;
oldTicTacToe_Game.removeGui();
end;
end;
end.