The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 313 Weeks Ago, 5 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Would you be able to help me again with another problem? I need to find bugs or errors and figure out a way to make them simpler.
Â
Week 5 Overview
We will 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.
Week 5 Activity
This week, you are to 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.
Â
this is the ccp file
Â
// GSP115 TREASURE CAVE
// Week 4
#pragma once
#include "GSP115_Course_Project.h"
// 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]);
// <WK4 status=permanent>
bool checkVisible(gameObject x, gameObject y, int dist );
// </WK4>
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
// <WK4 status=permanent>
gameObject torch = {-1, -1, false, true}; // the torch
gameObject noisemaker = {-1, -1, false, true}; // the noisemaker
// </WK4>
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
// <WK4 status=permanent>
bool monsterPause = false; // flag to indicate the monster has stopped moving
// </WK4>
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;
// <WK4 status=permanent>
//...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;
// </WK4>
//**Play Game*************
//...Begin Game Loop
while (!gameOver)
{
//....Display Board
// <WK4 status=permanent>
//.....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);
}
// </WK4>
//.....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;
// <WK4 status=permanent>
board[torch.row+2][torch.column+2] = showOnBoard(torch)?TOSymbol:MT;
// </WK4>
board[treasure.row+2][treasure.column+2] = showOnBoard(treasure)?TRSymbol:MT;
// <WK4 status=permanent>
board[noisemaker.row+2][noisemaker.column+2] = showOnBoard(noisemaker)?NSymbol:MT;
// </WK4>
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;
// <WK4 status=permanent>
case 'n':
if (player.hasNoisemaker)
{
msg = "You make an irritating noise!";
monsterPause = true;
}
else
{
msg = "You make a feeble whimper.";
}
break;
// </WK4>
case 'q':
gameOver = true;
msg = "Quitting?n";
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;
// <WK4 status=permanent>
//......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;
// </WK4>
//......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)
{
// <WK4 status=permanent>
if (monsterPause)
{
// Monster paused. Check if Monster starts moving again
if (rand()%3 == 1)
{
monsterPause = false;
}
}
else
{
// </WK4>
//......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
// <WK3 status=5>
if (player.hasWeapon)
{
msg = "The monster found you but was slain";
monster.isFound = true;
}
else
{
// <WK2 status=5>
gameOver = true;
player.alive = false;
msg = "The monster found you and you have died";
}
// </WK2>
}
else
{
//........Move Monster
//cave[monster.row][monster.column] = hold; // reveal what is under the monster <bug fix in week 6>
cave[monster.row][monster.column] = EMPTY; // clear the cave location <creates bug>
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 <bug fix in week 6>
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;
}
// <WK4 status=permanent>
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;
}
// </WK4>
bool showOnBoard(gameObject x)
{
return ((x.isVisible)&&(!x.isFound));
}
and this is the header file
Â
#pragma once
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
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
};
Â
I would greatly appriciate it if you help me.
Â
Thanks
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly