Advertisement
Guest User

Untitled

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