Advertisement
TheLegend12

Untitled

Oct 10th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.19 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 = 6;
  8. int countNumberOfDots(char boardArray[][COLS], char moveDirection, int row, int column) {
  9. int countDots = 0;
  10.  
  11. if (moveDirection == 'L') {
  12. for (int i = column - 1; i >= 0; --i) {
  13. if (boardArray[row][i] == '.') {
  14. countDots++;
  15. } else {
  16. break;
  17. }
  18. }
  19. } else if (moveDirection == 'R') {
  20. for (int i = column + 1; i < COLS; ++i) {
  21. if (boardArray[row][i] == '.') {
  22. countDots++;
  23. } else {
  24. break;
  25. }
  26. }
  27. } else if (moveDirection == 'U') {
  28. for (int i = row - 1; i >= 0; --i) {
  29. if (boardArray[i][column] == '.') {
  30. countDots++;
  31. } else {
  32. break;
  33. }
  34. }
  35. } else if (moveDirection == 'D') {
  36. for (int i = row + 1; i < ROWS; ++i) {
  37. if (boardArray[i][column] == '.') {
  38. countDots++;
  39. } else {
  40. break;
  41. }
  42. }
  43. }
  44.  
  45. return countDots;
  46. }
  47.  
  48. int findMin(int a, int b) {
  49. return (a < b) ? a : b;
  50. }
  51.  
  52. void displayBoard(char boardArray[][COLS]) {
  53. cout << endl;
  54. cout << "--------" << endl;
  55. for (int i = 0; i < ROWS; ++i) {
  56. cout << "|";
  57. for (int j = 0; j < COLS; ++j) {
  58. if(boardArray[i][j] == '-'){
  59. boardArray[i][j] = '.';
  60. }
  61. cout << boardArray[i][j];
  62. }
  63.  
  64. if (i == 2) {
  65. cout << "=" << endl;
  66. } else {
  67. cout << "|" << endl;
  68. }
  69. }
  70. cout << "--------" << endl;
  71. }
  72.  
  73.  
  74. void shiftCar(char boardArray[][COLS], int row, int col, char direction) {
  75. if (direction == 'L') {
  76. while (col - 1 >= 0 && boardArray[row][col - 1] == '.') {
  77. boardArray[row][col] = '.';
  78. boardArray[row][col - 1] = 'X'; // Symbolize a temporarily shifted car
  79. --col;
  80. }
  81. } else if (direction == 'R') {
  82. while (col + 1 < COLS && boardArray[row][col + 1] == '.') {
  83. boardArray[row][col] = '.';
  84. boardArray[row][col + 1] = 'X'; // Symbolize a temporarily shifted car
  85. ++col;
  86. }
  87. } else if (direction == 'U') {
  88. while (row - 1 >= 0 && boardArray[row - 1][col] == '.') {
  89. boardArray[row][col] = '.';
  90. boardArray[row - 1][col] = 'X'; // Symbolize a temporarily shifted car
  91. --row;
  92. }
  93. } else if (direction == 'D') {
  94. while (row + 1 < ROWS && boardArray[row + 1][col] == '.') {
  95. boardArray[row][col] = '.';
  96. boardArray[row + 1][col] = 'X'; // Symbolize a temporarily shifted car
  97. ++row;
  98. }
  99. }
  100. }
  101.  
  102. void moveLeft(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
  103. for (int i = 0; i < ROWS; ++i) {
  104. for (int j = 1; j < COLS; ++j) {
  105. if (boardArray[i][j] == carToMove) {
  106. int dots = countNumberOfDots(boardArray, moveDirection, i, j);
  107. int maxSpaces = findMin(numSpaces, dots);
  108.  
  109. for (int k = 0; k < maxSpaces; ++k) {
  110. if (j - k - 1 >= 0) {
  111. if (boardArray[i][j - k - 1] == '.') {
  112. boardArray[i][j - k] = '.';
  113. boardArray[i][j - k - 1] = carToMove;
  114. } else {
  115. // Shift the blocking car
  116. shiftCar(boardArray, i, j - k - 1, 'L');
  117. boardArray[i][j - k] = '.';
  118. boardArray[i][j - k - 1] = carToMove;
  119. }
  120. } else {
  121. cout << "Invalid move. Car would go out of bounds." << endl;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128.  
  129. void moveRight(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
  130. for (int i = 0; i < ROWS; ++i) {
  131. for (int j = COLS - 2; j >= 0; --j) {
  132. if (boardArray[i][j] == carToMove) {
  133. int dots = countNumberOfDots(boardArray, moveDirection, i, j);
  134. int maxSpaces = findMin(numSpaces, dots);
  135.  
  136. for (int k = 0; k < maxSpaces; ++k) {
  137. if (j + k + 1 < COLS) {
  138. if (boardArray[i][j + k + 1] == '.') {
  139. boardArray[i][j + k] = '.';
  140. boardArray[i][j + k + 1] = carToMove;
  141. } else {
  142. // Shift the blocking car
  143. shiftCar(boardArray, i, j + k + 1, 'R');
  144. boardArray[i][j + k] = '.';
  145. boardArray[i][j + k + 1] = carToMove;
  146. }
  147. } else {
  148. cout << "Invalid move. Car would go out of bounds." << endl;
  149. return;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156.  
  157. void moveUp(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
  158. for (int j = 0; j < COLS; ++j) {
  159. for (int i = 1; i < ROWS; ++i) {
  160. if (boardArray[i][j] == carToMove) {
  161. int dots = countNumberOfDots(boardArray, moveDirection, i, j);
  162. int maxSpaces = findMin(numSpaces, dots);
  163.  
  164. for (int k = 0; k < maxSpaces; ++k) {
  165. if (i - k - 1 >= 0) {
  166. if (boardArray[i - k - 1][j] == '.') {
  167. boardArray[i - k][j] = '.';
  168. boardArray[i - k - 1][j] = carToMove;
  169. } else {
  170. // Shift the blocking car
  171. shiftCar(boardArray, i - k - 1, j, 'U');
  172. boardArray[i - k][j] = '.';
  173. boardArray[i - k - 1][j] = carToMove;
  174. }
  175. } else {
  176. cout << "Invalid move. Car would go out of bounds." << endl;
  177. return;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184.  
  185. void moveDown(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
  186. for (int j = 0; j < COLS; ++j) {
  187. for (int i = ROWS - 2; i >= 0; --i) {
  188. if (boardArray[i][j] == carToMove) {
  189. int dots = countNumberOfDots(boardArray, moveDirection, i, j);
  190. int maxSpaces = findMin(numSpaces, dots);
  191.  
  192. for (int k = 0; k < maxSpaces; ++k) {
  193. if (i + k + 1 < ROWS) {
  194. if (boardArray[i + k + 1][j] == '.') {
  195. boardArray[i + k][j] = '.';
  196. boardArray[i + k + 1][j] = carToMove;
  197. } else {
  198. // Shift the blocking car
  199. shiftCar(boardArray, i + k + 1, j, 'D');
  200. boardArray[i + k][j] = '.';
  201. boardArray[i + k + 1][j] = carToMove;
  202. }
  203. } else {
  204. cout << "Invalid move. Car would go out of bounds." << endl;
  205. return;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212.  
  213. void moveAllCars(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
  214. if (moveDirection == 'L') {
  215. for (int i = 0; i < numSpaces; ++i) {
  216. moveLeft(boardArray, carToMove, 1, moveDirection); // Move left by 1 space each time
  217. }
  218. } else if (moveDirection == 'R') {
  219. for (int i = 0; i < numSpaces; ++i) {
  220. moveRight(boardArray, carToMove, 1, moveDirection); // Move right by 1 space each time
  221. }
  222. } else if (moveDirection == 'U') {
  223. for (int i = 0; i < numSpaces; ++i) {
  224. moveUp(boardArray, carToMove, 1, moveDirection); // Move up by 1 space each time
  225. }
  226. } else if (moveDirection == 'D') {
  227. for (int i = 0; i < numSpaces; ++i) {
  228. moveDown(boardArray, carToMove, 1, moveDirection); // Move down by 1 space each time
  229. }
  230. } else {
  231. cout << "Invalid move direction." << endl;
  232. return;
  233. }
  234. }
  235.  
  236. void movingCarsOnBoard(char boardArray[][COLS]) {
  237. int numSpaces;
  238. char carToMove, moveDirection;
  239. bool win = false;
  240.  
  241. while (win != true) { // Change the loop condition to check if win is false
  242. cout << "Enter next move (or Q to quit): ";
  243. cin >> carToMove;
  244. win = true;
  245. if (carToMove == 'Q' || carToMove == 'q') {
  246. break;
  247. }
  248.  
  249. bool validCar = false;
  250.  
  251. // Check if the specified car exists on the board
  252. for (int i = 0; i < ROWS; ++i) {
  253. for (int j = 0; j < COLS; ++j) {
  254. if (boardArray[i][j] == carToMove) {
  255. validCar = true;
  256. break;
  257. }
  258. }
  259. if (validCar) break;
  260. }
  261.  
  262. if (!validCar) {
  263. cout << "That car is not on the board." << endl;
  264. continue;
  265. }
  266.  
  267. cin >> numSpaces >> moveDirection;
  268.  
  269. if (numSpaces <= 0) {
  270. cout << "Invalid number of spaces to move." << endl;
  271. continue;
  272. }
  273.  
  274. // Move all instances of the specified car according to the specified direction
  275. moveAllCars(boardArray, carToMove, numSpaces, moveDirection);
  276.  
  277. // Display the board after the move
  278. displayBoard(boardArray);
  279.  
  280. // Check for win
  281. for (int i = 0; i < ROWS; ++i) {
  282. if (boardArray[2][4] == boardArray[2][5] && (boardArray[2][4] != '.' || boardArray[2][5] != '.')) {
  283. win = true;
  284. }
  285. }
  286.  
  287. if (win == true) {
  288. cout << "Congratulations! You won!" << endl;
  289. }
  290. }
  291. }
  292.  
  293.  
  294.  
  295.  
  296. // ... (rest of the code remains the same)
  297.  
  298.  
  299.  
  300.  
  301.  
  302. void readBoard(const string& fileName, char boardArray[][COLS]) {
  303. ifstream file(fileName);
  304. if (!file.is_open()) {
  305. cout << "Error opening file." << endl;
  306. return;
  307. }
  308.  
  309. string line;
  310. int row = 0;
  311.  
  312. // Read the board layout from the file
  313. while (getline(file, line) && row < ROWS) {
  314. // Ensure the line length is at most COLS characters
  315. line.resize(COLS, ' ');
  316.  
  317. for (int j = 0; j < COLS; ++j) {
  318. boardArray[row][j] = line[j];
  319. }
  320.  
  321. row++;
  322. }
  323.  
  324. // Fill any remaining cells with spaces
  325. for (; row < ROWS; ++row) {
  326. for (int j = 0; j < COLS; ++j) {
  327. boardArray[row][j] = ' ';
  328. }
  329. }
  330.  
  331. file.close();
  332. }
  333.  
  334.  
  335. int main() {
  336. string fileName;
  337.  
  338. cout << "Enter the file name: ";
  339. getline(cin, fileName);
  340.  
  341. char boardArray[ROWS][COLS];
  342.  
  343. readBoard(fileName, boardArray);
  344.  
  345. displayBoard(boardArray);
  346.  
  347. while (true) {
  348. movingCarsOnBoard(boardArray);
  349. }
  350.  
  351. return 0;
  352. }
  353.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement