- /*
- * ==============================================================================
- * Name : TicTacToeBoard.cpp
- * Part of : TicTacToe
- * Description :
- * Version :
- *
- * Copyright (c) 2005 Nokia Corporation.
- * ==============================================================================
- */
- // INCLUDE FILES
- #include "TicTacToeBoard.h"
- // EXTERNAL DATA STRUCTURES
- // EXTERNAL FUNCTION PROTOTYPES
- // CONSTANTS
- // MACROS
- // LOCAL CONSTANTS AND MACROS
- // MODULE DATA STRUCTURES
- // LOCAL FUNCTION PROTOTYPES
- // FORWARD DECLARATIONS
- // ============================ MEMBER FUNCTIONS ===============================
- // -----------------------------------------------------------------------------
- // TTicTacToeBoard::TTicTacToeBoard
- // C++ default constructor can NOT contain any code, that
- // might leave.
- // -----------------------------------------------------------------------------
- //
- TTicTacToeBoard::TTicTacToeBoard()
- {
- Reset();
- }
- // Destructor
- TTicTacToeBoard::~TTicTacToeBoard()
- {
- }
- // -----------------------------------------------------------------------------
- // TTicTacToeBoard::Reset
- // Reset the board to initial state.
- // -----------------------------------------------------------------------------
- //
- void TTicTacToeBoard::Reset()
- {
- for ( TInt i = 0; i < KTicTacToeCells ; i++)
- {
- iCells[i] = ETttPlayerNone;
- }
- iCanMove = ETrue;
- }
- // -----------------------------------------------------------------------------
- // TTicTacToeBoard::Player
- // Get the player who has taken a given cell.
- // -----------------------------------------------------------------------------
- //
- TTicTacToePlayer TTicTacToeBoard::Player(TInt aColumn, TInt aRow) const
- {
- TInt cell = aRow * KTicTacToeColumns + aColumn;
- if ( 0 <= cell && cell < KTicTacToeCells )
- {
- return iCells[cell];
- }
- return ETttPlayerNone;
- }
- // -----------------------------------------------------------------------------
- //
- //
- // -----------------------------------------------------------------------------
- //
- TBool TTicTacToeBoard::Move(TInt aCell, TTicTacToePlayer aPlayer)
- {
- if ( 0 <= aCell && aCell < KTicTacToeCells
- && iCanMove && ETttPlayerNone == iCells[aCell] )
- {
- iCells[aCell] = aPlayer;
- return ETrue;
- }
- return EFalse;
- }
- // -----------------------------------------------------------------------------
- //
- //
- // -----------------------------------------------------------------------------
- //
- TBool TTicTacToeBoard::HasWon(TTicTacToePlayer aPlayer)
- {
- if ( (iCells[0] == aPlayer && iCells[1] == aPlayer && iCells[2] == aPlayer)
- || (iCells[3] == aPlayer && iCells[4] == aPlayer && iCells[5] == aPlayer)
- || (iCells[6] == aPlayer && iCells[7] == aPlayer && iCells[8] == aPlayer)
- || (iCells[0] == aPlayer && iCells[3] == aPlayer && iCells[6] == aPlayer)
- || (iCells[1] == aPlayer && iCells[4] == aPlayer && iCells[7] == aPlayer)
- || (iCells[2] == aPlayer && iCells[5] == aPlayer && iCells[8] == aPlayer)
- || (iCells[0] == aPlayer && iCells[4] == aPlayer && iCells[8] == aPlayer)
- || (iCells[2] == aPlayer && iCells[4] == aPlayer && iCells[6] == aPlayer) )
- {
- // The game is over
- iCanMove = EFalse;
- return ETrue;
- }
- return EFalse;
- }
- // -----------------------------------------------------------------------------
- //
- //
- // -----------------------------------------------------------------------------
- //
- TBool TTicTacToeBoard::IsDraw()
- {
- // Check for empty cells
- for ( TInt i = 0; i < KTicTacToeCells ; i++)
- {
- if ( ETttPlayerNone == iCells[i] )
- {
- return EFalse;
- }
- }
- // The game is over
- iCanMove = EFalse;
- return ETrue;
- }
- // End of File