Need help with Tic Tac Toe

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 7
Reputation: Dannielf is an unknown quantity at this point 
Solved Threads: 0
Dannielf Dannielf is offline Offline
Newbie Poster

Need help with Tic Tac Toe

 
0
  #1
Dec 10th, 2008
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.


  
  1. //Tic Tac Toe game
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. //note: main program placed below function definitions for convenience
  7.  
  8. int game[9]={1,2,3,4,5,6,7,8,9}; //declares an array for the game board
  9.  
  10. /////////////////////////////////
  11. //Part 1: Function Definitions//
  12. ///////////////////////////////
  13.  
  14. void TicTacToe() //makes a function for the selection board
  15. {
  16. cout<<"This is the selection board:"<<endl;
  17. cout<<"-----"<<endl;
  18. cout<<"1|2|3"<<endl;
  19. cout<<"-----"<<endl;
  20. cout<<"4|5|6"<<endl;
  21. cout<<"-----"<<endl;
  22. cout<<"7|8|9"<<endl;
  23. cout<<"-----"<<endl;
  24. }
  25.  
  26. int move(int play,int turn) //puts player's turn in memory
  27. {
  28. game[turn-1]=play;
  29. if (turn==1)
  30. return play+1;
  31. else if (turn==2)
  32. return play-1;
  33. }
  34.  
  35. int state(int play,int first)
  36. {
  37. if((game[1]&&game[2]&&game[3]==play)||(game[1]&&game[4]&&game[7]==play)||(game[1]&&game[5]&&game[9]==play))
  38. return play;
  39. else if ((game[2]&&game[5]&&game[8]==play)||(game[3]&&game[5]&&game[7]==play)||(game[3]&&game[6]&&game[9]==play))
  40. return play;
  41. else if ((game[4]&&game[5]&&game[6]==play)||(game[7]&&game[8]&&game[9]== play))
  42. return play;
  43. }
  44.  
  45. void outputBoard() //outputs current state of board
  46. {
  47. for(int x=1;x<=9;x++) //makes a loop for each turn -- x and o
  48. {
  49. if (game[x-1]==1)
  50. cout<<"x";
  51. else if (game[x-1]==2)
  52. cout<<"o";
  53. else
  54. cout<<" ";
  55. cout <<"|";
  56. if (x==3||x==6)
  57. cout<<endl<<"-----"<<endl;
  58. }
  59. cout<<endl;
  60. }
  61.  
  62. /////////////////////////
  63. //Part 2: Main Program//
  64. ///////////////////////
  65.  
  66. int main ()
  67. {
  68. int won,turn1,turn2,first,move;
  69. TicTacToe();
  70. cout<<endl;
  71. for(int x=1;x<=9;x++) //searches for moves
  72. {
  73. if(first==1)
  74. cout<<turn1<<endl;
  75. else if(first==2)
  76. cout<<turn2<<endl;
  77. }
  78.  
  79. do //assures that each selection is possible
  80. {
  81. cout<<"Please select a number from the selection board.";
  82. cin>>move;
  83. if(game[move-1]!=0)
  84. cout << "That spot on the board is taken."<<endl;
  85. else if(game[move>9])
  86. cout<<"You cannot select a number over 9."<<endl;
  87. }
  88. while (move<1||move>9||game[move-1]!=0);
  89.  
  90. first=state(first,move); //calls function with current state of the board
  91. cout<<endl;
  92. outputBoard();
  93.  
  94. won=state(first,move); //outputs if program finds a winner
  95. if(won==1)
  96. {
  97. cout<<turn2<<"O wins."<<endl;
  98. break;
  99. }
  100. else if(won==2)
  101. {
  102. cout<<turn1<<"X wins."<<endl;
  103. break;
  104. }
  105. }
  106. system ("PAUSE");
  107. return 0;
  108. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: Need help with Tic Tac Toe

 
0
  #2
Dec 10th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 7
Reputation: Dannielf is an unknown quantity at this point 
Solved Threads: 0
Dannielf Dannielf is offline Offline
Newbie Poster

Re: Need help with Tic Tac Toe

 
0
  #3
Dec 10th, 2008
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:

  
  1. //Tic Tac Toe game
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. //note: main program placed below function definitions for convenience
  7.  
  8. int game[9]; //declares an array for the game board
  9.  
  10. /////////////////////////////////
  11. //Part 1: Function Definitions//
  12. ///////////////////////////////
  13.  
  14. void TicTacToe() //makes a function for the selection board
  15. {
  16. cout<<"This is the selection board:"<<endl;
  17. cout<<"-----"<<endl;
  18. cout<<"1|2|3"<<endl;
  19. cout<<"-----"<<endl;
  20. cout<<"4|5|6"<<endl;
  21. cout<<"-----"<<endl;
  22. cout<<"7|8|9"<<endl;
  23. cout<<"-----"<<endl;
  24. }
  25.  
  26. int move1(int play,int turn) //puts player's turn in memory
  27. {
  28. game[turn-1]=play;
  29. if (play==1)
  30. return play+1;
  31. else if (play==2)
  32. return play-1;
  33. }
  34.  
  35. int state(int play,int first)
  36. {
  37. if((game[1]&&game[2]&&game[3]==play)||(game[1]&&game[4]&&game[7]==play)||(game[1]&&game[5]&&game[9]==play))
  38. return play;
  39. else if ((game[2]&&game[5]&&game[8]==play)||(game[3]&&game[5]&&game[7]==play)||(game[3]&&game[6]&&game[9]==play))
  40. return play;
  41. else if ((game[4]&&game[5]&&game[6]==play)||(game[7]&&game[8]&&game[9]== play))
  42. return play;
  43. }
  44.  
  45. void outputBoard() //outputs current state of board
  46. {
  47. for(int x=1;x<=9;x++) //makes a loop for each turn -- x and o
  48. {
  49. if (game[x-1]==1)
  50. cout<<"x";
  51. else if (game[x-1]==2)
  52. cout<<"o";
  53. else
  54. cout<<" ";
  55. cout <<"|";
  56. if (x==3||x==6)
  57. cout<<endl<<"-----"<<endl;
  58. }
  59. cout<<endl;
  60. }
  61.  
  62. /////////////////////////
  63. //Part 2: Main Program//
  64. ///////////////////////
  65.  
  66. int main ()
  67. {
  68. int won,turn1,turn2,first,move;
  69. TicTacToe();
  70. cout<<endl;
  71.  
  72. cout<<"Who goes first? X (Player 1) or O (Player 2)?"<<endl;
  73. cin>>first;
  74. for(int x=1;x<=9;x++)
  75. {
  76.  
  77. // if(first==1)
  78. // cout<<turn1<<endl;
  79. //else if(first==2)
  80. // cout<<turn2<<endl;
  81.  
  82. do //assures that each selection is possible
  83. {
  84. cout<<"Please select a number from the selection board.";
  85. cin>>move;
  86. if(game[move-1]!=0)
  87. cout << "That spot on the board is taken."<<endl;
  88. else if(move<1 || move>9)
  89. cout<<"You cannot select a number over 9."<<endl;
  90. }
  91. while (move<1||move>9||game[move-1]!=0);
  92.  
  93. first=move1(first,move); //calls function with current state of the board
  94. cout<<endl;
  95. outputBoard();
  96.  
  97. won=state(first,move); //outputs if program finds a winner
  98. if(won==1)
  99. {
  100. cout<<turn2<<"O wins."<<endl;
  101. break;
  102. }
  103. else if(won==2)
  104. {
  105. cout<<turn1<<"X wins."<<endl;
  106. break;
  107. }
  108. }
  109. system ("PAUSE");
  110. return 0;
  111. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Need help with Tic Tac Toe

 
0
  #4
Dec 10th, 2008
is state() supposed to detect winners?

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Need help with Tic Tac Toe

 
0
  #5
Dec 10th, 2008
game[1]&&game[2]&&game[3]==play

That's wrong. && can't be chained like some other operators can be. Change it to :
  1. if( (game[1] == play &&
  2. game[2] == play &&
  3. game[3] == play) ||............
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 7
Reputation: Dannielf is an unknown quantity at this point 
Solved Threads: 0
Dannielf Dannielf is offline Offline
Newbie Poster

Re: Need help with Tic Tac Toe

 
0
  #6
Dec 10th, 2008
thank you all for the help. my original problem has been solved.

but now another problem arises: the program seems to now ignore when X or O wins. It just continues with the loop. any way i can solve this?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Need help with Tic Tac Toe

 
0
  #7
Dec 10th, 2008
Just commenting you can chain them together but you need:

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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Need help with Tic Tac Toe

 
0
  #8
Dec 10th, 2008
Could you post the new version of state()? (and any other code related to checking for wins?)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 7
Reputation: Dannielf is an unknown quantity at this point 
Solved Threads: 0
Dannielf Dannielf is offline Offline
Newbie Poster

Re: Need help with Tic Tac Toe

 
0
  #9
Dec 10th, 2008
Originally Posted by Murtan View Post
Could you post the new version of state()? (and any other code related to checking for wins?)
I didn't change state. Not sure what you were indicating, sorry.

Currently it's:
  1. int state(int play,int first)

And as for detecting wins:
  1. first=move1(first,move); //calls function with current state of the board
  2. cout<<endl;
  3. outputBoard();
  4.  
  5. won=state(first,move); //outputs if program finds a winner
  6. if(won==1)
  7. {
  8. cout<<turn2<<"O wins."<<endl;
  9. break;
  10. }
  11. else if(won==2)
  12. {
  13. cout<<turn1<<"X wins."<<endl;
  14. break;
  15. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Need help with Tic Tac Toe

 
0
  #10
Dec 10th, 2008
I wanted to see the body of state(), not the prototype.
State decides who won and was the source of a lot of your bugs earlier...I want to see how you have re-coded it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC