Monday, 21 March 2016

C++ Tic Tac Toe Game Project With Source Code

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++


C++ Tic Tac Toe Game Project With Source Code
Image Source Click Here

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance 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.

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.


Tip:- If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.

Solution:-

#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;
}

/*Part 1 ends Here*/

Output:-

First Screen

C++ Tic Tac Toe Game Project With Source Code

Game Chart Look

C++ Tic Tac Toe Game Project With Source Code

Winning Part Look

C++ Tic Tac Toe Game Project With Source Code


Check Other Projects


C++ Project's


1.C++ Program For Student Report Card ( SRC PROJECT ) With Source Code

2.C++ Program For School Management System ( SMS Project ) With Source Code

3.C++ Program For HANGMAN ( GAME PROJECT ) With Source Code

4. C++ Program For Casino Game: Number Guessing Program ( GAME PROJECT )

Java Project's

1.JAVA Project SMS (School Management System) Using Multiple Classes

2.Java Program For Calculator Using AWT Controls (GUI)



Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment