0% found this document useful (0 votes)
30 views

Tic Tac Toe Project

This C++ program implements a tic-tac-toe game. It uses a char array to represent the game board and functions to check for a winner, display the board, and control the main game loop. The checkwin() function evaluates all possible win conditions and returns 1 if a player has won, 0 if a draw, or -1 if the game continues. The main() function handles player turns, input, and displays the outcome.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Tic Tac Toe Project

This C++ program implements a tic-tac-toe game. It uses a char array to represent the game board and functions to check for a winner, display the board, and control the main game loop. The checkwin() function evaluates all possible win conditions and returns 1 if a player has won, 0 if a draw, or -1 if the game continues. The main() function handles player turns, input, and displays the outcome.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

using namespace std;

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


int checkwin()
{
if (box[1] == box[2] && box[2] == box[3])
return 1;
else if (box[4] == box[5] && box[5] == box[6])
return 1;
else if (box[7] == box[8] && box[8] == box[9])
return 1;
else if (box[1] == box[4] && box[4] == box[7])
return 1;
else if (box[2] == box[5] && box[5] == box[8])
return 1;
else if (box[3] == box[6] && box[6] == box[9])
return 1;
else if (box[1] == box[5] && box[5] == box[9])
return 1;
else if (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;
}

void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
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;
}
int main()
{
int player = 1,i,choice;
char sign;
do
{
board();

if(player % 2)
{
player = 1;
}
else
{
player = 2;
}
cout << "Player " << player << ", enter a number: ";
cin >> choice;
if(player == 1)
{
sign = 'X';
}
else
{
sign = 'O';
}
if (choice == 1 && box[1] == '1')
box[1] = sign;
else if (choice == 2 && box[2] == '2')
box[2] = sign;
else if (choice == 3 && box[3] == '3')
box[3] = sign;
else if (choice == 4 && box[4] == '4')
box[4] = sign;
else if (choice == 5 && box[5] == '5')
box[5] = sign;
else if (choice == 6 && box[6] == '6')
box[6] = sign;
else if (choice == 7 && box[7] == '7')
box[7] = sign;
else if (choice == 8 && box[8] == '8')
box[8] = sign;
else if (choice == 9 && box[9] == '9')
box[9] = sign;
else
{
cout<<"Invalid move ";
player--;
cin.ignore();
cin.get();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";
cin.ignore();
cin.get();
return 0;
}
/*********************************************
FUNCTION TO RETURN GAME STATUS
1 FOR GAME IS OVER WITH RESULT
-1 FOR GAME IS IN PROGRESS
O GAME IS DRAW
**********************************************

/*******************************************************************
END OF PROJECT
********************************************************************/

You might also like