Sei sulla pagina 1di 9

/*

A Program for Tic Tac Toe in C++ using AI technique.

Name: Anirudh Polawar


Roll No.: 40; Div.: D
G.R. No.: 1710239

Name: Tanay Rajwal


Roll No.: 42; Div.: D
G.R. No.: 1710919
*/

#include <iostream>
#include<conio.h>

using namespace std;

//Function for checking the winner


int whowon(int **a)
{
int x = 0, y = 0;
for (int i = 0; i < 3; i++)
{
x = 0;
y = 0;
for (int j = 0; j < 3; j++)
{
x = a[i][j] + x;
y = y + a[j][i];
}

//User winning is 300 and computer winning is 30.


//User playing is indicated by 100
//Computer playing is indicated by 10
if (x == 300)
return 2; //User Won
else if (x == 30)
return 1; //Computer Won

if (y == 300)
return 2; //User Won
else if (y == 30)
return 1; //Computer Won
}

//Condition for checking the first diagonal condition for the winner
x = a[1][1] + a[0][0] + a[2][2];

if (x == 300)
return 2; //User Won
else if (x == 30)
return 1; //Computer Won

//Condition for checking the second diagonal condition for the winner
x = a[1][1] + a[0][2] + a[2][0];

if (x == 300)
return 2; //User Won
else if (x == 30)
return 1;

return 0;
}

int count(int **a, int n)


{
int b[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] != 0)
b[i][j] = a[i][j];
else
b[i][j] = n;
}
}

int x = 0, y = 0, count = 0;
for (int i = 0; i < 3; i++)
{
x = 0;
y = 0;
for (int j = 0; j < 3; j++)
{
x = b[i][j] + x;
y = b[j][i] + y;
}
if (x == n * 3)
count++;
if (y == n * 3)
count++;

}
x = b[0][0] + b[1][1] + b[2][2];
y = b[0][2] + b[1][1] + b[2][0];
if (x == n * 3)
count++;
if (y == n * 3)
count++;

return count;

void sysplay(int **a)


{

int min = 8, p, q, x, y;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
if (a[i][j] == 0)
{
a[i][j] = 10;
x = count(a, 10);
y = count(a, 100);
if (min > x - y)
{
p = i;
q = j;
min = x - y;
}
a[i][j] = 100;
x = whowon(a);
if (x == 2)
{
a[i][j] = 10;
return;
}

a[i][j] = 0;

}
}
a[p][q] = 10;
}

void System_play(int **a)


{
//We want to try such that the user should not win and the System should ONLY win
int x;

for (int i = 0; i < 3; i++)


{
for (int j = 0; j < 3; j++)
{
//Checking if the space at the particular location is vacant
if (a[i][j] == 0)
{
//Placing the Computer's turn at the location pointed by i and j
a[i][j] = 100;

x = whowon(a);
if (x == 2)
{
a[i][j] = 10;
return;
}
a[i][j] = 10;
x = whowon(a);
if (x == 1)
{
a[i][j] = 10;
return;
}
//restoring the previous matrix
a[i][j] = 0;
}
}
}

//Prefered location for the system


int max = 0, k, p, y, u, q;
for (int i = 0; i < 3; i++)
{
x = 0;
y = 0;
for (int j = 0; j < 3; j++)
{
if (a[i][j] == 0)
{
y++;
k = i;
p = j;
}

else
if (a[i][j] == 10)
x++;
}
if (y>1 && x >= 1)
{
a[k][p] = 10;
return;
}
}

for (int i = 0; i < 3; i++)


{
x = 0;
y = 0;
for (int j = 0; j < 3; j++)
{
if (a[j][i] == 0)
{
y++;
k = j;
p = i;
}

else
if (a[j][i] == 10)
x++;
}
if (y>1 && x >= 1)
{
a[k][p] = 10;
return;
}
}

//AI makes the final decision if there is no location as per above


sysplay(a);

void print(int **a)


{
cout << "\n__________________________________________________\n";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[i][j] == 10)
cout << "X ";
else if (a[i][j] == 100)
cout << "O ";
else
cout << " ";
}
cout << "\n";
}
cout << "\n__________________________________________________\n";
}

int main()
{
cout << "A Program in C++ for playing Tic Tac Toe using AI technique\n\n";
cout << "A Program by Anirudh Polawar and Tanay Rajwal.\n\n";

//Dynamic allocation for 2D array using pointer for each 1D array


int **a = new int *[3];
for (int i = 0; i < 3; i++)
a[i] = new int[3];

for (int i = 0; i < 3; i++)


for (int j = 0; j < 3; j++)
a[i][j] = 0;

//By default computer started playing from the center location of the grid
a[1][1] = 10;

int p, q, x,flag;

//Taking the input for the player side


print(a);
for (int i = 0; i < 4; i++)
{
flag=1;
while(1)
{
cout << "Enter the row number : ";
cin >> p;
cout << "Enter the coulumn Number : ";
cin >> q;
if(a[p][q]==0&&p<3&&q<3&&p>=0&&q>=0)
break;
else
{
cout<<"\n\n$$$$$$Invalid Location to play Chose other location$$$$$$\n\n";

//100 is the default value for user. User playing would


//be indicated by the number 100 at the particular location
a[p][q] = 100;

//Checking the winner Computer Vs. The User


x = whowon(a);
if (x == 1)
{
cout << ":System Won \n\n";
break;
}
else if (x == 2)
{
cout << ":You Won \n\n";
break;

//Calling the Computer to play its turn


System_play(a);
//Checking if the Computer has won
x = whowon(a);
if (x == 1)
{
cout << "********System Won********\n";
break;
}
else if (x == 2)
{
cout << "********You Won********\n";
break;

}
print(a);

}
print(a);
return 0;
}

//output

Potrebbero piacerti anche