Advertisement
Guest User

Untitled

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