Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to implement a turn based game for a 2 player Minotaur maze game. I am trying to have the maze print the way

I'm trying to implement a turn based game for a 2 player Minotaur maze game. I am trying to have the maze print the way it is displayed in the code. Instead, it prints it vertically making it nearly impossible to see what is going on. I also don't know if the game ends successfully if the minotaur catches Theseus (NOTE: This is controlled by a separate player). I most likely missed a few other problems as well. Please help where you can, thanks.

#include

#include

#include

#include

#include

using namespace std;

const int WIDTH = 20;

const int HEIGHT = 10;

//---------------------------------------------------------------

// Function prototypes

//---------------------------------------------------------------

void printMaze(char maze[][WIDTH], int thesx, int thesy, int minX, int minY);

bool validMove(char maze[][WIDTH], int newX, int newY);

bool move(char maze[][WIDTH], int &thesX, int &thesY, int &minX, int &minY,

int newX, int newY);

//------------------------------------------------------------------------

// Return true or false if moving to the specified coordinate is valid

//------------------------------------------------------------------------

bool validMove(char maze[][WIDTH], int newX, int newY)

{

// Check for going off the maze edges

if (newX < 0 || newX >= WIDTH)

return false;

if (newY < 0 || newY >= HEIGHT)

return false;

// Check if target is a wall

if (maze[newY][newX]=='X')

return false;

return true;

}

//---------------------------------------------------------------

// Make the move on the maze to move to a new coordinate

// I passed curX and curY by reference so they are changed to

// the new coordinates. I assume the move coordinates are valid.

// This returns true if we move onto the exit, false otherwise.

//---------------------------------------------------------------

bool move(char maze[][WIDTH], int &thesX, int &thesY, int &minX, int &minY,

int newX, int newY)

{

bool foundExit = false;

if (maze[newY][newX]=='E') // Check for exit

foundExit = true;

thesX = newX; // Update location

thesY = newY;

return foundExit;

bool foundTheseus = false;

if (maze[newY][newX]=='T') // Check for Theseus

foundTheseus = true;

minX = newX; // Update location

minY = newY;

return foundTheseus;

}

//---------------------------------------------------------------

// Display the maze in ASCII

//---------------------------------------------------------------

void printMaze(char maze[][WIDTH], int thesx, int thesy, int minX, int minY)

{

for (int y=0; y < HEIGHT;y++)

{

for (int x=0; x < WIDTH; x++)

{

for (int b=0; b < HEIGHT;b++)

{

for (int a=0; a < WIDTH; a++)

{

if ((x==thesx) && (y==thesy))

cout << "T";

else

cout << maze[y][x];

cout << endl;

if ((a==minX) && (b==minY))

cout << "M";

else

cout << maze[b][a];

}

cout << endl;

}

}

}

}

//---------------------------------------------------------------

// MAIN PROGRAM

//---------------------------------------------------------------

int main()

{

char maze[HEIGHT][WIDTH] = {

{'X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X'},

{'X',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','X',' ',' ',' ','X'},

{'X',' ','X',' ',' ',' ','X',' ',' ',' ',' ','X',' ',' ',' ','X',' ',' ',' ','X'},

{'X',' ','X','X','X',' ','X',' ','X','X','X','X',' ',' ',' ',' ',' ',' ',' ','X'},

{'X',' ',' ',' ','X',' ','X',' ',' ',' ',' ',' ',' ','X','X','X',' ',' ','X','X'},

{'X',' ',' ',' ','X',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','X',' ',' ',' ','X'},

{'X',' ','X','X','X',' ',' ',' ',' ',' ',' ','X',' ','X','X','X',' ','X',' ','X'},

{'X',' ','X',' ',' ',' ','X',' ','X','X','X','X',' ',' ',' ',' ',' ','X',' ','X'},

{'X',' ',' ',' ',' ',' ','X',' ',' ',' ',' ',' ',' ',' ',' ','X',' ','X','E','X'},

{'X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X','X'}

};

int x = 1, y = 1;

int a = 1, b = 2;

bool foundExit = false;

while (!foundExit)

{

bool foundTheseus = false;

while (!foundTheseus)

{

printMaze(maze,x,y,a,b);

cout << "Enter WASD (for T) or JIKL (for M) to move." << endl;

cout << "TAKE TURNS" << endl;

char c;

cin >> c;

c = tolower(c);

switch (c)

{

case 'w':

if (validMove(maze,x,y-1))

foundExit = move(maze,x,y,x,y,x,y-1);

break;

case 'a':

if (validMove(maze,x-1,y))

foundExit = move(maze,x,y,x,y,x-1,y);

break;

case 's':

if (validMove(maze,x,y+1))

foundExit = move(maze,x,y,x,y,x,y+1);

break;

case 'd':

if (validMove(maze,x+1,y))

foundExit = move(maze,x,y,x,y,x+1,y);

case 'i':

if (validMove(maze,a,b-1))

foundTheseus = move(maze,a,b,a,b,a,b-1);

break;

case 'j':

if (validMove(maze,a-1,b))

foundTheseus = move(maze,a,b,a,b,a-1,b);

break;

case 'k':

if (validMove(maze,a,b+1))

foundTheseus = move(maze,a,b,a,b,a,b+1);

break;

case 'l':

if (validMove(maze,a+1,b))

foundTheseus = move(maze,a,b,a,b,a+1,b);

}

}

}

}

//=========================================================================

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

What are the advantages and disadvantages of grievance mediation?

Answered: 1 week ago