Question
This is a cave maze game. It runs great right now as it sits, I just need the code cleaned up and to make some
This is a cave maze game. It runs great right now as it sits, I just need the code cleaned up and to make some functions so its a cleaner program.
clean up the code by using functions to make the code easier to modify and to read. We will also identify several bugs in our design.
review the code and determine where functions could be used in place of existing code in main to make the code easier to read and maintain. Also, before making changes, note all the code behaviors you think need improving.
describe what you changed. list any bugs or features that need improving
here is the raw code
#pragma once
#include
#include
#include
#include
#include
#include
enum gameObjectType
{
EMPTY, PLAYER, TREASURE, MONSTER, WEAPON, TORCH, NOISEMAKER//, CAVE_EXIT -add in week 6
};
struct gameObject
{
int row; // row position of the object
int column; // column position of the object
bool isFound; // flag to indicate if the object has been found (or is dead, in the case of the monster)
bool isVisible; // flag to indicate if the object can be seen on the board -add in week 4
};
struct playerObject
{
bool alive; // flag to indicate if the player is alive or dead
bool hasWeapon; // flag to indicate if the player has the weapon
bool hasTreasure; // flag to indicate if the player has the treasure
bool hasTorch; // flag to indicate if the player has the torch -add in week 4
bool hasNoisemaker; // flag to indicate if the player has the noisemaker -add in week 4
gameObject position; // variables for row, column and visibility
};
// Global Constants
const int MAX_ROWS = 7;
const int MIN_ROWS = 0;
const int MAX_COLS = 7;
const int MIN_COLS = 0;
const int TOTAL_ROWS = MAX_ROWS + 1;
const int TOTAL_COLS = MAX_COLS + 1;
//Display Constants
const char ULC = 201; //Upper left corner
const char HB = 205; //Horizontal border
const char URC = 187; //Upper right corner
const char VB = 186; //Vertical border
const char LRC = 188; //Lower right corner
const char LLC = 200; //Lower left corner
const char MT = ' '; //Empty location
const char PSymbol = 'P'; //Player symbol
const char TOSymbol = 'T'; //Torch symbol
const char WSymbol = 'W'; //Weapon symbol
const char TRSymbol = '$'; //Treasure symbol
const char MSymbol = 'M'; //Monster symbol
const char NSymbol = 'N'; //Noisemaker symbol
const char XSymbol = 'X'; //Cave exit symbol
using namespace std;
// function prototypes
gameObject placeInCave(gameObjectType array[TOTAL_ROWS][TOTAL_COLS]);
//
bool checkVisible(gameObject x, gameObject y, int dist);
//
bool showOnBoard(gameObject x);
int main()
{
//**Initialize Variables**
srand(time(NULL)); // Seed the random number function
gameObjectType cave[TOTAL_ROWS][TOTAL_COLS]; // the cave--a two dimensional array
char board[TOTAL_ROWS + 3][TOTAL_COLS + 3] = // the game board--a two dimensional array
{
{ MT,MT,'0','1','2','3','4','5','6','7',MT },
{ MT,ULC,HB,HB,HB,HB,HB,HB,HB,HB,URC },
{ 'A',VB,MT,MT,MT,MT,MT,MT,MT,MT,VB },
{ 'B',VB,MT,MT,MT,MT,MT,MT,MT,MT,VB },
{ 'C',VB,MT,MT,MT,MT,MT,MT,MT,MT,VB },
{ 'D',VB,MT,MT,MT,MT,MT,MT,MT,MT,VB },
{ 'E',VB,MT,MT,MT,MT,MT,MT,MT,MT,VB },
{ 'F',VB,MT,MT,MT,MT,MT,MT,MT,MT,VB },
{ 'G',VB,MT,MT,MT,MT,MT,MT,MT,MT,VB },
{ 'H',VB,MT,MT,MT,MT,MT,MT,MT,MT,VB },
{ MT,LLC,HB,HB,HB,HB,HB,HB,HB,HB,LRC }
};
playerObject player = { true, false, false, false, false,{ -1, -1, false, true } }; // the player
gameObject treasure = { -1, -1, false, true }; // the treasure
gameObject monster = { -1, -1, false, true }; // the monster
gameObject weapon = { -1, -1, false, true }; // the weapon
//
gameObject torch = { -1, -1, false, true }; // the torch
gameObject noisemaker = { -1, -1, false, true }; // the noisemaker
//
int row, column; // temporarily hold the new player position
int MonsterMoveCounter = 6; // track and control monster movement around treasure
string msg; // status message variable
char command; // player input
//
bool monsterPause = false; // flag to indicate the monster has stopped moving
//
bool movePlayer = true; // flag to indicate the player position can be updated
bool gameOver = false; // status flag
//**Prepare Cave***********
//...Initialize an empty cave
for (gameObjectType(&R)[TOTAL_COLS] : cave)
{
for (auto &C : R) C = EMPTY;
}
//...Add player in rows 0-2, columns 0-2
player.position.row = rand() % 3;
player.position.column = rand() % 3;
cave[player.position.row][player.position.column] = PLAYER;
//...Add Treasure in rows 4-6, column 1-6
treasure.row = rand() % 3 + 4;
treasure.column = rand() % 6 + 1;
cave[treasure.row][treasure.column] = TREASURE;
//...Add Monster at treasure row +1 , column -1
monster.row = treasure.row + 1;
monster.column = treasure.column - 1;
cave[monster.row][monster.column] = MONSTER;
//...Add Weapon in any empty location
weapon = placeInCave(cave);
cave[weapon.row][weapon.column] = WEAPON;
//
//...Add Noisemaker in any empty location
noisemaker = placeInCave(cave);
cave[noisemaker.row][noisemaker.column] = NOISEMAKER;
//...Add Torch in any empty location
torch = placeInCave(cave);
cave[torch.row][torch.column] = TORCH;
//
//**Play Game*************
//...Begin Game Loop
while (!gameOver)
{
//....Display Board
//
//.....Check visibility
if (!player.hasTorch)
{
torch.isVisible = checkVisible(player.position, torch, 2);
weapon.isVisible = checkVisible(player.position, weapon, 2);
treasure.isVisible = checkVisible(player.position, treasure, 2);
monster.isVisible = checkVisible(player.position, monster, 2);
noisemaker.isVisible = checkVisible(player.position, noisemaker, 2);
}
//
//.....Place visible objects on board--note: changing the order below will create a visual bug.
board[weapon.row + 2][weapon.column + 2] = showOnBoard(weapon) ? WSymbol : MT;
//
board[torch.row + 2][torch.column + 2] = showOnBoard(torch) ? TOSymbol : MT;
//
board[treasure.row + 2][treasure.column + 2] = showOnBoard(treasure) ? TRSymbol : MT;
//
board[noisemaker.row + 2][noisemaker.column + 2] = showOnBoard(noisemaker) ? NSymbol : MT;
//
board[monster.row + 2][monster.column + 2] = showOnBoard(monster) ? MSymbol : MT;
board[player.position.row + 2][player.position.column + 2] = player.alive ? PSymbol : MT;
// Put the board on the screen
for (char(&R)[TOTAL_COLS + 3] : board)
{
for (char &C : R)
{
cout << C;
}
cout << endl;
}
cout << msg.c_str() << endl;
//....Get command
cout << "What is your command? ";
cin >> command;
//....Clear display and message
msg.clear();
system("cls");
//....Process player command
row = player.position.row;
column = player.position.column;
switch (command)
{
case 'a':
column = player.position.column - 1;
if (column < MIN_COLS)
{
column = player.position.column;
msg = "You have hit the west wall!";
}
break;
case 's':
row = player.position.row + 1;
if (row > MAX_ROWS)
{
row = player.position.row;
msg = "You have hit the south wall!";
}
break;
case 'w':
column = player.position.column;//Is this really needed?
row = player.position.row - 1;
if (row < MIN_ROWS)
{
row = player.position.row;
msg = "You have hit the north wall!";
}
break;
case 'd':
row = player.position.row;//Is this really needed?
column = player.position.column + 1;
if (column > MAX_COLS)
{
column = player.position.column;
msg = "You have hit the east wall!";
}
break;
//
case 'n':
if (player.hasNoisemaker)
{
msg = "You make an irritating noise!";
monsterPause = true;
}
else
{
msg = "You make a feeble whimper.";
}
break;
//
case 'q':
gameOver = true;
msg = "Quitting? ";
break;
default:
movePlayer = false;
break;
}
//....Check if the game is over
if (!gameOver)
{
//..... Check for events
switch (cave[row][column])
{
//......If treasure found, set flag to show player has treasure
case TREASURE:
player.hasTreasure = true;
treasure.isFound = true;
msg = "You found the treasure!";
gameOver = true;
break;
//......If weapon found, set flag to show player has weapon
case WEAPON:
player.hasWeapon = true;
weapon.isFound = true;
msg = "You have a weapon.";
break;
//
//......If noise-maker found, set flag to show player has noise-maker
case NOISEMAKER:
player.hasNoisemaker = true;
noisemaker.isFound = true;
msg = "You found a noisemaker.";
break;
//......If torch found, set flag to show player has torch
case TORCH:
player.hasTorch = true;
torch.isFound = true;
msg = "Let there be light! You found a torch.";
weapon.isVisible = true;
treasure.isVisible = true;
noisemaker.isVisible = true;
monster.isVisible = true;
break;
//
//......If monster found
case MONSTER:
if (!monster.isFound)
{
msg = "You have found the monster";
//.......Resolve combat
if (player.hasWeapon)
{
msg = "You have slain the monster";
monster.isFound = true;
}
else
{
gameOver = true;
player.alive = false;
msg = "The monster has killed you";
}
}
break;
}
//.....Move Player
if (movePlayer)
{
cave[player.position.row][player.position.column] = EMPTY; //updates position information
cave[row][column] = PLAYER;
board[player.position.row + 2][player.position.column + 2] = MT; //clear the screen where player was
player.position.row = row;
player.position.column = column;
}
movePlayer = true;
//.....Process Monster
if (!monster.isFound)
{
//
if (monsterPause)
{
// Monster paused. Check if Monster starts moving again
if (rand() % 3 == 1)
{
monsterPause = false;
}
}
else
{
//
//......Move Monster
MonsterMoveCounter = (++MonsterMoveCounter) % 8;
row = monster.row;
column = monster.column;
switch (MonsterMoveCounter)
{
case 0:
column++;
break;
case 1:
row--;
break;
case 2:
row--;
break;
case 3:
column--;
break;
case 4:
column--;
break;
case 5:
row++;
break;
case 6:
row++;
break;
case 7:
column++;
break;
default:
break;
}
//......Check for events
//.......If player found
if (cave[row][column] == PLAYER)
{
msg = "The monster has found you";
//........Resolve combat
//
if (player.hasWeapon)
{
msg = "The monster found you but was slain";
monster.isFound = true;
}
else
{
//
gameOver = true;
player.alive = false;
msg = "The monster found you and you have died";
}
//
}
else
{
//........Move Monster
//cave[monster.row][monster.column] = hold; // reveal what is under the monster
cave[monster.row][monster.column] = EMPTY; // clear the cave location
board[monster.row + 2][monster.column + 2] = MT; // clear the screen behind monster
//hold = cave[row][column]; // save what the monster is about to move over
monster.row = row; // update monster's row
monster.column = column; // update monster's column
cave[row][column] = MONSTER; // change monster's location in the cave
}
}//-add else clause in week4
}
}
}
//...End Game Loop
//**End Game**************
//...Provide end win/loss message
cout << msg.c_str() << endl;
if (player.alive)
{
if (player.hasTreasure) msg = "You are alive and rich!";
else msg = "You didn't get the treasure, but you live to seek adventure another day.";
}
else
{
msg = "RIP Adventurer";
}
cout << msg.c_str() << endl;
//...Do clean-up
//...Quit
return 0;
}
//--------------------------------------------------------------------------------
//================================================================================
gameObject placeInCave(gameObjectType array[TOTAL_ROWS][TOTAL_COLS])
{
int r, c;
gameObject obj;
do
{
r = rand() % 8;
c = rand() % 8;
} while (array[r][c] != EMPTY);
obj.row = r;
obj.column = c;
obj.isFound = false;
return obj;
}
//
bool checkVisible(gameObject x, gameObject y, int dist)
{
if ((abs(x.row - y.row) < dist && (abs(x.column - y.column) < dist))) return true;
return false;
}
//
bool showOnBoard(gameObject x)
{
return ((x.isVisible) && (!x.isFound));
}
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