943,563 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2232
  • C RSS
Nov 11th, 2007
0

How to make computer smarter in Tic Tac Toe?

Expand Post »
Hey, I've written a tic tac toe program.
Computer makes some random moves, but i want it to be smarter and make some intellegent moves.
any idea how to approach it?

here's the code i've written.
Although it's not that high-level, it's working fine.

  1. #include<stdio.h>
  2.  
  3.  
  4. #define BOARDSIZE 9
  5. #define USER 'O'
  6. #define COMPUTER 'X'
  7.  
  8.  
  9. // List of function prototypes
  10. void playOneGame();
  11. void clearGameBoard(char board[]);
  12. bool winner(char board[]);
  13. bool RowIsWinner(char s1, char s2, char s3);
  14. char TTTopponent(char player);
  15. void displayHorizontalLine();
  16. void displayOneLine(char a,char b,char c);
  17. void displayGameBoard(const char board[]);
  18. void displayGameBoard(const char board[]);
  19. int findEmptySpot(char[]);
  20. void computerPlay(char board[]);
  21. int userChoice(char board[]);
  22. void userPlay(char board[]);
  23. bool ValueInRange(int value,char board[]);
  24. bool userWantsToPlay();
  25. bool Filled(char board[]);
  26. void announceResult(int, char);
  27. bool isDraw(char board[]);
  28. int gameOver(char[]);
  29. void displayverticalLine();
  30. void displayInstructions();
  31.  
  32. //The main function of the Tic-Tac-Toe,Calls the "palyOneGame()" function
  33. //and ask the user if he wants to play again.
  34. // pre: None.
  35. // post: Return zero .
  36. int main()
  37. {
  38. displayInstructions();
  39. do
  40. {
  41. playOneGame();
  42. }while(userWantsToPlay());
  43. printf("Have a good Time\n");
  44. getchar();
  45. getchar();
  46. return 0;
  47. }
  48.  
  49. //Play one game, display and clear the game board,announce the winner and let player
  50. //and computer play.
  51. //Pre: None.
  52. //Post: None.
  53. void playOneGame()
  54. {
  55. char gameboard[BOARDSIZE]={'1','2','3','4','5','6','7','8','9'};
  56. displayGameBoard(gameboard);
  57. clearGameBoard(gameboard);
  58. char player=USER;
  59. int result;
  60. do
  61. {
  62. if(player==USER){
  63. userPlay(gameboard);
  64. }
  65. else
  66. {
  67. printf("\t\t\t U R too young to bit me!!!\n");
  68. computerPlay(gameboard);
  69. }
  70. displayGameBoard(gameboard);
  71. player=TTTopponent(player);
  72. result = gameOver(gameboard);
  73. }while(result ==0);
  74. announceResult(result, player);
  75. }
  76.  
  77. //Clears all the squares of the game board.
  78. //Pre: Game board passed to the function.
  79. //Post: None.
  80. void clearGameBoard(char board[])
  81. {
  82. for(int i=0;i<9;i++)
  83. {
  84. board[i]=' ';
  85. }
  86. }
  87.  
  88. //Determines if any of the horizontal, vertical and diagonal line is a winning line.
  89. //Pre: Game board passed to the bool type function.
  90. //Post: The function returns true if there is a winning row and false otherwise.
  91. bool winner(char board[])
  92. {
  93. return (RowIsWinner(board[0], board[1],board[2])||RowIsWinner(board[3], board[4], board[5])
  94. || RowIsWinner(board[6], board[7],board[8])||RowIsWinner(board[0], board[3],board[6])
  95. || RowIsWinner(board[1], board[4],board[7])||RowIsWinner(board[2], board[5],board[8])
  96. || RowIsWinner(board[0], board[4],board[8])||RowIsWinner(board[2], board[4],board[6]));
  97.  
  98. }
  99.  
  100. //The bool type function determines if a row is a winning and there is no empty spot in it.
  101. //Pre: Three char type defined [s1,s2,s3] values passed to the function.
  102. //Post: Returns true if the squares have equal values and not empty and returns false otherwise.
  103. bool RowIsWinner(char s1, char s2, char s3)
  104. {
  105. return (s1==s2 && s2 ==s3&& s2!=' ');
  106.  
  107. }
  108.  
  109. //The char type function switch the opponent.
  110. //Pre: The predifined symbols passed to the char type function.
  111. //Post: Returns the switched value.
  112. char TTTopponent(char player)
  113. {
  114. if(player==COMPUTER)
  115. return USER;
  116. else
  117. return COMPUTER;
  118. }
  119.  
  120. //The void function only display a horizontal row.
  121. //Pre: None.
  122. //Post: None.
  123. void displayHorizontalLine()
  124. {
  125. printf("\t\t\t\t --- --- ---\n");
  126. }
  127.  
  128. //The void function only displays the vertical lines.
  129. //Pre: None.
  130. //Post: None.
  131. void displayverticalLine()
  132. {
  133. printf("\t\t\t\t| | | |\n");
  134. }
  135.  
  136. //The void function displays character within the vertical lines.
  137. //Pre: Three defined char type value a,b,c passed to the function.
  138. //post: None.
  139. void displayOneLine(char a,char b,char c)
  140. {
  141. printf("\t\t\t\t| %c | %c | %c | \n",a,b,c);
  142. }
  143.  
  144. //The void function displays the game board on the screen.
  145. //Pre: Char type array board with constant cells is passed to the function.
  146. //Post:None.
  147. void displayGameBoard(const char board[])
  148. {
  149. displayHorizontalLine();
  150. displayverticalLine();
  151. displayOneLine(board[0],board [1],board [2]);
  152. displayverticalLine();
  153. displayHorizontalLine();
  154. displayverticalLine();
  155. displayOneLine(board[3],board [4],board [5]);
  156. displayverticalLine();
  157. displayHorizontalLine();
  158. displayverticalLine();
  159. displayOneLine(board[6],board [7],board [8]);
  160. displayverticalLine();
  161. displayHorizontalLine();
  162. printf("\n\n\n");
  163. }
  164.  
  165. //The void function displays the instruction of the game.
  166. //Pre: None.
  167. //Post: None.
  168. void displayInstructions()
  169. {
  170. printf("\t\t\tThe game starts by the user, Using an 'O'\n");
  171. printf("\t\t\t and computer, using an 'X'.\n");
  172. printf("\t\t\tThe first one who completes a row is winner.\n");
  173. }
  174.  
  175. //The int type function find the empty spots in the game board.
  176. //Pre: The char type board array is passed to the function.
  177. //Post: Returns integer.
  178. int findEmptySpot(char board[])
  179. {
  180. for(int i=0;i<=9;i++)
  181. {
  182. if(board[i]==' ')
  183. return i;
  184. }
  185. }
  186.  
  187. //The void type function that make moves as if computer is playing.
  188. //Pre: Char type board[] is passed to the function.
  189. //Post: None.
  190. void computerPlay(char board[])
  191. {
  192. int empty = findEmptySpot(board);
  193.  
  194. board[empty]='X';
  195. }
  196.  
  197. //The char type function that lets the user choose a valid number that is in range.
  198. //Pre: Char type array is passed to the function.
  199. //Post: Returns
  200. int userChoice(char board[])
  201. {
  202. int i;
  203. printf("\t\t\t It`s your turn buddy!!!\n");
  204. printf("\t\t\t choose an empty square\n");
  205. scanf("%d", &i);
  206. while(!ValueInRange(i-1,board))
  207. {
  208. printf("Your Choice is not valid.Please enter another one\n");
  209. getchar();
  210.  
  211. scanf("%d", &i);
  212. }
  213. return (i-1);
  214. }
  215.  
  216. //The void type function that lets the user play and make a choice and also calls
  217. //the "userChoice()" function within.
  218. //Pre: The char type board array is passed to the function.
  219. //Post: None.
  220. void userPlay(char board[])
  221. {
  222. int userSpot;
  223. userSpot= userChoice(board);
  224. board[userSpot] = USER;
  225. }
  226.  
  227. //A bool type function checks the value to be in range.
  228. //Pre: A defined integer value and a char type board is passed to the function.
  229. //Post: Returns true if the value is in range and not an space.
  230. bool ValueInRange(int value,char board[])
  231. {
  232. return(board[value]==' '&& value<10);
  233. }
  234.  
  235. //A bool type function that ask if the user wants to play.
  236. //Pre: None.
  237. //Post: True if yes and false otherwise.
  238. bool userWantsToPlay()
  239. {
  240. char yesNo;
  241. printf("\n Do you want to continue? (Y/N) ");
  242. getchar();
  243. scanf("%c",&yesNo);
  244. return (yesNo =='Y' || yesNo =='y');
  245. }
  246.  
  247. //A bool type function that check all the board cells to find out if empty or not.
  248. //Pre: A char type value board is passed to the function.
  249. //Post: Returns true if the board is full and false otherwise.
  250. bool Filled(char board[])
  251. {
  252. for(int i=0;i<9;i++)
  253. {
  254. if(board[i]==' ')
  255. return false;
  256. }
  257. return true;
  258. }
  259.  
  260. //A void type function that announce the winner of the game.
  261. //Pre: The int type "result" and char type "player" is passed to the function.
  262. //Post: None.
  263. void announceResult(int result, char player)
  264. {
  265. if (result==1)
  266. {
  267. player=TTTopponent(player);
  268. printf("\t\t\tThe winner is:%c", player);
  269. }
  270. else
  271. printf("\t\t\tThe Game Is Draw(Tie)");
  272. }
  273.  
  274. //A bool type function that determines if the game is draw.
  275. //Pre:The char type "board[]" is passed to the function.
  276. //Post:Ture is the board is full.
  277. bool isDraw(char board[])
  278. {
  279. return(Filled(board));
  280. }
  281.  
  282. //An int type function that determine if the game is over or not by selection.
  283. //Pre: The char type "board[]" is passed to the function.
  284. //Post: Returns integer 1 if there is a winner and 2 if the game is draw and 0
  285. //if the game is no over yet.
  286. int gameOver(char board[])
  287. {
  288. if (winner(board))
  289. return 1;
  290. if (isDraw(board))
  291. return 2;
  292. else
  293. return 0;
  294. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
megan-smith is offline Offline
13 posts
since Sep 2007
Nov 11th, 2007
0

Re: How to make computer smarter in Tic Tac Toe?

The first step would be to detect if the opponent is one move away from a winning move, then block that move.
X|0|.
X|.|0
O|.|.
In other words, detect that there are two 'X' in a line, and playing the red '0' is a damn fine idea at this point.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 12th, 2007
0

Re: How to make computer smarter in Tic Tac Toe?

One possible way to implement this is using a magic square:
  1. 4|3|8
  2. ------
  3. 9|5|1
  4. ------
  5. 2|7|6
It was effective when I wrote TTT the first time.
Last edited by WaltP; Nov 12th, 2007 at 8:17 pm.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006

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: A problem when I employ GNU Scientific Libraries in a Code
Next Thread in C Forum Timeline: Help in license plate generating program





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


Follow us on Twitter


© 2011 DaniWeb® LLC