Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- const int ROWS = 6;
- const int COLS = 7;
- void displayBoard(char boardArray[][COLS]) {
- cout << "--------" << endl;
- for (int i = 0; i < ROWS; ++i) {
- cout << "|";
- for (int j = 0; j < COLS; ++j) {
- cout << boardArray[i][j];
- }
- cout << "|" << endl;
- }
- cout << "--------" << endl;
- }
- void shiftCar(char boardArray[][COLS], int row, int col, char direction) {
- if (direction == 'L') {
- while (col - 1 >= 0 && boardArray[row][col - 1] == '.') {
- boardArray[row][col] = '.';
- boardArray[row][col - 1] = 'X'; // Symbolize a temporarily shifted car
- --col;
- }
- } else if (direction == 'R') {
- while (col + 1 < COLS && boardArray[row][col + 1] == '.') {
- boardArray[row][col] = '.';
- boardArray[row][col + 1] = 'X'; // Symbolize a temporarily shifted car
- ++col;
- }
- } else if (direction == 'U') {
- while (row - 1 >= 0 && boardArray[row - 1][col] == '.') {
- boardArray[row][col] = '.';
- boardArray[row - 1][col] = 'X'; // Symbolize a temporarily shifted car
- --row;
- }
- } else if (direction == 'D') {
- while (row + 1 < ROWS && boardArray[row + 1][col] == '.') {
- boardArray[row][col] = '.';
- boardArray[row + 1][col] = 'X'; // Symbolize a temporarily shifted car
- ++row;
- }
- }
- }
- void moveLeft(char boardArray[][COLS], char carToMove, int numSpaces) {
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 1; j < COLS; ++j) {
- if (boardArray[i][j] == carToMove) {
- for (int k = 0; k < numSpaces; ++k) {
- if (j - k - 1 >= 0) {
- if (boardArray[i][j - k - 1] == '.') {
- boardArray[i][j - k] = '.';
- boardArray[i][j - k - 1] = carToMove;
- } else {
- // Shift the blocking car
- shiftCar(boardArray, i, j - k - 1, 'L');
- boardArray[i][j - k] = '.';
- boardArray[i][j - k - 1] = carToMove;
- }
- } else {
- cout << "Invalid move. Car would go out of bounds." << endl;
- return;
- }
- }
- }
- }
- }
- }
- void moveRight(char boardArray[][COLS], char carToMove, int numSpaces) {
- for (int i = 0; i < ROWS; ++i) {
- for (int j = COLS - 2; j >= 0; --j) {
- if (boardArray[i][j] == carToMove) {
- for (int k = 0; k < numSpaces; ++k) {
- if (j + k + 1 < COLS) {
- if (boardArray[i][j + k + 1] == '.') {
- boardArray[i][j + k] = '.';
- boardArray[i][j + k + 1] = carToMove;
- } else {
- // Shift the blocking car
- shiftCar(boardArray, i, j + k + 1, 'R');
- boardArray[i][j + k] = '.';
- boardArray[i][j + k + 1] = carToMove;
- }
- } else {
- cout << "Invalid move. Car would go out of bounds." << endl;
- return;
- }
- }
- }
- }
- }
- }
- void moveUp(char boardArray[][COLS], char carToMove, int numSpaces) {
- for (int j = 0; j < COLS; ++j) {
- for (int i = 1; i < ROWS; ++i) {
- if (boardArray[i][j] == carToMove) {
- for (int k = 0; k < numSpaces; ++k) {
- if (i - k - 1 >= 0) {
- if (boardArray[i - k - 1][j] == '.') {
- boardArray[i - k][j] = '.';
- boardArray[i - k - 1][j] = carToMove;
- } else {
- // Shift the blocking car
- shiftCar(boardArray, i - k - 1, j, 'U');
- boardArray[i - k][j] = '.';
- boardArray[i - k - 1][j] = carToMove;
- }
- } else {
- cout << "Invalid move. Car would go out of bounds." << endl;
- return;
- }
- }
- }
- }
- }
- }
- void moveDown(char boardArray[][COLS], char carToMove, int numSpaces) {
- for (int j = 0; j < COLS; ++j) {
- for (int i = ROWS - 2; i >= 0; --i) {
- if (boardArray[i][j] == carToMove) {
- for (int k = 0; k < numSpaces; ++k) {
- if (i + k + 1 < ROWS) {
- if (boardArray[i + k + 1][j] == '.') {
- boardArray[i + k][j] = '.';
- boardArray[i + k + 1][j] = carToMove;
- } else {
- // Shift the blocking car
- shiftCar(boardArray, i + k + 1, j, 'D');
- boardArray[i + k][j] = '.';
- boardArray[i + k + 1][j] = carToMove;
- }
- } else {
- cout << "Invalid move. Car would go out of bounds." << endl;
- return;
- }
- }
- }
- }
- }
- }
- void moveAllCars(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
- if (moveDirection == 'L') {
- for (int i = 0; i < numSpaces; ++i) {
- moveLeft(boardArray, carToMove, 1); // Move left by 1 space each time
- }
- } else if (moveDirection == 'R') {
- for (int i = 0; i < numSpaces; ++i) {
- moveRight(boardArray, carToMove, 1); // Move right by 1 space each time
- }
- } else if (moveDirection == 'U') {
- for (int i = 0; i < numSpaces; ++i) {
- moveUp(boardArray, carToMove, 1); // Move up by 1 space each time
- }
- } else if (moveDirection == 'D') {
- for (int i = 0; i < numSpaces; ++i) {
- moveDown(boardArray, carToMove, 1); // Move down by 1 space each time
- }
- } else {
- cout << "Invalid move direction." << endl;
- return;
- }
- }
- void movingCarsOnBoard(char boardArray[][COLS]) {
- cout << "Enter next move (or Q to quit): ";
- char carToMove;
- int numSpaces;
- char moveDirection;
- cin >> carToMove >> numSpaces >> moveDirection;
- if (numSpaces <= 0) {
- cout << "Invalid number of spaces to move." << endl;
- return;
- }
- // Move all instances of the specified car according to the specified direction
- moveAllCars(boardArray, carToMove, numSpaces, moveDirection);
- // Display the board after the move
- displayBoard(boardArray);
- // Check for win
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 0; j < COLS; ++j) {
- if (boardArray[i][j] == '=') {
- cout << "Congratulations! You won!" << endl;
- return;
- }
- }
- }
- }
- void readBoard(const string& fileName, char boardArray[][COLS]) {
- fstream newfile;
- newfile.open(fileName.c_str(), ios::in);
- if (!newfile.is_open()) {
- cout << "Error opening file." << endl;
- return;
- }
- string line;
- int row = 0;
- // Read the board layout from the file
- while (getline(newfile, line) && row < ROWS) {
- // Ensure the line length is at most COLS characters
- line.resize(COLS, ' ');
- for (int j = 0; j < COLS; ++j) {
- boardArray[row][j] = line[j];
- }
- row++;
- }
- newfile.close();
- }
- int main() {
- string fileName;
- cout << "Enter the file name: ";
- getline(cin, fileName);
- char boardArray[ROWS][COLS];
- readBoard(fileName, boardArray);
- displayBoard(boardArray);
- while (true) {
- movingCarsOnBoard(boardArray);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment