ketan18710

Untitled

Oct 22nd, 2016
82
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. board[d-1][d-2] = 2;
  172. board[d-1][d-3] = 1;
  173.  
  174. }
  175. counter--;
  176. }
  177.  
  178. }
  179. }
  180. }
  181.  
  182. /**
  183. * Prints the board in its current state.
  184. */
  185. void draw(void)
  186. {
  187. // TODo
  188. for(int i=0;i<d;i++)
  189. {
  190. for(int j=0;j<d;j++)
  191. { if(board[i][j]>0)
  192. {
  193. printf("%d",board[i][j]);
  194. }
  195. else if (board[i][j]==0)
  196. {
  197. printf("_");
  198. }
  199.  
  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. // TODO
  211. int i,j,a,b,swap;
  212. for( i=0;i<d;i++)
  213. {
  214. for( j=0;j<d;j++)
  215. { if(board[i][j]==0)
  216. {
  217. a=i;
  218. b=j;
  219. }
  220. }
  221. }
  222. for( i=0;i<d;i++)
  223. {
  224. for( j=0;j<d;j++)
  225. {
  226. if(board[i][j]==tile)
  227. { if(i==a&&j==b)
  228. return false;
  229. if((i==a&&j==(b-1))||(i==a&&j==(b+1))||(i==a+1&&j==b)||(i==(a-1)&&j==b))
  230. {
  231. if(i>=0&&j>=0&&i<d&&j<d)
  232. {
  233. swap=board[a][b];
  234. board[a][b]=board[i][j];
  235. board[i][j]=swap;
  236. return true;
  237. }
  238. else
  239. return false;
  240. }
  241. }
  242. }
  243. }
  244. return false;
  245. }
  246.  
  247. /**
  248. * Returns true if game is won (i.e., board is in winning configuration),
  249. * else false.
  250. */
  251. bool won(void)
  252. {
  253. // TODO
  254. int i,j,counter=1,a[d][d],flag=0;
  255. for(i=0;i<d;i++)
  256. {
  257. for(j=0;j<d;j++)
  258. {
  259. a[i][j]=counter;
  260. counter++;
  261. }
  262. }
  263. a[d-1][d-1]=0;
  264. for(i=0;i<d;i++)
  265. {
  266. for(j=0;j<d;j++)
  267. {
  268. if(board[i][j]==a[i][j])
  269. {
  270. flag=0;
  271. }
  272. else if(board[i][j]!=a[i][j])
  273. {
  274. flag=1;
  275. }
  276. }
  277. }
  278. if(flag==0)
  279. {return true;}
  280.  
  281. return false;
  282. }
RAW Paste Data