For some reason, inside of my checkWin function, when I do my switch statement, the program never goes into the cases. I've run this program with debugging multiple times and the char values ARE equal. Can someone explain to me why the program does not go into the case statement? Help would be appreciated. I type casted the chars to ints to see if they had the same int value and they are equal. BTW I am very new to C++ so if I'm making a stupid mistake I apologize.
Global Variables:
char Board[3][3] = {{'1','2','3'},
{'4','5','6'},
{'7','8','9'}};
string Message;
string Winner;
Comp *AI;
Where I manipulate Board:
void takeMove(char c){
char index;
if(c=='X'){
string input;
cin >> input;
index = input[0]; // This is so that if the user enters more than 1 character there are no errors
}
else{
index = AI->decide();
}
if(index!='X' && index!='O'){
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
if(index==Board[x][y]){
Board[x][y] = c;
}
}
}
}
}
Where the problem is:
bool checkWin(){
int x1 = 0;
int o1 = 0;
int x2 = 0;
int o2 = 0;
int x3 = 0;
int o3 = 0;
int x4 = 0;
int o4 = 0;
for(int x=0;x<3;x++){ // check columns
int x1 = 0;
int o1 = 0;
for(int y=0;y<3;y++){
switch(Board[x][y]){ // <-- PROBLEM Board is equal to 'X', but does not go into case statement
case 'X':
x1++;
break;
case 'O':
o1++;
break;
default:
break;
}
}
switch(Board[x][x]){ // Checking Diagonal
case 'X':
x3++;
break;
case 'O':
o3++;
break;
default:
break;
}
switch(Board[0+x][2-x]){ // Check Diagonal/ What's weird is that this switch statement does go into the cases?
case 'X':
x4++;
break;
case 'O':
o4++;
break;
default:
break;
}
}
for(int x=0;x<3;x++){ // <-- PROBLEM Board is equal to 'X', but does not go into case statement
int x2=0;
int o2=0;
for(int y=0;y<3;y++){
switch(Board[y][x]){ // check rows
case 'X':
x2++;
break;
case 'O':
o2++;
break;
default:
break;
}
}
}
if(x1>=2){
Winner = "X";
return true;
}
else if(o1>=3){
Winner = "O";
return true;
}
else if(x2>=3){
Winner = "X";
return true;
}
else if(o2>=3){
Winner = "O";
return true;
}
else if(x3>=3){
Winner = "X";
return true;
}
else if(o3>=3){
Winner = "O";
return true;
}
else if(x4>=3){
Winner = "X";
return true;
}
else if(o4>=3){
Winner = "O";
return true;
}
int count=0;
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
if(Board[x][y]=='X' || Board[x][y]=='O'){
count++;
}
}
}
if(count>=9){
Winner = "NO ONE";
return true;
}
return false;
}
Main:
int main(){
AI = new Comp();
string s;
int games=0;
int moves=0;
cout << "Welcome to Tic Tac Toe by Hunter Vallejos!" << endl;
cout << "Type the number of the space you want to choose"<< endl;
do {
cout << "WARNING: This AI never loses. You will only be able to tie this AI" << endl;
system("PAUSE");
Message = " Type the number you want to choose and press enter. You are X and the AI is O";
do {
DisplayBoard();
switch(moves % 2){
case 0:
takeMove('X');
break;
case 1:
AI->setBoard(Board);
takeMove('O');
break;
}
moves++;
} while(!checkWin());
DisplayBoard();
cout << endl << "\t\t\t\tThe winner is " << Winner << endl;
} while(checkRepeat());
return 0;
}

New Topic/Question
Reply



MultiQuote






|