C++ Programming: (Please help me!! Thank you!!)
Problem A: Win SimUniversity (20 points) Currently there is not way to “win” the game.
Modify the SimUniversity.cpp file to have a “hours until graduation” variable that starts at 48
(you may change this number for testing, but ensure you the code you submit has it at 48). In
addition to the normal output, give the time left to graduate as shown in the example below.
When you have graduated, end the game and tell them they have won. If you are expected to
graduate right when you would normally lose the game, you should graduate anyways and win
the game (see 2nd example). Please ensure you follow the sample output exactly
This is my program named SimUniversity.cpp to modify (you copy and paste to C++ to modify)
This is an example that I should get
Example 1 (user input is bold, board is not displayed because it is too big... do not change it):
Current stats are: Energy = 70, Entertainment = 70, Smartness = 70.
Current time: 8:00. You are at the Dorm. Do you want to (S)leep, do (H)omeowrk or (W)atch
some YouTube?
Do you want to (G)o to a different location?
g
Where do you want to travel to: (O, U, D)?
u
You go to University.
From your actions changed your stats by... Energy: -4, Entertainment: 0, Smartness: 0.
You have 47 hours left until graduation...
(Press enter to advance an hour)
Current stats are: Energy = 66, Entertainment = 70, Smartness = 70.
Current time: 9:00.
You are at the University. Do you want to (A)ttend class or (S)leep through lecture? Do you
want to (G)o to a different location?
a
You take copious notes and pay close attention to the material. From your actions changed your
stats by... Energy: - 4, Entertainment: - 8, Smartness: 3.
You have 46 hours left until graduation...
(Press enter to advance an hour)
... (skipping some time, this line is not actually output!) Current stats are: Energy = 67,
Entertainment = 50, Smartness = 73. Current time: 9:00.
You are at the Dorm.
Do you want to (S)leep, do (H)omeowrk or (W)atch some YouTube?
Do you want to (G)o to a different location?
h
You break out the books and pound through some problems. From your actions changed your
stats by... Energy: - 3, Entertainment: -20, Smartness: 3. You have 1 hours left until graduation...
(Press enter to advance an hour)
Current stats are: Energy = 64, Entertainment = 30, Smartness = 76.
Current time: 10:00.
You are at the Dorm.
Do you want to (S)leep, do (H)omeowrk or (W)atch some YouTube?
Do you want to (G)o to a different location?
w
OMG!! CATS!!!! From your actions changed your stats by... Energy: - 2, Entertainment: 20,
Smartness: - 1. You have 0 hours left until graduation...
You graduate from college.
(Press enter to advance an hour)
Solution
#include
#include
using namespace std;
class Room {
private:
// letters outline where the location is
char mapLetter;
// locations are rectangles....
int topLeftX;
int topLeftY;
int bottomRightX;
int bottomRightY;
// all good things have a name, right?
str.
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
1. C++ Programming: (Please help me!! Thank you!!)
Problem A: Win SimUniversity (20 points) Currently there is not way to “win” the game.
Modify the SimUniversity.cpp file to have a “hours until graduation” variable that starts at 48
(you may change this number for testing, but ensure you the code you submit has it at 48). In
addition to the normal output, give the time left to graduate as shown in the example below.
When you have graduated, end the game and tell them they have won. If you are expected to
graduate right when you would normally lose the game, you should graduate anyways and win
the game (see 2nd example). Please ensure you follow the sample output exactly
This is my program named SimUniversity.cpp to modify (you copy and paste to C++ to modify)
This is an example that I should get
Example 1 (user input is bold, board is not displayed because it is too big... do not change it):
Current stats are: Energy = 70, Entertainment = 70, Smartness = 70.
Current time: 8:00. You are at the Dorm. Do you want to (S)leep, do (H)omeowrk or (W)atch
some YouTube?
Do you want to (G)o to a different location?
g
Where do you want to travel to: (O, U, D)?
u
You go to University.
From your actions changed your stats by... Energy: -4, Entertainment: 0, Smartness: 0.
You have 47 hours left until graduation...
(Press enter to advance an hour)
Current stats are: Energy = 66, Entertainment = 70, Smartness = 70.
Current time: 9:00.
You are at the University. Do you want to (A)ttend class or (S)leep through lecture? Do you
want to (G)o to a different location?
a
You take copious notes and pay close attention to the material. From your actions changed your
stats by... Energy: --4, Entertainment: --8, Smartness: 3.
You have 46 hours left until graduation...
(Press enter to advance an hour)
... (skipping some time, this line is not actually output!) Current stats are: Energy = 67,
Entertainment = 50, Smartness = 73. Current time: 9:00.
You are at the Dorm.
Do you want to (S)leep, do (H)omeowrk or (W)atch some YouTube?
2. Do you want to (G)o to a different location?
h
You break out the books and pound through some problems. From your actions changed your
stats by... Energy: --3, Entertainment: --20, Smartness: 3. You have 1 hours left until graduation...
(Press enter to advance an hour)
Current stats are: Energy = 64, Entertainment = 30, Smartness = 76.
Current time: 10:00.
You are at the Dorm.
Do you want to (S)leep, do (H)omeowrk or (W)atch some YouTube?
Do you want to (G)o to a different location?
w
OMG!! CATS!!!! From your actions changed your stats by... Energy: --2, Entertainment: 20,
Smartness: --1. You have 0 hours left until graduation...
You graduate from college.
(Press enter to advance an hour)
Solution
#include
#include
using namespace std;
class Room {
private:
// letters outline where the location is
char mapLetter;
// locations are rectangles....
int topLeftX;
int topLeftY;
int bottomRightX;
int bottomRightY;
// all good things have a name, right?
string roomName;
// constructor to make a room! need all the above information
public:
Room();
Room(string name, char symbol, int tlx, int tly, int brx, int bry);
3. // Below: get functions to get all the information about a room
string getName();
char getLetter();
int getTopLeftX();
int getTopLeftY();
int getBottomRightX();
int getBottomRightY();
};
Room::Room()
{
roomName="The Void";
mapLetter='?';
topLeftX=-1;
topLeftY=-1;
bottomRightX=-1;
bottomRightY=-1;
}
Room::Room(string name, char symbol, int tlx, int tly, int brx, int bry)
{
roomName=name;
mapLetter=symbol;
topLeftX=tlx;
topLeftY=tly;
bottomRightX=brx;
bottomRightY=bry;
}
string Room::getName()
{
return roomName;
}
char Room::getLetter()
{
return mapLetter;
}
int Room::getTopLeftX()
{
4. return topLeftX;
}
int Room::getTopLeftY()
{
return topLeftY;
}
int Room::getBottomRightX()
{
return bottomRightX;
}
int Room::getBottomRightY()
{
return bottomRightY;
}
/* ABOVE ROOM */
/* BELOW MAP */
class Map {
private:
// size of the world
static const int MAX_ROWS = 20;
static const int MAX_COLUMNS = 40;
// characters for blank space and you, the player
static const char BLANK = ' ';
static const char PLAYER = 'Y';
// stores all the characters on the world
char grid[MAX_ROWS][MAX_COLUMNS];
// list of the possible locations to go to
Room mapRooms[100];
int usedRooms;
// current place you are
Room currentLocation;
void generateMap();
public:
5. Map();
void addRoom(Room r);
void displayMap(); // this simply shows the map on the screen...
Room getPlayerLocation();
int getLocations(Room changeMe[100]); // copies rooms into changeMe, returns the size
void moveToRoom(Room destination);
};
Map::Map()
{
usedRooms = 0;
// initialize the map to be blank
for(int i=0; i < MAX_ROWS; i++)
{
for(int j=0; j < MAX_COLUMNS; j++)
{
grid[i][j] = BLANK;
}
}
// make 3 rooms
Room outside("Outside", 'O', 0,0, 39,19);
Room university("University", 'U', 5,5, 15,15);
Room dorm("Dorm", 'D', 25,5, 30,10);
// add them to our arrayList
addRoom(outside);
addRoom(university);
addRoom(dorm);
// start out in your room
currentLocation = dorm;
moveToRoom(currentLocation);
// generate the map (rooms)
generateMap();
}
void Map::addRoom(Room r)
{
6. mapRooms[usedRooms] = r;
usedRooms++;
}
void Map::generateMap()
{
// for all the rooms we have in our list...
for(int i=0; i < usedRooms; i ++)
{
Room place = mapRooms[i];
// Fill in top and bottom of rectangle with characters
for(int i = place.getTopLeftX(); i <= place.getBottomRightX(); i++)
{
grid[place.getTopLeftY()][i] = place.getLetter();
grid[place.getBottomRightY()][i] = place.getLetter();
}
// fill in left and right side...
for(int i = place.getTopLeftY(); i <= place.getBottomRightY(); i++)
{
grid[i][place.getTopLeftX()] = place.getLetter();
grid[i][place.getBottomRightX()] = place.getLetter();
}
}
}
// this simply shows the map on the screen...
void Map::displayMap()
{
for(int i=0; i < MAX_ROWS; i++)
{
for(int j=0; j < MAX_COLUMNS; j++)
{
cout << " " << grid[i][j];
}
cout << endl;
}
}
// get where we are
7. Room Map::getPlayerLocation()
{
return currentLocation;
}
// get the possible locations to go to
int Map::getLocations(Room changeMe[100])
{
for(int i=0; i < usedRooms; i++)
{
changeMe[i] = mapRooms[i];
}
return usedRooms;
}
// update the map so the player symbol moves to the a new location
void Map::moveToRoom(Room destination)
{
// if were somewhere before
if(currentLocation.getTopLeftY() != -1) // if this is false, the room does not exist
{
// remove the mark from the center of the previous location
grid[currentLocation.getTopLeftY() + (currentLocation.getBottomRightY() -
currentLocation.getTopLeftY())/2]
[currentLocation.getTopLeftX() +(currentLocation.getBottomRightX() -
currentLocation.getTopLeftX())/2]
= BLANK;
}
// update where we are
currentLocation = destination;
// add player mark to center of new location
grid[currentLocation.getTopLeftY() + (currentLocation.getBottomRightY() -
currentLocation.getTopLeftY())/2]
[currentLocation.getTopLeftX() + (currentLocation.getBottomRightX() -
currentLocation.getTopLeftX())/2]
= PLAYER;
8. }
class Player
{
private:
// character has 3 stats: entertainment, energy and smartness
Map world;
int entertainment;
int energy;
int smartness;
int totalHours;
public:
Player();
Player(Map world);
int removeSmartness(int amount);
void doAction(char action, int time);
int addSmartness(int amount);
int removeEnergy(int amount);
int getSmartness();
int addEnergy(int amount);
int getEnergy();
int removeEntertainment(int amount);
int addEntertainment(int amount);
int getEntertainment();
void displayStats();
void travel();
char requestAction();
void displayMap();
void decrementHour();
};
// Tell them where they are and what actions they can take (and ask them as the method says)
char Player::requestAction()
{
// get where you are
Room location = world.getPlayerLocation();
9. // display where you are
cout << "You are at the " + location.getName() + ". ";
// if at uni
if(location.getName().compare("University") == 0)
{
cout << "Do you want to (A)ttend class or (S)leep through lecture? ";
}
// if at dorm
else if(location.getName().compare("Dorm") == 0)
{
cout << "Do you want to (S)leep, do (H)omeowrk or (W)atch some YouTube? ";
}
// if outside
else if(location.getName().compare("Outside") == 0)
{
cout << "Do you want to (S)ocialize or (P)lay Rugby? ";
}
// if you are not in any of the above, we have a problem
else
{
cout << "You are lost in the abyss... ";
}
// they can also change locations by 'G'
cout << "Do you want to (G)o to a different location? ";
// let them enter a choice
string answer;
getline(cin,answer);
// if they entered something, give back the first character (capitalized)
if(answer.length() > 0)
{
return toupper(answer[0]);
}
// otherwise they just hit enter (trying to crash me!), so they do nothing
else
{
return ' ';
10. }
}
// chage locations
void Player::travel()
{
// destionations the names of possible destinations
Room destinations[100];
// to do this, we need to copy in each name manually
int roomCount = world.getLocations(destinations);
// show the possible destinations
cout << "Where do you want to travel to: (";
if(roomCount>0)
{
cout << destinations[0].getLetter();
}
for(int i=1; i < roomCount; i++)
{
cout << ", " << destinations[i].getLetter();
}
cout <<")? ";
// read from the keyboard where they want to go
string response;
getline(cin, response);
// if they didn't enter anything (shame on them) they go nowhere
if(response.length() == 0)
{
cout << "You go nowhere... ";
return;
}
// otherwise pull out the first character
char r = toupper(response[0]);
// find the room that matches this character
Room destination;
for(int i=0; i < roomCount; i++)
{
11. Room place = destinations[i];
if(place.getLetter() == r)
{
destination = place;
}
}
// if we didnt find the room, they entered a bad character!
if(destination.getBottomRightX() == -1 ) // not a valid room
{
cout << "Invalid location, you go nowhere... ";
}
// otherwise go to that location
else
{
cout << "You go to " + destination.getName() + ". ";
world.moveToRoom(destination);
}
}
// display stats... yes what the methods says it does...
void Player::displayStats()
{
cout << "Current stats are: ";
cout << "Energy = " << energy <<", ";
cout << "Entertainment = " << entertainment <<", ";
cout << "Smartness = " << smartness <<", ";
cout << "Current hours left = "< 0:00)
dayTime = (dayTime+1)%24;
}
bool updatePlayer()
{
// default: keep on player
bool gameOver = false;
// all work and no play...
if(player.getEntertainment() == 0)
{
12. cout << "Your life is sapped of color and you become a mindless cog in the population. ";
gameOver = true;
}
// dum dum dummmm!
else if(player.getSmartness() == 0)
{
cout << "You drop out of college and are forced to drive taxis for the rest of your life. ";
gameOver = true;
}
return gameOver;
}
// hack!
void clearScreen()
{
cout << " ";
}
int main()
{
// hey! its a map
Map world;
// player needs to know about the map
player = Player(world);
// not over yet!
bool gameOver = false;
while(!gameOver)
{
// methods names should be informative enough
clearScreen();
player.displayMap();
player.displayStats();
displayTime();
// get the action after showing a list of options
char action = player.requestAction();
cout << " ";
// find out the effects of that action
player.doAction(action, dayTime);
13. // process what happened and end the game if we lose
gameOver = updatePlayer();
// wait so the person can
cout << "(Press enter to advance an hour)";
string temp;
getline(cin, temp);
}
// ruh roh
cout << "Game Over! ";
return 0;
}