| | |
Need help with Tic Tac Toe
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 7
Reputation:
Solved Threads: 0
I recently wrote a program to generate a tic tac toe game. The program should ask the user to select a number from the 'selection board', and this number will correspond to a spot on the game board. The user is playing against him/herself. I've encountered a pretty glaring bug that I can't seem to fix, though. Every time I enter a designated number it outputs "That spot on the board is taken," even when it is not. Can anyone help me with this? I suspect it's just something small that I missed.
C++ Syntax (Toggle Plain Text)
//Tic Tac Toe game #include <iostream> using namespace std; //note: main program placed below function definitions for convenience int game[9]={1,2,3,4,5,6,7,8,9}; //declares an array for the game board ///////////////////////////////// //Part 1: Function Definitions// /////////////////////////////// void TicTacToe() //makes a function for the selection board { cout<<"This is the selection board:"<<endl; cout<<"-----"<<endl; cout<<"1|2|3"<<endl; cout<<"-----"<<endl; cout<<"4|5|6"<<endl; cout<<"-----"<<endl; cout<<"7|8|9"<<endl; cout<<"-----"<<endl; } int move(int play,int turn) //puts player's turn in memory { game[turn-1]=play; if (turn==1) return play+1; else if (turn==2) return play-1; } int state(int play,int first) { if((game[1]&&game[2]&&game[3]==play)||(game[1]&&game[4]&&game[7]==play)||(game[1]&&game[5]&&game[9]==play)) return play; else if ((game[2]&&game[5]&&game[8]==play)||(game[3]&&game[5]&&game[7]==play)||(game[3]&&game[6]&&game[9]==play)) return play; else if ((game[4]&&game[5]&&game[6]==play)||(game[7]&&game[8]&&game[9]== play)) return play; } void outputBoard() //outputs current state of board { for(int x=1;x<=9;x++) //makes a loop for each turn -- x and o { if (game[x-1]==1) cout<<"x"; else if (game[x-1]==2) cout<<"o"; else cout<<" "; cout <<"|"; if (x==3||x==6) cout<<endl<<"-----"<<endl; } cout<<endl; } ///////////////////////// //Part 2: Main Program// /////////////////////// int main () { int won,turn1,turn2,first,move; TicTacToe(); cout<<endl; for(int x=1;x<=9;x++) //searches for moves { if(first==1) cout<<turn1<<endl; else if(first==2) cout<<turn2<<endl; } do //assures that each selection is possible { cout<<"Please select a number from the selection board."; cin>>move; if(game[move-1]!=0) cout << "That spot on the board is taken."<<endl; else if(game[move>9]) cout<<"You cannot select a number over 9."<<endl; } while (move<1||move>9||game[move-1]!=0); first=state(first,move); //calls function with current state of the board cout<<endl; outputBoard(); won=state(first,move); //outputs if program finds a winner if(won==1) { cout<<turn2<<"O wins."<<endl; break; } else if(won==2) { cout<<turn1<<"X wins."<<endl; break; } } system ("PAUSE"); return 0; }
I just quickly glanced at your code so please forgive me if this is wrong but don't you initialize the game[] with the values {1,2,3,4,5,6,7,8,9} but then to tell if there is a move there you compare the value at an index with 0? If none of the values equal 0 then it will always return that the space is taken.
•
•
Join Date: Sep 2008
Posts: 7
Reputation:
Solved Threads: 0
Well, I fixed it up a bit. I still have one more problem: After 6 moves, the game declares a winner even if there isn't one. Here is my revised code:
C++ Syntax (Toggle Plain Text)
//Tic Tac Toe game #include <iostream> using namespace std; //note: main program placed below function definitions for convenience int game[9]; //declares an array for the game board ///////////////////////////////// //Part 1: Function Definitions// /////////////////////////////// void TicTacToe() //makes a function for the selection board { cout<<"This is the selection board:"<<endl; cout<<"-----"<<endl; cout<<"1|2|3"<<endl; cout<<"-----"<<endl; cout<<"4|5|6"<<endl; cout<<"-----"<<endl; cout<<"7|8|9"<<endl; cout<<"-----"<<endl; } int move1(int play,int turn) //puts player's turn in memory { game[turn-1]=play; if (play==1) return play+1; else if (play==2) return play-1; } int state(int play,int first) { if((game[1]&&game[2]&&game[3]==play)||(game[1]&&game[4]&&game[7]==play)||(game[1]&&game[5]&&game[9]==play)) return play; else if ((game[2]&&game[5]&&game[8]==play)||(game[3]&&game[5]&&game[7]==play)||(game[3]&&game[6]&&game[9]==play)) return play; else if ((game[4]&&game[5]&&game[6]==play)||(game[7]&&game[8]&&game[9]== play)) return play; } void outputBoard() //outputs current state of board { for(int x=1;x<=9;x++) //makes a loop for each turn -- x and o { if (game[x-1]==1) cout<<"x"; else if (game[x-1]==2) cout<<"o"; else cout<<" "; cout <<"|"; if (x==3||x==6) cout<<endl<<"-----"<<endl; } cout<<endl; } ///////////////////////// //Part 2: Main Program// /////////////////////// int main () { int won,turn1,turn2,first,move; TicTacToe(); cout<<endl; cout<<"Who goes first? X (Player 1) or O (Player 2)?"<<endl; cin>>first; for(int x=1;x<=9;x++) { // if(first==1) // cout<<turn1<<endl; //else if(first==2) // cout<<turn2<<endl; do //assures that each selection is possible { cout<<"Please select a number from the selection board."; cin>>move; if(game[move-1]!=0) cout << "That spot on the board is taken."<<endl; else if(move<1 || move>9) cout<<"You cannot select a number over 9."<<endl; } while (move<1||move>9||game[move-1]!=0); first=move1(first,move); //calls function with current state of the board cout<<endl; outputBoard(); won=state(first,move); //outputs if program finds a winner if(won==1) { cout<<turn2<<"O wins."<<endl; break; } else if(won==2) { cout<<turn1<<"X wins."<<endl; break; } } system ("PAUSE"); return 0; }
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
is state() supposed to detect winners?
First of all, it does not always return a value.
Secondly the expression
Third, when you declare the board with
First of all, it does not always return a value.
Secondly the expression
game[1]&&game[2]&&game[3]==play does not test positions 1, 2 and 3 for the value play. I think you wanted game[1] == play && game[2] == play && game[3] == play Third, when you declare the board with
game[9] the indexes are 0 through 8. There is no 9. Last edited by Murtan; Dec 10th, 2008 at 6:03 pm. Reason: missed array bounds problem.
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
game[1]&&game[2]&&game[3]==play
That's wrong. && can't be chained like some other operators can be. Change it to :
That's wrong. && can't be chained like some other operators can be. Change it to :
C++ Syntax (Toggle Plain Text)
if( (game[1] == play && game[2] == play && game[3] == play) ||............
Klatu Barada Nikto
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
Just commenting you can chain them together but you need:
BUT you get away with this because the possible values of game[] are 0,1,2. The above logic will fail if you are not using powers of 2 as the flag values.
Normally, the shorthand version I have written, gives better executions speed. Especially, since compilers are very bad at determining that game[] is binary bound. (and I also think it is almost impossible for a compiler to determine this)
if (game[1]&game[2]&game[3])==play) BUT you get away with this because the possible values of game[] are 0,1,2. The above logic will fail if you are not using powers of 2 as the flag values.
Normally, the shorthand version I have written, gives better executions speed. Especially, since compilers are very bad at determining that game[] is binary bound. (and I also think it is almost impossible for a compiler to determine this)
experience is the most expensive way to learn anything
•
•
Join Date: Sep 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Could you post the new version of state()? (and any other code related to checking for wins?)
Currently it's:
C++ Syntax (Toggle Plain Text)
int state(int play,int first)
And as for detecting wins:
C++ Syntax (Toggle Plain Text)
first=move1(first,move); //calls function with current state of the board cout<<endl; outputBoard(); won=state(first,move); //outputs if program finds a winner if(won==1) { cout<<turn2<<"O wins."<<endl; break; } else if(won==2) { cout<<turn1<<"X wins."<<endl; break; }
![]() |
Similar Threads
- Tic-Tac-Toe (C++)
- Tic Tac Toe Homework (C++)
- Tic Tac toe help (C)
- Tic Tac Toe AI help, where to reset variables. (C++)
Other Threads in the C++ Forum
- Previous Thread: Has to be a smarter way of doing this
- Next Thread: fstream problem.
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






