Skip to content

Instantly share code, notes, and snippets.

@sanketsudake
Created February 17, 2014 12:14
Show Gist options
  • Save sanketsudake/9049518 to your computer and use it in GitHub Desktop.
Save sanketsudake/9049518 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
using namespace std;
int poss_win(int *board,char choice)
{
int val, check;
if(choice == 'X')
val = 18;
else
val = 50;
int vertical[] = {0, 1, 4, 7};
int horizontal[]= {0, 1, 2, 3};
for(int i = 1; i <= 3; i++)
{
int ver = vertical[i];
//cout << "Prod" << board[ver] << board[ver + 1] << board[ver + 2] << endl;
if(board[ver] * board[ver + 1] * board[ver + 2] == val)
{
if(board[ver] == 2)
return ver;
else if(board[ver + 1] == 2)
return ver + 1;
else
return ver + 2;
}
int hor = horizontal[i];
if(board[hor] * board[hor + 3] * board[hor + 6] == val)
{
if(board[hor] == 2)
return hor;
else if(board[hor + 3] == 2)
return hor + 3;
else
return hor + 6;
}
}
if(board[1] * board[5] * board[9] == val)
{
if(board[1] == 2)
return 1;
else if(board[5] == 2)
return 5;
else
return 9;
}
if(board[3] * board[5] * board[7] == val)
{
if(board[3] == 2)
return 3;
else if(board[5] == 2)
return 5;
else
return 7;
}
return 0;
}
void print(int *board)
{
char arr[10];
arr[2] = ' ';
arr[3] = 'X';
arr[5] = 'O';
cout << endl;
for(int i = 1; i <= 9; i++)
{
//cout << board[i] ;
cout << " "<< arr[board[i]] << " | "<< " ";
if(i % 3 == 0)
cout << endl << "------------------" << endl;
}
cout << endl;
}
void go(int *board, int pos, int val)
{
board[pos] = val;
}
void comp(int *board, char choice)
{
int val;
if(choice == 'X')
val = 3;
else
val = 5;
if(board[5] == 2)
{
//cout << "D1" << endl;
go(board, 5, val);
return;
}
else
{
int x_win = poss_win(board, 'X');
int o_win = poss_win(board, 'O');
//cout << "POSS" << x_win << " " << o_win << endl;
if(val == 3 && x_win)
{
//cout << "3, x_win" << endl;
go(board, x_win, val);
print(board);
cout << "Computer Won" << endl;
exit(0);
}
else if(val == 3 && o_win)
{
//cout << "3, o_win" << endl;
go(board, o_win, val);
}
else if(val == 5 && o_win)
{
//cout << "5, o_win" << endl;
go(board, o_win, val);
print(board);
cout << "Computer Won" << endl;
exit(0);
}
else if(val == 5 && x_win)
{
//cout << "5, x_win" << endl;
go(board, x_win, val);
}
else
{
//cout << "D4" << endl;
int pref[] = {1, 3, 7, 9, 2, 4, 6, 8}, count = 0;
while(board[pref[count]] != 2 )
count++;
go(board, pref[count], val);
}
}
}
int* human(int *board, char choice)
{
int val = 2;
if(choice == 'X')
val = 3;
else
val = 5;
int in = 0;
while(in < 1 or in > 10 or (board[in] != 2))
{
cout << "Give position : ";
cin >> in;
cout << endl;
}
go(board, in, val);
return board;
}
void play(int *board, char choice)
{
char human_choice, comp_choice;
human_choice = choice;
if(choice == 'X')
{
cout << "Human Player " << endl;
human(board, human_choice);
print(board);
comp_choice = 'O';
}
else
comp_choice = 'X';
for(int i = 1; i <= 4; i++)
{
cout << "Computer Player " << endl;
comp(board, comp_choice);
print(board);
cout << "Human Player " << endl;
human(board, human_choice);
print(board);
}
if(choice == 'O')
{
cout << "Computer Player " << endl;
comp(board, comp_choice);
print(board);
}
}
int main(int argc, char *argv[])
{
char choice;
cout << "Select X or O : ";
cin >> choice;
cout << endl;
if(choice == 'X' or choice == 'O')
{
int board[9];
// 2 blank
// 3 X
// 5 O
for(int i = 1; i <= 9; i++)
board[i]= 2;
play(board, choice);
}
else
{
cout << "Wrong choice." << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment