ketan18710

Untitled

Oct 21st, 2016
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. { int counter = d*d-1;
  162. // TODO
  163. for(int i=0;i<d;i++)
  164. {
  165. for(int j=0;j<d;j++)
  166. { if(counter>=0)
  167. {
  168. board[i][j] = counter;
  169. if(d%2 ==0)
  170. {
  171. if(counter==2)
  172. {
  173. board[i][j+1] = 2;
  174. board[i][j] = 1;
  175. }
  176. }
  177. counter--;
  178. }
  179.  
  180. }
  181. }
  182. }
  183.  
  184. /**
  185. * Prints the board in its current state.
  186. */
  187. void draw(void)
  188. {
  189. // TODo
  190. for(int i=0;i<d;i++)
  191. {
  192. for(int j=0;j<d;j++)
  193. { if(board[i][j]>0)
  194. {
  195. printf("%d",board[i][j]);
  196. }
  197. else if (board[i][j]==0)
  198. {
  199. printf("_");
  200. }
  201.  
  202. }
  203. }
  204. }
  205.  
  206. /**
  207. * If tile borders empty space, moves tile and returns true, else
  208. * returns false.
  209. */
  210. bool move(int tile)
  211. {
  212. // TODO
  213. int i,j,a,b,swap;
  214. for( i=0;i<d;i++)
  215. {
  216. for( j=0;j<d;j++)
  217. { if(board[i][j]==0)
  218. {
  219. a=i;
  220. b=j;
  221. }
  222. }
  223. }
  224. for( i=0;i<d;i++)
  225. {
  226. for( j=0;j<d;j++)
  227. {
  228. if(board[i][j]==tile)
  229. {
  230. if((i==a||i==(a-1)||i==(a+1))&&(j==b||j==(b-1)||j==(b+1)))
  231. {
  232. if(i>=0&&j>=0&&i<d&&j<d)
  233. {
  234. swap=board[a][b];
  235. board[a][b]=board[i][j];
  236. board[i][j]=swap;
  237. return true;
  238. }
  239. else
  240. return false;
  241. }
  242. }
  243. }
  244. }
  245. return false;
  246. }
  247.  
  248. /**
  249. * Returns true if game is won (i.e., board is in winning configuration),
  250. * else false.
  251. */
  252. bool won(void)
  253. {
  254. // TODO
  255. int i,j,counter=1,a[d][d],flag=0;
  256. for(i=0;i<d;i++)
  257. {
  258. for(j=0;j<d;j++)
  259. {
  260. a[i][j]=counter;
  261. counter++;
  262. }
  263. }
  264. a[d-1][d-1]=0;
  265. for(i=0;i<d;i++)
  266. {
  267. for(j=0;j<d;j++)
  268. {
  269. if(board[i][j]==a[i][j])
  270. {
  271. flag=0;
  272. }
  273. else if(board[i][j]!=a[i][j])
  274. {
  275. flag=1;
  276. }
  277. }
  278. }
  279. if(flag==0)
  280. {return true;}
  281.  
  282. return false;
  283. }
RAW Paste Data