Guest User

Untitled

a guest
Dec 10th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. /**
  2. * fifteen.c
  3. *
  4. * CS50 AP
  5. * Fifteen
  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. * Extra features including printing an actual grid to make it look more
  18. * tile-like, and using ANSI color sequences for some additional customizing
  19. *
  20. * Implement init./Implement draw./Implement move./Implement won.
  21. **/
  22.  
  23. #define _XOPEN_SOURCE 500
  24.  
  25. #include <cs50.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29.  
  30. // constants
  31. #define DIM_MIN 3
  32. #define DIM_MAX 9
  33.  
  34. // ansi escape sequence to print grid color
  35. // replace the number beteen [ and m with 31 for red, 32 for green, 33 for brown,
  36. // 34 for blue, 35 for purple, 36 for cyan, 37 for gray
  37. #define COLOR "\033[32m"
  38.  
  39. // board
  40. int board[DIM_MAX][DIM_MAX];
  41. int b1[DIM_MAX][DIM_MAX];
  42. //_ thing at the end
  43. string u = "_";
  44. int a;
  45. int b;
  46.  
  47. // dimensions
  48. int d;
  49.  
  50. // saved locations of the blank tile
  51. int blank_row;
  52. int blank_col;
  53.  
  54. // prototypes
  55. void clear(void);
  56. void greet(void);
  57. void init(void);
  58. void draw(void);
  59. bool move(int tile);
  60. bool won(void);
  61. void swap(int *a, int *b);
  62. void print_grid_row(int d);
  63. void print_tile(int tile);
  64.  
  65. int main(int argc, string argv[])
  66. {
  67. // ensure proper usage
  68. if (argc != 2)
  69. {
  70. printf("Usage: fifteen d\n");
  71. return 1;
  72. }
  73.  
  74. // ensure valid dimensions
  75. d = atoi(argv[1]);
  76. if (d < DIM_MIN || d > DIM_MAX)
  77. {
  78. printf("Board must be between %i x %i and %i x %i, inclusive.\n",DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
  79. return 2;
  80. }
  81.  
  82. // open log
  83. FILE *file = fopen("log.txt", "w");
  84. if (file == NULL)
  85. {
  86. return 3;
  87. }
  88.  
  89. // greet user with instructions
  90. greet();
  91.  
  92. // initialize the board
  93. init();
  94.  
  95. // accept moves until game is won
  96. while (true)
  97. {
  98. // clear the screen
  99. clear();
  100.  
  101. // draw the current state of the board
  102. draw();
  103.  
  104. // log the current state of the board (for testing)
  105. for (int i = 0; i < d; i++)
  106. {
  107. for (int j = 0; j < d; j++)
  108. {
  109. fprintf(file, "%i", board[i][j]);
  110. if (j < d - 1)
  111. {
  112. fprintf(file, "|");
  113. }
  114. }
  115. fprintf(file, "\n");
  116. }
  117. fflush(file);
  118.  
  119. // check for win
  120. if (won())
  121. {
  122. printf("ftw!\n");
  123. break;
  124. }
  125.  
  126. // prompt for move
  127. printf("Tile to move: ");
  128. int tile = get_int();
  129.  
  130. // quit if user inputs 0 (for testing)
  131. if (tile == 0)
  132. {
  133. break;
  134. }
  135.  
  136. // log move (for testing)
  137. fprintf(file, "%i\n", tile);
  138. fflush(file);
  139.  
  140. // move if possible, else report illegality
  141. if (!move(tile))
  142. {
  143. printf("\nIllegal move.\n");
  144. usleep(500000);
  145. }
  146.  
  147. // sleep thread for animation's sake
  148. usleep(50000);
  149. }
  150.  
  151. // close log
  152. fclose(file);
  153.  
  154. // success
  155. return 0;
  156. }
  157.  
  158. /**
  159. * Clears screen using ANSI escape sequences.
  160. */
  161. void clear(void)
  162. {
  163. printf("\033[2J");
  164. printf("\033[%d;%dH", 0, 0);
  165. }
  166.  
  167. /**
  168. * Greets player.
  169. */
  170. void greet(void)
  171. {
  172. clear();
  173. printf("WELCOME TO GAME OF FIFTEEN\n");
  174. usleep(2000000);
  175. }
  176.  
  177. /**
  178. * Initializes the game's board with tiles numbered 1 through d*d - 1 d = int dimensions
  179. * (i.e., fills 2D array with values but does not actually print them).
  180. */
  181. void init(void)
  182. {
  183. int value = d*d - 1;
  184. //int a;
  185. //int b;
  186.  
  187. //rows (except the last row)
  188. for (a=0; a < d-1; a++) //repeats how many rows there are (d number of times
  189. {
  190. for (b = 0; b < d; b ++) //assign number in just 1 row and column
  191. {
  192. board[a][b] = value--;
  193. }
  194. }
  195. //last row except _
  196. for (b = 0; b < d-1; b ++) //assign number in just 1 row and column
  197. {
  198. board[a][b] = value--;
  199. }
  200. //IF D IS EVEN, SWAP 2 AND 1
  201. int x;
  202. if (d%2 == 0)
  203. {
  204. x = board[d-1][d-3]; //store variable
  205. board[d-1][d-3] = board[d-1][d-2]; //swap unstored variable to right place
  206. board[d-1][d-2] = x;
  207. }
  208. }
  209.  
  210. /**
  211. * Prints the board in its current state.
  212. **/
  213. void draw(void)
  214. {
  215. for (a = 0; a < d; a++) //for each row
  216. {
  217. for (b = 0; b < d; b++) //for each column
  218. {
  219. if (board[a][b] != 0) //if it's any value other than _
  220. {
  221. printf(" %2i", board[a][b]);
  222. }
  223. else
  224. {
  225. printf(" %s", u); //print _
  226. }
  227. }
  228. printf("\n"); //print new line
  229. }
  230. }
  231.  
  232. /**
  233. * If tile borders empty space, moves tile and returns true, else returns false.
  234. */
  235. bool move(int tile)
  236. {
  237. int x;
  238. //int a;
  239. //int b;
  240. for (int i = 0; i < d; i++) //for each row
  241. {
  242. for (int j = 0; j < d; j++) //for each column
  243. {
  244. if (board[i][j] == 0)
  245. {
  246. x = board[i][j];
  247. a = i;
  248. b = j;
  249. }
  250. }
  251. }
  252. if (tile == board[a-1][b])
  253. {
  254. board[a-1][b] = x;
  255. board[a][b] = tile;
  256. }
  257. if (tile == board[a][b+1])
  258. {
  259. board[a][b+1] = x;
  260. board[a][b] = tile;
  261. {
  262. if (tile == board[a+1][b])
  263. {
  264. board[a+1][b] = x;
  265. board[a][b] = tile;
  266. }
  267. if (tile == board[a][b-1])
  268. {
  269. board[a][b-1] = x;
  270. board[a][b] = tile;
  271. }
  272. return true;
  273. }
  274. /**
  275. * Returns true if game is won (i.e., board is in winning configuration),
  276. * else false.
  277. **/
  278.  
  279. bool won(void);
  280. int order = 1;
  281. int i;
  282. int j;
  283. for (i = 0; i < d; i++) //for making an array
  284. {
  285. for (j = 0; j < d; j++)
  286. {
  287. if (board[i][j] == order) //if board equals value of order
  288. {
  289. order++; //increase value of order
  290. }
  291. else
  292. {
  293. if (board[d-1][d-1] == 0)
  294. {
  295. return true;
  296. }
  297. return false;
  298. }
  299. }
  300. }
  301. /* else if (board[i][j] != 0) //if board doesn't equal value of order
  302. {
  303. return false; //return false
  304. }
  305. else if (board[i][j] == 0)
  306. {
  307. return true;
  308. }
  309. else //if board equals 0
  310. {
  311. return true; //return true
  312. */ //return true after running thru the code */
  313. }
  314. }
Add Comment
Please, Sign In to add comment