rahulb5

Untitled

Jun 7th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. /**
  2. * fifteen.c
  3. *
  4. * Computer Science 50
  5. * Problem Set 3
  6. *
  7. * Implements the 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 [MIN,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. // board's minimal dimension
  26. #define MIN 3
  27.  
  28. // board's maximal dimension
  29. #define MAX 9
  30. #define BLANK 2422
  31. // board, whereby board[i][j] represents row i and column j
  32. int board[MAX][MAX];
  33.  
  34. // board's dimension
  35. int d;
  36.  
  37. int space = 0 ;
  38. int g = 0 ;
  39. int h = 0 ;
  40. // prototypes
  41. void clear(void);
  42. void greet(void);
  43. void init(void);
  44. void draw(void);
  45. bool move(int tile);
  46. bool won(void);
  47. void save(void);
  48.  
  49. int main(int argc, string argv[])
  50. {
  51. // greet player
  52. greet();
  53.  
  54. // ensure proper usage
  55. if (argc != 2)
  56. {
  57. printf("Usage: ./fifteen d\n");
  58. return 1;
  59. }
  60.  
  61. // ensure valid dimensions
  62. d = atoi(argv[1]);
  63. if (d < MIN || d > MAX)
  64. {
  65. printf("Board must be between %i x %i and %i x %i, inclusive.\n",
  66. MIN, MIN, MAX, MAX);
  67. return 2;
  68. }
  69.  
  70. // initialize the board
  71. init();
  72.  
  73. // accept moves until game is won
  74. while (true)
  75. {
  76. // clear the screen
  77. clear();
  78.  
  79. // draw the current state of the board
  80. draw();
  81.  
  82.  
  83.  
  84. // saves the current state of the board (for testing)
  85. save();
  86.  
  87. // check for win
  88. if (won())
  89. {
  90. printf("ftw!\n");
  91. break;
  92. }
  93.  
  94. // prompt for move
  95. printf("Tile to move: ");
  96. int tile = GetInt();
  97.  
  98. // move if possible, else report illegality
  99. if (!move(tile))
  100. {
  101. printf("\nIllegal move.\n");
  102. usleep(500000);
  103. }
  104.  
  105. // sleep for animation's sake
  106. usleep(500000);
  107. }
  108.  
  109. // that's all folks
  110. return 0;
  111. }
  112.  
  113. /**
  114. * Clears screen using ANSI escape sequences.
  115. */
  116. void clear(void)
  117. {
  118. printf("\033[2J");
  119. printf("\033[%d;%dH", 0, 0);
  120. }
  121.  
  122. /**
  123. * Greets player.
  124. */
  125. void greet(void)
  126. {
  127. clear();
  128. printf("GAME OF FIFTEEN\n");
  129. usleep(2000000);
  130. }
  131.  
  132. /**
  133. * Initializes the game's board with tiles numbered 1 through d*d - 1,
  134. * (i.e., fills board with values but does not actually print them),
  135. * whereby board[i][j] represents row i and column j.
  136. */
  137.  
  138. void init(void)
  139. {
  140. bool isOdd = false;
  141. // TODO
  142. int k = d*d - 1 ;
  143. for(g = 0 ; g < d ; g++)
  144. {
  145. for(h = 0 ; h < d ; h++)
  146. {
  147. board[g][h] = k ;
  148. k-- ;
  149. }
  150. }
  151. if(k % 2 == 1 )
  152. isOdd = true ;
  153. if(isOdd == true )
  154. {
  155. board[d-1][d] = board[d-2][d];
  156. board[d-2][d] = board[d-1][d];
  157. }
  158. }
  159.  
  160.  
  161. /**
  162. * Prints the board in its current state.
  163. */
  164. void draw(void)
  165. {
  166. // TODO
  167. for ( g = 0 ; g < d ; g++ )
  168. {
  169. for ( h = 0 ; h < d ; h++ )
  170. {
  171. printf (" ");
  172.  
  173. if(board[g][h] != 0 )
  174. printf("%2d",board[g][h]);
  175. else{ printf(" ");}
  176. }
  177. printf("\n");
  178. }
  179. }
  180.  
  181. /**
  182. * If tile borders empty space, moves tile and returns true, else
  183. * returns false.
  184. */
  185. int empty = 32 ;
  186. bool move (int tile)
  187. {
  188. for (int i = 0 ; i < d ; i++ )
  189. {
  190. for (int j = 0 ; j < d ; j++ )
  191. {
  192. if(board[i][j] == tile )
  193. {
  194. if (board[i][j-1] == empty)
  195. {
  196. empty = board[i][j];
  197. board[i][j] = board[i][j-1];
  198. board[i][j-1] = empty;
  199. return true;
  200. }
  201. else if (board[i][j+1] == empty)
  202. {
  203. empty = board[i][j];
  204. board[i][j] = board[i][j+1];
  205. board[i][j+1] = empty;
  206. return true;
  207. }
  208. else if (board[i-1][j] == empty)
  209. {
  210. empty = board[i][j];
  211. board[i][j] = board[i-1][j];
  212. board[i-1][j] = empty;
  213. return true;
  214. }
  215. else if (board[i+1][j] == empty)
  216. {
  217. empty = board[i][j];
  218. board[i][j] = board[i+1][j];
  219. board[i+1][j] = empty;
  220. return true;
  221. }
  222. else
  223. return false;
  224. }
  225. }
  226. }
  227.  
  228. }
  229.  
  230. /**
  231. * Returns true if game is won (i.e., board is in winning configuration),
  232. * else false.
  233. */
  234. bool won(void)
  235. {
  236. // TODO
  237. return false;
  238. }
  239.  
  240. /**
  241. * Saves the current state of the board to disk (for testing).
  242. */
  243. void save(void)
  244. {
  245. // log
  246. const string log = "log.txt";
  247.  
  248. // delete existing log, if any, before first save
  249. static bool saved = false;
  250. if (!saved)
  251. {
  252. unlink(log);
  253. saved = true;
  254. }
  255.  
  256. // open log
  257. FILE* p = fopen(log, "a");
  258. if (p == NULL)
  259. {
  260. return;
  261. }
  262.  
  263. // log board
  264. fprintf(p, "{");
  265. for (int i = 0; i < d; i++)
  266. {
  267. fprintf(p, "{");
  268. for (int j = 0; j < d; j++)
  269. {
  270. fprintf(p, "%i", board[i][j]);
  271. if (j < d - 1)
  272. {
  273. fprintf(p, ",");
  274. }
  275. }
  276. fprintf(p, "}");
  277. if (i < d - 1)
  278. {
  279. fprintf(p, ",");
  280. }
  281. }
  282. fprintf(p, "}\n");
  283.  
  284. // close log
  285. fclose(p);
  286. }
Advertisement
Add Comment
Please, Sign In to add comment