Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 0f6ce0b25acdcd181d55972614bde16bfb03addc 1 parent a895a27
George Phillips GeorgePhillips authored
29 #Item.h#
... ... @@ -1,29 +0,0 @@
1   -#ifndef ITEM_H_
2   -#define ITEM_H_
3   -#include <string>
4   -#include <map>
5   -#include <iostream>
6   -using namespace std;
7   -
8   -/* Item class stores all information relevant to a particular item object */
9   -class Item {
10   - string name;
11   - string description;
12   - map<string, string> verb_expressions;
13   -public:
14   - Item(string name, string description, map<string, string>);
15   - Item();
16   - string getName(void);
17   - void setName(string name);
18   - string getDescription(void);
19   - void setDescription(string description);
20   - void addVerb(string verb, string expression);
21   - void removeVerb(string verb);
22   - void printVerb(void);
23   - string getVerbExpression(string verb);
24   - string print
25   - ~Item();
26   -};
27   -
28   -
29   -#endif
18 Compiler.cpp
@@ -37,6 +37,7 @@ void Compiler::Compile() {
37 37 #include <string>\n\
38 38 #include <sstream>\n\
39 39 #include \"Location.h\"\n\
  40 +#include \"Item.h\"\n\
40 41 #include \"Player.h\"\n\
41 42 using namespace std;\n\
42 43 #define WELCOME_MESSAGE \"" + parser->initialDescription + "\"\n\
@@ -91,7 +92,7 @@ int main(int argc, char **argv) {\n\
91 92 map<string, Item*>::iterator objects;
92 93 for (objects = parser->items.begin(); objects != parser->items.end(); objects++) {
93 94 output += "Item " + objects->first + "(\"" + objects->second->getName() + "\", \"" + objects->second->getDescription() + "\");\n";
94   - output += objects->first + ".setLocation(&" + objects->second->getLocation()->getVariableName() + ");\n";
  95 + output += "// " +objects->first + ".setLocation(&" + objects->second->getLocation()->getVariableName() + ");\n";
95 96 output += objects->second->getLocation()->getVariableName() + ".addItem(\"" + objects->second->getName() + "\", " + objects->first + ");\n";
96 97 }
97 98
@@ -147,6 +148,11 @@ int main(int argc, char **argv) {\n\
147 148
148 149 // DIRECTIONS
149 150 output += "\n\
  151 + if ((verb == \"i\") || (verb == \"inventory\") || (verb == \"invent\")) {\n\
  152 + cout << " + parser->player->getVariableName() + "->getInventory()->listItems();\n\
  153 + cout << " + parser->player->getVariableName() + "->getNumberOfItems() << \"/\" << " + parser->player->getVariableName() + "->getMaxItems() << endl;\n\
  154 + goto main_loop;\n\
  155 + }\n\n\
150 156 if(verb==\"north\") {\n\
151 157 if (!" + parser->player->getVariableName() + "->getLocation()->hasNorth()) {\n\
152 158 cout << \"Sorry you can not go North\" << endl;\n\
@@ -182,7 +188,7 @@ int main(int argc, char **argv) {\n\
182 188 " cout << \"Sorry you can not go East\" << endl;\n\
183 189 goto main_loop;\n\
184 190 } else {\n\
185   - player->setLocation(" + parser->player->getVariableName() + "->getLocation()->getEast());\n\
  191 + " + parser->player->getVariableName() + "->setLocation(" + parser->player->getVariableName() + "->getLocation()->getEast());\n\
186 192 cout << " + parser->player->getVariableName() + "->getLocation()->getDescription() << endl;\n\
187 193 goto main_loop;\n\
188 194 }\n\
@@ -306,14 +312,16 @@ string Compiler::CompileNounVerb(Item *item) {
306 312 }
307 313 if (location == "") {
308 314 if (line.find(parser->player->getVariableName()) < line.length()) {
309   - location = parser->player->getVariableName() + ".getInventory()";
310   - break;
  315 + location = parser->player->getVariableName() + "->getInventory()";
  316 + } else if (line.find("currentLocation") < line.length()) {
  317 + location = parser->player->getVariableName() + "->getLocation()";
311 318 }
312 319 }
313 320 if (location == "" || item == "") {
314 321 cerr << "Unreadable setLocation Command: \"" << line << "\"" << endl;
315 322 } else {
316   - output += location + "->addItem(" + item + "->getName(), " + item + ")\n";
  323 + cerr << location << "->addItem(" << item << ".getName(), " << item << ");\n";
  324 + output += location + "->addItem(" + item + ".getName(), " + item + ");\n";
317 325 }
318 326 } else {
319 327 cerr << "Unreadable/Unknown Command: \"" << line << "\"" << endl;
3  Location.h
@@ -10,9 +10,8 @@
10 10 #include <iostream>
11 11 #include <cstdlib>
12 12 #include <map>
13   -
14 13 using namespace std;
15   -
  14 +class Item;
16 15 /* Location class will store all information relevant to a particular location*/
17 16 class Location {
18 17 public:
3  output/Item.h
@@ -10,9 +10,10 @@
10 10
11 11 #include <string>
12 12 #include <iostream>
13   -#include "Location.h"
14 13 using namespace std;
15 14
  15 +class Location;
  16 +
16 17 /* Item class will store all relevant information for a particular item */
17 18 class Item {
18 19 Location* location;
BIN  output/Item.h.gch
Binary file not shown
15 output/Location.cpp
@@ -14,16 +14,6 @@ Location::Location() {
14 14 this->east = NULL;
15 15 }
16 16
17   -/* Constructor */
18   -Location::Location(string name, string description, map<string, Item> items) {
19   - this->name = name;
20   - this->description = description;
21   - this->items = items;
22   - this->north = NULL;
23   - this->south = NULL;
24   - this->west = NULL;
25   - this->east = NULL;
26   -}
27 17
28 18 /* Constructor */
29 19 Location::Location(string name, string description) {
@@ -75,6 +65,11 @@ bool Location::hasItem(string item_name) {
75 65 return this->items.count(item_name) != 0;
76 66 }
77 67
  68 +/* Checks to see if item is in this location */
  69 +int Location::getItemCount() {
  70 + return this->items.size();
  71 +}
  72 +
78 73 /* Removes an item from the room */
79 74 void Location::removeItem(string item_name) {
80 75 this->items.erase(item_name);
2  output/Location.h
@@ -25,7 +25,6 @@ class Location {
25 25 map<string, Item> items;
26 26 public:
27 27 // Constructors
28   - Location(string, string, map<string, Item>);
29 28 Location(string, string);
30 29 Location();
31 30
@@ -53,6 +52,7 @@ class Location {
53 52
54 53 /* This set of methods will deal with Items stored in this location */
55 54 void addItem(string item_name, Item item);
  55 + int getItemCount();
56 56 bool hasItem(string item_name);
57 57 void removeItem(string item_name);
58 58 string listItems(void);
BIN  Player.h.gch → output/Location.h.gch
Binary file not shown
BIN  output/Play
Binary file not shown
6 output/Player.cpp
@@ -29,12 +29,8 @@ void Player::setDescription(string description) {
29 29 }
30 30
31 31 /* Returns the number of items currently held by the player */
32   -int Player::getNumberOfItems(void) {return current_number_of_items;}
  32 +int Player::getNumberOfItems(void) {return inventory->getItemCount();}
33 33
34   -/* Sets the number of items that the player currently has */
35   -void Player::setNumberOfItems(int number_of_items) {
36   - current_number_of_items = number_of_items ;
37   -}
38 34
39 35 /* Returns the max number of items the player can hold */
40 36 int Player::getMaxItems(void) {return max_items;}
4 output/Player.h
@@ -19,7 +19,6 @@ class Player {
19 19 string name;
20 20 string description;
21 21 int max_items;
22   - int current_number_of_items;
23 22 Location* current_location;
24 23 Location* inventory;
25 24 public:
@@ -44,9 +43,6 @@ class Player {
44 43 /* Returns the number of items currently held by the player */
45 44 int getNumberOfItems(void);
46 45
47   - /* Sets the number of items that the player currently has */
48   - void setNumberOfItems(int number_of_items);
49   -
50 46 /* Returns the max number of items the player can hold */
51 47 int getMaxItems(void);
52 48
201 output/compiled_game.cpp
@@ -3,11 +3,11 @@
3 3 #include <string>
4 4 #include <sstream>
5 5 #include "Location.h"
  6 +#include "Item.h"
6 7 #include "Player.h"
7 8 using namespace std;
8 9 #define WELCOME_MESSAGE "Ed Is a Fucking homo"
9 10 #define QUIT_GAME "quit"
10   -
11 11 int main(int argc, char **argv) {
12 12 string username;
13 13 string command;
@@ -15,133 +15,106 @@ int main(int argc, char **argv) {
15 15 string noun;
16 16 string prompt;
17 17 int count;
  18 +
  19 +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.");
  20 +Location livingRoom("The Living Room", "The Living Room");
18 21
19   - 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.");
20   - Location livingRoom("The Living Room", "The Living Room");
  22 +kitchen.setNorth(&livingRoom);
  23 +livingRoom.setSouth(&kitchen);
  24 +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");
  25 +// television.setLocation(&kitchen);
  26 +kitchen.addItem("Television", television);
  27 +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) {
  28 + quit_loop:
  29 + string quit = "";
  30 + cout << "Do you really want to quit?: [y]or[n] "; cout.flush();
  31 + cin >> quit;
  32 + cin.ignore();
  33 + if (quit == "y") {
  34 + cout << "Thanks for playing!" << endl;
  35 + break;
  36 + } else if (quit == "n") {
  37 + goto main_loop;
  38 + } else {
  39 + goto quit_loop;
  40 + }
  41 + } if (count == 2) {
21 42
22   - kitchen.setNorth(&livingRoom);
23   - livingRoom.setSouth(&kitchen);
24   - Location inventory("Inventory", "Description of the inventory");
25   - Player* mother = new Player();
26   - mother->setLocation(&livingRoom);
27   - mother->setInventory(&inventory);
28   - mother->setMaxItems(8);
29   - Item television("Television", "A small television placed in the corner of the kitchen. It looks broken");
30   - television.setLocation(kitchen);
31   - kitchen.addItem("Television", television);
32   - cout << WELCOME_MESSAGE << endl;
33   - prompt = ">>> ";
34   - cout << mother->getLocation()->getDescription() << endl;
35   - while (true) {
36   - main_loop:
37   - cout << prompt;
38   - getline(cin, command);
39   - istringstream word(command);
40   - verb = "";
41   - noun = "";
42   - count = 0;
43   - while (word) {
44   - if (count > 2) {
45   - cout << "I do not understand your command. Enter 2 words at most, a verb followed by a noun" << endl;
  43 + if ((verb == "i") || (verb == "inventory") || (verb == "invent")) {
  44 + cout << mother->getInventory()->listItems();
  45 + cout << mother->getNumberOfItems() << "/" << mother->getMaxItems() << endl;
46 46 goto main_loop;
47   - }
48   - if (count == 0) {
49   - word >> verb;
50   - } else {
51   - word >> noun;
52   - }
53   - count++;
54 47 }
55   - if (command == QUIT_GAME) {
56   -quit_loop:
57   - string quit = "";
58   - cout << "Do you really want to quit?: [y]or[n] ";
59   - cout.flush();
60   - cin >> quit;
61   - cin.ignore();
62   - if (quit == "y") {
63   - cout << "Thanks for playing!" << endl;
64   - break;
65   - } else if (quit == "n") {
  48 +
  49 + if(verb=="north") {
  50 + if (!mother->getLocation()->hasNorth()) {
  51 + cout << "Sorry you can not go North" << endl;
66 52 goto main_loop;
67 53 } else {
68   - goto quit_loop;
  54 + mother->setLocation(mother->getLocation()->getNorth());
  55 + cout << mother->getLocation()->getDescription() << endl;
  56 + goto main_loop;
69 57 }
70 58 }
71   - if (count == 2) {
72   -
73   - if (verb == "north") {
74   - if (!mother->getLocation()->hasNorth()) {
75   - cout << "Sorry you can not go North" << endl;
76   - goto main_loop;
77   - } else {
78   - mother->setLocation(mother->getLocation()->getNorth());
79   - cout << mother->getLocation()->getDescription() << endl;
80   - goto main_loop;
81   - }
82   - }
83 59
84   - if (verb == "south") {
85   - if (!mother->getLocation()->hasSouth()) {
86   - cout << "Sorry you can not go South" << endl;
87   - goto main_loop;
88   - } else {
89   - mother->setLocation(mother->getLocation()->getSouth());
90   - cout << mother->getLocation()->getDescription() << endl;
91   - goto main_loop;
92   - }
  60 + if(verb=="south") {
  61 + if (!mother->getLocation()->hasSouth()) {
  62 + cout << "Sorry you can not go South" << endl;
  63 + goto main_loop;
  64 + } else {
  65 + mother->setLocation(mother->getLocation()->getSouth());
  66 + cout << mother->getLocation()->getDescription() << endl;
  67 + goto main_loop;
93 68 }
  69 + }
94 70
95   - if (verb == "west") {
96   - if (!mother->getLocation()->hasWest()) {
97   - cout << "Sorry you can not go West" << endl;
98   - goto main_loop;
99   - } else {
100   - mother->setLocation(mother->getLocation()->getWest());
101   - cout << mother->getLocation()->getDescription() << endl;
102   - goto main_loop;
103   - }
104   - }
105   - if (verb == "east") {
106   - if (!mother->getLocation()->hasEast()) {
107   - cout << "Sorry you can not go East" << endl;
108   - goto main_loop;
109   - } else {
110   - player->setLocation(mother->getLocation()->getEast());
111   - cout << mother->getLocation()->getDescription() << endl;
112   - goto main_loop;
113   - }
114   - }
115   - if (verb == "look") {
  71 + if(verb=="west") {
  72 + if (!mother->getLocation()->hasWest()) {
  73 + cout << "Sorry you can not go West" << endl;
  74 + goto main_loop;
  75 + } else {
  76 + mother->setLocation(mother->getLocation()->getWest());
116 77 cout << mother->getLocation()->getDescription() << endl;
117   - cout << mother->getLocation()->listItems() << endl;
118   -
119 78 goto main_loop;
120 79 }
121   - cout << "I don't know how to " << verb << " here" << endl;
122   - } else {
123   - if (mother->getLocation()->hasItem("Television") || mother->getInventory()->hasItem("Television")) {
124   - cerr << "hit object in room" << endl;
125   - if (verb == "drop") {
126   - cerr << "DOING drop" << endl;
127   - cout << "I don't have";
128   - cout << television.getName();
129   - cout << ".";
130   - goto main_loop;
131   - }
132   - if (verb == "examine") {
133   - cerr << "DOING examine" << endl;
134   - cout << television.getDescription();
135   - goto main_loop;
136   - }
137   - if (verb == "take") {
138   - cerr << "DOING take" << endl;
139   - goto main_loop;
140   - }
141   - cout << "Sorry you can not " << verb << "on" << "Television" << endl;
  80 + }
  81 + if(verb=="east") {
  82 + if (!mother->getLocation()->hasEast()) {
  83 + cout << "Sorry you can not go East" << endl;
  84 + goto main_loop;
  85 + } else {
  86 + mother->setLocation(mother->getLocation()->getEast());
  87 + cout << mother->getLocation()->getDescription() << endl;
142 88 goto main_loop;
143 89 }
144   - cout << "I can't find a " << noun << " here" << endl;
145   - }
146   - }
  90 + }if (verb=="look"){
  91 +cout << mother->getLocation()->getDescription() << endl;
  92 +cout << mother->getLocation()->listItems() << endl;
  93 +
  94 +goto main_loop;}
  95 +cout << "I don't know how to " << verb << " here" << endl;
  96 +} else {
  97 +if (mother->getLocation()->hasItem("Television") || mother->getInventory()->hasItem("Television")) {
  98 +cerr << "hit object in room" << endl;
  99 +if (verb == "drop") {
  100 +cerr << "DOING drop" << endl;
  101 +mother->getLocation()->addItem(television.getName(), television);
  102 +cout << "I don't have";
  103 +cout << television.getName();
  104 +cout << ".";
  105 +goto main_loop;}
  106 +if (verb == "examine") {
  107 +cerr << "DOING examine" << endl;
  108 +cout << television.getDescription();
  109 +goto main_loop;}
  110 +if (verb == "take") {
  111 +cerr << "DOING take" << endl;
  112 +mother->getInventory()->addItem(television.getName(), television);
  113 +cout << "You are carrying to much already";
  114 +goto main_loop;}
  115 +cout << "Sorry you can not " << verb << "on" << "Television" << endl;
  116 +goto main_loop;
  117 +}cout << "I can't find a " << noun << " here" << endl;
  118 +}
  119 +}
147 120 }
2  output/makefile
... ... @@ -1,7 +1,7 @@
1 1 CC=g++
2 2 CFLAGS=-c -Wall
3 3 LDFLAGS=
4   -SOURCES= Location.cpp Player.cpp compiled_game.cpp
  4 +SOURCES= compiled_game.cpp Location.cpp Player.cpp Item.cpp
5 5 OBJECTS=$(SOURCES:.cpp=.o)
6 6 EXECUTABLE=Play
7 7

0 comments on commit 0f6ce0b

Please sign in to comment.
Something went wrong with that request. Please try again.