I'm currently attempting to make a two player Tic-Tac-Toe Game for 2 players I am currently very happy with the results I am having. Except I cannot get the CHECK_X() or the CHECK_O() function to work properly(my way to determine if someone has won) could someone please explain to me why I am having issues?
#include<iostream>
#include<string>
usingnamespace std;
void CS()
{
cout<<string(100,'\n');
}
string GAP1;
string GAP2;
string GAP3;
string track="123456789";//keeps track of all spaces
void STR_GAP1()
{
GAP1[1]=track[0];
GAP1[4]=track[1];
GAP1[7]=track[2];
}
void STR_GAP2()
{
GAP2[1]=track[3];
GAP2[4]=track[4];
GAP2[7]=track[5];
}
void STR_GAP3()
{
GAP3[1]=track[6];
GAP3[4]=track[7];
GAP3[7]=track[8];
}
int CHECK_X()
{
if(track[0]==track[1]==track[2]=='X') return 1;
elseif(track[3]==track[4]==track[5]=='X') return 1;
elseif(track[6]==track[7]==track[8]=='X') return 1;
elseif(track[0]==track[3]==track[6]=='X') return 1;
elseif(track[1]==track[4]==track[7]=='X') return 1;
elseif(track[2]==track[5]==track[8]=='X') return 1;
elseif(track[0]==track[4]==track[8]=='X') return 1;
elseif(track[2]==track[4]==track[6]=='X') return 1;
return 0;
}
int CHECK_O()
{
if(track[0]==track[1]==track[2]==O[0]) return 1;
elseif(track[3]==track[4]==track[5]==O[0]) return 1;
elseif(track[6]==track[7]==track[8]==O[0]) return 1;
elseif(track[0]==track[3]==track[6]==O[0]) return 1;
elseif(track[1]==track[4]==track[7]==O[0]) return 1;
elseif(track[2]==track[5]==track[8]==O[0]) return 1;
elseif(track[0]==track[4]==track[8]==O[0]) return 1;
elseif(track[2]==track[4]==track[6]==O[0]) return 1;
return 0;
}
void main()
{
CS();//lines up the game so between frame one and two it doesnt jar
cout<<"This is a Tic-Tac-Toe game for 2 players :)"<<endl;
string Line="+--+--+--+\n";
GAP1 ="|1 |2 |3 |\n";
GAP2 ="|4 |5 |6 |\n";
GAP3 ="|7 |8 |9 |\n";
int k=1;
int j=0, l=0;
int x=0;//breaks while statement and also indicates who has won
while(k<10&&x==0)
{
cout<<Line<<GAP1<<Line<<GAP2<<Line<<GAP3<<Line;
if(k%2==0)
{
cout<<"It is O's turn\nWhere would you like to go?\n";
for(;l==0;)
{
cin>>j;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
if(j!=0&&j<10&&track[j-1]!='O'&&track[j-1]!='X')
{
track[j-1]='O';//places an O in the proper place in track
l=1;
}
else cout<<"ERROR Please enter a proper number";
}
l=0;
}
else
{
cout<<"It is X's turn\nWhere would you like to go?\n";
for(;l==0;)
{
cin>>j;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
if(j!=0&&j<10&&track[j-1]!='O'&&track[j-1]!='X')
{
track[j-1]='X';//places an X int he proper place in track
l=1;
}
else cout<<"ERROR Please enter a proper number";
}
l=0;
}
STR_GAP1();//reinitalizes STR_GAP 1-3 to have proper X's and O's
STR_GAP2();
STR_GAP3();
CS();//clears the screen
k++;
if (CHECK_X()==1) x=1;//checks if X won
elseif(CHECK_O()==1) x=2;//checks if O won
}
cout<<Line<<GAP1<<Line<<GAP2<<Line<<GAP3<<Line;//prints the board one more time
if (x==1)
cout<<"Congratulations X you won the game!!!";
elseif(x==2)
cout<<"Congratulations O you won the game!!!";
else
cout<<"CATS GAME!!!!";
cout<<endl;
cin>>j;
}
It took a while for me to see why that wasn't working, at a quick glance those functions should return the values correctly.
It isn't working because of how C++ is reading the order of them. For example this line if(track[0]==track[1]==track[2]=='X')
It first checks to see if track[0] is equal to track[1], if they are equal then it makes that value 1 (for true) then compares it to track[2] etc...which of course wont work.
To fix this problem rewrite them so they look something like if( (track[0] == track[1]) && ( track[1] == track[2]) && (track[2] == 'X') ) return 1;
Then it should check each one of them individually.
Thank you very much that was driving me crazy. Now that I have it working does anyone see a way to optimize it, I know my code is sloppy at best. I still have only a basic understanding of C++.