home *** CD-ROM | disk | FTP | other *** search
- //
-
- // File : TicTacToe_GameUnit.pas
-
- //
-
- // Project : PCProject
-
- // Configuration : PCConfig 1
-
- // Phase : Implementation 1
-
- // System : TicTacToe 1
-
- //
-
- unit TicTacToe_GameUnit;
-
-
-
- interface
-
-
-
- uses
-
- // Start user include section
-
- // End user include section
-
- ClassDict,
-
- TicTacToe_CellUnit;
-
-
-
- type
-
- // The Tic Tac Toe game itself.
-
- TicTacToe_Game = class
-
-
-
- private
-
- // User defined attributes
-
- turnNumber: Integer;
-
- players: String (* = 'XO' *);
-
- drawText: String (* = 'It''s a draw!!!' *);
-
- winText: String (* = ' is the winner!!!' *);
-
-
-
- // Association attributes
-
- guiRef: Pointer;
-
- boardDict: TClassDict;
-
-
-
- // User defined methods
-
-
-
- // Properties
-
-
-
- protected
-
- // User defined attributes
-
-
-
- // User defined methods
-
-
-
- // Properties
-
-
-
- public
-
- // User defined attributes
-
-
-
- // Default constructor/destructor
-
- constructor Create;
-
- destructor Destroy; override;
-
-
-
- // User defined methods
-
- procedure selectCell(number: Integer);
-
- procedure connectCells;
-
- procedure checkResult(number: Integer);
-
- procedure createCells;
-
- procedure startGame;
-
- function getActivePlayer: String;
-
- procedure clearBoard;
-
-
-
- // Access methods
-
- procedure setWinText(newWinText: String);
-
- procedure setPlayers(newPlayers: String);
-
- function getDrawText: String;
-
- procedure setDrawText(newDrawText: String);
-
- function getTurnNumber: Integer;
-
- procedure setTurnNumber(newTurnNumber: Integer);
-
- function getPlayers: String;
-
- function getWinText: String;
-
-
-
- // Association methods
-
- procedure removeBoard(Index: Integer);
-
- function getGui: Pointer;
-
- function getBoard(Index: Integer): TicTacToe_Cell;
-
- procedure removeGui;
-
- procedure setGui(newTTicTacToe_GUI: Pointer);
-
- procedure setBoard(Index: Integer; newTicTacToe_Cell: TicTacToe_Cell);
-
-
-
- // Properties
-
-
-
- published
-
- // User defined attributes
-
-
-
- // User defined methods
-
-
-
- // Properties
-
- end;
-
-
-
-
-
- implementation
-
-
-
- uses
-
- // Start user include section
-
- // End user include section
-
- TTicTacToe_GUIUnit;
-
-
-
- constructor TicTacToe_Game.Create;
-
- // Start user section
-
- // End user section
-
- begin
-
- players := 'XO';
-
- drawText := 'It''s a draw!!!';
-
- winText := ' is the winner!!!';
-
-
-
- inherited Create;
-
- boardDict := TClassDict.Create;
-
- // Start user section
-
- createCells;
-
- connectCells;
-
- // End user section
-
- end;
-
-
-
-
-
- destructor TicTacToe_Game.Destroy;
-
- // Start user section
-
- // End user section
-
- begin
-
- // Start user section
-
- // End user section
-
- removeGui;
-
- boardDict.Destroy;
-
-
-
- inherited Destroy;
-
- end;
-
-
-
-
-
- // Marks the selected cell and checks to
-
- // see if there is a result.
-
- procedure TicTacToe_Game.selectCell(number: Integer);
-
- var
-
- cell: TicTacToe_Cell;
-
- begin
-
- cell := getBoard(number);
-
- cell.setContents(getActivePlayer);
-
- checkResult(number);
-
- end;
-
-
-
-
-
- // Connects all the cells of the board.
-
- procedure TicTacToe_Game.connectCells;
-
- var
-
- N, E, W, S: Integer;
-
- i: Integer;
-
- cell: TicTacToe_Cell;
-
-
-
- begin
-
- N := 3;
-
- W := -1;
-
- E := -W;
-
- S := -N;
-
-
-
- for i := 1 to 9 do
-
- begin
-
- cell := getBoard(i);
-
- cell.setNeighbour('N', getBoard(i + N));
-
- if ((i mod 3) <> 0) then
-
- begin
-
- cell.setNeighbour('NE', getBoard(i + N + E));
-
- cell.setNeighbour('E', getBoard(i + E));
-
- cell.setNeighbour('SE', getBoard(i + S + E));
-
- end;
-
- cell.setNeighbour('S', getBoard(i + S));
-
- if ((i mod 3) <> 1) then
-
- begin
-
- cell.setNeighbour('SW', getBoard(i + S + W));
-
- cell.setNeighbour('W', getBoard(i + W));
-
- cell.setNeighbour('NW', getBoard(i + N + W));
-
- end;
-
- end;
-
- end;
-
-
-
-
-
- // Checks if there is a winner or a draw
-
- // and displays this result.
-
- procedure TicTacToe_Game.checkResult(number: Integer);
-
- var
-
- gui: TTicTacToe_GUI;
-
- cell: TicTacToe_Cell;
-
- begin
-
- gui := getGUI();
-
- cell := getBoard(number);
-
-
-
- if cell.getMaxLine() = 3 then
-
- begin
-
- gui.disableBoard();
-
- gui.displayResult(getActivePlayer() + getWinText());
-
- end
-
- else
-
- begin
-
- if (getTurnNumber = 9 -1) then
-
- begin
-
- gui.disableBoard();
-
- gui.displayResult(getDrawText());
-
- end;
-
- end;
-
- setTurnNumber(getTurnNumber() + 1);
-
- end;
-
-
-
-
-
- // Creates all the cells of the board.
-
- //
-
- procedure TicTacToe_Game.createCells;
-
- var
-
- aCell: TicTacToe_Cell;
-
- i: Integer;
-
-
-
- begin
-
- for i := 1 to 9 do
-
- begin
-
- aCell := TicTacToe_Cell.Create;
-
- setBoard(i, aCell);
-
- end;
-
- end;
-
-
-
-
-
- // Initialize everything for a new game.
-
- procedure TicTacToe_Game.startGame;
-
- var
-
- gui: TTicTacToe_GUI;
-
-
-
- begin
-
- gui := getGUI();
-
-
-
- setTurnNumber(0);
-
- gui.ClearBoard();
-
- Self.ClearBoard();
-
- gui.enableBoard();
-
- gui.displayResult('');
-
- end;
-
-
-
-
-
- // Determines the active player.
-
- function TicTacToe_Game.getActivePlayer: String;
-
- begin
-
- getActivePlayer := getPlayers()[turnNumber mod Length(getPlayers()) + 1];
-
- end;
-
-
-
-
-
- // Clears all the cells of the board.
-
- procedure TicTacToe_Game.clearBoard;
-
- var
-
- i: Integer;
-
- cell: TicTacToe_Cell;
-
- begin
-
- for i := 1 to boardDict.Count do
-
- begin
-
- cell := getBoard(i);
-
- cell.setContents(' ');
-
- end;
-
- end;
-
-
-
-
-
- // Do not delete this line -- regeneration marker
-
-
-
- procedure TicTacToe_Game.setWinText(newWinText: String);
-
- begin
-
- winText := newWinText;
-
- end;
-
-
-
-
-
- procedure TicTacToe_Game.setPlayers(newPlayers: String);
-
- begin
-
- players := newPlayers;
-
- end;
-
-
-
-
-
- function TicTacToe_Game.getDrawText: String;
-
- begin
-
- getDrawText := drawText;
-
- end;
-
-
-
-
-
- procedure TicTacToe_Game.setDrawText(newDrawText: String);
-
- begin
-
- drawText := newDrawText;
-
- end;
-
-
-
-
-
- function TicTacToe_Game.getTurnNumber: Integer;
-
- begin
-
- getTurnNumber := turnNumber;
-
- end;
-
-
-
-
-
- procedure TicTacToe_Game.setTurnNumber(newTurnNumber: Integer);
-
- begin
-
- turnNumber := newTurnNumber;
-
- end;
-
-
-
-
-
- function TicTacToe_Game.getPlayers: String;
-
- begin
-
- getPlayers := players;
-
- end;
-
-
-
-
-
- function TicTacToe_Game.getWinText: String;
-
- begin
-
- getWinText := winText;
-
- end;
-
-
-
-
-
- procedure TicTacToe_Game.removeBoard(Index: Integer);
-
- begin
-
- boardDict.RemoveUsingKey(Index)
-
- end;
-
-
-
-
-
- function TicTacToe_Game.getGui: Pointer;
-
- begin
-
- getGui := guiRef;
-
- end;
-
-
-
-
-
- function TicTacToe_Game.getBoard(Index: Integer): TicTacToe_Cell;
-
- begin
-
- getBoard := boardDict.Item(Index);
-
- end;
-
-
-
-
-
- procedure TicTacToe_Game.removeGui;
-
- var
-
- oldTTicTacToe_GUI: TTicTacToe_GUI;
-
-
-
- begin
-
- if (guiRef <> NIL) then
-
- begin
-
- oldTTicTacToe_GUI := guiRef;
-
- guiRef := NIL;
-
- oldTTicTacToe_GUI.removeGame();
-
- end;
-
- end;
-
-
-
-
-
- procedure TicTacToe_Game.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).removeGame;
-
- end;
-
- guiRef := newTTicTacToe_GUI;
-
- TTicTacToe_GUI(newTTicTacToe_GUI).setGame(SELF);
-
- end;
-
- end
-
- else
-
- begin
-
- removeGui;
-
- end;
-
- end;
-
-
-
-
-
- procedure TicTacToe_Game.setBoard(Index: Integer; newTicTacToe_Cell: TicTacToe_Cell);
-
- begin
-
- if (newTicTacToe_Cell <> NIL) then
-
- begin
-
- boardDict.Add(Index, newTicTacToe_Cell);
-
- end;
-
- end;
-
-
-
-
-
-
-
- end.
-
-