Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 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. int board[9][9];
  35. int row=0;
  36. int col=0;
  37.  
  38.  
  39. // prototypes
  40. void clear(void);
  41. void greet(void);
  42. void init(void);
  43. void draw(void);
  44. bool move(int tile);
  45. bool won(void);
  46.  
  47. int main(int argc, string argv[])
  48. {
  49.  
  50.  
  51.  
  52. // ensure proper usage
  53. if (argc != 2)
  54. {
  55. printf("Usage: fifteen d\n");
  56. return 1;
  57. }
  58.  
  59. // ensure valid dimensions
  60. d = atoi(argv[1]);
  61. if (d < DIM_MIN || d > DIM_MAX)
  62. {
  63. printf("Board must be between %i x %i and %i x %i, inclusive.\n",
  64. DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
  65. return 2;
  66. }
  67.  
  68. // open log
  69. FILE* file = fopen("log.txt", "w");
  70. if (file == NULL)
  71. {
  72. return 3;
  73. }
  74.  
  75. // greet user with instructions
  76. greet();
  77.  
  78. // initialize the board
  79. init();
  80.  
  81. // accept moves until game is won
  82. while (true)
  83. {
  84. // clear the screen
  85. clear();
  86.  
  87. // draw the current state of the board
  88. draw();
  89.  
  90. // log the current state of the board (for testing)
  91. for (int i = 0; i < d; i++)
  92. {
  93. for (int j = 0; j < d; j++)
  94. {
  95. fprintf(file, "%i", board[i][j]);
  96. if (j < d - 1)
  97. {
  98. fprintf(file, "|");
  99. }
  100. }
  101. fprintf(file, "\n");
  102. }
  103. fflush(file);
  104.  
  105. // check for win
  106. if (won())
  107. {
  108. printf("ftw!\n");
  109. break;
  110. }
  111.  
  112. // prompt for move
  113. printf("Tile to move: ");
  114. int tile = GetInt();
  115.  
  116. // quit if user inputs 0 (for testing)
  117. if (tile == 0)
  118. {
  119. break;
  120. }
  121.  
  122. // log move (for testing)
  123. fprintf(file, "%i\n", tile);
  124. fflush(file);
  125.  
  126. // move if possible, else report illegality
  127. if (!move(tile))
  128. {
  129. printf("\nIllegal move.\n");
  130. usleep(500000);
  131. }
  132.  
  133. // sleep thread for animation's sake
  134. usleep(500000);
  135. }
  136.  
  137. // close log
  138. fclose(file);
  139.  
  140. // success
  141. return 0;
  142. }
  143.  
  144. /**
  145. * Clears screen using ANSI escape sequences.
  146. */
  147. void clear(void)
  148. {
  149. printf("\033[2J");
  150. printf("\033[%d;%dH", 0, 0);
  151. }
  152.  
  153.  
  154.  
  155. /**
  156. * Greets player.
  157. */
  158. void greet(void)
  159. {
  160. clear();
  161. printf("WELCOME TO GAME OF FIFTEEN\n");
  162. usleep(2000000);
  163. }
  164.  
  165. /**
  166. * Initializes the game's board with tiles numbered 1 through d*d - 1
  167. * (i.e., fills 2D array with values but does not actually print them).
  168. */
  169. void init(void)
  170. {
  171. row=d-1;
  172. col=d-1;
  173. int key=1;
  174. for(int i=0;i<d;i++)
  175. {
  176.  
  177. for(int j=0;j<d;j++)
  178. {
  179.  
  180. board[i][j]=(d*d)-key;
  181. key++;
  182.  
  183. }
  184. }
  185. if(d%2==0)
  186. {
  187. int temp=0;
  188. temp=board[d-1][d-2];
  189. board[d-1][d-2]=board[d-1][d-3];
  190. board[d-1][d-3]=temp;
  191.  
  192. }
  193. }
  194.  
  195. /**
  196. * Prints the board in its current state.
  197. */
  198. void draw(void)
  199. {
  200.  
  201.  
  202. for(int i=0;i<d;i++)
  203. {
  204.  
  205. for(int j=0;j<d;j++)
  206. {
  207.  
  208. printf("%d ",board[i][j]);
  209.  
  210. }
  211. printf("\n");
  212.  
  213.  
  214. }
  215.  
  216.  
  217. }
  218.  
  219. /**
  220. * If tile borders empty space, moves tile and returns true, else
  221. * returns false.
  222. */
  223. bool move(int tile)
  224. {
  225. for(int i=0;i<d;i++)
  226. {
  227. for(int j=0;j<d;j++)
  228. {
  229. if(board[row][col]==board[i][j])
  230. {
  231. if (tile==board[row][col+1] )
  232. {
  233.  
  234. board[row][col+1]=board[row][col];
  235. board[row][col]=tile;
  236.  
  237. col=col+1;
  238. return true;
  239. }
  240. else if(tile==board[row+1][col])
  241. {
  242.  
  243. board[row+1][col]=board[row][col];
  244. board[row][col]=tile;
  245. row=row+1;
  246.  
  247. return true;
  248. }
  249. else if (tile==board[row][col-1])
  250. {
  251.  
  252. board[row][col-1]=board[row][col];
  253. board[row][col]=tile;
  254.  
  255. col=col-1;
  256. return true;
  257. }
  258. else if (tile==board[row-1][col] )
  259. {
  260.  
  261. board[row-1][col]=board[row][col];
  262. board[row][col]=tile;
  263. row=row-1;
  264.  
  265. return true;
  266. }
  267. else
  268. {
  269. return false;
  270. }
  271. }
  272.  
  273. }
  274. }
  275.  
  276.  
  277. return 0;
  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. int array22[d][d];
  287.  
  288. int key=(d*d)-1;
  289. for (int i=0;i<d;i++)
  290. {
  291. for(int j=0;j<d;j++)
  292. { if(i==d-1 && j==d-1)
  293. {
  294. array22[i][j]=d-d;
  295.  
  296. }
  297. else
  298. {
  299. array22[i][j]=(d*d)-key;
  300. key--;
  301.  
  302. }
  303. }
  304. printf("\n");
  305. }
  306. for(int i=0;i<d;i++)
  307. {
  308. for(int j=0;j<d;j++)
  309. {
  310. if (array22[i][j]==board[i][j])
  311. {
  312. return true;
  313. }
  314. else
  315. return false;
  316. }
  317. }
  318. return 0;
  319.  
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement