I have been trying to write tictactoe in c++ but I don't know why the if else statements aren't getting executed.
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
char XorO;
int n;
char a = '1';
char b = '2';
char c = '3';
char d = '4';
char e = '5';
char f = '6';
char g = '7';
char h = '8';
char i = '9';
int charchange[] = {79, 80, 81, 575, 6786, 75, 7879, 67697, 689698};
void Welcome() // FUNCTION1
{
cout << "\nWelcome to Tic-Tac-toe!!!\n";
cout << "You are going to play with the computer!!\n";
cout << "Press X to choose X or press O to choose O for the game!!: ";
}
void framework() //FUNCTION2
{
cout << " _ _ _ " << endl;
cout << "| | | |" << endl;
cout << "|"<< a <<"|"<< b <<"|"<< c <<"|" << endl;
cout << "|"<< d <<"|"<< e <<"|"<< f <<"|" << endl;
cout << "|"<< g <<"|"<< h <<"|"<< i <<"|" << endl;
cout << "|_|_|_|" << endl;
}
int main()
{
aiyeee:
Welcome();
cin >> XorO; ////////////GETS EXCECUTED TILL HERE
if (XorO == 'X') ////////////////////////DSEN'T EXECUTE
{
for(n=1;n>=9;n++)
{
if(n%2 != 0){
framework();
cout << "\nOk:)";
aeneas:
cout << " Your turn!! Which number to change to X: ";
cin >> charchange[(n-1)];
for (int ty = 1; ty <= 9; ty++)
{
if(charchange[(n-1)] != ty)
{
cout << "INVALID INPUT!!\n";
goto aeneas;
}
}
switch(charchange[(n-1)])
{
case 1:
if (a == 'O')
{
cout << "\nAlready Changed to 'O'!!\n";
goto aeneas;
}
a = 'X';
framework();
break;
case 2:
if (b == 'O')
{
cout << "\nAlready Changed to 'O'!!\n";
goto aeneas;
}
b = 'X';
framework();
break;
case 3:
if (c == 'O')
{
cout << "\nAlready Changed to 'O'!!\n";
goto aeneas;
}
c = 'X';
framework();
break;
case 4:
if (d == 'O')
{
cout << "\nAlready Changed to O!!\n";
goto aeneas;
}
d = 'X';
framework();
break;
case 5:
if (e == 'O')
{
cout << "\nAlready Changed to 'O'!!\n";
goto aeneas;
}
e = 'X';
framework();
break;
case 6:
if (f == 'O')
{
cout << "\nAlready Changed to 'O'!!\n";
goto aeneas;
}
f = 'X';
framework();
break;
case 7:
if (g == 'O')
{
cout << "\nAlready Changed to 'O'!!\n";
goto aeneas;
}
g = 'X';
framework();
break;
case 8:
if (h == 'O')
{
cout << "\nAlready Changed to 'O'!!\n";
goto aeneas;
}
h = 'X';
framework();
break;
case 9:
if (i == 'O')
{
cout << "\nAlready Changed to 'O'!!\n";
goto aeneas;
}
i = 'X';
framework();
break;
}
if(n>4)
{
if ((a==b && b==c)||(d==e && e==f)||(g==h && h==i)||(a==d && d==g)||(b==e && g==h)||(c==f && f==i)||(a==e && e==i)||(c==e && e==f))
{
cout << "YOU WIN!!!\n";
goto allagulla;
}
}
}
else if(n%2 == 0)
{
aghhg:
charchange[n-1] = ((rand()%9) + 1);
switch (charchange[n-1])
{
case 1:
if(a == 'X')
{
goto aghhg;
}
a = 'O';
framework();
break;
case 2:
if(b == 'X')
{
goto aghhg;
}
b = 'O';
framework();
break;
case 3:
if(c == 'X')
{
goto aghhg;
}
c = 'O';
framework();
break;
case 4:
if(d == 'X')
{
goto aghhg;
}
d = 'O';
framework();
break;
case 5:
if(e == 'X')
{
goto aghhg;
}
e = 'O';
framework();
break;
case 6:
if(f == 'X')
{
goto aghhg;
}
f = 'O';
framework();
break;
case 7:
if(g == 'X')
{
goto aghhg;
}
g = 'O';
framework();
break;
case 8:
if(h == 'X')
{
goto aghhg;
}
h = 'O';
framework();
break;
case 9:
if(i == 'X')
{
goto aghhg;
}
i = 'O';
framework();
break;
}
if(n>4)
{
if ((a==b && b==c)||(d==e && e==f)||(g==h && h==i)||(a==d && d==g)||(b==e && g==h)||(c==f && f==i)||(a==e && e==i)||(c==e && e==f))
{
cout << "COMPUTER WINS!!\n";
break;
}
}
}
}
}
else if (XorO == 'O')
{
}
else
{
cout << "\nINVALID INPUT!!!\n";
goto aiyeee;
cin >> XorO;
}
allagulla:
cout << "\nWanna play again?(y/n) ";/////////////////////COMES DOWN STRAIGHT HERE AFTER THE PREV COMM
char yesno;
yesno = getche();
if (yesno == 'y')
{
goto aiyeee;
}
}
After main() starts it inputs the XorO but then it comes down to Wanna play again? Seriously I dunno why:( Sorry for the long program maybe it could have been shorter actually I am learning c++.
Aucun commentaire:
Enregistrer un commentaire