Advertisement
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 = 6;
- int countNumberOfDots(char boardArray[][COLS], char moveDirection, int row, int column) {
- int countDots = 0;
- if (moveDirection == 'L') {
- for (int i = column - 1; i >= 0; --i) {
- if (boardArray[row][i] == '.') {
- countDots++;
- } else {
- break;
- }
- }
- } else if (moveDirection == 'R') {
- for (int i = column + 1; i < COLS; ++i) {
- if (boardArray[row][i] == '.') {
- countDots++;
- } else {
- break;
- }
- }
- } else if (moveDirection == 'U') {
- for (int i = row - 1; i >= 0; --i) {
- if (boardArray[i][column] == '.') {
- countDots++;
- } else {
- break;
- }
- }
- } else if (moveDirection == 'D') {
- for (int i = row + 1; i < ROWS; ++i) {
- if (boardArray[i][column] == '.') {
- countDots++;
- } else {
- break;
- }
- }
- }
- return countDots;
- }
- int findMin(int a, int b) {
- return (a < b) ? a : b;
- }
- void displayBoard(char boardArray[][COLS]) {
- cout << endl;
- cout << "--------" << endl;
- for (int i = 0; i < ROWS; ++i) {
- cout << "|";
- for (int j = 0; j < COLS; ++j) {
- if(boardArray[i][j] == '-'){
- boardArray[i][j] = '.';
- }
- cout << boardArray[i][j];
- }
- if (i == 2) {
- cout << "=" << endl;
- } else {
- 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, char moveDirection) {
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 1; j < COLS; ++j) {
- if (boardArray[i][j] == carToMove) {
- int dots = countNumberOfDots(boardArray, moveDirection, i, j);
- int maxSpaces = findMin(numSpaces, dots);
- for (int k = 0; k < maxSpaces; ++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;
- }
- }
- }
- }
- }
- }
- void moveRight(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
- for (int i = 0; i < ROWS; ++i) {
- for (int j = COLS - 2; j >= 0; --j) {
- if (boardArray[i][j] == carToMove) {
- int dots = countNumberOfDots(boardArray, moveDirection, i, j);
- int maxSpaces = findMin(numSpaces, dots);
- for (int k = 0; k < maxSpaces; ++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, char moveDirection) {
- for (int j = 0; j < COLS; ++j) {
- for (int i = 1; i < ROWS; ++i) {
- if (boardArray[i][j] == carToMove) {
- int dots = countNumberOfDots(boardArray, moveDirection, i, j);
- int maxSpaces = findMin(numSpaces, dots);
- for (int k = 0; k < maxSpaces; ++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, char moveDirection) {
- for (int j = 0; j < COLS; ++j) {
- for (int i = ROWS - 2; i >= 0; --i) {
- if (boardArray[i][j] == carToMove) {
- int dots = countNumberOfDots(boardArray, moveDirection, i, j);
- int maxSpaces = findMin(numSpaces, dots);
- for (int k = 0; k < maxSpaces; ++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, moveDirection); // Move left by 1 space each time
- }
- } else if (moveDirection == 'R') {
- for (int i = 0; i < numSpaces; ++i) {
- moveRight(boardArray, carToMove, 1, moveDirection); // Move right by 1 space each time
- }
- } else if (moveDirection == 'U') {
- for (int i = 0; i < numSpaces; ++i) {
- moveUp(boardArray, carToMove, 1, moveDirection); // Move up by 1 space each time
- }
- } else if (moveDirection == 'D') {
- for (int i = 0; i < numSpaces; ++i) {
- moveDown(boardArray, carToMove, 1, moveDirection); // Move down by 1 space each time
- }
- } else {
- cout << "Invalid move direction." << endl;
- return;
- }
- }
- void movingCarsOnBoard(char boardArray[][COLS]) {
- int numSpaces;
- char carToMove, moveDirection;
- bool win = false;
- while (win != true) { // Change the loop condition to check if win is false
- cout << "Enter next move (or Q to quit): ";
- cin >> carToMove;
- win = true;
- if (carToMove == 'Q' || carToMove == 'q') {
- break;
- }
- bool validCar = false;
- // Check if the specified car exists on the board
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 0; j < COLS; ++j) {
- if (boardArray[i][j] == carToMove) {
- validCar = true;
- break;
- }
- }
- if (validCar) break;
- }
- if (!validCar) {
- cout << "That car is not on the board." << endl;
- continue;
- }
- cin >> numSpaces >> moveDirection;
- if (numSpaces <= 0) {
- cout << "Invalid number of spaces to move." << endl;
- continue;
- }
- // 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) {
- if (boardArray[2][4] == boardArray[2][5] && (boardArray[2][4] != '.' || boardArray[2][5] != '.')) {
- win = true;
- }
- }
- if (win == true) {
- cout << "Congratulations! You won!" << endl;
- }
- }
- }
- // ... (rest of the code remains the same)
- void readBoard(const string& fileName, char boardArray[][COLS]) {
- ifstream file(fileName);
- if (!file.is_open()) {
- cout << "Error opening file." << endl;
- return;
- }
- string line;
- int row = 0;
- // Read the board layout from the file
- while (getline(file, 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++;
- }
- // Fill any remaining cells with spaces
- for (; row < ROWS; ++row) {
- for (int j = 0; j < COLS; ++j) {
- boardArray[row][j] = ' ';
- }
- }
- file.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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement