this is my c tic tac toe ai cant figure why it is not work
*******************************************************************************
this is my c++ tic tac toe ai -- cant figure why it is not working help
cannot use random number - playing against the computer for more points(create ai) but willing to use random number
************************************************************************************
#include
#include
#include
#include
using namespace std;
int randomnumberfunction(int, int);
int randomnumberfunction2(int, int);
int randomnumberfunction3(int, int);
int randomnumberfunction4(int, int);
bool check(char, char);
int game(int, int, int, int);
void gameboard();
void checkwin();
const int Amount = 1;
int spot1[Amount];
int main()
{
string YesorNo;
int e,f,g,h,i;
int maxnum = 3;
int minnum = 1;
int maxnum2 = 67;
int minnum2 = 65;
unsigned seed = time(0);
srand(seed);
do
{
e = randomnumberfunction(maxnum, minnum);
f = randomnumberfunction2(maxnum2, minnum2);
g = randomnumberfunction(maxnum, minnum);
h = randomnumberfunction(maxnum, minnum);
game(e,f,g,h);
checkwin();
cout << \"Do you want to play again? enter yes or no: \" << endl;
cin >> YesorNo;
}while(YesorNo == \"Yes\" || YesorNo == \"yes\");
return 0;
}
int randomnumberfunction(int max1, int min1)
{
int randomnumber;
randomnumber = rand() % (max1 - min1 + 1) + min1;
return randomnumber;
}
int randomnumberfunction2(int max1, int min1)
{
int randomnumber2;
randomnumber2 = rand() % (max1 - min1 + 1) + min1;
return randomnumber2;
}
int randomnumberfunction3(int max1, int min1)
{
int randomnumber2;
randomnumber2 = rand() % (max1 - min1 + 1) + min1;
return randomnumber2;
}
int randomnumberfunction4(int max1, int min1)
{
int randomnumber2;
randomnumber2 = rand() % (max1 - min1 + 1) + min1;
return randomnumber2;
}
bool check(char j, char k)
{
bool f = false;
if(j == k)
{
return false;
}
else
{
return true;
}
return f;
}
void gameboard()
{
char A = \'A\',B = \'B\',C = \'C\';
char _;
cout << setw(5) << \"1\" << setw(5) << \"2\" << setw(5) << \"3\" << endl;
cout <<\" A \" << A << _ << \" | \" << A << _ << \" | \" << A << _ << endl;
cout <<\" B \" << B << _ << \" | \" << B << _ << \" | \" << B << _ << endl;
cout <<\" C \" << C << _ << \" | \" << C << _ << \" | \" << C << _ << endl;
}
void checkwin()
{
int A = 65, B = 66, C = 67;
for(int i = 0; i < Amount; i++)
{
if(spot1[i] == 1 && A == 65 && spot1[i] == 2 && A == 65 && spot1[i] == 3 && A == 65)
{
cout << \"user wins! \" << endl;
}
else if(spot1[i] == 1 && B == 66 && spot1[i] == 2 && B == 66 && spot1[i] == 3 && B == 66)
{
cout << \"user wins \" << endl;
}
else if(spot1[i] == 1 && C == 67 && spot1[i] == 2 && C == 67 && spot1[i] == 3 && C == 67)
{
cout << \"user wins \" << endl;
}
else if(spot1[i] == 1 && A == 65 && spot1[i] == 2 && B == 66 && spot1[i] == 3 && C == 67)
{
cout << \"user wins \" << endl;
}
else if(spot1[i] == 1 && C == 67 && spot1[i] == 2 && B == 66 && spot1[i] == 3 && A == 65)
{
cout << \"user wins \" << endl;
}
else if(spot1[i] == 1 && A == 65 && spot1[i] == 1 && B == 66 && spot1[i] == 1 && C == 67)
{
cout << \"user wins \" << endl;
}
else if(spot1[i] == 2 && A == 65 && spot1[i] == 2 && B == 66 && spot1[i] == 2 && C == 67)
{
cout << \"user wins \" << endl;
}
else if(spot1[i] == 3 && A == 65 && spot1[i] == 3 && B == 66 && spot1[i] == 3 && C == 67)
{
cout << \"user wins \" << endl;
}
else
{
cout << \"you lose or its a draw: \" << endl;
}
}
}
int game(int a, int b, int c, int d)
{
bool F = false;
char _ ;
char A = \'A\',B = \'B\',C = \'C\', h = \'a\';
char letter, letter2;
char yesorno;
cout << \"do you want to go first? enter y(yes) or n(no).\" << endl;
cin >> yesorno;
switch(yesorno)
{
case \'Y\':
case \'y\':
{
cout << \"enter the letter O or X: \" << endl;
cin >> letter;
if(letter == \'O\' || letter == \'o\')
{
letter2 = \'X\';
}
else if(letter == \'X\' || letter == \'x\')
{
letter2 = \'O\';
}
for(int i = 0; i < Amount; i++)
{
gameboard();
cout << \"Enter what column you want to put letter at \" << letter << \": enter 1,2, or 3 for a spot: \" << endl;
cin >> spot1[i];
cout << \"enter the row A, B, C: \" << endl;
cin >> A;
cout << \"you place the spot \" << A << spot1[i] << endl;
if(a == 1 || a == 2 || a == 3)
{
if(b == 65 || b == 66 || b == 67 || b == 97 || b == 98 || b == 99)
{
while(b != A)
{
cout << \"the computer enters \" << letter2 << \" at \" << char(b) << a << endl;
break;
}
}
}
gameboard();
cout << \"Enter what column you want to put \" << letter << \": enter 1,2, or 3 for a spot: \" << endl;
cin >> spot1[i];
cout << \"enter the row A, B, C: \" << endl;
cin >> B;
cout << \"you place the spot \" << B << spot1[i] << endl;
if(c == 1 || c == 2 || c == 3)
{
if(b == 65 || b == 66 || b == 67)
{
while(b != B && b != A)
{
cout << \"the computer enters \" << letter2 << \" at \" << char(b) << c << endl;
break;
}
}
}
gameboard();
cout << \"Enter what column you want to put \" << letter << \": enter 1,2, or 3 for a spot: \" << endl;
cin >> spot1[i];
cout << \"enter the row A, B, C: \" << endl;
cin >> C;
cout << \"you place the spot \" << C << spot1[i] << endl;
if(d == 1 || d == 2 || d == 3)
{
if(b == 65 || b == 66 || b == 67 || b == 97 || b == 98 || b == 99)
{
while(b != A && b != B && b != C)
{
cout << \"the computer enters \" << letter2 << \" at \" << char(b) << d << endl;
break;
}
}
}
gameboard();
checkwin();
cout << \"Next round see if you can win two out of two: \" << endl;
}
}
case \'n\':
case \'N\':
{
if(a == 1)
{
letter2 == \'X\';
letter == \'O\';
}
else if(a == 2)
{
letter2 == \'O\';
letter == \'X\';
}
else if(a == 3)
{
letter2 == \'X\';
letter == \'O\';
}
for(int i = 0; i < Amount; i++)
{
gameboard();
if(a == 1 || a == 2 || a == 3)
{
if(b == 65 || b == 66 || b == 67 || b == 97 || b == 98 || b == 99)
{
cout << \"the computer enters \" << letter2 << \" at \" << char(b) << a << endl;
}
}
cout << \"Enter what column you want to put letter at \" << letter << \": enter 1,2, or 3 for a spot: \" << endl;
cin >> spot1[i];
cout << \"enter the row A, B, C: \" << endl;
cin >> A;
cout << \"you place the spot \" << A << spot1[i] << endl;
gameboard();
if(c == 1 || c == 2 || c == 3)
{
if(b == 65 || b == 66 || b == 67)
{
cout << \"the computer enters \" << letter2 << \" at \" << char(b) << c << endl;
}
}
cout << \"Enter what column you want to put \" << letter << \": enter 1,2, or 3 for a spot: \" << endl;
cin >> spot1[i];
cout << \"enter the row A, B, C: \" << endl;
cin >> B;
cout << \"you place the spot \" << B << spot1[i] << endl;
gameboard();
if(d == 1 || d == 2 || d == 3)
{
if(b == 65 || b == 66 || b == 67 || b == 97 || b == 98 || b == 99)
{
cout << \"the computer enters \" << letter2 << \" at \" << char(b) << d << endl;
}
}
cout << \"Enter what column you want to put \" << letter << \": enter 1,2, or 3 for a spot: \" << endl;
cin >> spot1[i];
cout << \"enter the row A, B, C: \" << endl;
cin >> C;
cout << \"you place the spot \" << C << spot1[i] << endl;
gameboard();
checkwin();
}
} }
}
Solution
hello,can you please follow the given code for tic tac toe game vs computer,it will help u a lot








