#include <iostream>
#include <Windows.h>
#include <ctime>
using namespace std;
void DrawGame();
void Navigate();
void ChoicePlayer();
void Choose();
void ComputerPlay();
void Random();
void Check();
void Won();
void Drow();
void ClearPlays();
int rndPlay;
char Plays[9] = {'?','?','?','?','?','?','?','?','?'};
int Selected = 0;
char Player;
char Computer;
int main(){
ChoicePlayer();
ClearPlays();
DrawGame();
while(1){
Navigate();
Choose();
Check();
}
system("pause");
return 0;
}
char Winner;
void ClearPlays(){
Plays[0] = '?';
Plays[1] = '?';
Plays[2] = '?';
Plays[3] = '?';
Plays[4] = '?';
Plays[5] = '?';
Plays[6] = '?';
Plays[7] = '?';
Plays[8] = '?';
return;
}
char PlayAgain;
void Won(){
ClearPlays();
system("cls");
cout << "\t\t\tThe winner is: " << Winner << endl;
if(Player != Winner){
cout << "\t\t\tYou just lost =(" << endl;
}else {
cout << "\t\t\tYOU WIN!" << endl;
}
ClearPlays();
Winner = NULL;
system("pause");
ClearPlays();
main();
}
void Check(){
// Computer Checks
if(Plays[0] == Computer && Plays[3] == Computer && Plays[6] == Computer){
Winner = Computer;
Won();
return; }
if(Plays[1] == Computer && Plays[4] == Computer && Plays[7] == Computer){
Winner = Computer;
Won();
return; }
if(Plays[2] == Computer && Plays[5] == Computer && Plays[8] == Computer){
Winner = Computer;
Won();
return; }
if(Plays[0] == Computer && Plays[1] == Computer && Plays[2] == Computer){
Winner = Computer;
Won();
return; }
if(Plays[3] == Computer && Plays[4] == Computer && Plays[5] == Computer){
Winner = Computer;
Won();
return; }
if(Plays[6] == Computer && Plays[7] == Computer && Plays[8] == Computer){
Winner = Computer;
Won();
return; }
if(Plays[0] == Computer && Plays[4] == Computer && Plays[8] == Computer){
Winner = Computer;
Won();
return; }
if(Plays[2] == Computer && Plays[4] == Computer && Plays[6] == Computer){
Winner = Computer;
Won();
return; }
//Player Checks
if(Plays[0] == Player && Plays[3] == Player && Plays[6] == Player){
Winner = Player;
Won();
return; }
if(Plays[1] == Player && Plays[4] == Player && Plays[7] == Player){
Winner = Player;
Won();
return; }
if(Plays[2] == Player && Plays[5] == Player && Plays[8] == Player){
Winner = Player;
Won();
return; }
if(Plays[0] == Player && Plays[1] == Player && Plays[2] == Player){
Winner = Player;
Won();
return; }
if(Plays[3] == Player && Plays[4] == Player && Plays[5] == Player){
Winner = Player;
Won();
return; }
if(Plays[6] == Player && Plays[7] == Player && Plays[8] == Player){
Winner = Player;
Won();
return; }
if(Plays[0] == Player && Plays[4] == Player && Plays[8] == Player){
Winner = Player;
Won();
return; }
if(Plays[2] == Player && Plays[4] == Player && Plays[6] == Player){
Winner = Player;
Won();
return; }
if(Plays[0] != '?' && Plays[1] != '?' && Plays[2] != '?' && Plays[3] != '?' && Plays[4] != '?' && Plays[5] != '?' && Plays[6] != '?' && Plays[7] != '?' && Plays[8] != '?'){
Drow();
}
}
void Drow(){
ClearPlays();
system("cls");
cout << "\t\t\tNo one won!" << Winner << endl;
cout << "\t\t\tLets play again :P" << endl;
Sleep(2000);
ClearPlays();
Winner = NULL;
system("pause");
ClearPlays();
main();
}
void Random(){
time_t t;
time(&t);
srand(t);
rndPlay = (rand() % 9);
}
#define PC Computer
#define EU Player
void ComputerPlay(){
//wins
if(Plays[2] == '?'){
if(Plays[0] == PC && Plays[1] == PC){
Plays[2] = PC;
return;
}
}
if(Plays[5] == '?'){
if(Plays[3] == PC && Plays[4] == PC){
Plays[5] = PC;
return;
}
}
if(Plays[8] == '?'){
if(Plays[6] == PC && Plays[7] == PC){
Plays[8] = PC;
return;
}
}
if(Plays[0] == '?'){
if(Plays[1] == PC && Plays[2] == PC){
Plays[0] = PC;
return;
}
}
if(Plays[3] == '?'){
if(Plays[4] == PC && Plays[5] == PC){
Plays[3] = PC;
return;
}
}
if(Plays[6] == '?'){
if(Plays[7] == PC && Plays[8] == PC){
Plays[6] = PC;
return;
}
}
if(Plays[6] == '?'){
if(Plays[0] == PC && Plays[3] == PC){
Plays[6] = PC;
return;
}
}
if(Plays[5] == '?'){
if(Plays[1] == PC && Plays[4] == PC){
Plays[5] = PC;
return;
}
}
if(Plays[8] == '?'){
if(Plays[2] == PC && Plays[5] == PC){
Plays[8] = PC;
return;
}
}
if(Plays[0] == '?'){
if(Plays[6] == PC && Plays[3] == PC){
Plays[0] = PC;
return;
}
}
if(Plays[1] == '?'){
if(Plays[7] == PC && Plays[4] == PC){
Plays[1] = PC;
return;
}
}
if(Plays[2] == '?'){
if(Plays[8] == PC && Plays[5] == PC){
Plays[2] = PC;
return;
}
}
if(Plays[2] == '?'){
if(Plays[6] == PC && Plays[4] == PC){
Plays[2] = PC;
return;
}
}
if(Plays[0] == '?'){
if(Plays[8] == PC && Plays[4] == PC){
Plays[0] = PC;
return;
}
}
if(Plays[8] == '?'){
if(Plays[0] == PC && Plays[4] == PC){
Plays[8] = PC;
return;
}
}
if(Plays[6] == '?'){
if(Plays[2] == PC && Plays[4] == PC){
Plays[6] = PC;
return;
}
}
/*
01 = 2 >>>
34 = 5
67 = 8
12 = 0 <<
45 = 3
78 = 6
03=6 v
14=5 v
25=8
63 = 0 ^
74 =1 ^
85=2
64 = 2 ^>
84=0
04=8
24=6
*/
//------------END WINS
//Dont lost
if(Plays[2] == '?'){
if(Plays[0] == EU && Plays[1] == EU){
Plays[2] = PC;
return;
}
}
if(Plays[5] == '?'){
if(Plays[3] == EU && Plays[4] == EU){
Plays[5] = PC;
return;
}
}
if(Plays[8] == '?'){
if(Plays[6] == EU && Plays[7] == EU){
Plays[8] = PC;
return;
}
}
if(Plays[0] == '?'){
if(Plays[1] == EU && Plays[2] == EU){
Plays[0] = PC;
return;
}
}
if(Plays[3] == '?'){
if(Plays[4] == EU && Plays[5] == EU){
Plays[3] = PC;
return;
}
}
if(Plays[6] == '?'){
if(Plays[7] == EU && Plays[8] == EU){
Plays[6] = PC;
return;
}
}
if(Plays[6] == '?'){
if(Plays[0] == EU && Plays[3] == EU){
Plays[6] = PC;
return;
}
}
if(Plays[5] == '?'){
if(Plays[1] == EU && Plays[4] == EU){
Plays[5] = PC;
return;
}
}
if(Plays[8] == '?'){
if(Plays[2] == EU && Plays[5] == EU){
Plays[8] = PC;
return;
}
}
if(Plays[0] == '?'){
if(Plays[6] == EU && Plays[3] == EU){
Plays[0] = PC;
return;
}
}
if(Plays[1] == '?'){
if(Plays[7] == EU && Plays[4] == EU){
Plays[1] = PC;
return;
}
}
if(Plays[2] == '?'){
if(Plays[8] == EU && Plays[5] == EU){
Plays[2] = PC;
return;
}
}
if(Plays[2] == '?'){
if(Plays[6] == EU && Plays[4] == EU){
Plays[2] = PC;
return;
}
}
if(Plays[0] == '?'){
if(Plays[8] == EU && Plays[4] == EU){
Plays[0] = PC;
return;
}
}
if(Plays[8] == '?'){
if(Plays[0] == EU && Plays[4] == EU){
Plays[8] = PC;
return;
}
}
if(Plays[6] == '?'){
if(Plays[2] == EU && Plays[4] == EU){
Plays[6] = PC;
return;
}
}
if(Plays[1] == '?'){
if(Plays[0] == EU && Plays[2] == EU){
Plays[1] = PC;
return;
}
}
if(Plays[4] == '?'){
if(Plays[3] == EU && Plays[5] == EU){
Plays[4] = PC;
return;
}
}
if(Plays[7] == '?'){
if(Plays[6] == EU && Plays[8] == EU){
Plays[7] = PC;
return;
}
}
if(Plays[3] == '?'){
if(Plays[0] == EU && Plays[6] == EU){
Plays[3] = PC;
return;
}
}
if(Plays[4] == '?'){
if(Plays[7] == EU && Plays[1] == EU){
Plays[4] = PC;
return;
}
}
if(Plays[5] == '?'){
if(Plays[8] == EU && Plays[2] == EU){
Plays[5] = PC;
return;
}
}
if(Plays[4] == '?'){
if(Plays[6] == EU && Plays[2] == EU){
Plays[4] = PC;
return;
}
}
if(Plays[4] == '?'){
if(Plays[0] == EU && Plays[8] == EU){
Plays[4] = PC;
return;
}
}
/*
02 = 1
35 = 4
68 = 7
06=3
71 = 4
82=5
62 = 4
08 = 4
*/
rndBegin:
Random();
if(Plays[rndPlay] == '?'){
Plays[rndPlay] = Computer;
} else {
goto rndBegin;
}
}
void Choose(){
if(GetAsyncKeyState(VK_SPACE)){
switch(Selected){
case 0:
if(Plays[0] == '?'){
Plays[0] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
case 1:
if(Plays[1] == '?'){
Plays[1] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
case 2:
if(Plays[2] == '?'){
Plays[2] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
case 3:
if(Plays[3] == '?'){
Plays[3] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
case 4:
if(Plays[4] == '?'){
Plays[4] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
case 5:
if(Plays[5] == '?'){
Plays[5] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
case 6:
if(Plays[6] == '?'){
Plays[6] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
case 7:
if(Plays[7] == '?'){
Plays[7] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
case 8:
if(Plays[8] == '?'){
Plays[8] = Player;
Check();
ComputerPlay();}
DrawGame();
break;
default:
break;
}
Sleep(900);
}
}
char choice;
void ChoicePlayer(){
begin:
ClearPlays();
system("cls");
cout << "\t\t\t What do you want to be? X or O?" << endl;
cin >> choice;
if(choice == 'X' || choice == 'x'){
Player = 'X';
Computer = 'O';
cout << "\t\t\tYou are now the player: " << Player << endl;
Sleep(2000);
ClearPlays();
return;}
if(choice == 'O' || choice == 'o'){
Player = 'O';
Computer = 'X';
cout << "\t\t\tYou are now the player: " << Player << endl;
Sleep(2000);
ClearPlays();
return;}
if(choice != 'o' || choice != 'O' && choice != 'x' || choice != 'X'){
cout << "\t\t\t Only X or O, please choose again." << endl;
Sleep(2000);
ClearPlays();
goto begin;
}
ClearPlays();
}
void Navigate(){
if(GetAsyncKeyState(VK_DOWN)){
Selected += 3;
if(Selected > 8)Selected = 0;
DrawGame();
Sleep(900);}
if(GetAsyncKeyState(VK_UP)){
Selected -= 3;
if(Selected < 0)Selected = 0;
DrawGame();
Sleep(900);}
if(GetAsyncKeyState(VK_LEFT)){
Selected--;
if(Selected < 0)Selected = 8;
DrawGame();
Sleep(900);}
if(GetAsyncKeyState(VK_RIGHT)){
Selected++;
if(Selected > 8)Selected = 0;
DrawGame();
Sleep(900);}
}
void LINE(){
cout << "\n\n\n\n\n\n\t\t\t" << endl;
}
void DrawGame(){
system("cls");
LINE();
if(Selected == 0){
cout << "\t\t\t" << "[" << Plays[0] << "]" << "\t" << Plays[1] << "\t" << Plays[2] << endl;
cout << "\t\t\t" << Plays[3] << "\t" << Plays[4] << "\t" << Plays[5] << endl;
cout << "\t\t\t" << Plays[6] << "\t" << Plays[7] << "\t" << Plays[8] << endl;
}
else if(Selected == 1) {
cout << "\t\t\t" << Plays[0] << "\t" << "[" << Plays[1] << "]" << "\t" << Plays[2] << endl;
cout << "\t\t\t" << Plays[3] << "\t" << Plays[4] << "\t" << Plays[5] << endl;
cout << "\t\t\t" << Plays[6] << "\t" << Plays[7] << "\t" << Plays[8] << endl;
} else if (Selected == 2){
cout << "\t\t\t" << Plays[0] << "\t" << Plays[1] << "\t" << "[" << Plays[2] << "]" << endl;
cout << "\t\t\t" << Plays[3] << "\t" << Plays[4] << "\t" << Plays[5] << endl;
cout << "\t\t\t" << Plays[6] << "\t" << Plays[7] << "\t" << Plays[8] << endl;
} else if(Selected == 3){
cout << "\t\t\t" << Plays[0] << "\t" << Plays[1] << "\t" << Plays[2] << endl;
cout << "\t\t\t" << "[" << Plays[3] << "]" << "\t" << Plays[4] << "\t" << Plays[5] << endl;
cout << "\t\t\t" << Plays[6] << "\t" << Plays[7] << "\t" << Plays[8] << endl;
}else if(Selected == 4){
cout << "\t\t\t" << Plays[0] << "\t" << Plays[1] << "\t" << Plays[2] << endl;
cout << "\t\t\t" << Plays[3] << "\t" << "[" << Plays[4] << "]" << "\t" << Plays[5] << endl;
cout << "\t\t\t" << Plays[6] << "\t" << Plays[7] << "\t" << Plays[8] << endl;
} else if(Selected == 5){
cout << "\t\t\t" << Plays[0] << "\t" << Plays[1] << "\t" << Plays[2] << endl;
cout << "\t\t\t" << Plays[3] << "\t" << Plays[4] << "\t" << "[" << Plays[5] << "]" << endl;
cout << "\t\t\t" << Plays[6] << "\t" << Plays[7] << "\t" << Plays[8] << endl;
} else if(Selected == 6){
cout << "\t\t\t" << Plays[0] << "\t" << Plays[1] << "\t" << Plays[2] << endl;
cout << "\t\t\t" << Plays[3] << "\t" << Plays[4] << "\t" << Plays[5] << endl;
cout << "\t\t\t" << "[" << Plays[6] << "]" << "\t" << Plays[7] << "\t" << Plays[8] << endl;
}else if(Selected == 7){
cout << "\t\t\t" << Plays[0] << "\t" << Plays[1] << "\t" << Plays[2] << endl;
cout << "\t\t\t" << Plays[3] << "\t" << Plays[4] << "\t" << Plays[5] << endl;
cout << "\t\t\t" << Plays[6] << "\t" << "[" << Plays[7] << "]" << "\t" << Plays[8] << endl;
} else if(Selected == 8){
cout << "\t\t\t" << Plays[0] << "\t" << Plays[1] << "\t" << Plays[2] << endl;
cout << "\t\t\t" << Plays[3] << "\t" << Plays[4] << "\t" << Plays[5] << endl;
cout << "\t\t\t" << Plays[6] << "\t" << Plays[7] << "\t" << "[" << Plays[8] << "]" << endl;
}
}
