C++ Practice Assignment #5 - Tic Tac Toe in C++

When the blackjack assignment was shared on StumbleUpon, it generated a large amount of traffic towards the C++ Better Explained site with over 2500 new visitors to the site. Due to popular demand, here is another article on game development with C++. This one is on how to write Tic Tac Toe in C++ which plays against the computer.

tic tac toe

Just like the previous example of implementing blackjack in C++, there are several elements of the Tic Tac toe game that we need to break down. The following code example shows just how that is being done by creating a class of the game and creating a separate function for need piece of the game that is required.

</pre>
<pre>//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
//Tic Tac Toe Class
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
#ifndef TIC_TAC_TOE
#define TIC_TAC_TOE

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using std::cout;

class TicTacToe
{
private:
int pos[9]; // board position
int player; // current player
int totalTurns; // how many turns so far?
int numberPlayers; // 1 to 20
bool playerType[20]; // player #, 0 = comp, 1 = human

public:
TicTacToe();
void printTurn();
bool playerHuman();
void humanMove();
void computerMove();
void drawBoard();
bool Winner();
bool fullBoard();
void nextTurn();

};

#endif</time.h></stdlib.h></stdio.h></iostream></pre>
<pre><iostream><stdio.h><stdlib.h><time.h>


Tic_Tac_Toe.cpp file

</pre>
<pre>//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Tic Tac Toe constructor
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
TicTacToe::TicTacToe()
{
 srand (time(0));

 player = 1; // who starts first?
 totalTurns = 0;

 // new player setup
 numberPlayers = 2;
 playerType[1] = 1; // playerType[player] is human (1)
 playerType[2] = 0; // playerType[player] is computer (0)
 
 for (int i = 0; i < 9; i++)
 {
 pos[i] = 0;
 }

}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Draw the Tic Tac Toe Board and seperate the board
// with the help of |
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
void TicTacToe::drawBoard()
{

 std::cout << std::endl
 << pos[0] << " | " << pos[1] << " | " << pos[2]
 << "\n−\n"
 << pos[3] << " | " << pos[4] << " | " << pos[5]
 << "\n─\n"
 << pos[6] << " | " << pos[7] << " | " << pos[8]
 << std::endl;
}

void TicTacToe::printTurn()
{
 std::cout << "\nPlayer " << player << "'s turn.\n";
}

void TicTacToe::nextTurn()
{
 totalTurns++;

 if (++player > numberPlayers)
 player = 1;
}

bool TicTacToe::playerHuman()
{
 return playerType[player];
}

void TicTacToe::humanMove()
{

 std::cout << "\nEnter your move: ";
 int move;

 do
 {
 std::cin >> move;
 move−−; // so user can enter 1−9 instead of 0−8
 }
 while (move < 0 || move > 8 || pos[move] != 0);

 pos[move] = player;
}

void TicTacToe::computerMove()
{
 int move;

 do
 {
 // Pick a random number from 1 − 9
 move = rand() % 9; // just pick a random number
 }
 while (move < 0 || move > 8 || pos[move] != 0);

 pos[move] = player;

}

bool TicTacToe::Winner()
{
 int board[8][3] = {{0,1,2},

 // winning possibilities
 {3,4,5},
 {6,7,8},
 {0,3,6},
 {1,4,7},
 {2,5,8},
 {0,4,8},
 {2,4,6}}; 

 // Go through the list of possibilities
 for (int i = 0; i < 8; i++)
 {
 if ((pos[board[i][0]] == pos[board[i][1]]) && (pos[board[i][1]] == pos[board[i][2]]) && pos[board[i][0]] != 0)
 {
 cout << "\nPlayer " << pos[board[i][0]] << " wins!\n\n";

 return 1; // return winner true
 }
 }

 return 0; // winner false
}

bool TicTacToe::fullBoard()
{
 if (totalTurns == 9)
 {
 cout << "\nTie game!\n\n";
 return 1;
 }
 else
 return 0;
}</pre>
<pre>

Main.cpp file

</pre>
<pre>#include "Tic_Tac_Toe.h"

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Main function of the game
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
int main()
{
TicTacToe Game;
Game.drawBoard();

do
{
Game.printTurn();

if (Game.playerHuman()) // human turn?
Game.humanMove();
else
Game.computerMove();

Game.drawBoard();
Game.nextTurn();

}

// Keep running the game until there is a winner and the game board is full
while (!Game.Winner() && !Game.fullBoard());

return 0;
}</pre>
<pre>
Share this post