Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. // Tristen ################### 8/13/2017 7:01 A.M.
  7. // Tic-tac-toe program.
  8.  
  9. // Grid size for the user (1-9). 0-8 for arrays.
  10. #define GRID_SIZE 9
  11.  
  12. int grid[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
  13.  
  14. // Gets the player's character based on the value provided.
  15. char convert_digits(int digit)
  16. {
  17. if (digit == 1)
  18. {
  19. return 'O';
  20. }
  21. else if (digit == -1)
  22. {
  23. return 'X';
  24. }
  25. else
  26. {
  27. return '-';
  28. }
  29. }
  30.  
  31. int valid_position(short pos)
  32. {
  33. if (grid[pos] == 0)
  34. {
  35. return 1;
  36. }
  37. else
  38. {
  39. return 0;
  40. }
  41. }
  42.  
  43. void cat_message()
  44. {
  45. printf("\nCat - Nobody wins.");
  46. }
  47.  
  48. void lost_message(char c)
  49. {
  50. printf("\nPlayer %c loses the game.", c);
  51. }
  52.  
  53. void win_message(char c)
  54. {
  55. printf("\nPlayer %c wins the game.", c);
  56. }
  57.  
  58. // Find the number of empty spots left.
  59. int find_empty_indices_l()
  60. {
  61. short indices_left = 0;
  62. short i;
  63. for (i = 0; i < GRID_SIZE; i++)
  64. {
  65. if (grid[i] == 0)
  66. {
  67. indices_left += 1;
  68. }
  69. }
  70. return indices_left;
  71. }
  72.  
  73. // Find the index of the empty spots that are left, and then store those indexes
  74. // in an array that is the length of find_empty_indexes_l().
  75. int* find_empty_indices()
  76. {
  77. short indices_left;
  78. short i;
  79. short i_indices_left = 0;
  80.  
  81. indices_left = find_empty_indices_l();
  82. int * indices_array = malloc(sizeof(char) * indices_left);
  83. for (i = 0; i < GRID_SIZE ; i++)
  84. {
  85. if (grid[i] == 0)
  86. {
  87. indices_array[i_indices_left] = i;
  88. i_indices_left += 1;
  89. }
  90. }
  91.  
  92. return indices_array;
  93. }
  94.  
  95. // Returns 0 if no cat detected. Returns 1 if cat is detected.
  96. int check_cat()
  97. {
  98. if (find_empty_indices() == 0) return 1;
  99. return 0;
  100. }
  101.  
  102. // Allows the player to move. Uses scanf for input and uses error-checking to avoid invalid parameters.
  103. // Places an X at that location.
  104. void pla_move()
  105. {
  106. short pos;
  107. int check = 0;
  108.  
  109. while (check == 0)
  110. {
  111. printf("\nChoose a position (1-9): \n");
  112. scanf("%d", &pos); // Entering any alphabetical characters seems to put this code into an infinite loop, bypassing intended user input. Works as intended for integers.
  113. if (valid_position(pos - 1) != 1 || pos > 9)
  114. {
  115. printf("Invalid position: try again\n");
  116. }
  117. else
  118. {
  119. check = 1;
  120. }
  121. }
  122.  
  123. grid[pos - 1] = -1; // -1 corresponds to X.
  124. }
  125.  
  126. // Allows the bot to move. Calculates what spots are left and moves to one randomly.
  127. // Finds a random index for the list of spots left, and choses the value at that index.
  128. // Places an O at that location.
  129. void bot_move()
  130. {
  131. short pos;
  132. int* valid_pos = find_empty_indices();
  133. srand(time(NULL));
  134. int pos_random = (rand() % find_empty_indices_l());
  135.  
  136. pos = *(valid_pos+pos_random); // Randomly decided position on the main static grid.
  137.  
  138. grid[pos] = 1;
  139.  
  140. printf("\nThe bot moved to position %d\n", pos + 1);
  141. free(valid_pos); // malloc() allocates to the heap, so we want to free this just in-case.
  142. }
  143.  
  144. // The win conditions for the game.
  145. int win_conditions(int b)
  146. {
  147. int win_flag = 0;
  148. if (grid[0] == b && grid[1] == b && grid[2] == b && win_flag == 0) // Top row
  149. {
  150. win_message(convert_digits(b));
  151. lost_message(convert_digits(-b));
  152. win_flag = 1;
  153. }
  154. if (grid[0] == b && grid[4] == b && grid[8] == b && win_flag == 0) // Top-left to bottom-right diagonal
  155. {
  156. win_message(convert_digits(b));
  157. lost_message(convert_digits(-b));
  158. win_flag = 1;
  159. }
  160. if (grid[0] == b && grid[3] == b && grid[6] == b && win_flag == 0) // Left row
  161. {
  162. win_message(convert_digits(b));
  163. lost_message(convert_digits(-b));
  164. win_flag = 1;
  165. }
  166. if (grid[1] == b && grid[4] == b && grid[7] == b && win_flag == 0) // Middle row (up-down)
  167. {
  168. win_message(convert_digits(b));
  169. lost_message(convert_digits(-b));
  170. win_flag = 1;
  171. }
  172. if (grid[2] == b && grid[5] == b && grid[8] == b && win_flag == 0) // Right row (up-down)
  173. {
  174. win_message(convert_digits(b));
  175. lost_message(convert_digits(-b));
  176. win_flag = 1;
  177. }
  178. if (grid[3] == b && grid[4] == b && grid[5] == b && win_flag == 0) // Middle row (left-right)
  179. {
  180. win_message(convert_digits(b));
  181. lost_message(convert_digits(-b));
  182. win_flag = 1;
  183. }
  184. if (grid[6] == b && grid[7] == b && grid[8] == b && win_flag == 0) // Bottom row (left-right)
  185. {
  186. win_message(convert_digits(b));
  187. lost_message(convert_digits(-b));
  188. win_flag = 1;
  189. }
  190. if (grid[2] == b && grid[4] == b && grid[6] == b && win_flag == 0) // Top-right to bottom-left diagonal
  191. {
  192. win_message(convert_digits(b));
  193. lost_message(convert_digits(-b));
  194. win_flag = 1;
  195. }
  196. if (win_flag == 0)
  197. {
  198. if (check_cat() == 1)
  199. {
  200. cat_message();
  201. }
  202. return win_flag;
  203. }
  204.  
  205. return win_flag;
  206. }
  207.  
  208. // Prints the digit grid instead of converting the digits to their respective 'X' or 'O' characters.
  209. void print_digit_grid()
  210. {
  211. short b;
  212. int t;
  213. for(b = 0; b < GRID_SIZE; b++)
  214. {
  215. t = grid[b];
  216. printf("%d", t);
  217. }
  218. }
  219.  
  220. // Simply prints the current grid with the corresponding 'X' and 'O' characters for 1's and -1's.
  221. void print_grid()
  222. {
  223. printf("\n");
  224. short i;
  225. char x_or_o;
  226. for(i = 0; i <= GRID_SIZE - 1; i++)
  227. {
  228. x_or_o = convert_digits(grid[i]);
  229. printf("%c", x_or_o);
  230.  
  231. if ((i + 1) % 3 == 0) // Format properly
  232. printf("\n");
  233. }
  234. }
  235.  
  236. int main(int argc, char* argv[])
  237. {
  238. int win_flag = 0;
  239.  
  240. while (win_flag != 1 && win_flag != -1)
  241. {
  242. pla_move();
  243. print_grid();
  244. win_flag = win_conditions(-1); // Check win condition for the player (-1)
  245. if (win_flag == 1)
  246. {
  247. break;
  248. }
  249. bot_move();
  250. print_grid();
  251. win_flag = win_conditions(1); // Check the win condition for the computer (1)
  252. }
  253.  
  254. if (win_flag == 1)
  255. {
  256. print_grid(grid);
  257. }
  258.  
  259. return 0;
  260. }
  261.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement