Tic-Tac-Toe Game
Once more, the game chosen for this paper is Tic-Tac-Toe. In this
game, there is a board with nine slots and two players where each make
moves alternatively by putting an x or an o, if the move by a player
makes a straight set (horizontal, vertical, or diagonal) of three x’s
or three o’s, then that player is declared the winner. If none of the
horizontal, vertical, or diagonal slots are filled and there aren’t
anymore slots to fill, then the game ends in a draw. The following
pictures show the progression of a game from start to finish, started
by a player and winner with the symbol x:
For brevity and simplicity, we assume that in this implementation, a
human plays against the computer. There are several variations [17] of
this board game. The game can be played human to human, computer to
human, human to computer, and computer to computer. Following the
diagram below, the design would be to make it possible to go to the
previous states of the current game. There will be statistics visible
about the games played, including win and draw counts. Other details
and controls that are available are shown by the following diagram:
For planning purposes, the diagram shown above with two columns of
elements will be used. We will describe all the elements on this
diagram with a Tic-tac-toe board and the statistics along with other
elements during gameplay:
The first element in the left column of the elements is a label
element displaying the name of the game Tic-tac-toe. The second pair
of elements shows the mode of the game. It could be H2H for a human to
human game when alternative play between two humans, H2C when a human
(lead player) is playing against the computer, C2H when the computer
(lead player) is playing against a human, and C2C when the computer is
playing against itself. This element can be set by the user by
clicking on the setUp element. Next, the group element of nine cells
shows the tic-tac-toe board, where the player can click and make the
move. Next, the group of five elements shows the controls to go to the
initial (“«”) state, previous (“<”) state, the move number, control to
go to the next (“>”) state, and the final (“»”) state in the current
game. The two elements (“«”, “»”) move the state of the board to the
beginning and the last states respectively without changing the
statistics of the game set. Next element shows the status or the
result of the recent move (e.g., showing Winner is x).
In the right column of elements, swingout shows the number of games
won by the player x so far, oWinCount shows the games won by the
player o, drawCount shows the number of draws, leadPlayer shows the player who made the first move, nextPlayer shows the player who is
going to make the next move, and movesCount shows the number of moves
made so far in the current game. The element setUp invokes setting up
the game parameters, such as the game mode (H2H, H2C, C2H, or C2C),
the leadPlayer (x or o), and other parameters which could be added
later to enhance the game. The element initialize initializes the
board elements to play the next game, the element move instructs the
computer to make the next move (if the mode is C2C), and element stop
halts the game.
Initially, these labels convey future actions or values represented by
these elements for the developers. Once the software is fullydeveloped, these labels can be altered so that they are clearly
understood by the software’s player or user.
The purpose of this diagram is to clearly specify the output required
and give an insight into the software’s function. This will also allow
us to define a class with appropriate data and function members. We
will not attempt to address these issues related to the user interface
in this paper.
class CTicTacToe
{
private: string mode; // mode of the game H2H, H2C, C2H, or C2C
char board[3][3]; // board
char result; // result of the game ‘c’, ‘x’, ‘o’, or ‘d’
int xWinCount; // win count for 'X'
int oWinCount; // win count for 'O'
int drawCount; // draw count
char leadPlayer; // first player 'o' or 'x' for all games
char nextPlayer; // next player 'o' or 'x', and blank (' ') if game over
int movesCount; // moves made so far in current game
int nextR; // current or next move's row
int nextC; // current or next move's column
string history[9]; // for each move x|o, r, c, and x (9 tuples max for a game)
int cursor; // the position of move if viewing history of moves
public:
CTicTacToe(void); // default constructor for a game
~CticTacToe(); // destructor for class CTicTacToe
void setUp(void); // sets the game parameters
void displayCurrentState(void); // display board with stat
void getNextMove(void); // get nextR and nextC for the next player
void makeNextMove(void); // make next move based on nextR and nextC
void checkResult(void); // sets result to x won | o won | d draw | c continue
void stop(void); // stops current game set and display stat
private:
void updateResult(void); // display message after current game is over
void initialize(void); // resets board to blanks
void moveToPreviousState(void); // update board elements with previous board state
void moveToNextState(void); // update board elements with next board state
void moveToFirstState(void); // update board elements with first board state
void moveToLastState(void); // update board elements with last board state
};
Main algorithm for the game Create a gameSet of type CTicTacToe
Display gameBoard (3x3 board and the stats)
Do forever
{
Get action
If exit from game desired then exit loop
Take action
Update gameBoard
CheckStatus Display Status
If current game is over
Initialize gameBoard for new game
Update gameBoard
Endif
}
class CTicTacToe
{
private: string mode; // mode of the game H2H, H2C, C2H, or C2C
char board[3][3]; // board
char result; // result of the game ‘c’, ‘x’, ‘o’, or ‘d’
int xWinCount; // win count for 'X'
int oWinCount; // win count for 'O'
int drawCount; // draw count
char leadPlayer; // first player 'o' or 'x' for all games
char nextPlayer; // next player 'o' or 'x', and blank (' ') if game over
int movesCount; // moves made so far in current game
int nextR; // current or next move's row
int nextC; // current or next move's column
string history[9]; // for each move x|o, r, c, and x (9 tuples max for a game)
int cursor; // the position of move if viewing history of moves
public:
CTicTacToe(void); // default constructor for a game
~CticTacToe(); // destructor for class CTicTacToe
void setUp(void); // sets the game parameters
void displayCurrentState(void); // display board with stat
void getNextMove(void); // get nextR and nextC for the next player
void makeNextMove(void); // make next move based on nextR and nextC
void checkResult(void); // sets result to x won | o won | d draw | c continue
void stop(void); // stops current game set and display stat
private:
void updateResult(void); // display message after current game is over
void initialize(void); // resets board to blanks
void moveToPreviousState(void); // update board elements with previous board state
void moveToNextState(void); // update board elements with next board state
void moveToFirstState(void); // update board elements with first board state
void moveToLastState(void); // update board elements with last board state
};
Main algorithm for the game Create a gameSet of type CTicTacToe
Display gameBoard (3x3 board and the stats)
Do forever
{
Get action
If exit from game desired then exit loop
Take action
Update gameBoard
CheckStatus Display Status
If current game is over
Initialize gameBoard for new game
Update gameBoard
Endif
}
No comments:
Post a Comment