Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace std;
- const int ROWS = 6;
- const int COLS = 6;
- 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 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 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 = 0; i < ROWS; ++i) {
- // if (boardArray[i][column] == '.') {
- // countDots++;
- // } else {
- // break;
- // }
- // }
- // }
- // return countDots;
- // }
- // int findMin(int a, int b) {
- // return (a < b) ? a : b;
- // }
- // 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) {
- // int startCol = -1; // Store the starting column of the car
- // // Find the starting column of the car
- // for (int j = 0; j < COLS; ++j) {
- // if (boardArray[i][j] == carToMove) {
- // startCol = j;
- // break;
- // }
- // }
- // if (startCol == -1)
- // continue; // Car not found in this row
- // // Check if there are spaces and no cars in the way
- // bool canMove = true;
- // for (int k = 1; k <= numSpaces; ++k) {
- // if (startCol - k < 0 || boardArray[i][startCol - k] != '.') {
- // canMove = false;
- // break;
- // }
- // }
- // if (canMove) {
- // // Move the entire car together
- // for (int k = 0; k < numSpaces; ++k) {
- // if (boardArray[i][startCol - k] == carToMove) {
- // boardArray[i][startCol - k] = '.';
- // boardArray[i][startCol - k - 1] = carToMove;
- // }
- // }
- // } else {
- // cout << "Invalid move. Car would go out of bounds or is blocked." << endl;
- // }
- // }
- // }
- // void moveRight(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
- // for (int i = 0; i < ROWS; ++i) {
- // int startCol = -1; // Store the starting column of the car
- // // Find the starting column of the car
- // for (int j = COLS - 1; j >= 0; --j) {
- // if (boardArray[i][j] == carToMove) {
- // startCol = j;
- // break;
- // }
- // }
- // if (startCol == -1)
- // continue; // Car not found in this row
- // // Check if there are spaces and no cars in the way
- // bool canMove = true;
- // for (int k = 1; k <= numSpaces; ++k) {
- // if (startCol + k >= COLS || boardArray[i][startCol + k] != '.') {
- // canMove = false;
- // break;
- // }
- // }
- // if (canMove) {
- // // Move the entire car together
- // for (int k = 0; k < numSpaces; ++k) {
- // if (boardArray[i][startCol + k] == carToMove) {
- // boardArray[i][startCol + k] = '.';
- // boardArray[i][startCol + k + 1] = carToMove;
- // }
- // }
- // } else {
- // cout << "Cannot move " << carToMove << " in the specified direction." << endl;
- // }
- // }
- // }
- // void moveUp(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
- // for (int j = 0; j < COLS; ++j) {
- // int startRow = -1; // Store the starting row of the car
- // // Find the starting row of the car
- // for (int i = 0; i < ROWS; ++i) {
- // if (boardArray[i][j] == carToMove) {
- // startRow = i;
- // break;
- // }
- // }
- // if (startRow == -1)
- // continue; // Car not found in this column
- // // Check if there are spaces and no cars in the way
- // bool canMove = true;
- // for (int k = 1; k <= numSpaces; ++k) {
- // if (startRow - k < 0 || boardArray[startRow - k][j] != '.') {
- // canMove = false;
- // break;
- // }
- // }
- // if (canMove) {
- // // Move the entire car together
- // for (int k = 0; k < numSpaces; ++k) {
- // if (boardArray[startRow - k][j] == carToMove) {
- // boardArray[startRow - k][j] = '.';
- // boardArray[startRow - k - 1][j] = carToMove;
- // }
- // }
- // } else {
- // cout << "Cannot move " << carToMove << " in the specified direction." << endl;
- // }
- // }
- // }
- // void moveDown(char boardArray[][COLS], char carToMove, int numSpaces, char moveDirection) {
- // for (int j = 0; j < COLS; ++j) {
- // int startRow = -1; // Store the starting row of the car
- // // Find the starting row of the car
- // for (int i = ROWS - 1; i >= 0; --i) {
- // if (boardArray[i][j] == carToMove) {
- // startRow = i;
- // break;
- // }
- // }
- // if (startRow == -1)
- // continue; // Car not found in this column
- // // Check if there are spaces and no cars in the way
- // bool canMove = true;
- // for (int k = 1; k <= numSpaces; ++k) {
- // if (startRow + k >= ROWS || boardArray[startRow + k][j] != '.') {
- // canMove = false;
- // break;
- // }
- // }
- // if (canMove) {
- // // Move the entire car together
- // for (int k = 0; k < numSpaces; ++k) {
- // if (boardArray[startRow + k][j] == carToMove) {
- // boardArray[startRow + k][j] = '.';
- // boardArray[startRow + k + 1][j] = carToMove;
- // }
- // }
- // } else {
- // cout << "Cannot move " << carToMove << " in the specified direction." << endl;
- // }
- // }
- // }
- // bool canMove(char boardArray[][COLS], char carToMove, char moveDirection) {
- // for (int i = 0; i < ROWS; ++i) {
- // for (int j = 0; j < COLS; ++j) {
- // if (boardArray[i][j] == carToMove) {
- // int dots = countNumberOfDots(boardArray, moveDirection, i, j);
- // int newRow, newCol;
- // // Check if there are spaces around the car in the specified direction
- // if (moveDirection == 'L') {
- // newRow = i;
- // newCol = j - dots;
- // } else if (moveDirection == 'R') {
- // newRow = i;
- // newCol = j + dots;
- // } else if (moveDirection == 'U') {
- // newRow = i - dots;
- // newCol = j;
- // } else if (moveDirection == 'D') {
- // newRow = i + dots;
- // newCol = j;
- // }
- // // Check if there are 0 surrounding cars in the specified direction
- // int surroundingCars = 0;
- // if (moveDirection == 'L' && newCol >= 0) {
- // if (newCol - 1 >= 0 && boardArray[i][newCol - 1] != '.')
- // surroundingCars++;
- // } else if (moveDirection == 'R' && newCol < COLS) {
- // if (newCol + 1 < COLS && boardArray[i][newCol + 1] != '.')
- // surroundingCars++;
- // } else if (moveDirection == 'U' && newRow >= 0) {
- // if (newRow - 1 >= 0 && boardArray[newRow - 1][j] != '.')
- // surroundingCars++;
- // } else if (moveDirection == 'D' && newRow < ROWS) {
- // if (newRow + 1 < ROWS && boardArray[newRow + 1][j] != '.')
- // surroundingCars++;
- // }
- // if (surroundingCars == 0)
- // continue;
- // return false;
- // }
- // }
- // }
- // return true;
- // }
- // void movingCarsOnBoard(char boardArray[][COLS]) {
- // char carToMove;
- // int numSpaces;
- // char moveDirection;
- // displayBoard(boardArray);
- // cout << "Enter the car you want to move (A, B, C, etc.): ";
- // cin >> carToMove;
- // cout << "Enter the number of spaces to move: ";
- // cin >> numSpaces;
- // cout << "Enter the direction to move (L for left, R for right, U for up, D for down): ";
- // cin >> moveDirection;
- // // Convert input to uppercase
- // carToMove = toupper(carToMove);
- // moveDirection = toupper(moveDirection);
- // // Call the moveCar function to move the car
- // moveCar(boardArray, carToMove, numSpaces, moveDirection);
- // }
- // 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();
- // }
- void moveCar(char boardArray[][COLS], char usersCar, char moveCarDirection, int j, int i, int ROWS, int COLS, int numSpaces){
- for(int k = 0; k < numSpaces; ++k){
- if(moveCarDirection == 'L'){
- for(int i = 0; i < ROWS; ++i){
- for(j = j-1; j >= 0; --j){
- if(boardArray[i][j] == '.'){
- boardArray[i][j] = usersCar;
- boardArray[i][j+1] = '.';
- }
- }
- }
- }
- else if(moveCarDirection == 'R'){
- for(int i = 0; i < ROWS; ++i){
- for(j = j + 1; j < COLS; ++j){
- if(boardArray[i][j] == '.'){
- boardArray[i][j] = usersCar;
- boardArray [i][j - 1] = '.';
- }
- }
- }
- }
- else if(moveCarDirection == 'U'){
- for(i = i - 1; i >= 0; --i){
- if(boardArray[i][j] == '.'){
- boardArray[i-1][j] = usersCar;
- boardArray [i + 1][j] = '.';
- }
- }
- }
- else if(moveCarDirection == 'D'){
- for(i = i + 1; i < ROWS; ++i){
- if(boardArray[i][j] == '.'){
- boardArray[i+1][j] = usersCar;
- boardArray [i - 1][j] = '.';
- }
- }
- }
- }
- }
- bool checkForSpacesInDirection(char boardArray[][COLS], char usersCar, char moveCarDirection, int totalCars, int ROWS, int COLS, int j, int i){
- while(i != totalCars){
- if(moveCarDirection == 'L'){
- for(int i = 0; i < ROWS; ++i){
- for(j = j-1; j >= 0; --j){
- if(boardArray[i][j] == '.'){
- i += 1;
- }
- }
- }
- }
- else if(moveCarDirection == 'R'){
- for(int i = 0; i < ROWS; ++i){
- for(j = j + 1; j < COLS; ++j){
- if(boardArray[i][j] == '.'){
- i += 1;
- }
- }
- }
- }
- else if(moveCarDirection == 'U'){
- for(i = i - 1; i >= 0; --i){
- if(boardArray[i][j] == '.'){
- i += 1;
- }
- }
- }
- else if(moveCarDirection == 'D'){
- for(i = i + 1; i < ROWS; ++i){
- if(boardArray[i][j] == '.'){
- i += 1;
- }
- }
- }
- }
- return true;
- }
- // int countCarsOnBoard(char boardArray[][COLS], char usersCar, int ROWS, int COLS){
- // int totalCars = 0
- // for(int i = 0; i < ROWS; ++i){
- // for(int j = 0; j < COLS; ++j){
- // if (boardArray[i][j] == usersCar){
- // checkForSpacesInDirection(board, usersCar, moveCarDirection, totalCars, ROWS)
- // totalCars += 1;
- // }
- // }
- // }
- // return totalCars;
- // }
- bool doesCarExist(char boardArray[][COLS], char usersCar, const int ROWS, const int COLS){
- for(int i = 0; i < ROWS; ++i){
- for(int j = 0; j < COLS; ++j){
- if(boardArray[i][j] == usersCar){
- return true;
- }
- }
- cout << "This car is not on the board." << endl;
- return false;
- }
- }
- int main() {
- string fileName;
- char usersCar;
- int numSpaces;
- char moveCarDirection;
- int i;
- int j;
- cout << "Enter the file name: ";
- getline(cin, fileName);
- char boardArray[ROWS][COLS];
- readBoard(fileName, boardArray);
- displayBoard(boardArray);
- int totalCars = 0;
- while(usersCar != 'Q'){
- cout << "Enter next move (or Q to quit): ";
- cin >> usersCar;
- usersCar = toupper(usersCar);
- cin >> numSpaces;
- cin >> moveCarDirection;
- moveCarDirection = toupper(moveCarDirection);
- if (doesCarExist(boardArray, usersCar, ROWS, COLS) == true){
- for( i = 0; i < ROWS; ++i){
- for(j = 0; j < COLS; ++j){
- if (boardArray[i][j] == usersCar){
- totalCars += 1;
- }
- }
- }
- bool areSpacesThere = checkForSpacesInDirection(boardArray, usersCar, moveCarDirection, totalCars, ROWS, COLS
- , j, i);
- if(areSpacesThere == true){
- moveCar( boardArray, usersCar, moveCarDirection, j, i, ROWS, COLS, numSpaces);
- }
- }
- displayBoard(boardArray);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement