TheLegend12

Untitled

Oct 9th, 2023
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. const int ROWS = 6;
  7. const int COLS = 7;
  8.  
  9. void displayBoard(char boardArray[][COLS]) {
  10. cout << "--------" << endl;
  11. for (int i = 0; i < ROWS; ++i) {
  12. cout << "|";
  13. for (int j = 0; j < COLS; ++j) {
  14. cout << boardArray[i][j];
  15. }
  16. cout << "|" << endl;
  17. }
  18. cout << "--------" << endl;
  19. }
  20.  
  21. void shiftCar(char boardArray[][COLS], int row, int col, char direction) {
  22. if (direction == 'L') {
  23. while (col - 1 >= 0 && boardArray[row][col - 1] == '.') {
  24. boardArray[row][col] = '.';
  25. boardArray[row][col - 1] = 'X'; // Symbolize a temporarily shifted car
  26. --col;
  27. }
  28. } else if (direction == 'R') {
  29. while (col + 1 < COLS && boardArray[row][col + 1] == '.') {
  30. boardArray[row][col] = '.';
  31. boardArray[row][col + 1] = 'X'; // Symbolize a temporarily shifted car
  32. ++col;
  33. }
  34. } else if (direction == 'U') {
  35. while (row - 1 >= 0 && boardArray[row - 1][col] == '.') {
  36. boardArray[row][col] = '.';
  37. boardArray[row - 1][col] = 'X'; // Symbolize a temporarily shifted car
  38. --row;
  39. }
  40. } else if (direction == 'D') {
  41. while (row + 1 < ROWS && boardArray[row + 1][col] == '.') {
  42. boardArray[row][col] = '.';
  43. boardArray[row + 1][col] = 'X'; // Symbolize a temporarily shifted car
  44. ++row;
  45. }
  46. }
  47. }
  48.  
  49. void moveLeft(char boardArray[][COLS], char carToMove, int numSpaces) {
  50. for (int i = 0; i < ROWS; ++i) {
  51. for (int j = 1; j < COLS; ++j) {
  52. if (boardArray[i][j] == carToMove) {
  53. for (int k = 0; k < numSpaces; ++k) {
  54. if (j - k - 1 >= 0) {
  55. if (boardArray[i][j - k - 1] == '.') {
  56. boardArray[i][j - k] = '.';
  57. boardArray[i][j - k - 1] = carToMove;
  58. } else {
  59. // Shift the blocking car
  60. shiftCar(boardArray, i, j - k - 1, 'L');
  61. boardArray[i][j - k] = '.';
  62. boardArray[i][j - k - 1] = carToMove;
  63. }
  64. } else {
  65. cout << "Invalid move. Car would go out of bounds." << endl;
  66. return;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. void moveRight(char boardArray[][COLS], char carToMove, int numSpaces) {
  75. for (int i = 0; i < ROWS; ++i) {
  76. for (int j = COLS - 2; j >= 0; --j) {
  77. if (boardArray[i][j] == carToMove) {
  78. for (int k = 0; k < numSpaces; ++k) {
  79. if (j + k + 1 < COLS) {
  80. if (boardArray[i][j + k + 1] == '.') {
  81. boardArray[i][j + k] = '.';
  82. boardArray[i][j + k + 1] = carToMove;
  83. } else {
  84. // Shift the blocking car
  85. shiftCar(boardArray, i, j + k + 1, 'R');
  86. boardArray[i][j + k] = '.';
  87. boardArray[i][j + k + 1] = carToMove;
  88. }
  89. } else {
  90. cout << "Invalid move. Car would go out of bounds." << endl;
  91. return;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98.  
  99. void moveUp(char boardArray[][COLS], char carToMove, int numSpaces) {
  100. for (int j = 0; j < COLS; ++j) {
  101. for (int i = 1; i < ROWS; ++i) {
  102. if (boardArray[i][j] == carToMove) {
  103. for (int k = 0; k < numSpaces; ++k) {
  104. if (i - k - 1 >= 0) {
  105. if (boardArray[i - k - 1][j] == '.') {
  106. boardArray[i - k][j] = '.';
  107. boardArray[i - k - 1][j] = carToMove;
  108. } else {
  109. // Shift the blocking car
  110. shiftCar(boardArray, i - k - 1, j, 'U');
  111. boardArray[i - k][j] = '.';
  112. boardArray[i - k - 1][j] = carToMove;
  113. }
  114. } else {
  115. cout << "Invalid move. Car would go out of bounds." << endl;
  116. return;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123.  
  124.  
  125. void moveDown(char boardArray[][COLS], char carToMove, int numSpaces) {
  126. for (int j = 0; j < COLS; ++j) {
  127. for (int i = ROWS - 2; i >= 0; --i) {
  128. if (boardArray[i][j] == carToMove) {
  129. for (int k = 0; k < numSpaces; ++k) {
  130. if (i + k + 1 < ROWS) {
  131. if (boardArray[i + k + 1][j] == '.') {
  132. boardArray[i + k][j] = '.';
  133. boardArray[i + k + 1][j] = carToMove;
  134. } else {
  135. // Shift the blocking car
  136. shiftCar(boardArray, i + k + 1, j, 'D');
  137. boardArray[i + k][j] = '.';
  138. boardArray[i + k + 1][j] = carToMove;
  139. }
  140. } else {
  141. cout << "Invalid move. Car would go out of bounds." << endl;
  142. return;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. void moveAllCars(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
  151. if (moveDirection == 'L') {
  152. for (int i = 0; i < numSpaces; ++i) {
  153. moveLeft(boardArray, carToMove, 1); // Move left by 1 space each time
  154. }
  155. } else if (moveDirection == 'R') {
  156. for (int i = 0; i < numSpaces; ++i) {
  157. moveRight(boardArray, carToMove, 1); // Move right by 1 space each time
  158. }
  159. } else if (moveDirection == 'U') {
  160. for (int i = 0; i < numSpaces; ++i) {
  161. moveUp(boardArray, carToMove, 1); // Move up by 1 space each time
  162. }
  163. } else if (moveDirection == 'D') {
  164. for (int i = 0; i < numSpaces; ++i) {
  165. moveDown(boardArray, carToMove, 1); // Move down by 1 space each time
  166. }
  167. } else {
  168. cout << "Invalid move direction." << endl;
  169. return;
  170. }
  171. }
  172.  
  173. void movingCarsOnBoard(char boardArray[][COLS]) {
  174. cout << "Enter next move (or Q to quit): ";
  175. char carToMove;
  176. int numSpaces;
  177. char moveDirection;
  178. cin >> carToMove >> numSpaces >> moveDirection;
  179.  
  180. if (numSpaces <= 0) {
  181. cout << "Invalid number of spaces to move." << endl;
  182. return;
  183. }
  184.  
  185. // Move all instances of the specified car according to the specified direction
  186. moveAllCars(boardArray, carToMove, numSpaces, moveDirection);
  187.  
  188. // Display the board after the move
  189. displayBoard(boardArray);
  190.  
  191. // Check for win
  192. for (int i = 0; i < ROWS; ++i) {
  193. for (int j = 0; j < COLS; ++j) {
  194. if (boardArray[i][j] == '=') {
  195. cout << "Congratulations! You won!" << endl;
  196. return;
  197. }
  198. }
  199. }
  200. }
  201.  
  202. void readBoard(const string& fileName, char boardArray[][COLS]) {
  203. fstream newfile;
  204. newfile.open(fileName.c_str(), ios::in);
  205.  
  206. if (!newfile.is_open()) {
  207. cout << "Error opening file." << endl;
  208. return;
  209. }
  210.  
  211. string line;
  212. int row = 0;
  213.  
  214. // Read the board layout from the file
  215. while (getline(newfile, line) && row < ROWS) {
  216. // Ensure the line length is at most COLS characters
  217. line.resize(COLS, ' ');
  218.  
  219. for (int j = 0; j < COLS; ++j) {
  220. boardArray[row][j] = line[j];
  221. }
  222.  
  223. row++;
  224. }
  225.  
  226. newfile.close();
  227. }
  228.  
  229. int main() {
  230. string fileName;
  231.  
  232. cout << "Enter the file name: ";
  233. getline(cin, fileName);
  234.  
  235. char boardArray[ROWS][COLS];
  236.  
  237. readBoard(fileName, boardArray);
  238.  
  239. displayBoard(boardArray);
  240.  
  241. while (true) {
  242. movingCarsOnBoard(boardArray);
  243. }
  244.  
  245. return 0;
  246. }
  247.  
Add Comment
Please, Sign In to add comment