Tic Tac Game is very interesting game in which a player wins if he successfully completes a straight line. Program prompts user to select a symbol to play and one symbol for computer. At the Winning or losing the prompt message will display and asked user to play again or not.
Source Code
#include<iostream>
#include<windows.h>
#include<cctype>
using namespace std;
char box[10] = {'0','1','2','3','4','5','6','7','8','9'};
int winCheckOfPlayer();
int winCheckOfComp();
void makingOfBoard();
void funcOfComp();
void funcOfPlayer();
void process();
void menu();
char mark1;
char mark2;
int count1 = 0;
int count2 = 0;
int a;
int main()
{
system("cls");
menu();
return 0;
}
void makingOfBoard()
{
system("cls");
cout << endl;
cout << " | | " << endl;
cout << " " << box[1] << " | " << box[2] << " | " << box[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << box[4] << " | " << box[5] << " | " << box[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << box[7] << " | " << box[8] << " | " << box[9] << endl;
cout << " | | " << endl << endl;
}
void funcOfPlayer()
{
do{
a = 0;
int choice = 0;
bool b = false;
do
{
cout<<"\nPlease enter any number you want to mark "<<endl;
cin>>choice;
b = cin.fail();
if(b)
{
cout<<"wrong input"<<endl;
cin.clear();
cin.ignore();
}
}while(b);
if (choice == 1 && box[1] == '1')
{
box[1] = mark1;
}
else if (choice == 2 && box[2] == '2')
{
box[2] = mark1;
}
else if (choice == 3 && box[3] == '3')
{
box[3] = mark1;
}
else if (choice == 4 && box[4] == '4')
{
box[4] = mark1;
}
else if (choice == 5 && box[5] == '5')
{
box[5] = mark1;
}
else if (choice == 6 && box[6] == '6')
{
box[6] = mark1;
}
else if (choice == 7 && box[7] == '7')
{
box[7] = mark1;
}
else if (choice == 8 && box[8] == '8')
{
box[8] = mark1;
}
else if (choice == 9 && box[9] == '9')
{
box[9] = mark1;
}
else
{
a = a+1;
cout<<"Error!!Invalid move,Try again! ";
cin.clear();
cin.ignore();
}
}while(!(a==0));
}
int winCheckOfPlayer()
{
if (box[1] == mark1 && box[1] == box[2] && box[2]== box[3])
{
return 1;
}
else if (box[4] == mark1 && box[4] == box[5] && box[5] == box[6])
{
return 1;
}
else if (box[7] == mark1 && box[7] == box[8] && box[8] == box[9])
{
return 1;
}
else if (box[1] == mark1 && box[1] == box[4] && box[4] == box[7])
{
return 1;
}
else if (box[2] == mark1 && box[2] == box[5] && box[5] == box[8])
{
return 1;
}
else if (box[3] == mark1 && box[3] == box[6] && box[6] ==box [9])
{
return 1;
}
else if (box[1] == mark1 && box[1] == box[5] && box[5] == box[9])
{
return 1;
}
else if (box[3] == mark1 && box[3] == box[5] && box[5] == box[7])
{
return 1;
}
else if
(box[1] != '1' && box[2] != '2' && box[3] != '3' && box[4] != '4' && box[5] != '5' && box[6] != '6' && box[7] != '7' && box[8] != '8' && box[9] != '9')
{
return 0;
}
else
return -1;
}
int winCheckOfComp()
{
if (box[1] == mark2 && box[1] == box[2] && box[2]== box[3])
{
return 2;
}
else if (box[4] == mark2 && box[4] == box[5] && box[5] == box[6])
{
return 2;
}
else if (box[7] == mark2 && box[7] == box[8] && box[8] == box[9])
{
return 2;
}
else if (box[1] == mark2 && box[1] == box[4] && box[4] == box[7])
{
return 2;
}
else if (box[2] == mark2 && box[2] == box[5] && box[5] == box[8])
{
return 2;
}
else if (box[3] == mark2 && box[3] == box[6] && box[6] ==box [9])
{
return 2;
}
else if (box[1] == mark2 && box[1] == box[5] && box[5] == box[9])
{
return 2;
}
else if (box[3] == mark2 && box[3] == box[5] && box[5] == box[7])
{
return 2;
}
else if (box[1] != '1' && box[2] != '2' && box[3] != '3' && box[4] != '4' && box[5] != '5' && box[6] != '6' && box[7] != '7' && box[8] != '8' && box[9] != '9')
{
return 0;
}
else
return -1;
}
void funcOfComp()
{
if(box[5] == '5')
{
box[5] = mark2;
}
else if(box[1] == mark2 && box[2] ==mark2 && box[3] == '3')
{
box[3] = mark2;
}
else if(box[2] == mark2 && box[3] ==mark2 && box[1] == '1')
{
box[1] = mark2;
}
else if(box[1] == mark2 && box[3] ==mark2 && box[2] == '2')
{
box[2] = mark2;
}
else if(box[4] == mark2 && box[5] ==mark2 && box[6] == '6')
{
box[6] = mark2;
}
else if(box[5] == mark2 && box[6] ==mark2 && box[4] == '4')
{
box[4] = mark2;
}
else if(box[4] == mark2 && box[6] ==mark2 && box[5] == '5')
{
box[5] = mark2;
}
else if(box[7] == mark2 && box[8] ==mark2 && box[9] == '9')
{
box[9] = mark2;
}
else if(box[9] == mark2 && box[8] ==mark2 && box[7] == '7')
{
box[7] = mark2;
}
else if(box[7] == mark2 && box[9] ==mark2 && box[8] == '8')
{
box[8] = mark2;
}
else if(box[1] == mark2 && box[4] ==mark2 && box[7] == '7')
{
box[7] = mark2;
}
else if(box[4] == mark2 && box[7] ==mark2 && box[1] == '1')
{
box[1] = mark2;
}
else if(box[1] == mark2 && box[7] ==mark2 && box[4] == '4')
{
box[4] = mark2;
}
else if(box[2] == mark2 && box[5] ==mark2 && box[8] == '8')
{
box[8] = mark2;
}
else if(box[5] == mark2 && box[8] ==mark2 && box[2] == '2')
{
box[2] = mark2;
}
else if(box[2] == mark2 && box[8] ==mark2 && box[5] == '5')
{
box[2] = mark2;
}
else if(box[3] == mark2 && box[6] ==mark2 && box[9] == '9')
{
box[9] = mark2;
}
else if(box[6] == mark2 && box[9] ==mark2 && box[3] == '3')
{
box[3] = mark2;
}
else if(box[3] == mark2 && box[9] ==mark2 && box[6] == '6')
{
box[6] = mark2;
}
else if(box[1] == mark2 && box[5] ==mark2 && box[9] == '9')
{
box[9] = mark2;
}
else if(box[5] == mark2 && box[9] ==mark2 && box[1] == '1')
{
box[1] = mark2;
}
else if(box[1] == mark2 && box[9] ==mark2 && box[5] == '5')
{
box[5] = mark2;
}
else if(box[3] == mark2 && box[5] ==mark2 && box[7] == '7')
{
box[7] = mark2;
}
else if(box[5] == mark2 && box[7] ==mark2 && box[3] == '3')
{
box[3] = mark2;
}
else if(box[3] == mark2 && box[7] ==mark2 && box[5] == '5')
{
box[5] = mark2;
}
else if(box[1] == mark1 && box[2] ==mark1 && box[3] == '3')
{
box[3] = mark2;
}
else if(box[2] == mark1 && box[3] ==mark1 && box[1] == '1')
{
box[1] = mark2;
}
else if(box[1] == mark1 && box[3] ==mark1 && box[2] == '2')
{
box[2] = mark2;
}
else if(box[4] == mark1 && box[5] ==mark1 && box[6] == '6')
{
box[6] = mark2;
}
else if(box[5] == mark1 && box[6] ==mark1 && box[4] == '4')
{
box[4] = mark2;
}
else if(box[4] == mark1 && box[6] ==mark1 && box[5] == '5')
{
box[5] = mark2;
}
else if(box[7] == mark1 && box[8] ==mark1 && box[9] == '9')
{
box[9] = mark2;
}
else if(box[9] == mark1 && box[8] ==mark1 && box[7] == '7')
{
box[7] = mark2;
}
else if(box[7] == mark1 && box[9] ==mark1 && box[8] == '8')
{
box[8] = mark2;
}
else if(box[1] == mark1 && box[4] ==mark1 && box[7] == '7')
{
box[7] = mark2;
}
else if(box[4] == mark1 && box[7] ==mark1 && box[1] == '1')
{
box[1] = mark2;
}
else if(box[1] == mark1 && box[7] ==mark1 && box[4] == '4')
{
box[4] = mark2;
}
else if(box[2] == mark1 && box[5] ==mark1 && box[8] == '8')
{
box[8] = mark2;
}
else if(box[5] == mark1 && box[8] ==mark1 && box[2] == '2')
{
box[2] = mark2;
}
else if(box[2] == mark1 && box[8] ==mark1 && box[5] == '5')
{
box[2] = mark2;
}
else if(box[3] == mark1 && box[6] ==mark1 && box[9] == '9')
{
box[9] = mark2;
}
else if(box[6] == mark1 && box[9] ==mark1 && box[3] == '3')
{
box[3] = mark2;
}
else if(box[3] == mark1 && box[9] ==mark1 && box[6] == '6')
{
box[6] = mark2;
}
else if(box[1] == mark1 && box[5] ==mark1 && box[9] == '9')
{
box[9] = mark2;
}
else if(box[5] == mark1 && box[9] ==mark1 && box[1] == '1')
{
box[1] = mark2;
}
else if(box[1] == mark1 && box[9] ==mark1 && box[5] == '5')
{
box[5] = mark2;
}
else if(box[3] == mark1 && box[5] ==mark1 && box[7] == '7')
{
box[7] = mark2;
}
else if(box[5] == mark1 && box[7] ==mark1 && box[3] == '3')
{
box[3] = mark2;
}
else if(box[3] == mark1 && box[7] ==mark1 && box[5] == '5')
{
box[5] = mark2;
}
else if((box[3] == mark1 || box[7] == mark1 || box[9] == mark1) && box[1] == '1')
{
box[1] = mark2;
}
else if((box[1] == mark1 || box[7] == mark1 || box[9] == mark1) && box[3] == '3')
{
box[3] = mark2;
}
else if((box[3] == mark1 || box[1] == mark1 || box[9] == mark1) && box[7] == '7')
{
box[7] = mark2;
}
else if((box[3] == mark1 || box[7] == mark1 || box[1] == mark1) && box[9] == '9')
{
box[9] = mark2;
}
else if((box[8] == mark1 || box[3] == mark1 || box[1] == mark1) && box[2] == '2')
{
box[2] = mark2;
}
else if((box[1] == mark1 || box[7] == mark1 || box[6] == mark1) && box[4] == '4')
{
box[4] = mark2;
}
else if((box[2] == mark1 || box[4] == mark1 || box[9] == mark1) && box[6] == '6')
{
box[6] = mark2;
}
else if((box[2] == mark1 || box[7] == mark1 || box[9] == mark1) && box[8] == '8')
{
box[8] = mark2;
}
else
{
if(box[1] == '1')
{
box[1] = mark2;
}
}
}
void menu()
{
int reply = 0;
bool val = false;
system("cls");
system("color 4a");
cout<<"\n\n\t *****************************************************";
cout << "\n\n\t\t Welcome to Tick Cross Game \n\n\t";
cout<<"*****************************************************\n\n";
cout<<"\n 1: Play with X";
cout<<"\n 2: Play with O";
cout<<"\n 3: Exit";
do{
cout<<"\nEnter your choice : ";
cin>>reply;
val = cin.fail();
if(val)
{
cout<<"Wrong input"<<endl;
cin.clear();
cin.ignore();
}
}while(val);
switch(reply)
{
case 1:
{
mark1 = 'X';
mark2 = 'O';
process();
}
break;
case 2:
{
mark1 = 'O';
mark2 = 'X';
process();
}
break;
case 3:
{
exit(1);
}
break;
default:
menu();
}
}
void process()
{
//char choice;
// do
// {
system("cls");
do
{
makingOfBoard();
funcOfPlayer();
count1= winCheckOfPlayer();
if(count1 != -1)
{
break;
}
funcOfComp();
count2= winCheckOfComp();
}while(count1 == -1 && count2 == -1);
makingOfBoard();
if(count1 == 1)
{
cout<<"Congrats!!You won the game"<<endl;
system("pause");
}
else if(count2 == 2)
{
cout<<"Computer wins the game "<<endl;
system("pause");
}
else
{
cout<<"Game draw "<<endl;
system("pause");
}
/*cout<<"Do you want to play again?? Press Y or N!"<<endl;
cin>>choice;
choice = (tolower(choice));
}while(choice == 'y');*/
}
|
Tic Tac Toe Game in C++ |
Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of
BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
Thanks for giving that Code i need it for Project :)
ReplyDelete