Guest User

CS50_Fiftenn_Hardik

a guest
Aug 20th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. void init(void)
  2. {
  3. int val = d*d - 1;
  4. int c;
  5.  
  6. for(int i = 0; i < d; i++)
  7. {
  8. for(int j = 0; j < d; j++)
  9. {
  10. board[i][j] = val;
  11. val--;
  12. }
  13.  
  14. }
  15. if(d % 2 == 0)
  16. {
  17. c = board[d - 1][d - 2];
  18. board[d - 1][d - 2] = board[d - 1][d - 3];
  19. board[d - 1][d - 3] = c;
  20. }
  21. }
  22.  
  23. /**
  24. * Prints the board in its current state.
  25. */
  26. void draw(void)
  27. {
  28. for(int i = 0; i < d; i++)
  29. {
  30. for(int j = 0; j < d; j++)
  31. {
  32. if(board[i][j] == 0)
  33. {
  34. printf(" _ ");
  35. }
  36. else
  37. {
  38. printf("%2i ", board[i][j]);
  39. }
  40. }
  41. printf("\n");
  42. }
  43. }
  44.  
  45. /**
  46. * If tile borders empty space, moves tile and returns true, else
  47. * returns false.
  48. */
  49. bool move(int tile)
  50. {
  51. int a,b,c;
  52.  
  53. for(int i = 0; i < d; i++)
  54. {
  55. for(int j = 0; j < d; j++)
  56. {
  57. if(board[i][j] == 0)
  58. {
  59. a = i;
  60. b = j;
  61. }
  62. }
  63. }
  64.  
  65.  
  66. for(int i = 0; i < d; i++)
  67. {
  68. for(int j = 0; j < d; j++)
  69. {
  70. if(board[i][j] == tile)
  71. {
  72. if(abs(i - a) + abs(j - b) == 1)
  73. {
  74. c = board[i][j];
  75. board[i][j] = board[a][b];
  76. board[a][b] = c;
  77. return true;
  78. }
  79. }
  80. }
  81. }
  82. return false;
  83. }
  84.  
  85. /**
  86. * Returns true if game is won (i.e., board is in winning configuration),
  87. * else false.
  88. */
  89. bool won(void)
  90. {
  91. for (int i = 0; i < d; i++)
  92. {
  93. for(int j = 0; j < d; j++)
  94. {
  95. if(!board[i][j] != (j + 1) + (i * d) || board[i][j] != 0 )
  96. {
  97. return false;
  98. }
  99.  
  100. }
  101. }
  102.  
  103. return true;
  104. }
Add Comment
Please, Sign In to add comment