Proxy > Gmail Facebook Yahoo!

Tic Tac Toe in C++



This program is a game program, Tic Tac Toe. Most of us have played this game in school days, we will be making a C++ program on it now. This program is quite lengthy, so I’ll first explain each variable and function.
Variables used:
(global) int gameover: This variable is initialized to 0 and its value is altered to 1 when the game is over. The program stops execution at gameover = 1.

int pos[9]
: This array contains the positions 1 – 9. This array is just used in one function (void print_board_start())
(global) char set[9] : This array is initialized to values 1 to 9. The values of 1 to 9 are altered to either X or O depending on user input. For instance: Player 1 (X) selects position 5 to mark as ‘X’. The function input_board_x() sets the value of set[5-1] = X. Similar process happens with Player 2(O).
(global) int taken[9]: This array to check if a position is taken. For instance, if a user selects position 5, taken[5-1] will be set to 1. While taking input from user, the program first checks if taken[x_pos - 1] = 1. If it is, it would tell the Player 1 that the position is already taken and asks to re-enter the position.
count: To count the number of moves. This variable cannot exceed 8 as there can only be 9 moves in a tic tac toe game.
void print_board_start(): This objective of this function is basically to print the starting board without any ‘X’ or ‘O’.
print_board_start()
void input_board_x()/void input_board_y(): These two functions alter the values of the positions of set[] to ‘X’ or ‘O’ depending on the users’ choice.
check_gameover();: This function tests if the game is over. If it is, then the value of gameover is set to 1.
check_duplicate();: Checks if user has entered a position that is already taken.
Program:
#include <iostream>
#include <conio.h>
using namespace std;
int gameover= 0;
int pos[9]={1,2,3,4,5,6,7,8,9};
char set[9]={'1','2','3','4','5','6','7','8','9'};
int taken[9]={0,0,0,0,0,0,0,0,0};
char cPlayer1 = 'X';
char cPlayer2 = 'O';
void print_board_start()  //Prints board at the start of the program
{
 cout<<"Welcome to Tic Tac Toe."<<endl;
 cout<<"|"<<pos[0]<<"|"<<pos[1]<<"|"<<pos[2]<<"|"<<endl;
 cout<<"-------"<<endl;
 cout<<"|"<<pos[3]<<"|"<<pos[4]<<"|"<<pos[5]<<"|"<<endl;
 cout<<"-------"<<endl;
 cout<<"|"<<pos[6]<<"|"<<pos[7]<<"|"<<pos[8]<<"|"<<endl;
 cout<<"-------"<<endl;
}
void input_board_x(int user_pos_x) //Sets the value for Player 1(X).
{
 set[user_pos_x-1]= 'X';

void input_board_y(int user_pos_y) //Sets position for Player 2(O)
{
 set[user_pos_y-1]= 'O';
}
void final_result_board() //Prints board after the game is won.
{
cout<<"|"<<set[0]<<"|"<<set[1]<<"|"<<set[2]<<endl;
cout<<"-------"<<endl;
cout<<"|"<<set[3]<<"|"<<set[4]<<"|"<<set[5]<<endl;
cout<<"-------"<<endl;
cout<<"|"<<set[6]<<"|"<<set[7]<<"|"<<set[8]<<endl;
cout<<"-------"<<endl;
}

int check_gameover() // Checks if the game is over
{
 //***---Checking if game is over.***---//
  //First possibilty
  if(set[0] == set[1] && set[2] == set[0])
  {
   cout<<"GAME OVER!"<<endl;
   gameover = 1;
   if(set[0] == cPlayer1)
   {
   cout<<"Player 1 wins!"<<endl;
   }
   else
    cout<<"Player 2 wins!"<<endl;
  }
   //Second possiblity
  if(set[0] == set[3] && set[6] == set[0])
  {
   cout<<"GAME OVER!"<<endl;
   gameover = 1;
   if(set[0] == cPlayer1)
   {
   cout<<"Player 1 wins!"<<endl;
   }
   else
    cout<<"Player 2 wins!"<<endl;
  }
  //Third possibility
  if(set[0] == set[4] && set[8] == set[0])
  {
   cout<<"GAME OVER!"<<endl;
   gameover = 1;
   if(set[0] == cPlayer1)
   {
   cout<<"Player 1 wins!"<<endl;
   }
   else
    cout<<"Player 2 wins!"<<endl;
  }
  //Fourth possibility
  if(set[1] == set[4] && set[7] == set[1])
  {
   cout<<"GAME OVER!"<<endl;
   gameover = 1;
   if(set[1] == cPlayer1)
   {
   cout<<"Player 1 wins!"<<endl;
   }
   else
    cout<<"Player 2 wins!"<<endl;
  }
  //Fifth possibility
  if(set[2] == set[5] && set[2] == set[8])
  {
   cout<<"GAME OVER!"<<endl;
   gameover = 1;
   if(set[2] == cPlayer1)
   {
   cout<<"Player 1 wins!"<<endl;
   }
   else
    cout<<"Player 2 wins!"<<endl;
  }
  //Sixth possibilty
  if(set[2] == set[4] && set[4] == set[6])
  {
   cout<<"GAME OVER!"<<endl;
   gameover = 1;
   if(set[2] == cPlayer1)
   {
   cout<<"Player 1 wins!"<<endl;
   }
   else
    cout<<"Player 2 wins!"<<endl;
  }
  //Seventh possibilty
  if(set[6] == set[7] && set[8] == set[6])
  {
   cout<<"GAME OVER!"<<endl;
   gameover = 1;
   if(set[2] == cPlayer1)
   {
   cout<<"Player 1 wins!"<<endl;
   }
   else
    cout<<"Player 2 wins!"<<endl;
  }
  return gameover;
}
int check_duplicate(int i) //Checks if any of the two players add a duplicate data.
{
 while(taken[i-1] == 1)
 {
  cout<<i<<" is already taken, please select another spot ";
  cin>>i;
 }
 return i;
}
int main()
{
 int count = 0;
 int x_pos,y_pos;
 print_board_start();
 cout<<"Player 1 : X"<<endl<<"Player 2 : 0"<<endl;
 while(gameover==0 && count<=8)
 {
  count++;
  cout<<"Enter the position for X :"<<endl;
  cin>>x_pos;
  while(x_pos>9 || x_pos <1 )
  {
   cout<<"Invalid Move! Please select position from 1 to 9:  ";
   cin>>x_pos;
  }

  if(taken[x_pos-1] == 1)
  {
   x_pos=check_duplicate(x_pos);
  }
  input_board_x(x_pos); //Setting the position of X.
  taken[x_pos -1] = 1; ////Setting the user entered position to taken.
  check_gameover();  //Check if the game is over.
  if(gameover == 1) //If game ends, this part of code will terminate the while loop
  {
   break;
  }
  if(count == 9) //If there have been 9 moves till now, loops stop executing
  {
   break;
  }
  count++;
  final_result_board(); //Prints current game progress
  cout<<"Enter the position for 0 : "<<endl;
  cin>>y_pos;
  while(y_pos>9 || y_pos <1 ) //If user enters invalid move. That is more than 9 and less than 0.
  {
   cout<<"Invalid Move! Please select position from 1 to 9:  ";
   cin>>y_pos;
  }
  if(taken[y_pos-1] == 1)
  {
   y_pos=check_duplicate(y_pos);
  }
  input_board_y(y_pos); //Setting the position of Y.
  taken[y_pos -1] = 1; //Setting the user entered position to taken.
  check_gameover();  // Checks if the game is over
  final_result_board(); //Prints current game progress
  if(gameover == 1) //If game ends, this part of code will terminate the while loop
  {
   break;
  }
 }
 if(gameover == 0) //If there is no result, i.e gameover stays at 0 after 9 iteration.
 {
  cout<<"Draw"<<endl;
 }
 final_result_board();
 getch();
}


Responses

0 Respones to "Tic Tac Toe in C++"


Send mail to your Friends.  

Expert Feed

 
Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar