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 = 7;
- int findMin(int a, int b) {
- return (a < b) ? a : b;
- }
- void displayBoard(char boardArray[][COLS]) {
- cout << "--------" << endl;
- for (int i = 0; i < ROWS; ++i) {
- cout << "|";
- for (int j = 0; j < COLS; ++j) {
- if (boardArray[i][j] == '.') {
- cout << ".";
- } else if (boardArray[i][j] == '=') {
- cout << "=";
- } else {
- cout << boardArray[i][j];
- }
- }
- cout << "|" << endl;
- }
- cout << "--------" << endl;
- }
- void shiftCar(char boardArray[][COLS], int row, int col, char direction) {
- while ((direction == 'L' && col - 1 >= 0 && boardArray[row][col - 1] == '.') ||
- (direction == 'R' && col + 1 < COLS && boardArray[row][col + 1] == '.') ||
- (direction == 'U' && row - 1 >= 0 && boardArray[row - 1][col] == '.') ||
- (direction == 'D' && row + 1 < ROWS && boardArray[row + 1][col] == '.')) {
- if (direction == 'L') col--;
- else if (direction == 'R') col++;
- else if (direction == 'U') row--;
- else if (direction == 'D') row--;
- boardArray[row][col] = 'X'; // Symbolize a temporarily shifted car
- boardArray[row][col - 1] = '.';
- }
- }
- 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) {
- int maxSpaces = findMin(numSpaces, j);
- for (int k = 0; k < maxSpaces; ++k) {
- 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;
- }
- }
- }
- }
- }
- }
- 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) {
- int maxSpaces = findMin(numSpaces, COLS - j - 1);
- for (int k = 0; k < maxSpaces; ++k) {
- 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;
- }
- }
- }
- }
- }
- }
- 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) {
- int maxSpaces = findMin(numSpaces, i);
- for (int k = 0; k < maxSpaces; ++k) {
- 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;
- }
- }
- }
- }
- }
- }
- void moveDown(char boardArray[][COLS], char carToMove, int numSpaces) {
- if (){
- for (int j = 0; j < COLS; ++j) {
- for (int i = ROWS - 2; i >= 0; --i) {
- if (boardArray[i][j] == carToMove) {
- int maxSpaces = findMin(numSpaces, ROWS - i - 1);
- for (int k = 0; k < maxSpaces; ++k) {
- 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;
- }
- }
- }
- }
- }
- }
- }
- void moveCar(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
- if (moveDirection == 'L') {
- moveLeft(boardArray, carToMove, numSpaces);
- } else if (moveDirection == 'R') {
- moveRight(boardArray, carToMove, numSpaces);
- } else if (moveDirection == 'U') {
- moveUp(boardArray, carToMove, numSpaces);
- } else if (moveDirection == 'D') {
- moveDown(boardArray, carToMove, numSpaces);
- } else {
- cout << "Invalid move direction." << endl;
- }
- }
- 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 the specified car according to the specified direction
- moveCar(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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement