Permalink
Browse files

Fixed bug, setLocation is now working. Next Step If statements

  • Loading branch information...
1 parent a895a27 commit 0f6ce0b25acdcd181d55972614bde16bfb03addc @GeorgePhillips GeorgePhillips committed May 22, 2011
View
29 #Item.h#
@@ -1,29 +0,0 @@
-#ifndef ITEM_H_
-#define ITEM_H_
-#include <string>
-#include <map>
-#include <iostream>
-using namespace std;
-
-/* Item class stores all information relevant to a particular item object */
-class Item {
- string name;
- string description;
- map<string, string> verb_expressions;
-public:
- Item(string name, string description, map<string, string>);
- Item();
- string getName(void);
- void setName(string name);
- string getDescription(void);
- void setDescription(string description);
- void addVerb(string verb, string expression);
- void removeVerb(string verb);
- void printVerb(void);
- string getVerbExpression(string verb);
- string print
- ~Item();
-};
-
-
-#endif
View
18 Compiler.cpp
@@ -37,6 +37,7 @@ void Compiler::Compile() {
#include <string>\n\
#include <sstream>\n\
#include \"Location.h\"\n\
+#include \"Item.h\"\n\
#include \"Player.h\"\n\
using namespace std;\n\
#define WELCOME_MESSAGE \"" + parser->initialDescription + "\"\n\
@@ -91,7 +92,7 @@ int main(int argc, char **argv) {\n\
map<string, Item*>::iterator objects;
for (objects = parser->items.begin(); objects != parser->items.end(); objects++) {
output += "Item " + objects->first + "(\"" + objects->second->getName() + "\", \"" + objects->second->getDescription() + "\");\n";
- output += objects->first + ".setLocation(&" + objects->second->getLocation()->getVariableName() + ");\n";
+ output += "// " +objects->first + ".setLocation(&" + objects->second->getLocation()->getVariableName() + ");\n";
output += objects->second->getLocation()->getVariableName() + ".addItem(\"" + objects->second->getName() + "\", " + objects->first + ");\n";
}
@@ -147,6 +148,11 @@ int main(int argc, char **argv) {\n\
// DIRECTIONS
output += "\n\
+ if ((verb == \"i\") || (verb == \"inventory\") || (verb == \"invent\")) {\n\
+ cout << " + parser->player->getVariableName() + "->getInventory()->listItems();\n\
+ cout << " + parser->player->getVariableName() + "->getNumberOfItems() << \"/\" << " + parser->player->getVariableName() + "->getMaxItems() << endl;\n\
+ goto main_loop;\n\
+ }\n\n\
if(verb==\"north\") {\n\
if (!" + parser->player->getVariableName() + "->getLocation()->hasNorth()) {\n\
cout << \"Sorry you can not go North\" << endl;\n\
@@ -182,7 +188,7 @@ int main(int argc, char **argv) {\n\
" cout << \"Sorry you can not go East\" << endl;\n\
goto main_loop;\n\
} else {\n\
- player->setLocation(" + parser->player->getVariableName() + "->getLocation()->getEast());\n\
+ " + parser->player->getVariableName() + "->setLocation(" + parser->player->getVariableName() + "->getLocation()->getEast());\n\
cout << " + parser->player->getVariableName() + "->getLocation()->getDescription() << endl;\n\
goto main_loop;\n\
}\n\
@@ -306,14 +312,16 @@ string Compiler::CompileNounVerb(Item *item) {
}
if (location == "") {
if (line.find(parser->player->getVariableName()) < line.length()) {
- location = parser->player->getVariableName() + ".getInventory()";
- break;
+ location = parser->player->getVariableName() + "->getInventory()";
+ } else if (line.find("currentLocation") < line.length()) {
+ location = parser->player->getVariableName() + "->getLocation()";
}
}
if (location == "" || item == "") {
cerr << "Unreadable setLocation Command: \"" << line << "\"" << endl;
} else {
- output += location + "->addItem(" + item + "->getName(), " + item + ")\n";
+ cerr << location << "->addItem(" << item << ".getName(), " << item << ");\n";
+ output += location + "->addItem(" + item + ".getName(), " + item + ");\n";
}
} else {
cerr << "Unreadable/Unknown Command: \"" << line << "\"" << endl;
View
3 Location.h
@@ -10,9 +10,8 @@
#include <iostream>
#include <cstdlib>
#include <map>
-
using namespace std;
-
+class Item;
/* Location class will store all information relevant to a particular location*/
class Location {
public:
View
3 output/Item.h
@@ -10,9 +10,10 @@
#include <string>
#include <iostream>
-#include "Location.h"
using namespace std;
+class Location;
+
/* Item class will store all relevant information for a particular item */
class Item {
Location* location;
View
BIN output/Item.h.gch
Binary file not shown.
View
15 output/Location.cpp
@@ -14,16 +14,6 @@ Location::Location() {
this->east = NULL;
}
-/* Constructor */
-Location::Location(string name, string description, map<string, Item> items) {
- this->name = name;
- this->description = description;
- this->items = items;
- this->north = NULL;
- this->south = NULL;
- this->west = NULL;
- this->east = NULL;
-}
/* Constructor */
Location::Location(string name, string description) {
@@ -75,6 +65,11 @@ bool Location::hasItem(string item_name) {
return this->items.count(item_name) != 0;
}
+/* Checks to see if item is in this location */
+int Location::getItemCount() {
+ return this->items.size();
+}
+
/* Removes an item from the room */
void Location::removeItem(string item_name) {
this->items.erase(item_name);
View
2 output/Location.h
@@ -25,7 +25,6 @@ class Location {
map<string, Item> items;
public:
// Constructors
- Location(string, string, map<string, Item>);
Location(string, string);
Location();
@@ -53,6 +52,7 @@ class Location {
/* This set of methods will deal with Items stored in this location */
void addItem(string item_name, Item item);
+ int getItemCount();
bool hasItem(string item_name);
void removeItem(string item_name);
string listItems(void);
View
BIN Player.h.gch → output/Location.h.gch
Binary file not shown.
View
BIN output/Play
Binary file not shown.
View
6 output/Player.cpp
@@ -29,12 +29,8 @@ void Player::setDescription(string description) {
}
/* Returns the number of items currently held by the player */
-int Player::getNumberOfItems(void) {return current_number_of_items;}
+int Player::getNumberOfItems(void) {return inventory->getItemCount();}
-/* Sets the number of items that the player currently has */
-void Player::setNumberOfItems(int number_of_items) {
- current_number_of_items = number_of_items ;
-}
/* Returns the max number of items the player can hold */
int Player::getMaxItems(void) {return max_items;}
View
4 output/Player.h
@@ -19,7 +19,6 @@ class Player {
string name;
string description;
int max_items;
- int current_number_of_items;
Location* current_location;
Location* inventory;
public:
@@ -44,9 +43,6 @@ class Player {
/* Returns the number of items currently held by the player */
int getNumberOfItems(void);
- /* Sets the number of items that the player currently has */
- void setNumberOfItems(int number_of_items);
-
/* Returns the max number of items the player can hold */
int getMaxItems(void);
View
201 output/compiled_game.cpp
@@ -3,145 +3,118 @@
#include <string>
#include <sstream>
#include "Location.h"
+#include "Item.h"
#include "Player.h"
using namespace std;
#define WELCOME_MESSAGE "Ed Is a Fucking homo"
#define QUIT_GAME "quit"
-
int main(int argc, char **argv) {
string username;
string command;
string verb;
string noun;
string prompt;
int count;
+
+Location kitchen("Kitchen", "A kitchen is a room or part of a room used for cooking and food preparation. In the West, a modern residential kitchen is typically equipped with a stove, a sink with hot and cold running water, a refrigerator and kitchen cabinets arranged according to a modular design. Many households have a microwave oven, a dishwasher and other electric appliances. The main function of a kitchen is cooking or preparing food but it may also be used for dining and entertaining.");
+Location livingRoom("The Living Room", "The Living Room");
- Location kitchen("Kitchen", "A kitchen is a room or part of a room used for cooking and food preparation. In the West, a modern residential kitchen is typically equipped with a stove, a sink with hot and cold running water, a refrigerator and kitchen cabinets arranged according to a modular design. Many households have a microwave oven, a dishwasher and other electric appliances. The main function of a kitchen is cooking or preparing food but it may also be used for dining and entertaining.");
- Location livingRoom("The Living Room", "The Living Room");
+kitchen.setNorth(&livingRoom);
+livingRoom.setSouth(&kitchen);
+Location inventory("Inventory", "Description of the inventory");Player* mother = new Player();mother->setLocation(&livingRoom);mother->setInventory(&inventory);mother->setMaxItems(8);Item television("Television", "A small television placed in the corner of the kitchen. It looks broken");
+// television.setLocation(&kitchen);
+kitchen.addItem("Television", television);
+cout << WELCOME_MESSAGE << endl;prompt = ">>> ";cout << mother->getLocation()->getDescription() << endl;while (true) { main_loop: cout << prompt; getline(cin, command); istringstream word(command); verb = ""; noun = ""; count = 0; while (word) { if (count > 2) { cout << "I do not understand your command. Enter 2 words at most, a verb followed by a noun" << endl; goto main_loop; } if (count == 0) { word >> verb; } else { word >> noun; } count++; } if (command == QUIT_GAME) {
+ quit_loop:
+ string quit = "";
+ cout << "Do you really want to quit?: [y]or[n] "; cout.flush();
+ cin >> quit;
+ cin.ignore();
+ if (quit == "y") {
+ cout << "Thanks for playing!" << endl;
+ break;
+ } else if (quit == "n") {
+ goto main_loop;
+ } else {
+ goto quit_loop;
+ }
+ } if (count == 2) {
- kitchen.setNorth(&livingRoom);
- livingRoom.setSouth(&kitchen);
- Location inventory("Inventory", "Description of the inventory");
- Player* mother = new Player();
- mother->setLocation(&livingRoom);
- mother->setInventory(&inventory);
- mother->setMaxItems(8);
- Item television("Television", "A small television placed in the corner of the kitchen. It looks broken");
- television.setLocation(kitchen);
- kitchen.addItem("Television", television);
- cout << WELCOME_MESSAGE << endl;
- prompt = ">>> ";
- cout << mother->getLocation()->getDescription() << endl;
- while (true) {
- main_loop:
- cout << prompt;
- getline(cin, command);
- istringstream word(command);
- verb = "";
- noun = "";
- count = 0;
- while (word) {
- if (count > 2) {
- cout << "I do not understand your command. Enter 2 words at most, a verb followed by a noun" << endl;
+ if ((verb == "i") || (verb == "inventory") || (verb == "invent")) {
+ cout << mother->getInventory()->listItems();
+ cout << mother->getNumberOfItems() << "/" << mother->getMaxItems() << endl;
goto main_loop;
- }
- if (count == 0) {
- word >> verb;
- } else {
- word >> noun;
- }
- count++;
}
- if (command == QUIT_GAME) {
-quit_loop:
- string quit = "";
- cout << "Do you really want to quit?: [y]or[n] ";
- cout.flush();
- cin >> quit;
- cin.ignore();
- if (quit == "y") {
- cout << "Thanks for playing!" << endl;
- break;
- } else if (quit == "n") {
+
+ if(verb=="north") {
+ if (!mother->getLocation()->hasNorth()) {
+ cout << "Sorry you can not go North" << endl;
goto main_loop;
} else {
- goto quit_loop;
+ mother->setLocation(mother->getLocation()->getNorth());
+ cout << mother->getLocation()->getDescription() << endl;
+ goto main_loop;
}
}
- if (count == 2) {
-
- if (verb == "north") {
- if (!mother->getLocation()->hasNorth()) {
- cout << "Sorry you can not go North" << endl;
- goto main_loop;
- } else {
- mother->setLocation(mother->getLocation()->getNorth());
- cout << mother->getLocation()->getDescription() << endl;
- goto main_loop;
- }
- }
- if (verb == "south") {
- if (!mother->getLocation()->hasSouth()) {
- cout << "Sorry you can not go South" << endl;
- goto main_loop;
- } else {
- mother->setLocation(mother->getLocation()->getSouth());
- cout << mother->getLocation()->getDescription() << endl;
- goto main_loop;
- }
+ if(verb=="south") {
+ if (!mother->getLocation()->hasSouth()) {
+ cout << "Sorry you can not go South" << endl;
+ goto main_loop;
+ } else {
+ mother->setLocation(mother->getLocation()->getSouth());
+ cout << mother->getLocation()->getDescription() << endl;
+ goto main_loop;
}
+ }
- if (verb == "west") {
- if (!mother->getLocation()->hasWest()) {
- cout << "Sorry you can not go West" << endl;
- goto main_loop;
- } else {
- mother->setLocation(mother->getLocation()->getWest());
- cout << mother->getLocation()->getDescription() << endl;
- goto main_loop;
- }
- }
- if (verb == "east") {
- if (!mother->getLocation()->hasEast()) {
- cout << "Sorry you can not go East" << endl;
- goto main_loop;
- } else {
- player->setLocation(mother->getLocation()->getEast());
- cout << mother->getLocation()->getDescription() << endl;
- goto main_loop;
- }
- }
- if (verb == "look") {
+ if(verb=="west") {
+ if (!mother->getLocation()->hasWest()) {
+ cout << "Sorry you can not go West" << endl;
+ goto main_loop;
+ } else {
+ mother->setLocation(mother->getLocation()->getWest());
cout << mother->getLocation()->getDescription() << endl;
- cout << mother->getLocation()->listItems() << endl;
-
goto main_loop;
}
- cout << "I don't know how to " << verb << " here" << endl;
- } else {
- if (mother->getLocation()->hasItem("Television") || mother->getInventory()->hasItem("Television")) {
- cerr << "hit object in room" << endl;
- if (verb == "drop") {
- cerr << "DOING drop" << endl;
- cout << "I don't have";
- cout << television.getName();
- cout << ".";
- goto main_loop;
- }
- if (verb == "examine") {
- cerr << "DOING examine" << endl;
- cout << television.getDescription();
- goto main_loop;
- }
- if (verb == "take") {
- cerr << "DOING take" << endl;
- goto main_loop;
- }
- cout << "Sorry you can not " << verb << "on" << "Television" << endl;
+ }
+ if(verb=="east") {
+ if (!mother->getLocation()->hasEast()) {
+ cout << "Sorry you can not go East" << endl;
+ goto main_loop;
+ } else {
+ mother->setLocation(mother->getLocation()->getEast());
+ cout << mother->getLocation()->getDescription() << endl;
goto main_loop;
}
- cout << "I can't find a " << noun << " here" << endl;
- }
- }
+ }if (verb=="look"){
+cout << mother->getLocation()->getDescription() << endl;
+cout << mother->getLocation()->listItems() << endl;
+
+goto main_loop;}
+cout << "I don't know how to " << verb << " here" << endl;
+} else {
+if (mother->getLocation()->hasItem("Television") || mother->getInventory()->hasItem("Television")) {
+cerr << "hit object in room" << endl;
+if (verb == "drop") {
+cerr << "DOING drop" << endl;
+mother->getLocation()->addItem(television.getName(), television);
+cout << "I don't have";
+cout << television.getName();
+cout << ".";
+goto main_loop;}
+if (verb == "examine") {
+cerr << "DOING examine" << endl;
+cout << television.getDescription();
+goto main_loop;}
+if (verb == "take") {
+cerr << "DOING take" << endl;
+mother->getInventory()->addItem(television.getName(), television);
+cout << "You are carrying to much already";
+goto main_loop;}
+cout << "Sorry you can not " << verb << "on" << "Television" << endl;
+goto main_loop;
+}cout << "I can't find a " << noun << " here" << endl;
+}
+}
}
View
2 output/makefile
@@ -1,7 +1,7 @@
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
-SOURCES= Location.cpp Player.cpp compiled_game.cpp
+SOURCES= compiled_game.cpp Location.cpp Player.cpp Item.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=Play

0 comments on commit 0f6ce0b

Please sign in to comment.