944,204 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5704
  • C++ RSS
Jan 26th, 2005
0

Tic Tac Toe AI help, where to reset variables.

Expand Post »
I have just started using C++ after a year or two of a BASIC language, and I wanted to do something that required some logic, but not a lot of different commands. I decided to do a tic tac toe playing program where your opponent was the program.

The way this is supposed to work is that the program goes through loops and put down pieces until the game ends, and keeping track of wins, losses, and draws in all of those possibilities, and find the best move based on the count. My question comes as pieces are put down, and after the sim-game has ended inside the counting cycle. I'm almost sure that if the spaces are not reset to empty before the next time it is used, it will interefere with the simulations. I was wondering when an where to reset the spaces.

My time on the computer is almost up for a while so I will post the start of my code and return tomorrow.

C++ Syntax (Toggle Plain Text)
  1. //simple AI for tic tac toe.
  2.  
  3. //load headers and namespace
  4. #include <iostream>
  5. #include <conio.h>
  6. #include <windows.h>
  7.  
  8. using namespace std;
  9.  
  10. //begin main function
  11. int main()
  12. {
  13.  
  14. int pos [9] ;
  15. int turn ;
  16. turn=9 ;
  17. int space [9] ;
  18. int wins [9] ;
  19. int move [9] ;
  20.  
  21. /*
  22.  
  23. space positions
  24.  
  25. 1 2 3
  26. 4 5 6
  27. 7 8 9
  28.  
  29. Winning possibilities
  30. 1 2 3
  31. - - -
  32. - - -
  33.  
  34. - - -
  35. 4 5 6
  36. - - -
  37.  
  38. - - -
  39. - - -
  40. 7 8 9
  41.  
  42. 1 - -
  43. 4 - -
  44. 7 - -
  45.  
  46. - 2 -
  47. - 5 -
  48. - 8 -
  49.  
  50. - - 3
  51. - - 6
  52. - - 9
  53.  
  54. 1 - -
  55. - 5 -
  56. - - 9
  57.  
  58. - - 3
  59. - 5 -
  60. 7 - -
  61.  
  62.  
  63. */
  64.  
  65. //first turn
  66. if (turn==9)
  67. {
  68. //first turn, layer 1
  69. for (pos [1]=1; pos [1]<4; pos [1]++)
  70. {
  71. //reset all spaces to unocupied (0)
  72. pos [1]=0;
  73. pos [2]=0;
  74. pos [3]=0;
  75. pos [4]=0;
  76. pos [5]=0;
  77. pos [6]=0;
  78. pos [7]=0;
  79. pos [8]=0;
  80. pos [9]=0;
  81. //occupy chosen space with an AI piece (1)
  82. if (pos [1]==1) {space [1]=1;}
  83. if (pos [1]==1) {space [2]=1;}
  84. if (pos [1]==1) {space [5]=1;}
  85. //first turn, layer 2
  86. for (pos [2]=1; pos [2]<10; pos [2]++)
  87. {
  88. //check to see if chosen space is unoccupied or not
  89. if (space [pos [2]]=0)
  90. {
  91. //occupy space with human piece (2)
  92. space [pos [2]]=2;
  93. //first turn, layer 3
  94. for (pos [3]=1; pos [3]<10; pos [3]++)
  95. {

Thank you for whatever help I get and I am sorry for not being able to answer any questions today.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Khishin is offline Offline
23 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Base converter
Next Thread in C++ Forum Timeline: keystroke help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC