![]() |
MD_TTT Library
1.0
TicTacToe Game Decision Engine Library
|
#include <MD_TTT.h>
Public Member Functions | |
Methods for Setup and Initialization. | |
MD_TTT (void(*_mh)(uint8_t pos, int8_t player)) | |
~MD_TTT (void) | |
Methods for Game Management. | |
bool | start () |
bool | doMove (uint8_t pos, int8_t player) |
bool | setAutoPlayer (int8_t player) |
int8_t | getAutoPlayer (void) |
Methods for Board Management. | |
bool | isGameOver (void) |
int8_t | getGameWinner (void) |
uint8_t | getWinLine (void) |
int8_t | getBoardPosition (uint8_t pos) |
Protected Member Functions | |
uint8_t | doAutoMove (int8_t player) |
work out a move for the auto player | |
void | unpackByte (uint8_t *pb, uint8_t b) |
unpack the byte into the array | |
bool | randomChoice (void) |
return true or false randomly | |
Protected Attributes | |
int8_t | _board [TTT_BOARD_SIZE] |
the game board | |
uint8_t | _movesLeft |
the number of moves left in the game | |
bool | _gameOver |
flag to know when the game is over | |
int8_t | _gameWinner |
id of player who won | |
uint8_t | _winLine |
the winning line (TTT_WL_*) or 0xff | |
int8_t | _autoPlayer |
the computer player (TTT_P0 if neither) | |
void(* | _cbMoveHandler )(uint8_t pos, int8_t player) |
callback into user code to process the move | |
Core object for the MD_TTT library. This class contains all logic and status information for the game.
MD_TTT::MD_TTT | ( | void(*)(uint8_t pos, int8_t player) | _mh | ) |
Class Constructor.
Creates a newly initialised MD_TTT object. The parameter mh is the address of a user callback function with prototype
void tttCallback(uint8_t pos, int8_t player)
This callback function is called from the library at the completion of every move to allow the user code to animate or otherwise deal with the user interface component of the game. The callback is given the position board position of the last move (0-8) in pos and the player identifier for the player occupying that position (TTT_P0, TTT_P1 or TTT_P2) in player. TTT_P0 denotes an empty cell.
The callback function may use any of the library status functions to determine the status of the game at that point. It is intended that all in-game user interface updates should occur only during the callback.
_mh | pointer to user callback function. |
MD_TTT::~MD_TTT | ( | void | ) |
Class Destructor.
Release allocated memory and does the necessary to clean up once the object is no longer required.
bool MD_TTT::doMove | ( | uint8_t | pos, |
int8_t | player | ||
) |
Execute the next game move.
Instruct the library to execute the next move. The location for the move is given in pos and the identifier for the player who is making this move is passed through player. If player corresponds to the auto player, then pos is ignored and the libraries make a decision on the next move for this player. The user callback function is always invoked after the move is completed and all game status values have been settled.
pos | position on the board for the move [0..8]. |
player | player identifier TT_P1 or TT_P2. |
int8_t MD_TTT::getAutoPlayer | ( | void | ) |
Get the computer player id.
Returns the player identifier of the designated auto player. This would have previously been set by a call to setAutoPlayer(). Note that the return value is an int8_t (ie, a signed value).
int8_t MD_TTT::getBoardPosition | ( | uint8_t | pos | ) |
Get the occupier of a board position
Returns the player identifier (TTT_P*) for the player occupying the board position pos. Note that the return value is an int8_t (ie, a signed value).
pos | the position to check |
int8_t MD_TTT::getGameWinner | ( | void | ) |
Return the player that won
Used to determine who won a game. Note that the return value is an int8_t (ie, a signed value).
uint8_t MD_TTT::getWinLine | ( | void | ) |
Return the winning line
Returns the winning line for the game. The calling program should first check isGameOver() to ensure that the winning line returned is valid. The winning line will be one of the identifiers TTT_WL_* in MD_TTT.h. The function should be used to easily determine which line to ‘strike out’ at the end of the game.
bool MD_TTT::isGameOver | ( | void | ) |
Return if the game is over
Used to check if a game has been won.
bool MD_TTT::setAutoPlayer | ( | int8_t | player | ) |
Set the computer player.
Sets player to be the library controlled player. By default, this is TT_P0 (ie, not one of the players). If the auto player is not set then the library will never invoke the move generation logic for any player in doMove().
player | player identifier TT_P1 or TT_P2. |
bool MD_TTT::start | ( | void | ) |
Reset the board for a new game.
Resets the board for a new game. All the internal tracking parameters are reset and the game board is cleared. The user callback function is invoked for each location that is changed to its reset state.