Q:

C++ Tic Tac Toe Game Project With Source Code

0

Project:- C++ Tic Tac Toe Game project or tic tac toe c++ or simple tic tac toe c++ or TicTacToe Game Using Arrays in C++ or simple tic tac toe game in c++ or tic tac toe c++ program code or tic tac toe simple code in c++

Explanation:- Tic Tac Toe is a very popular Game, we often play this game Classrooms and hostel while we are in student life. Now I am going for Tic Tac Toe Game by step by step for better understanding I divide the Tic Tac Toe Game into three parts. Their three-part are as follows
 
1.GameChart draw.
2.Changing the Value of GameChart.
3.Check Win.
 
All three parts are defined in a code in a comment so first check all these parts of Tic Tac Toe Game. Now discuss the part by the part code below is a part first explanation.
 
1. GameChart Draw
 
First design a game chart, in C++ it is difficult to design graphics in console screen that is why I choose another option for drawing a Tic Tac Toe Game Environment via the help of COUT(in small) and try to manage that each chart looks goods when we enter the values on it.
 
2. Changing the Value of GameChart
 
Before changing the value of game chart first we have to put some value in a chart or we can say in a matrix. So I took a string matrix and put some values between 0 to 9 or 1 to 9 and also divide the matrix in 3 * 3 in GameChart Draw in the first part. After that when both users enter their names our program clear the first screen and display the current screen. Now I ask both Gamers to enter the number (in a matrix shows 1 to 9 where you want to draw your turn) so if a Player enters the values between 1 to 9 we change the value of that Player in a GameChart. The Same process repeated again and again and alternative for each Player one by one.
 
3. Check Win
 
Tic Tac Toe Game is a very famous game in this game everyone wants to win so that each and every input of user we check winning condition if our condition satisfy then it will print the message according to Game that either First player win or second Player win or Game is draw No one win Both Are Skilled Gamer.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;

char CharMatrixDraw[10] = {'0','1','2',
       '3','4','5',
       '6','7','8','9'};

int winner();
void GameChart(string,string);

/*Visit www.programmingwithbasics.com

This program is divided into 3 Parts read a Full Article for understanding full code

1.GameChart draw.
2.Changing the Value of GameChart.
3.Check Win.

*/
int main()
{
  int Gamer = 1, i, choice;
 string name1;
 string name2;

 cout<<"Enter First Gamer Name: ";
 cin>>name1;

 cout<<"\nEnter Second Gamer Name: ";
 cin>>name2;

  char mark;
  
  do
  {
    GameChart(name1,name2);
    Gamer=(Gamer%2)?1:2;
 
 if (Gamer==1)
 {
  cout <<name1<< " Your Turn, Enter a Number:  ";
    cin >> choice;
 }
 else
 {
  cout <<name2 << " Your Turn, Enter a Number:  ";
    cin >> choice;
 }

/* Part 2 Start Here*/

   mark=(Gamer == 1) ? 'X' : '0';

   if (choice == 1 && CharMatrixDraw[1] == '1')

    CharMatrixDraw[1] = mark;
   else if (choice == 2 && CharMatrixDraw[2] == '2')

    CharMatrixDraw[2] = mark;
   else if (choice == 3 && CharMatrixDraw[3] == '3')

    CharMatrixDraw[3] = mark;
   else if (choice == 4 && CharMatrixDraw[4] == '4')

    CharMatrixDraw[4] = mark;
   else if (choice == 5 && CharMatrixDraw[5] == '5')

    CharMatrixDraw[5] = mark;
   else if (choice == 6 && CharMatrixDraw[6] == '6')

    CharMatrixDraw[6] = mark;
   else if (choice == 7 && CharMatrixDraw[7] == '7')

    CharMatrixDraw[7] = mark;
   else if (choice == 8 && CharMatrixDraw[8] == '8')

    CharMatrixDraw[8] = mark;
   else if (choice == 9 && CharMatrixDraw[9] == '9')

    CharMatrixDraw[9] = mark;
   else
   {
     cout<<"\nInvalid Choice Try Again ";
     Gamer--;
     cin.ignore();
     cin.get();
   }
    i=winner();
    Gamer++;
  }while(i==-1);
  GameChart(name1,name2);
  if(i==1)
 {
  cout<<"\n=============================\n";
  cout<<"\a"<<name1<<" Is A Winner \n";
  cout<<"=============================\n";
 }
  else
   {
  cout<<"\n=============================\n";
  cout<<"\aGame Is Draw\n";
  cout<<"=============================\n";
 }

  cin.ignore();
  cin.get();
  return 0;
}

/*Part 2 ends Here*/

/*Part 3 Start Here*/

int winner()
{
  if (CharMatrixDraw[1] == CharMatrixDraw[2] && CharMatrixDraw[2] == CharMatrixDraw[3])

   return 1;
  else if (CharMatrixDraw[4] == CharMatrixDraw[5] && CharMatrixDraw[5] == CharMatrixDraw[6])

   return 1;
  else if (CharMatrixDraw[7] == CharMatrixDraw[8] && CharMatrixDraw[8] == CharMatrixDraw[9])

   return 1;
  else if (CharMatrixDraw[1] == CharMatrixDraw[4] && CharMatrixDraw[4] == CharMatrixDraw[7])

   return 1;
  else if (CharMatrixDraw[2] == CharMatrixDraw[5] && CharMatrixDraw[5] == CharMatrixDraw[8])

   return 1;
  else if (CharMatrixDraw[3] == CharMatrixDraw[6] && CharMatrixDraw[6] == CharMatrixDraw[9])

   return 1;
  else if (CharMatrixDraw[1] == CharMatrixDraw[5] && CharMatrixDraw[5] == CharMatrixDraw[9])

   return 1;
  else if (CharMatrixDraw[3] == CharMatrixDraw[5] && CharMatrixDraw[5] == CharMatrixDraw[7])

   return 1;
  else if (CharMatrixDraw[1] != '1' && CharMatrixDraw[2] != '2' && CharMatrixDraw[3] != '3'
                    && CharMatrixDraw[4] != '4' && CharMatrixDraw[5] != '5' && CharMatrixDraw[6] != '6'
                  && CharMatrixDraw[7] != '7' && CharMatrixDraw[8] != '8' && CharMatrixDraw[9] != '9')

   return 0;
  else
   return -1;
}

/*Part 3 ends Here*/

/*Part 1 Start Here*/

void GameChart(string name1,string name2)
{
  system("cls");
  
 cout<<"\n==========================";
  cout << "\n\tTic Tac Toe\n";
 cout<<"\n==========================\n";
  
 string nam1 = name1;
 string nam2 = name2;
  
 cout <<nam1<<"( X )"<<"========"<<nam2<<" (0)\n\n";

  cout << "     ||     ||     " << endl;
  cout << "  " << CharMatrixDraw[1] << "  ||  " << CharMatrixDraw[2] << "  ||  " << CharMatrixDraw[3] << endl;

  cout << "=====||=====||=====" << endl;
  cout << "     ||     ||     " << endl;

  cout << "  " << CharMatrixDraw[4] << "  ||  " << CharMatrixDraw[5] << "  ||  " << CharMatrixDraw[6] << endl;

  cout << "=====||=====||=====" << endl;
  cout << "     ||     ||     " << endl;

  cout << "  " << CharMatrixDraw[7] << "  ||  " << CharMatrixDraw[8] << "  ||  " << CharMatrixDraw[9] << endl;

  cout << "     ||     ||     " << endl << endl;
}

 

Output:

Enter First Gamer Name:  Ghanendra

 

Enter Second Gamer Name: Prashant

==========================

Tic Tac Toe

==========================

Ghanendra( X )========Prashant (0)

 

     ||     ||     

  1  ||  2  ||  3

=====||=====||=====

     ||     ||     

  4  ||  5  ||  6

=====||=====||=====

     ||     ||     

  7  ||  8  ||  9

     ||     ||     

 

Ghanendra Your Turn, Enter a Number:  1

==========================

Tic Tac Toe

==========================

Ghanendra( X )========Prashant (0)

 

     ||     ||     

  X  ||  2  ||  3

=====||=====||=====

     ||     ||     

  4  ||  5  ||  6

=====||=====||=====

     ||     ||     

  7  ||  8  ||  9

     ||     ||     

 

Prashant Your Turn, Enter a Number:  5

==========================

Tic Tac Toe

==========================

Ghanendra( X )========Prashant (0)

 

     ||     ||     

  X  ||  2  ||  3

=====||=====||=====

     ||     ||     

  4  ||  0  ||  6

=====||=====||=====

     ||     ||     

  7  ||  8  ||  9

     ||     ||     

 

Ghanendra Your Turn, Enter a Number:  4

 

==========================

Tic Tac Toe

==========================

Ghanendra( X )========Prashant (0)

 

     ||     ||     

  X  ||  2  ||  3

=====||=====||=====

     ||     ||     

  X  ||  0  ||  6

=====||=====||=====

     ||     ||     

  7  ||  8  ||  9

     ||     ||     

 

Prashant Your Turn, Enter a Number:  2

==========================

Tic Tac Toe

==========================

Ghanendra( X )========Prashant (0)

 

     ||     ||     

  X  ||  0  ||  3

=====||=====||=====

     ||     ||     

  X  ||  0  ||  6

=====||=====||=====

     ||     ||     

  7  ||  8  ||  9

     ||     ||     

 

Ghanendra Your Turn, Enter a Number:  7

 

==========================

Tic Tac Toe

==========================

Ghanendra( X )========Prashant (0)

 

     ||     ||     

  X  ||  0  ||  3

=====||=====||=====

     ||     ||     

  X  ||  0  ||  6

=====||=====||=====

     ||     ||     

  X  ||  8  ||  9

     ||     ||     

=============================

Ghanendra Is A Winner 

=============================

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now