Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. /**
  2. * fifteen.c
  3. *
  4. * Computer Science 50
  5. * Problem Set 3
  6. *
  7. * Implements Game of Fifteen (generalized to d x d).
  8. *
  9. * Usage: fifteen d
  10. *
  11. * whereby the board's dimensions are to be d x d,
  12. * where d must be in [DIM_MIN,DIM_MAX]
  13. *
  14. * Note that usleep is obsolete, but it offers more granularity than
  15. * sleep and is simpler to use than nanosleep; `man usleep` for more.
  16. */
  17.  
  18. #define _XOPEN_SOURCE 500
  19.  
  20. #include <cs50.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24.  
  25. // constants
  26. #define DIM_MIN 3
  27. #define DIM_MAX 9
  28.  
  29. // board
  30. int board[DIM_MAX][DIM_MAX];
  31.  
  32. // dimensions
  33. int d;
  34.  
  35. // prototypes
  36. void clear(void);
  37. void greet(void);
  38. void init(void);
  39. void draw(void);
  40. bool move(int tile);
  41. bool won(void);
  42.  
  43. int main(int argc, string argv[])
  44. {
  45. // ensure proper usage
  46. if (argc != 2)
  47. {
  48. printf("Usage: fifteen d\n");
  49. return 1;
  50. }
  51.  
  52. // ensure valid dimensions
  53. d = atoi(argv[1]);
  54. if (d < DIM_MIN || d > DIM_MAX)
  55. {
  56. printf("Board must be between %i x %i and %i x %i, inclusive.\n",
  57. DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
  58. return 2;
  59. }
  60.  
  61. // open log
  62. FILE* file = fopen("log.txt", "w");
  63. if (file == NULL)
  64. {
  65. return 3;
  66. }
  67.  
  68. // greet user with instructions
  69. greet();
  70.  
  71. // initialize the board
  72. init();
  73.  
  74. // accept moves until game is won
  75. while (true)
  76. {
  77. // clear the screen
  78. clear();
  79.  
  80. // draw the current state of the board
  81. draw();
  82.  
  83. // log the current state of the board (for testing)
  84. for (int i = 0; i < d; i++)
  85. {
  86. for (int j = 0; j < d; j++)
  87. {
  88. fprintf(file, "%i", board[i][j]);
  89. if (j < d - 1)
  90. {
  91. fprintf(file, "|");
  92. }
  93. }
  94. fprintf(file, "\n");
  95. }
  96. fflush(file);
  97.  
  98. // check for win
  99. if (won())
  100. {
  101. printf("ftw!\n");
  102. break;
  103. }
  104.  
  105. // prompt for move
  106. printf("Tile to move: ");
  107. int tile = GetInt();
  108.  
  109. // quit if user inputs 0 (for testing)
  110. if (tile == 0)
  111. {
  112. break;
  113. }
  114.  
  115. // log move (for testing)
  116. fprintf(file, "%i\n", tile);
  117. fflush(file);
  118.  
  119. // move if possible, else report illegality
  120. if (!move(tile))
  121. {
  122. printf("\nIllegal move.\n");
  123. usleep(500000);
  124. }
  125.  
  126. // sleep thread for animation's sake
  127. usleep(500000);
  128. }
  129.  
  130. // close log
  131. fclose(file);
  132.  
  133. // success
  134. return 0;
  135. }
  136.  
  137. /**
  138. * Clears screen using ANSI escape sequences.
  139. */
  140. void clear(void)
  141. {
  142. printf("\033[2J");
  143. printf("\033[%d;%dH", 0, 0);
  144. }
  145.  
  146. /**
  147. * Greets player.
  148. */
  149. void greet(void)
  150. {
  151. clear();
  152. printf("WELCOME TO GAME OF FIFTEEN\n");
  153. usleep(2000000);
  154. }
  155.  
  156. /**
  157. * Initializes the game's board with tiles numbered 1 through d*d - 1
  158. * (i.e., fills 2D array with values but does not actually print them).
  159. */
  160. void init(void)
  161. {
  162. int n = (d*d) - 1;
  163. int i, j = 0;
  164.  
  165. for(i = 0; i < d; i++)
  166. {
  167. for(j = 0; j < d; j++)
  168. {
  169. board[i][j] = n;
  170. n--;
  171.  
  172. }
  173.  
  174. }
  175.  
  176. if(d % 2 == 0)
  177. {
  178. // if number of tiles is odd swaps the position of 2 and 1
  179. int swap;
  180. swap = board[d-1][d-2];
  181. board[d-1][d-2] = board[d-1][d-3];
  182. board[d-1][d-3] = swap;
  183. }
  184. }
  185.  
  186. /**
  187. * Prints the board in its current state.
  188. */
  189. void draw(void)
  190. {
  191. int i, j = 0;
  192. for(i = 0; i < d; i++)
  193. {
  194. for(j = 0; j < d; j++)
  195. {
  196. printf("%2d ", board[i][j]);
  197.  
  198. }
  199. printf("\n\n");
  200. }
  201.  
  202. }
  203.  
  204. /**
  205. * If tile borders empty space, moves tile and returns true, else
  206. * returns false.
  207. */
  208. bool move(int tile)
  209. {
  210. int i, j, temp, blank_i, blank_j;
  211.  
  212. // searching for the tile
  213.  
  214. // rows
  215. for (i = 0; i < d; i++)
  216. {
  217. // coloumns
  218. for (j = 0; j < d; j++)
  219. {
  220. // finding the tile
  221. if (board[i][j] == tile)
  222. {
  223.  
  224. // searching for the blank
  225.  
  226. // rows
  227. for (blank_i = 0; blank_i < d; blank_i++)
  228. {
  229. // coloumns
  230. for (blank_j = 0; blank_j < d; blank_j++)
  231. {
  232. // finding the blank
  233. if (board[blank_i][blank_j] == 0)
  234. {
  235. // making sure the move is legal
  236. if ((i == blank_i && j+1 == blank_j) || (i == blank_i && j-1 == blank_j) ||
  237. (i+1 == blank_i && j == blank_j) || (i-1 == blank_i && j == blank_j))
  238. {
  239. // swapping the tile and the 0 (blank)
  240. temp = board[i][j];
  241. board[i][j] = board[blank_i][blank_j];
  242. board[blank_i][blank_j] = temp;
  243. return true;
  244. }
  245. }
  246. }
  247. }
  248.  
  249. }
  250. }
  251. }
  252. return false;
  253. }
  254.  
  255. /**
  256. * Returns true if game is won (i.e., board is in winning configuration),
  257. * else false.
  258. */
  259. bool won(void)
  260. {
  261. int i, j = 0;
  262. for(i = 0; i < d; i++)
  263. {
  264. for(j = 0; j < d; j++)
  265. {
  266. if(board[i][j+1] != board[i][j] - 1)
  267. {
  268. return false;
  269. }
  270.  
  271. }
  272.  
  273. }
  274. return true;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement