home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
komix
/
DATA.Z
/
TicTacToe_ComputerUnit.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-08-20
|
5KB
|
252 lines
//
// File : TicTacToe_ComputerUnit.pas
//
// Project : PCProject
// Configuration : PCConfig 1
// Phase : Implementation 1
// System : TicTacToe 1
//
unit TicTacToe_ComputerUnit;
interface
uses
// Start user include section
// End user include section
TicTacToe_GameUnit;
type
// The computer opponent.
TicTacToe_Computer = class
private
// User defined attributes
id: String (* = '' *);
// Association attributes
guiRef: Pointer;
// User defined methods
// Properties
protected
// User defined attributes
// User defined methods
// Properties
public
// User defined attributes
// Association attributes
gameRef: TicTacToe_Game;
// Default constructor/destructor
constructor Create(newgame: TicTacToe_Game);
destructor Destroy; override;
// User defined methods
function getMaxValue(x: Integer): Integer;
procedure checkTurn;
procedure selectCell;
// Access methods
procedure setId(newId: String);
function getId: String;
// Association methods
function getGui: Pointer;
procedure removeGui;
procedure setGame(newTicTacToe_Game: TicTacToe_Game);
function getGame: TicTacToe_Game;
procedure setGui(newTTicTacToe_GUI: Pointer);
// Properties
published
// User defined attributes
// User defined methods
// Properties
end;
implementation
uses
// Start user include section
TicTacToe_CellUnit,
// End user include section
TTicTacToe_GUIUnit,
SysUtils;
constructor TicTacToe_Computer.Create(newgame: TicTacToe_Game);
// Start user section
// End user section
begin
id := '';
if (newgame <> NIL) then
begin
gameRef := newgame;
end
else
raise EInvalidOp.Create('Object newgame has mandatory relation. NIL object reference not allowed.');
inherited Create;
// Start user section
// End user section
end;
destructor TicTacToe_Computer.Destroy;
// Start user section
// End user section
begin
// Start user section
// End user section
removeGui;
inherited Destroy;
end;
// Determines the strategic value of the specified empty cell.
function TicTacToe_Computer.getMaxValue(x: Integer): Integer;
var
i, retValue, temp: Integer;
game: TicTacToe_Game;
cell: TicTacToe_Cell;
begin
game := getGame();
retValue := -1;
cell := game.getBoard(x);
for i := 1 to Length(game.getPlayers()) do
begin
cell.setContents(game.getPlayers()[i]);
temp := cell.getMaxLine();
if (retValue < temp) or (retValue = -1) then
retValue := temp;
end;
cell.setContents(' ');
getMaxValue := retValue;
end;
// Checks if it's its turn.
procedure TicTacToe_Computer.checkTurn;
begin
if getId() = TicTacToe_Game (getGame()).getActivePlayer then selectCell;
end;
// Chooses a good empty cell.
procedure TicTacToe_Computer.selectCell;
var
i, maxi, temp: Integer;
selection: Integer;
game: TicTacToe_Game;
gui: TTicTacToe_GUI;
cell: TicTacToe_Cell;
begin
game := getGame();
gui := getGUI();
maxi := 0;
selection := -1;
for i:= 1 to 9 do
begin
cell := game.getBoard(i);
if cell.getContents() = ' ' then
begin
temp := getMaxValue(i);
if (maxi < temp) or (selection = -1) then
begin
maxi := temp;
selection := i;
end;
end;
end;
if selection <> -1 then
gui.cellButtonClick(selection);
end;
// Do not delete this line -- regeneration marker
procedure TicTacToe_Computer.setId(newId: String);
begin
id := newId;
end;
function TicTacToe_Computer.getId: String;
begin
getId := id;
end;
function TicTacToe_Computer.getGui: Pointer;
begin
getGui := guiRef;
end;
procedure TicTacToe_Computer.removeGui;
var
oldTTicTacToe_GUI: TTicTacToe_GUI;
begin
if (guiRef <> NIL) then
begin
oldTTicTacToe_GUI := guiRef;
guiRef := NIL;
oldTTicTacToe_GUI.removeComputer();
end;
end;
procedure TicTacToe_Computer.setGame(newTicTacToe_Game: TicTacToe_Game);
begin
if (newTicTacToe_Game <> NIL) then
begin
gameRef := newTicTacToe_Game;
end;
end;
function TicTacToe_Computer.getGame: TicTacToe_Game;
begin
getGame := gameRef;
end;
procedure TicTacToe_Computer.setGui(newTTicTacToe_GUI: Pointer);
begin
if (newTTicTacToe_GUI <> NIL) then
begin
if (newTTicTacToe_GUI <> guiRef) then
begin
if (guiRef <> NIL) then
begin
TTicTacToe_GUI(guiRef).removeComputer;
end;
guiRef := newTTicTacToe_GUI;
TTicTacToe_GUI(newTTicTacToe_GUI).setComputer(SELF);
end;
end
else
begin
removeGui;
end;
end;
end.