Вы находитесь на странице: 1из 14

TicTacToe Game in C++ language

By : Ravindra Acharya
dt : 18th May 2009
e-mail id : ravi.acharya26@gmail.com

Topic : Tic Tac Toe Game in C++ language.

#
#include <iostream>
#
#include <cstdlib>
#
#include <conio.h>
#

#
using namespace std;
#

#
enum field_t {EMPTY = 0, IN_USE = 1, FULL = 2};
#

#
class tictactoe {
#
private:
#
char board[3][3]; // our game board
#
struct {
#
short computer;
#
short player;
#
short draw;
#
} score;
#
string possible_moves;
#
field_t field_state;
#
char digit2chr(short digit);
#
short chr2digit(char chr);
#
void updateFieldState();
#
bool check_player_win();
Page 1
TicTacToe Game in C++ language
#
bool check_computer_win();
#
bool find_player_win();
#
bool find_computer_win();
#
public:
#
void draw_board();
#
void initialize_board();
#
void player_doMove();
#
void find_computer_move();
#
void checkWinner();
#
void getKey();
#
short get_player_score();
#
short get_computer_score();
#
field_t checkfield();
#
tictactoe();
#
};
#

#
tictactoe::tictactoe()
#
{
#
// constructor
#
score.player = 0;
#
score.computer = 0;
#
score.draw = 0;
#
initialize_board();
#
}
#

#
Page 2
TicTacToe Game in C++ language
short chr2digit(char chr)
#
{
#
// convert a character to a digit
#
if('0' <= chr && chr <= '9') return
static_cast<short>(chr-'0');
#
return -1;
#
}
#

#
char tictactoe::digit2chr(short digit)
#
{
#
// convert a digit to a character
#
if(0 <= digit && digit <= 9) return
static_cast<char>(digit+'0');
#
return '?';
#
}
#

#
void tictactoe::getKey()
#
{
#
short c = static_cast<short>(getch());
#
if(c == 113)
#
{
#
exit(0);
#
}
#
}
#

#
void tictactoe::checkWinner()
#
{
Page 3
TicTacToe Game in C++ language
#
// check whether there's a winner
#
updateFieldState();
#
if(check_computer_win())
#
{
#
// computer wins
#
score.computer++;
#

#
cout << "Computer Wins !" << endl;
#

#
// new game
#
getKey();
#
initialize_board();
#
draw_board();
#
} else if(check_player_win()) {
#
// human player wins
#
score.player++;
#

#
cout << "You Win !" << endl;
#

#
// new game
#
getKey();
#
initialize_board();
#
draw_board();
#
} else if(checkfield() == FULL) {
#
// it's a draw
#
Page 4
TicTacToe Game in C++ language
score.draw++;
#

#
cout << "It's a draw !" << endl;
#

#
// new game
#
getKey();
#
initialize_board();
#
draw_board();
#
}
#
}
#

#
void tictactoe::find_computer_move()
#
{
#
// the computer has to play a move
#

#
// 1: If there's a winning play available for the computer,
play this one:
#
if(find_computer_win()) return;
#

#
// 2: If there's a winning play available for the human
player, block this one by playing it:
#
if(find_player_win()) return;
#

#
// 3: Is the box in the middle already played? If no, play
this one:
#
if(board[1][1] == ' ')
#
{
#
Page 5
TicTacToe Game in C++ language
board[1][1] = 'O';
#
return;
#
}
#

#
// 4: Just play the first possible move (find the first empty
field):
#
bool looptime = true;
#
for(int i = 0; ((i < 3) && looptime); i++)
#
{
#
for(int j = 0; ((j < 3) && looptime); j++)
#
{
#
if(board[i][j] == ' ')
#
{
#
looptime = false;
#
board[i][j] = 'O';
#
updateFieldState();
#
}
#
}
#
}
#
}
#

#
bool tictactoe::find_computer_win()
#
{
#
for(int i = 0; i < 3; i++)
#
{
#
for(int j = 0; j < 3; j++)
#
Page 6
TicTacToe Game in C++ language
{
#
if(board[i][j] == ' ')
#
{
#
// empty field
#
board[i][j] = 'O';
#
if(!check_computer_win())
#
{
#
board[i][j] = ' ';
#
} else {
#
return true;
#
}
#
}
#
}
#
}
#
return false;
#
}
#

#
bool tictactoe::find_player_win()
#
{
#
for(int i = 0; i < 3; i++)
#
{
#
for(int j = 0; j < 3; j++)
#
{
#
if(board[i][j] == ' ')
#
{
#
// empty field
Page 7
TicTacToe Game in C++ language
#
board[i][j] = 'X';
#
if(!check_player_win())
#
{
#
board[i][j] = ' ';
#
} else {
#
board[i][j] = 'O';
#
return true;
#
}
#
}
#
}
#
}
#
return false;
#
}
#

#
bool tictactoe::check_player_win()
#
{
#
// check whether the human player can win
#
if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] ==
'X') return true;
#
if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] ==
'X') return true;
#
if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] ==
'X') return true;
#
if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] ==
'X') return true;
#
if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] ==
'X') return true;
#
if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] ==
Page 8
TicTacToe Game in C++ language
'X') return true;
#
if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] ==
'X') return true;
#
if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] ==
'X') return true;
#
return false;
#
}
#

#
bool tictactoe::check_computer_win()
#
{
#
// check whether the computer player can win
#
if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] ==
'O') return true;
#
if(board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] ==
'O') return true;
#
if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] ==
'O') return true;
#
if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] ==
'O') return true;
#
if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] ==
'O') return true;
#
if(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] ==
'O') return true;
#
if(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] ==
'O') return true;
#
if(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] ==
'O') return true;
#
return false;
#
}
#

#
field_t tictactoe::checkfield()
Page 9
TicTacToe Game in C++ language
#
{
#
// return the board state
#
return field_state;
#
}
#

#
void tictactoe::updateFieldState()
#
{
#
// update the board state (EMPTY, IN_USE, FULL)
#
short empty_c = 0;
#
for(int i = 0; i < 3; i++)
#
{
#
for(int j = 0; j < 3; j++)
#
{
#
if(board[i][j] == ' ')
#
{
#
empty_c++;
#
field_state = IN_USE;
#
break;
#
} else {
#
if(empty_c == 0)
#
{
#
field_state = FULL;
#
} else {
#
field_state = EMPTY;
#
}
#
Page 10
TicTacToe Game in C++ language
}
#
}
#
}
#
}
#

#
void tictactoe::player_doMove()
#
{
#
// the human player plays a move
#
short row, col;
#
cout << "Enter row, col: ";
#
cin >> row >> col;
#
if(row > 3 || col > 3)
#
{
#
cout << endl << "Invalid move !" << endl << endl;
#
player_doMove();
#
} else {
#
if(board[row-1][col-1] != ' ')
#
{
#
// this field has already been played
#
cout << endl << "This field is already played
!" << endl << endl;
#
player_doMove();
#
} else {
#
// this is an empty field, play the move
#
board[row-1][col-1] = 'X';
#
}
#
Page 11
TicTacToe Game in C++ language
}
#
updateFieldState();
#
}
#

#
void tictactoe::draw_board()
#
{
#
// draw the game board
#
system("CLS");
#

#
cout << endl;
#

#
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
<< endl;
#
cout << "TicTacToe Game v1.0 written by Ravindra Acharya" <<
endl;
#
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
<< endl << endl;
#

#
for(int i = 0; i < 3; i++)
#
{
#
cout << "|---|---|---|" << endl;
#
for(int j = 0; j < 3; j++)
#
{
#
cout << "| " << board[i][j] << " ";
#
}
#
cout << "|" << endl;
#
}
#
Page 12
TicTacToe Game in C++ language
cout << "|---|---|---|" << endl << endl;
#

#
cout << "Computer score: " << score.computer << "\n" <<
"Player score: " << score.player << "\n" <<

"Draws: " << score.draw << endl << endl;


#
}
#

#
void tictactoe::initialize_board()
#
{
#
// reset the game board (clear all fields)
#
for(int i = 0; i < 3; i++)
#
for(int j = 0; j < 3; j++) board[i][j] = ' ';
#
field_state = EMPTY;
#
}
#

#
short tictactoe::get_player_score()
#
{
#
// get the player score
#
return score.player;
#
}
#

#
short tictactoe::get_computer_score()
#
{
#
// get the computer score
#
return score.computer;
#
}
#
Page 13
TicTacToe Game in C++ language

#
int main()
#
{
#
/* The most famous function is this one :P */
#
tictactoe test;
#
test.draw_board();
#
while(test.checkfield() != FULL)
#
{
#
test.player_doMove();
#
test.draw_board();
#
test.checkWinner();
#
test.find_computer_move();
#
test.draw_board();
#
test.checkWinner();
#
}
#

#
return 0;
#
}

Page 14

Вам также может понравиться