-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.cpp
More file actions
142 lines (119 loc) · 3.04 KB
/
Item.cpp
File metadata and controls
142 lines (119 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "Item.h"
#include <cassert>
#include "Location.h"
Item::Item()
{
id = ID_NOT_INITIALIZED;
Location();
is_in_inventory = false;
world_description = "";
inventory_description = "";
points = 0;
}
void Item::init (char id1,
const Location& location,
int points1,
const string& world_description1,
const string& inventory_description1) //initialize the Item
{
assert (id1 != ID_NOT_INITIALIZED);
assert (world_description1 != "");
assert (inventory_description1 != "");
id = id1;
points = points1;
start_location = location;
current_location = location;
is_in_inventory = false;
world_description = world_description1;
inventory_description = inventory_description1;
assert(invariant ());
}
void Item::debugPrint () const //to print all fields of the item
{
cout << "id: " << '\'' << id << '\'' << '\n';
cout << "start: " << start_location << '\n';
cout << "current: " << current_location << '\n';
cout << "is_in_inventory: " << is_in_inventory << '\n';
cout << "points: " << points << '\n';
cout << "world_description: " << '\"' << world_description << '\"' << '\n';
cout << "inventory_description: " << '\"' << inventory_description << '\"' << '\n';
}
bool Item::isInitialized () const
{
assert(invariant ());
if(id != ID_NOT_INITIALIZED)
return true;
else
return false;
}
char Item::getId () const //to get item's id
{
assert(invariant ());
return id;
}
bool Item::isInInventory () const //to return whether the items is in the player inventory
{
assert(invariant ());
return is_in_inventory;
}
bool Item::isAtLocation (const Location& location) const //to return whether the item is in the player's location
{
assert(invariant ());
if (!(current_location == location))
return false;
else
return true;
}
int Item::getPlayerPoints () const //to return how many points the player gets
{
assert(invariant ());
if(is_in_inventory == true)
return points;
else
return 0;
}
void Item::printDescription () const //to print the description if the item is in inventory
{
assert(invariant ());
if(is_in_inventory == true)
cout << inventory_description << '\n';
else
cout << world_description << '\n';
}
void Item::reset () //to set the game
{
assert(invariant ());
current_location = start_location;
is_in_inventory = false;
assert(invariant ());
}
void Item::moveToInventory () //to move the item to player inventory
{
assert(invariant ());
is_in_inventory = true;
assert(invariant ());
}
void Item::moveToLocation (const Location& location) //to move the item to the specified location
{
assert(invariant ());
current_location = location;
is_in_inventory = false;
assert(invariant ());
}
bool Item::operator< (const Item& other) const
{
assert(invariant ());
if (id < other.id)
return true;
else
false;
}
bool Item::invariant () const
{
bool status = true;
if (world_description != "" && inventory_description != "")
status = true;
else
status = false;
return status;
}