Tic-tac-toe

Hey. I'm having a little problem. Im almost done with my tic tac toe . Just when checking at last who won ..I had to write around 12 if-statements . I was wondering if there was an easier method to it... !

Can you post your code?
#include <iostream>
#include <string>
#include <math.h>
#include <cctype>
#include <stdlib.h>
using namespace std;
void carrayout();
int main()
{
int x,i,z,oponz,s,k,userinput1R,userinput1C,fuserinput1R,fuserinput1C,userinput2R,userinput2C,fuserinput2R,fuserinput2C,colorr;
char a,b,playa1sign,m,choice,playa2sign,textcolorr,tictactoe[3][3]={'1','2','3','4','5','6','7','8','9'};
string name,name2;
cout<<"Hii , Player 1 Name : ";
cin>>name;
cout<<"Hii , PLayer 2 Name : ";
cin>>name2;
point:
for (i=0;i<3;i++)
{
for(x=0;x<3;x++)
{
cout<<tictactoe[i][x]<<"|";
}
cout<<endl;

}
back:
system("color fc");
cout<<"Choose Your Sign "<<name2<<" ?(X/O)\n";
cin>>playa2sign;
if (playa2sign=='x'||playa2sign=='X')
{
system("color 0b");
cout<<"YOUR SYMBOL IS X . LETS START\n";
playa2sign ='X';
playa1sign ='O';
}
else if (playa2sign=='o'||playa2sign=='O')
{
system("color 9d");
cout<<"YOUR SYMBOL IS O . LETS START\n";
playa2sign = 'O';
playa1sign = 'X';
}
else
{
cout<<"Please Choose from the options given below?";
goto back;
}
while(k<100000000)
{
k++;
}
//INSERT TOSS HERE
playa1st:
cout<<name<<" -\nRow :";
cin>>userinput1R;
if (userinput1R>3||userinput1R<1)
{
cout<<"PLease Enter The Correct Row Number ,"<<name<<"\n";
goto playa1st;
}
coloumn :
cout<<"Coloumn :";
cin>>userinput1C;
if (userinput1C>3||userinput1C<1)
{
cout<<"PLease Enter The Correct Row Number, "<<name<<"\n";
goto coloumn;
}
fuserinput1R = userinput1R-1;
fuserinput1C = userinput1C-1;
if (tictactoe[fuserinput1R][fuserinput1C]== 'X'||tictactoe[fuserinput1R][fuserinput1C]=='O')
{
cout<<"This location is already taken .. \nPlease re-enter the co-ordinates.";
goto playa1st;

}
tictactoe[fuserinput1R][fuserinput1C] = playa1sign ;
for (i=0;i<3;i++)
{
for(x=0;x<3;x++)
{
cout<<tictactoe[i][x]<<"|";
}
cout<<endl;
}
if(tictactoe[0][0]&&tictactoe[0][1]&&tictactoe[0][2]==playa1sign||tictactoe[1][0]&&tictactoe[1][1]&&tictactoe[1][2]==playa1sign)
{
cout<<name<<" WINS !. Congratulations ";
goto end;
}
if(tictactoe[2][0]&&tictactoe[2][1]&&tictactoe[2][2]==playa1sign||tictactoe[0][0]&&tictactoe[1][0]&&tictactoe[2][0]==playa1sign)
{
cout<<name<<" WINS !. Congratulations ";
goto end;
}
playa2st:
cout<<name2<<" -\nRow :";
cin>>userinput2R;
if (userinput2R>3||userinput2R<1)
{
cout<<"PLease Enter The Correct Row Number ,"<<name2<<"\n";
goto playa2st;
}
coloumn2:
cout<<"Coloumn :";
cin>>userinput2C;
if (userinput2C>3||userinput2C<1)
{
cout<<"PLease Enter The Correct Coloumn Number, "<<name2<<"\n";
goto coloumn2;
}
fuserinput2R = userinput2R-1;
fuserinput2C = userinput2C-1;
if (tictactoe[fuserinput2R][fuserinput2C]== 'X'||tictactoe[fuserinput2R][fuserinput2C]=='O')
{
cout<<"This location is already taken .. \nPlease re-enter the co-ordinates.\n";
goto playa2st;
}
tictactoe[fuserinput2R][fuserinput2C] = playa2sign ;
for (i=0;i<3;i++)
{
for(x=0;x<3;x++)
{
cout<<tictactoe[i][x]<<"|";
}
cout<<endl;
}
if(tictactoe[0][0]&&tictactoe[0][1]&&tictactoe[0][2]==playa2sign||tictactoe[1][0]&&tictactoe[1][1]&&tictactoe[1][2]==playa2sign)
{
cout<<name2<<" WINS !. Congratulations ";
goto end;
}
if(tictactoe[2][0]&&tictactoe[2][1]&&tictactoe[2][2]==playa2sign||tictactoe[0][0]&&tictactoe[1][0]&&tictactoe[2][0]==playa2sign)
{
cout<<name2<<" WINS !. Congratulations ";
goto end;
}
goto playa1st ;
end:
system("pause");
return 0;

}


^^^^^what they said


it really depends on how you organized your data. if you used a 2d array
a 2 3
b 2 3
c 2 3


you could have a loop that checks each row and each column pretty easily
There are easier ways of doing this, using a 2d array, using numbers (such as the numpad) to represent grid positions for user entry, and the use of ternary operators to check for victories. When I learned about the ternary operators (sometimes referred to as conditional operators) I was able to reduce the number of if/else statements involved significantly. An example would be something like:
win=((array[0][0)==1 && array[0][1]==1 && array[0][2]==1) ? 1 : win);
This translates roughly to "If array[0][0],array[0][1], and array[0][2] all equal 1, then win does too, otherwise it is equal to itself and nothing changes."

BTW, you can greatly increase the readability of your code examples using code tags. You can use them easily by clicking the "<>" button and putting your code between the generated code tags, so you don't have to type "[clode] or [/clode]". (Deliberately misspelled so they would show up, it is supposed to be "code".)
Last edited on
Topic archived. No new replies allowed.