0% found this document useful (0 votes)
8 views8 pages

14 Bilal TicTacToe

Uploaded by

Areeb Malik
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)
8 views8 pages

14 Bilal TicTacToe

Uploaded by

Areeb Malik
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/ 8

TIC TAC TOE

C++ project
MADE BY:
NAME REG#
1- NC BILAL ABDULLAH 287542
2- NC MUHAMMAD AREEB JAFFAR 310455
3- NC MUHAMMAD ABDUL-WAHAB KHAN 295357

CODE:
#include<iostream>
#include<conio.h>
#include<cstdlib>
using namespace std;
void menu();
void startgame();
bool check_draw();
void draw_board();
void playerfirst();
void putx_o(char, int);
char choosex[], chooseo[];
char choice;
char turn;
char board[3][3] = { {'1','2','3'},{'4','5','6'},{'7','8','9'} };
int pos;
int row=0, coloumn=0;
bool draw;
char start;

void main()

{
system("color 2f");
menu();
while (check_draw())
{
draw_board();
putx_o(choice, pos);
draw_board();
check_draw();
}
if (turn == 'x' && draw == false)
{
cout << "\t\t\tP L A Y E R [ O ] W I N S.\n\n\t\t\tC O N G R A T U
L A T I O N S P L A Y E R [ O ]\n";
}
else if (turn == 'o' && draw == false)
{
cout << "\t\t\tP L A Y E R [ X ] W I N S.\n\n\t\t\tC O N G R A T U
L A T I O N S P L A Y E R [ X ]\n";
}
else if (draw == true)
{
cout << "\t\t\tG A M E D R A W S.\n\n";
}

}
void menu()
{
cout << "\n\t\t\t W E L C O M E T O \n\n";
cout << "\t\t\t T I C T A C T O E\n\n";
cout << "\t\t\t T Y P E 'Y' T O S T A R T T H E G A M
E\n";
cin >> start;
if (start == 'y'||start=='Y')
{
startgame();
}
}
void startgame()
{
cout << "\n\t\tW h a t w i l l b e y o u r c h o i c e : ' X ' o r ' O '
?\n";
cin >> choice;
playerfirst();
}
void playerfirst()
{
there:
if (choice == 'x' || choice == 'X')
{
turn = 'x';
cout << "\nP l a y e r 1 [ X ] \nP l a y e r 2 [ O ]\n ";
cout << "\t\t\tP l a y e r [ X ] t u r n 1 s t\n\n";
}
else if (choice == 'o' || choice == 'O')
{
turn = 'o';
cout << "\nP l a y e r 1 [ O ]\nP l a y e r 2 [ X ]\n\n ";
cout << "\t\t\tP l a y e r [ O ] t u r n 1 s t\n\n";
}
else {
cout << "I n v a l i d c h o i c e . P l e a s e e n t e r a g a i
n\n";
cin >> choice;
goto there;
}
cout << "\n\tI N S T R U C T I O N S:-\n";
cout << "\n\t\tE n t e r a n y n u m b e r b e t w e e n 1 t o 9 t o f i l
l t h e r e s p e c t i v e b o x\n";
cout << "\n\t\tP r e s s a n y k e y t o s t a r t g a m e\n";
system("pause");
}
void draw_board()
{
system("cls");
cout << "\t\t\t | | \n";
cout << "\t\t\t " << board[0][0] << " |" << board[0][1] << " |" <<
board[0][2] << " \n";
cout << "\t\t\t_____|_____|_____\n";
cout << "\t\t\t | | \n";
cout << "\t\t\t "<<board[1][0]<< " |"<<board[1][1]<<" |" << board[1][2] << "
\n";
cout << "\t\t\t_____|_____|_____\n";
cout << "\t\t\t | | \n";
cout << "\t\t\t " << board[2][0] << " |" << board[2][1] << " |" <<
board[2][2] << " \n";
cout << "\t\t\t | | \n\n";
if (check_draw())
{
if (turn == 'x')
cout << "\t\tP l a y e r [ X ] t u r n :\n";
else if (turn == 'o')
cout << "\t\tP l a y e r [ O ] t u r n :\n";
}
}
void putx_o(char choice, int pos)
{
cin >> pos;
here:
switch (pos)
{
case 1: {
row = 0; coloumn = 0;
break;
}
case 2: {
row = 0; coloumn = 1;
break;
}
case 3: {
row = 0; coloumn = 2;
break;
}
case 4: {
row = 1; coloumn = 0;
break;
}
case 5: {
row = 1; coloumn = 1;
break;
}
case 6: {
row = 1; coloumn = 2;
break;
}
case 7: {
row = 2; coloumn = 0;
break; }
case 8: {
row = 2; coloumn = 1;
break;
}
case 9: {
row = 2; coloumn = 2;
break;
}
default: {
cout << "Invalid choice. Please try again\n";
cin >> pos;
goto here;
break;
}
}
if (turn == 'x' && board[row][coloumn] != 'X' && board[row][coloumn] != 'O')
{
board[row][coloumn] = 'X';
turn = 'o';
}
else if (turn == 'o' && board[row][coloumn] != 'X' && board[row][coloumn] != 'O')
{
board[row][coloumn] = 'O';
turn = 'x';
}
else if (board[row][coloumn] == 'X' || board[row][coloumn ]== 'O')
{
cout << "\tB o x i s a l r e a d y f i l l e d . P l e a s e t r y
a g a i n\n";
cin >> pos;
goto here;
}
}
bool check_draw()
{
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][0] == board[i][2])
{
return false;
}
}
for (int i = 0; i < 3; i++) {
if (board[0][i] == board[1][i] && board[0][i] == board[2][i])
{
return false;
}
}
if (board[0][0] == board[1][1] && board[0][0] == board[2][2])
return false;
else if (board[0][2] == board[1][1] && board[0][2] == board[2][0])
return false;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] != 'X' && board[i][j] != 'O')
{
draw = false;
return true;
}
}
}
draw = true;
return false;

OUTPUTS:
1-

2-
3-

4-
5-

6-

You might also like