Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. #include <string.h> // for strcpy()
  6. #include <conio.h> // for getche()
  7.  
  8. #include <windows.h> // for COLOR!
  9.  
  10. #define NUM_ROWS 3 // should not be changed for this solution
  11. #define NUM_COLS 3 // should not be changed for this soultion
  12.  
  13. #define PIVOT -1 // used to mark the pivot spot (blank area) on the puzzle
  14. #define PIVOT_SYMBOL "*" // used to show the pivot location when drawing the board
  15.  
  16. // direction codes (part of the slideTile() interface)
  17. #define SLIDE_UP 1 // pass to slideTile() to trigger UP movement
  18. #define SLIDE_DOWN 2 // pass to slideTile() to trigger DOWN movement
  19. #define SLIDE_LEFT 3 // pass to slideTile() to trigger LEFT movement
  20. #define SLIDE_RIGHT 4 // pass to slideTile() to trigger RIGHT movement
  21.  
  22. #define UNSET -1 // used to arbitrarily indicate an undetermined state in a constuct
  23.  
  24. #define COLOR_DEFAULT 7
  25. #define COLOR_RED 12
  26. #define COLOR_GREEN 10
  27.  
  28.  
  29. // PROTOTYPES
  30. void InitializeBoard(int[NUM_ROWS][NUM_COLS]);
  31. void PrintBoard(int[NUM_ROWS][NUM_COLS]);
  32. bool slideTile(int[NUM_ROWS][NUM_COLS], int);
  33. void scrambleBoard(int[NUM_ROWS][NUM_COLS]); // depends upon slideTile()
  34. bool isBoardSolved(int[NUM_ROWS][NUM_COLS]); // indicates if the board is in the SOLVED state
  35.  
  36. // DEVELOPMENT EXTRAS
  37. //void printTheRainbow(); // A little reminder on how to do color with the Windows API.
  38.  
  39.  
  40. int main() {
  41. // Declarations
  42. int slidingBoard[NUM_ROWS][NUM_COLS]; // the board that holds the sliding tiles
  43. char keyStroke = ' '; // holds the user's keystrokes as they come in
  44. int directionCode = UNSET; // used to build a direction code to be sent to slideTile()
  45. InitializeBoard(slidingBoard);
  46. PrintBoard(slidingBoard);
  47. //scrambleBoard(slidingBoard);
  48.  
  49. while (!isBoardSolved(slidingBoard))
  50. {
  51. keyStroke = _getch();
  52.  
  53. if (keyStroke == 'w')
  54. directionCode = SLIDE_UP;
  55. else if (keyStroke == 's')
  56. directionCode = SLIDE_DOWN;
  57. else if (keyStroke == 'a')
  58. directionCode = SLIDE_LEFT;
  59. else if (keyStroke == 'd')
  60. directionCode = SLIDE_RIGHT;
  61. else
  62. directionCode = UNSET;
  63.  
  64. if (slideTile(slidingBoard, directionCode))
  65. {
  66. PrintBoard(slidingBoard);
  67. }
  68. }
  69.  
  70. _getch(); // Exit
  71. return 0;
  72. }
  73.  
  74. void InitializeBoard(int theBoard[NUM_ROWS][NUM_COLS])
  75. {
  76. int counter = 1;
  77.  
  78. for (int i = 0; i < NUM_ROWS; i++)
  79. {
  80. for (int j = 0; j < NUM_COLS; j++)
  81. {
  82. if (i == (NUM_ROWS - 1) && j == (NUM_COLS - 1))
  83. {
  84. theBoard[i][j] = PIVOT;
  85. }
  86. else
  87. {
  88. theBoard[i][j] = counter;
  89. counter++;
  90. }
  91. }
  92. }
  93. }
  94.  
  95. void PrintBoard(int theBoard[NUM_ROWS][NUM_COLS])
  96. {
  97. system("CLS");
  98. HANDLE hConsole;
  99. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  100.  
  101. for (int i = 0; i < NUM_ROWS; i++)
  102. {
  103. for (int j = 0; j < NUM_COLS; j++)
  104. {
  105. if (theBoard[i][j] == PIVOT)
  106. {
  107. cout << setw(3) << PIVOT_SYMBOL;
  108. }
  109. else
  110. {
  111. if (isBoardSolved(theBoard))
  112. {
  113. SetConsoleTextAttribute(hConsole, COLOR_GREEN);
  114. }
  115.  
  116. cout << setw(3) << theBoard[i][j];
  117. SetConsoleTextAttribute(hConsole, COLOR_DEFAULT);
  118. }
  119. }
  120. cout << endl;
  121. }
  122.  
  123. cout << "\nisBoardSolved(): ";
  124.  
  125. if (isBoardSolved(theBoard))
  126. {
  127. cout << "true" << endl;
  128. }
  129. else
  130. {
  131. cout << "false" << endl;
  132. }
  133.  
  134. cout << "\n| w = UP | s = DOWN | a = LEFT | d = RIGHT |" << endl;
  135. cout << "Which way to slide:";
  136. }
  137.  
  138. bool slideTile(int theBoard[NUM_ROWS][NUM_COLS], int slideDirection)
  139. {
  140. for (int i = 0; i < NUM_ROWS; i++)
  141. {
  142. for (int j = 0; j < NUM_COLS; j++)
  143. {
  144. if (theBoard[i][j] == PIVOT)
  145. {
  146. if (slideDirection == SLIDE_UP)
  147. {
  148. if (i == NUM_ROWS - 1)
  149. return false;
  150. else
  151. {
  152. theBoard[i][j] = theBoard[i + 1][j];
  153. theBoard[i + 1][j] = -1;
  154. return true;
  155. }
  156. }
  157.  
  158. else if (slideDirection == SLIDE_DOWN)
  159. {
  160. if (i == 0)
  161. return false;
  162. else
  163. {
  164. theBoard[i][j] = theBoard[i - 1][j];
  165. theBoard[i - 1][j] = -1;
  166. return true;
  167. }
  168. }
  169.  
  170. else if (slideDirection == SLIDE_LEFT)
  171. {
  172. if (j == NUM_COLS - 1)
  173. return false;
  174. else
  175. {
  176. theBoard[i][j] = theBoard[i][j + 1];
  177. theBoard[i][j + 1]= -1;
  178. return true;
  179. }
  180. }
  181.  
  182. else if (slideDirection == SLIDE_RIGHT)
  183. {
  184. if (j == 0)
  185. return false;
  186. else
  187. {
  188. theBoard[i][j] = theBoard[i][j - 1];
  189. theBoard[i][j - 1] = -1;
  190. return true;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197.  
  198. void scrambleBoard(int theBoard[NUM_ROWS][NUM_COLS])
  199. {
  200.  
  201. }
  202.  
  203. bool isBoardSolved(int amISolved[NUM_ROWS][NUM_COLS])
  204. {
  205. int solvedBoard[3][3] = { { 1, 2, 3 },
  206. { 4, 5, 6 },
  207. { 7, 8, PIVOT } };
  208.  
  209. for (int i = 0; i < NUM_ROWS; i++)
  210. {
  211. for (int j = 0; j < NUM_COLS; j++)
  212. {
  213. if (amISolved[i][j] != solvedBoard[i][j])
  214. {
  215. return false;
  216. }
  217. }
  218. }
  219. return true;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement