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, 6 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
Wondering if you could help me again haha.
Â
I need to implement one additional game feature. I would like to put a Pause "P" action in there and I think the code that I have is right (below). The code below that is what I will be putting it into. I think it would go after the case 'q' but i am not sure. I also would like to put a begin with a "b" where I paused. Can you show me if I am correct?
Â
I know I also need to change some code in the header but not sure where
Â
Thanks again!
Â
Â
case 'p':
cout<<"The game is PausednPress 'b' to begin the game.n";
char ch;
do
{
ch =_getch();
}while(ch!='b');
system("cls");
Â
HEADER OF CODE
Â
#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
};
Â
Â
MAIN CCP OF CODE
Â
// GSP115 TREASURE CAVE
// Week 6
#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;
// <WK6 status=permanent>
// Combat Result Constants
const int PLAYER_DIES_WITH_WEAPON = 80;
const int MONSTER_DIES_WITH_WEAPON = 30;
const int PLAYER_DIES_NO_WEAPON = 20;
const int MONSTER_DIES_NO_WEAPON = 18;
// <WK6>
//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);
// <WK6 status=permanent>
bool resolveCombat(playerObject &p, gameObject &m, int YouDie, int MonsterDies, string &str, bool &moveP, bool &mPause);
void setcolor(int c);
void gamecolor(unsigned char type);
// </WK6>
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}
};
// <WK6 status=permanent>
gameObjectType hold = EMPTY; // Holds objects under the monster<bug fix>
// </WK6>
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
// <WK6 status=permanent>
gameObject caveExit = {-1, -1, false, true}; // the cave exit
// </WK6>
int row, column; // temporarily hold the new player position
int MonsterMoveCounter = 6; // track and control monster movement around treasure
// <WK6 status=permanent>
int killedValue = PLAYER_DIES_NO_WEAPON; // a random value above this level means you are killed by the monster
int killMonsterValue = MONSTER_DIES_NO_WEAPON; // a random value above this level and below killedValue means you kill the monster
// </WK6>
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;
// <WK6 status=permanent>
//...Add Cave Exit in any empty location.
caveExit = placeInCave(cave);
cave[caveExit.row][caveExit.column] = CAVE_EXIT;
// </WK6>
//**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);
// <WK6 status=permanent>
caveExit.isVisible = checkVisible(player.position, caveExit, 2);
// </WK6>
}
//.....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;
// <WK6 status=permanent>
board[caveExit.row+2][caveExit.column+2] = showOnBoard(caveExit)?XSymbol:MT;
// </WK6>
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)
{
// <WK6 status=permanent>
gamecolor(C);
// </WK6>
cout << C;
}
cout << endl;
}
// <WK6 status=permanent>
gamecolor('Z');
// </WK6>
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?n";
break;
default:
movePlayer = false;
break;
}
//....Check if the game is over
if (!gameOver)
{
//..... Check for events
switch (cave[row][column])
{
//......If exit found, set flag to exit game
// <WK6 status=permanent>
case CAVE_EXIT:
gameOver = true;
msg = "You exit the cave.";
break;
// </WK6>
//......If treasure found, set flag to show player has treasure
case TREASURE:
player.hasTreasure = true;
treasure.isFound = true;
msg = "You found the treasure!";
break;
//......If weapon found, set flag to show player has weapon
case WEAPON:
player.hasWeapon = true;
weapon.isFound = true;
msg = "You have a weapon.";
// <WK6 status=permanent>
killedValue = PLAYER_DIES_WITH_WEAPON;
killMonsterValue = MONSTER_DIES_WITH_WEAPON;
// </WK6>
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.";
// <WK6 status=permanent>
caveExit.isVisible = true;
// </WK6>
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
// <WK6 status=permanent>
gameOver = resolveCombat(player, monster, killedValue, killMonsterValue,msg, movePlayer, monsterPause);
// </WK6>
}
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
// <WK6 status=permanent>
gameOver = resolveCombat(player, monster, killedValue-2, killMonsterValue-2,msg, movePlayer, monsterPause);
// </WK6>
}
else
{
//........Move Monster
// <WK6 status=permanent>
cave[monster.row][monster.column] = hold; // reveal what is under the monster <bug fix in week 6>
// </WK6>
board[monster.row+2][monster.column+2] = MT; // clear the screen behind monster
// <WK6 status=permanent>
hold = cave[row][column]; // save what the monster is about to move over <bug fix in week 6>
// </WK6>
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
}
}
}
}
}
//...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));
}
// <WK6 status=permanent>
bool resolveCombat(playerObject &p, gameObject &m, int YouDie, int MonsterDies, string &str, bool &moveP, bool &mPause)
{
int combatRoll = rand()%100;
//........Killed by Monster
if (combatRoll > YouDie)
{
str += " and you have been eaten by a Grue!nGAME OVER!";
moveP = false;
p.position.isVisible = false;
p.alive = false;
return true;
}
//........Kill Monster
else if (combatRoll > MonsterDies)
{
if (p.hasWeapon) str += " and you slay the monster with your weapon.";
else str += " , but--against all odds--you actually killed the monster with your bare hands.";
m.isFound = true;
return false;
}
//........Survive Attack
else
{
str += " and you managed to survive the monster's attack.";
moveP = false;
mPause = true;
return false;
}
}
// </WK6>
// <WK6 status=permanent>
void gamecolor(unsigned char type)
{
switch(type){
case 201: case 205: case 200: case 188: case 186: case 187:
setcolor(FOREGROUND_RED);
break;
case 'W':
setcolor(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
break;
case '$':
setcolor(FOREGROUND_GREEN|FOREGROUND_INTENSITY);
break;
case 'P':
setcolor(FOREGROUND_BLUE|FOREGROUND_INTENSITY|BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_INTENSITY);
break;
case 'T':
setcolor(FOREGROUND_RED|FOREGROUND_INTENSITY);
break;
case 'X':
setcolor(BACKGROUND_BLUE|BACKGROUND_GREEN);
break;
case 'M':
setcolor(FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
break;
case 'N':
setcolor(FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
break;
default:
setcolor(FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED);
}
}
// </WK6>
// <WK6 status=permanent>
void setcolor(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
// </WK6
----------- Â ----------- 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