TIC TAC TOE C++ CODE (PLAYER VS COMPUTER)
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdio.h>
int checkwin();
int calcpos();
int a;
char square[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
void cleararray()
{
square[0] = '1';
square[1] = '2';
square[2] = '3';
square[3] = '4';
square[4] = '5';
square[5] = '6';
square[6] = '7';
square[7] = '8';
square[8] = '9';
}
int welcome()
{
int ch;
cout << "\n\n*************WELCOME TO TIC TAC TOE***************\n";
cout << " ______________________\n";
cout << " | | | |\n";
cout << " \t| " << 1 << " | " << 2 << " | " << 3 << " |\n";
cout << " |______|_______|_______|\n";
cout << " | | | |\n";
cout << " \t| " << 4 << " | " << 5 << " | " << 6 << " |\n";
cout << " |______|_______|_______|\n";
cout << " | | | |\n";
cout << " \t| " << 7 << " | " << 8 << " | " << 9 << " |\n";
cout << " |______|_______|_______|\n";
cout << "\n\nChoose:\n1--->Play\n2--->Exit\n";
cin >> ch;
return ch;
}
void board()
{
cout << "\n\nPLAYER 1_--->(X) PLAYER 2/COMPUTER--->(O)\n";
cout << " ______________________\n";
cout << " | | | |\n";
cout << " \t| " << square[0] << " | " << square[1] << " | " << square[2] << " |\n";
cout << " |______|_______|_______|\n";
cout << " | | | |\n";
cout << " \t| " << square[3] << " | " << square[4] << " | " << square[5] << " |\n";
cout << " |______|_______|_______|\n";
cout << " | | | |\n";
cout << " \t| " << square[6] << " | " << square[7] << " | " << square[8] << " |\n";
cout << " |______|_______|_______|\n";
}
int check()
{
if (square[0] == square[1] && square[1] == square[2]) return 1;
else if (square[3] == square[4] && square[4] == square[5]) return 1;
else if (square[6] == square[7] && square[7] == square[8]) return 1;
else if (square[0] == square[3] && square[3] == square[6]) return 1;
else if (square[1] == square[4] && square[4] == square[7]) return 1;
else if (square[2] == square[5] && square[5] == square[8]) return 1;
else if (square[0] == square[4] && square[4] == square[8]) return 1;
else if (square[2] == square[4] && square[4] == square[6]) return 1;
else if (square[0] != '1'&&square[1] != '2'&&square[2] != '3'&&square[3] != '4'&&square[4] != '5'&&square[5] != '6'&&square[6] != '7'&&square[7] != '8'&&square[8] != '9')
return 0;
else
return -1;
}
void computer()
{
int player = 1, i, choice, turn = 0;
char mark;
do
{
board();
if (player == 1 && turn == 0)
{
cout << "Player " << player << ", enter a number: ";
cin >> choice;
}
else
{
choice = calcpos();
}
if (turn == 0)
{
mark = 'X';
turn = 1;
}
else
{
mark = 'O';
turn = 0;
}
if(choice == 1 && square[0] == '1') square[0] = mark;
else if (choice == 2 && square[1] == '2') square[1] = mark;
else if (choice == 3 && square[2] == '3') square[2] = mark;
else if (choice == 4 && square[3] == '4') square[3] = mark;
else if (choice == 5 && square[4] == '5')square[4] = mark;
else if (choice == 6 && square[5] == '6')square[5] = mark;
else if (choice == 7 && square[6] == '7')square[6] = mark;
else if (choice == 8 && square[7] == '8')square[7] = mark;
else if (choice == 9 && square[8] == '9')square[8] = mark;
else
{
cout << "Invalid move ";
turn = 0;
}
i = check();
} while (i == -1);
board();
if (i == 1)
{
if (turn == 1)
{
cout << "==>\nPlayer " << player << " win ";
cleararray();
}
else
cout << "==>\nComputer win "; cleararray();
}
else
{
cout << "==>\nGame draw";
cleararray();
}
}
int calcpos()
{
if (square[4] == '5')
return 5;
else if (checkwin())
return (checkwin());
else
{
if (square[6] == square[5] && square[5] == 'X') return 9;
else if (square[0] == square[7] && square[7] == 'X') return 6;
else if (square[2] == square[7] && square[7] == 'X') return 4;
else if (square[5] == square[7] && square[8] == '9') return 9;
else if (square[0] == square[9] && square[9] == 'X') return 2;
else if (square[2] == square[6] && square[6] == 'X') return 2;
else if (square[0] == square[8] && square[8] == 'X') return 2;
else if (square[0] == '1') return 1;
else if (square[2] == '3') return 3;
else if (square[6] == '7') return 7;
else if (square[3] == '4') return 4;
else if (square[1] == '2') return 2;
else if (square[5] == '6') return 6;
}
}
int checkwin()
{
if (square[0] == square[1] && square[2] == '3') return 3;
else if (square[0] == square[2] && square[1] == '2') return 2;
else if (square[0] == square[3] && square[6] == '7') return 7;
else if (square[0] == square[6] && square[3] == '4') return 4;
else if (square[0] == square[4] && square[8] == '9') return 9;
else if (square[0] == square[8] && square[4] == '5') return 5;
else if (square[1] == square[4] && square[7] == '8') return 8;
else if (square[1] == square[7] && square[4] == '5') return 5;
else if (square[1] == square[2] && square[0] == '1') return 1;
else if (square[3] == square[6] && square[0] == '1') return 1;
else if (square[3] == square[5] && square[4] == '5') return 5;
else if (square[3] == square[4] && square[5] == '6') return 6;
else if (square[2] == square[1] && square[0] == '1') return 1;
else if (square[2] == square[4] && square[6] == '7') return 7;
else if (square[2] == square[6] && square[4] == '5') return 5;
else if (square[2] == square[5] && square[8] == '9') return 9;
else if (square[2] == square[8] && square[5] == '6') return 6;
else if (square[6] == square[3] && square[0] == '1') return 1;
else if (square[6] == square[8] && square[7] == '8') return 8;
else if (square[6] == square[7] && square[8] == '9') return 9;
else if (square[6] == square[4] && square[2] == '3') return 3;
else if (square[6] == square[2] && square[4] == '5') return 5;
else if (square[8] == square[5] && square[2] == '3') return 3;
else if (square[8] == square[4] && square[0] == '1') return 1;
else if (square[7] == square[4] && square[1] == '2') return 2;
else if (square[7] == square[8] && square[6] == '7') return 7;
else if (square[5] == square[4] && square[3] == '4') return 4;
else return 0;
}
void main()
{
do
{
a = welcome();
switch (a)
{
case 1:
{computer(); board();}break;
case 2:break;
default:break;
}
} while (a <= 2);
system("pause");
}
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdio.h>
int checkwin();
int calcpos();
int a;
char square[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
void cleararray()
{
square[0] = '1';
square[1] = '2';
square[2] = '3';
square[3] = '4';
square[4] = '5';
square[5] = '6';
square[6] = '7';
square[7] = '8';
square[8] = '9';
}
int welcome()
{
int ch;
cout << "\n\n*************WELCOME TO TIC TAC TOE***************\n";
cout << " ______________________\n";
cout << " | | | |\n";
cout << " \t| " << 1 << " | " << 2 << " | " << 3 << " |\n";
cout << " |______|_______|_______|\n";
cout << " | | | |\n";
cout << " \t| " << 4 << " | " << 5 << " | " << 6 << " |\n";
cout << " |______|_______|_______|\n";
cout << " | | | |\n";
cout << " \t| " << 7 << " | " << 8 << " | " << 9 << " |\n";
cout << " |______|_______|_______|\n";
cout << "\n\nChoose:\n1--->Play\n2--->Exit\n";
cin >> ch;
return ch;
}
void board()
{
cout << "\n\nPLAYER 1_--->(X) PLAYER 2/COMPUTER--->(O)\n";
cout << " ______________________\n";
cout << " | | | |\n";
cout << " \t| " << square[0] << " | " << square[1] << " | " << square[2] << " |\n";
cout << " |______|_______|_______|\n";
cout << " | | | |\n";
cout << " \t| " << square[3] << " | " << square[4] << " | " << square[5] << " |\n";
cout << " |______|_______|_______|\n";
cout << " | | | |\n";
cout << " \t| " << square[6] << " | " << square[7] << " | " << square[8] << " |\n";
cout << " |______|_______|_______|\n";
}
int check()
{
if (square[0] == square[1] && square[1] == square[2]) return 1;
else if (square[3] == square[4] && square[4] == square[5]) return 1;
else if (square[6] == square[7] && square[7] == square[8]) return 1;
else if (square[0] == square[3] && square[3] == square[6]) return 1;
else if (square[1] == square[4] && square[4] == square[7]) return 1;
else if (square[2] == square[5] && square[5] == square[8]) return 1;
else if (square[0] == square[4] && square[4] == square[8]) return 1;
else if (square[2] == square[4] && square[4] == square[6]) return 1;
else if (square[0] != '1'&&square[1] != '2'&&square[2] != '3'&&square[3] != '4'&&square[4] != '5'&&square[5] != '6'&&square[6] != '7'&&square[7] != '8'&&square[8] != '9')
return 0;
else
return -1;
}
void computer()
{
int player = 1, i, choice, turn = 0;
char mark;
do
{
board();
if (player == 1 && turn == 0)
{
cout << "Player " << player << ", enter a number: ";
cin >> choice;
}
else
{
choice = calcpos();
}
if (turn == 0)
{
mark = 'X';
turn = 1;
}
else
{
mark = 'O';
turn = 0;
}
if(choice == 1 && square[0] == '1') square[0] = mark;
else if (choice == 2 && square[1] == '2') square[1] = mark;
else if (choice == 3 && square[2] == '3') square[2] = mark;
else if (choice == 4 && square[3] == '4') square[3] = mark;
else if (choice == 5 && square[4] == '5')square[4] = mark;
else if (choice == 6 && square[5] == '6')square[5] = mark;
else if (choice == 7 && square[6] == '7')square[6] = mark;
else if (choice == 8 && square[7] == '8')square[7] = mark;
else if (choice == 9 && square[8] == '9')square[8] = mark;
else
{
cout << "Invalid move ";
turn = 0;
}
i = check();
} while (i == -1);
board();
if (i == 1)
{
if (turn == 1)
{
cout << "==>\nPlayer " << player << " win ";
cleararray();
}
else
cout << "==>\nComputer win "; cleararray();
}
else
{
cout << "==>\nGame draw";
cleararray();
}
}
int calcpos()
{
if (square[4] == '5')
return 5;
else if (checkwin())
return (checkwin());
else
{
if (square[6] == square[5] && square[5] == 'X') return 9;
else if (square[0] == square[7] && square[7] == 'X') return 6;
else if (square[2] == square[7] && square[7] == 'X') return 4;
else if (square[5] == square[7] && square[8] == '9') return 9;
else if (square[0] == square[9] && square[9] == 'X') return 2;
else if (square[2] == square[6] && square[6] == 'X') return 2;
else if (square[0] == square[8] && square[8] == 'X') return 2;
else if (square[0] == '1') return 1;
else if (square[2] == '3') return 3;
else if (square[6] == '7') return 7;
else if (square[3] == '4') return 4;
else if (square[1] == '2') return 2;
else if (square[5] == '6') return 6;
}
}
int checkwin()
{
if (square[0] == square[1] && square[2] == '3') return 3;
else if (square[0] == square[2] && square[1] == '2') return 2;
else if (square[0] == square[3] && square[6] == '7') return 7;
else if (square[0] == square[6] && square[3] == '4') return 4;
else if (square[0] == square[4] && square[8] == '9') return 9;
else if (square[0] == square[8] && square[4] == '5') return 5;
else if (square[1] == square[4] && square[7] == '8') return 8;
else if (square[1] == square[7] && square[4] == '5') return 5;
else if (square[1] == square[2] && square[0] == '1') return 1;
else if (square[3] == square[6] && square[0] == '1') return 1;
else if (square[3] == square[5] && square[4] == '5') return 5;
else if (square[3] == square[4] && square[5] == '6') return 6;
else if (square[2] == square[1] && square[0] == '1') return 1;
else if (square[2] == square[4] && square[6] == '7') return 7;
else if (square[2] == square[6] && square[4] == '5') return 5;
else if (square[2] == square[5] && square[8] == '9') return 9;
else if (square[2] == square[8] && square[5] == '6') return 6;
else if (square[6] == square[3] && square[0] == '1') return 1;
else if (square[6] == square[8] && square[7] == '8') return 8;
else if (square[6] == square[7] && square[8] == '9') return 9;
else if (square[6] == square[4] && square[2] == '3') return 3;
else if (square[6] == square[2] && square[4] == '5') return 5;
else if (square[8] == square[5] && square[2] == '3') return 3;
else if (square[8] == square[4] && square[0] == '1') return 1;
else if (square[7] == square[4] && square[1] == '2') return 2;
else if (square[7] == square[8] && square[6] == '7') return 7;
else if (square[5] == square[4] && square[3] == '4') return 4;
else return 0;
}
void main()
{
do
{
a = welcome();
switch (a)
{
case 1:
{computer(); board();}break;
case 2:break;
default:break;
}
} while (a <= 2);
system("pause");
}