C++ Program Create a game that consists of a 2 dimensional grid of randomly gene
ID: 3817050 • Letter: C
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 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 <iostream>
#include <cstdlib>
using namespace std;
struct position
{
int x = 0;
int y = 0;
};
typedef bool* boolArray;
void display(boolArray a[], position him);
bool mover(boolArray a[], boolArray p[], position& him, int move);
boolArray* makeboard();
int getMove();
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;
}
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
struct position
{
int x = 0;
int y = 0;
};
typedef bool* boolArray;
void display(boolArray a[], position him);
bool mover(boolArray a[], boolArray p[], position& him, int move);
boolArray* makeboard();
int getMove();
int boardSize ;
void runGame();
void winCall();
void newGame();
int main()
{
boardSize=8;
runGame();
}
void winCall()
{
cout<<"You Won !!"<<endl;
newGame();
}
void newGame()
{
boardSize+=8;
runGame();
}
void runGame()
{
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 < boardSize; i++)
{
delete[] board[i];
}
delete[] board;
board = NextBoard;
NextBoard = makeboard();
}
}
}
void display(boolArray a[], position him)
{
for (int y = 0; y < boardSize; y++)
{
for (int x = 0; x < boardSize; 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 < boardSize)
{
if (a[him.x + 1][him.y])
{
him.x++;
}
}
else if (him.x + 1 == boardSize)
{
winCall();
}
}
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 < boardSize)
{
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[boardSize];
for (int i = 0; i < boardSize; i++)
{
p[i] = new bool[boardSize];
}
for (int x = 0; x < boardSize; x++)
{
for (int y = 0; y < boardSize; 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.