Guest User

Untitled

a guest
Jun 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. /*------------Libraries------------*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. /*------------PRINT BOARD: prints the current board state------------*/
  8. /*
  9. Input: the current board state, represented by a char* of length 10 (9char+\0), where spaces that have been played
  10. in have an "X" or "O" (whichever player played there), and unplyed spaces have a lowercase letter between a
  11. and i, in increasing order from top left.
  12. Output: Prints the board state, does not return any value.
  13. */
  14. void printBoard(char *board)
  15. {
  16. printf("\033[2J"); //clear screen
  17. printf("\n %c | %c | %c \n", board[0], board[1], board[2]);
  18. printf("-----------\n");
  19. printf(" %c | %c | %c \n", board[3], board[4], board[5]);
  20. printf("-----------\n");
  21. printf(" %c | %c | %c \n", board[6], board[7], board[8]);
  22. }
  23.  
  24.  
  25.  
  26. /*------------GAME OVER: Prints congratulatory message------------*/
  27. //Input: (int) If someone won, the int of the ASCII Char of the winner.
  28. //Output: Prints the result of the game, then returns 1 (unless there's a problem).
  29. int gameOver(int result)
  30. {
  31. if ((int) result == 88 || (int) result == 79) //if X or O won
  32. {
  33. printf("\nGAME OVER!\nPlayer %c is the winner!!\n", (char) result);
  34. return 1;
  35. }
  36. else if (result == 1) //tie
  37. {
  38. printf("\nIt's a tie!!!\n");
  39. return 1;
  40. }
  41. else //Should never happen
  42. {
  43. printf("\nError in Game Over. Wrong result passed to fn.\n");
  44. return 0;
  45. }
  46. }
  47.  
  48.  
  49.  
  50. /*------------CHANGE PLAYER: Changes the player whose turn it is, then prompts for the current player's move.------------*/
  51. //Input: (char *player) the char representing the current player. Must be "X" or "O", aka (char) 88 or (char) 79.
  52. //Output: (char) a char representing the NEW current player. Must be "X" or "O", aka (char) 88 or (char) 79.
  53. char changePlayer(char player)
  54. {
  55. if (player == 88) //X
  56. {
  57. player = 79;
  58. }
  59. else if (player == 79) //O
  60. {
  61. player = 88;
  62. }
  63. else //Should never happen
  64. {
  65. printf("ERROR IN MOVE: Wrong player passed");
  66. }
  67. return player;
  68. }
  69.  
  70.  
  71.  
  72. /*------------PROMPT MOVE: Prompts for the current player's move.------------*/
  73. /*
  74. Input:
  75. 1. (char *board) the current board state as a pointer to allocated memory (must be writable).
  76. 2. (char *player) the char representing the current player. Must be "X" or "O", aka (char) 88 or (char) 79.
  77. Output:
  78. (int) an integer between 0 and 8 (inclusive) represnting the space on the board that the current player
  79. wants to play in.
  80. */
  81. int promptMove(char *board, char player)
  82. {
  83. printf("Player %c, which space would you like to play?: ", player);
  84. int last = 41; //int value of ASCII character; used to check for double newlines & quits
  85. //loop to check validity of submission
  86. while (1 == 1)
  87. {
  88. int space; //int (0-8); the space on the board that the player has selected
  89. int input; //int value of ASCII character; the immediate, most recent typed value
  90. int answer = 0; //int value of ASCII character;where input goes if its not garbage.
  91. while ((input = getchar()) != EOF && input != '\n') //Toss newlines and EOF
  92. //NOTE: Need to make this while loop more robust. I want it to only accept one char at a time & to toss ANSI escape codes, but haven't figured it out yet. Something with fgets, it sounds like.
  93. {
  94. if (answer == 0 && input >= 97 && input <= 105) //Check if it's an ASCII char btwn 'a' and 'i'
  95. {
  96. answer = input;
  97. space = answer - 97; //translate ASCII code to space in board array
  98. if (board[space] >= 97 && board[space] <= 105) //check if that space has been played
  99. {
  100. return space;
  101. }
  102. else
  103. {
  104. printBoard(board);
  105. printf("\nThat space is taken already! Please try again:");
  106. }
  107. }
  108. else if (input == 113 || input == 81) //quit
  109. {
  110. last = input;
  111. printf("\nAre you sure you would like to quit? (y/n)\n");
  112.  
  113. }
  114. else //they entered something weird
  115. {
  116. if ((last == 81 || last == 113) && (input == 89 || input == 121)) //if last key was qQ and this is yY, quit
  117. {
  118. printf("\nQuitting game...\n");
  119. exit(0);
  120. }
  121. printBoard(board);
  122. printf("Type in the letter of the space you want to play in.\nIt must be a lowercase letter between 'a' and 'i'.\n------\nPlayer %c, please try again:",
  123. player);
  124. }
  125. }
  126. }
  127. }
  128.  
  129.  
  130.  
  131. /*------------Checks whether a win has occurred------------*/
  132. /*
  133. Input: Current board state (see printBoard above, input is identical)
  134. Output:
  135. - If someone won, the int of the char representing that player. Must be 88 (X) or 79 (O).
  136. - If the board is full but no win happened (aka a tie), return 1.
  137. - If no win yet, return 0.
  138. */
  139. int checkGame(char *board)
  140. {
  141. int tie = 0;
  142. for (int i = 0; i <= strlen(board); i ++)
  143. {
  144. //case: horizontal win
  145. if (i % 3 == 0 && board[i] == board[i + 1] && board[i] == board[i + 2])
  146. {
  147. return (int) board[i];
  148. }
  149.  
  150. //case: vertical win
  151. else if (i <= 2 && board[i] == board[i + 3] && board[i] == board[i + 6])
  152. {
  153. return (int) board[i];
  154. }
  155.  
  156. //case: diagonal win
  157. if (i == 0 || i == 2)
  158. {
  159. int add;
  160. if (i == 0)
  161. {
  162. add = 4;
  163. }
  164. else if (i == 2)
  165. {
  166. add = 2;
  167. }
  168. else //should never happen
  169. {
  170. printf("\nERROR in Board Check Function!!!\n");
  171. return 0;
  172. }
  173.  
  174. if (board[i] == board[i + add] && board[i] == board[i + (2 * add)])
  175. {
  176. return (int) board[i];
  177. }
  178. }
  179. //counting filled spaces; if all spaces are filled, game's over (tie)
  180. if (board[i] == 88 || board[i] == 79)
  181. {
  182. tie = tie + 1;
  183. }
  184. if (tie > 8)
  185. {
  186. return 1;
  187. }
  188. }
  189. return 0;
  190. }
  191.  
  192.  
  193.  
  194. int main(void)
  195. {
  196. //Declare variables & allocate memory
  197. char playerChar = (char) 88; //current player
  198. char *boardState = "abcdefghi"; //initial board state
  199. int currMove = 0; //the space played in most recently (Current)
  200. int game = 0; //boolean for whether the game is still running
  201. char *oldBoard = malloc(10 * sizeof(char)); //old board state, just storage
  202. if (oldBoard == NULL)
  203. {
  204. return 1;
  205. }
  206. char *newBoard = malloc(10 * sizeof(char)); //new board state (Current)
  207. if (newBoard == NULL)
  208. {
  209. return 1;
  210. }
  211.  
  212. //Assign correct initial values to old & new board states
  213. strncpy(oldBoard, boardState, 11);
  214. strncpy(newBoard, boardState, 11);
  215.  
  216. //The big loop of play. Continues as play occurs, until game ends.
  217. while (game == 0)
  218. {
  219. printBoard(newBoard);
  220. playerChar = changePlayer(playerChar);
  221. currMove = promptMove(newBoard, playerChar);
  222.  
  223. //Copy new board state into newBoard variable
  224. for (int i = 0, n = strlen(boardState); i < n; i++)
  225. {
  226. if (i == currMove)
  227. {
  228. newBoard[i] = playerChar;
  229. }
  230. else
  231. {
  232. newBoard[i] = oldBoard[i];
  233. }
  234. }
  235. strncpy(oldBoard, newBoard, 11);
  236. game = checkGame(newBoard);
  237. }
  238. printBoard(newBoard);
  239. return gameOver(game);
  240. }
Add Comment
Please, Sign In to add comment