Question
C++ Program Create a game that consists of a 2 dimensional grid of randomly generated spaces and obstacles. Have the player start on the left
C++ Program
Create a game that consists of a 2 dimensional grid of randomly generated spaces and obstacles. Have the player start on the left of the game-board and have to move their way to the right to win. Allow the player to move up,down,left and right but do not allow them to move onto spaces with obstacles.
Every time the player makes it across the board create a new game board that is larger in both dimensions.
I already wrote part of the code I just need a function that will tell the player that he/she Won if he/she is able to make it acroos the board. I also need to create another function that will create a new game board that is larger in both dimensions when the player has won.
Extra credit: if possible create a Path finder function so the user can use when he/she gets stuck. Not Necessary but i'd appreciate it, Thanks in advance
Here's my code so far:
#include
int main() { int move; position theGuy; boolArray *board = makeboard(); boolArray *NextBoard = makeboard(); while (1) { display(board, theGuy); move = getMove(); if (mover(board, NextBoard, theGuy, move)) { for (int i = 0; i < 8; i++) { delete[] board[i]; } delete[] board; board = NextBoard; NextBoard = makeboard(); } }
}
void display(boolArray a[], position him) { for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (him.x != x || him.y != y) { if (a[x][y]) cout << " "; else { cout << "X"; } } else cout << "8"; } cout << endl; } }
bool mover(boolArray a[], boolArray p[], position &him, int move) { //right if (move == 6) { if (him.x + 1 < 8) { if (a[him.x + 1][him.y]) { him.x++; } } } else if (move == 4)//left { if (him.x - 1 >= 0) { if (a[him.x - 1][him.y]) { him.x--; } } } else if (move == 2)//down { if (him.y + 1 < 8) { if (a[him.x][him.y + 1]) { him.y++; } }
} else if (move == 8)//up { if (him.y - 1 >= 0) { if (a[him.x][him.y - 1]) { him.y--; } } } return 0; }
boolArray* makeboard() { boolArray *p = new boolArray[8]; for (int i = 0; i < 8; i++) { p[i] = new bool[8]; }
for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { p[x][y] = (rand() % 100 + 1 > 20); } } return p; }
int getMove() { cout << "2 = down; 4 = left; 6 = right; 8 = up; what way: "; int move; cin >> move; return move; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started