Hi guys. A true beginner here. This is my tic-tac-toe program that allows two players to play competitively. It checks to see if either player has won, or if the board is filled completely. (This is a Practice Problem from Alex's book.)
The problem is - my program won't stop when it's a tie. It prints "It's a TIE.", and than immediately after that expects a move from Player 2. What am I doing wrong? (I'm aware that I could just add break statement and exit the loop immediately when tie_check==true, but I'm trying to learn.)
I apologize for bothering you with posting all of my code - I just hope it will give you a better picture of this situation.
One more thing. This was my first version of a function that checks if the board is filled completely. The idea was:Code:#include <iostream> using namespace std; void drawBoard(string position[3][3]) { cout << " 0 1 2 \n"; cout << " | | \n"; cout << "0 " << position[0][0] << " | " << position[0][1] << " | " << position[0][2] << "\n"; cout << " ____|____|____\n"; cout << " | | \n"; cout << "1 " << position[1][0] << " | " << position[1][1] << " | " << position[1][2] << "\n"; cout << " ____|____|____\n"; cout << " | | \n"; cout << "2 " << position[2][0] << " | " << position[2][1] << " | " << position[2][2] << "\n"; cout << " | | \n\n"; } int checkForWinner (string position[3][3]) { /// check to see if X won the game //horizontal if (position[0][0]=="X" && position[0][1]=="X" && position[0][2]=="X") return 1; if (position[1][0]=="X" && position[1][1]=="X" && position[1][2]=="X") return 1; if (position[2][0]=="X" && position[2][1]=="X" && position[2][2]=="X") return 1; //vertical if (position[0][0]=="X" && position[1][0]=="X" && position[2][0]=="X") return 1; if (position[0][1]=="X" && position[1][1]=="X" && position[2][1]=="X") return 1; if (position[0][2]=="X" && position[1][2]=="X" && position[2][2]=="X") return 1; //diagonal if (position[0][0]=="X" && position[1][1]=="X" && position[2][2]=="X") return 1; if (position[0][2]=="X" && position[1][1]=="X" && position[2][0]=="X") return 1; /// check if O won //horizontal if (position[0][0]=="O" && position[0][1]=="O" && position[0][2]=="O") return 2; if (position[1][0]=="O" && position[1][1]=="O" && position[1][2]=="O") return 2; if (position[2][0]=="O" && position[2][1]=="O" && position[2][2]=="O") return 2; // vertical if (position[0][0]=="O" && position[1][0]=="O" && position[2][0]=="O") return 2; if (position[0][1]=="O" && position[1][1]=="O" && position[2][1]=="O") return 2; if (position[0][2]=="O" && position[1][2]=="O" && position[2][2]=="O") return 2; // diagonal if (position[0][0]=="O" && position[1][1]=="O" && position[2][2]=="O") return 2; if (position[0][2]=="O" && position[1][1]=="O" && position[2][0]=="O") return 2; } bool checkForTie ( string position[3][3]) { bool tie; int non_empty; non_empty=0; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { if (position[i][j]!=" ") { non_empty++; } } } if (non_empty==9) { tie=true; } else { tie=false; } return tie; } int main() { bool winner; int win_check; bool tie_check; string position [3][3]; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { position[i][j]=" "; } } drawBoard(position); do { int i; int j; cout<<"Player 1, your move. \n"; cout<<"Row?\n"; cin>>i; cout<<"Column?\n"; cin>>j; position[i][j]="X"; drawBoard(position); win_check=checkForWinner(position); if (win_check==1) { winner=true; cout<<"We have a WINNER. Player 1 WON!\n"; } else { winner=false; } tie_check=checkForTie(position); if (tie_check==true) { cout<<"It's a TIE."; } cout<<"Player 2, your move. \n"; cout<<"Row?"; cin>>i; cout<<"Column?\n"; cin>>j; position[i][j]="O"; drawBoard(position); win_check=checkForWinner(position); if (win_check==2) { winner=true; cout<<"We have a WINNER. Player 2 WON!\n"; } else { winner=false; } tie_check=checkForTie(position); if (tie_check==true) { cout<<"It's a TIE."; } } while (winner!=true && tie_check!=true); }
for each element in the array
if element==" ";
return false immediately;
//... and if this whole loop completes without detecting false, then return true;
Here's the code:
It never detected a tie. Why?Code:bool checkForTie (string position [3][3]) { bool tie; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { if (position[i][j]==" ") { tie=false; break; } } } if (tie!=false) { tie=true; } return tie; }
Sorry for the super-long post. Any help is much appreciated.