-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
144 lines (130 loc) · 2.9 KB
/
Copy pathGame.cpp
File metadata and controls
144 lines (130 loc) · 2.9 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
143
144
#include "Game.h"
Game::Game (const string& game_name)
:world(game_name),itemmanager(game_name),player_location()
{
player_location=world.getStart();
assert(invariant ());
}
bool Game::isOver () const
{
assert(invariant ());
if(world.isDeath(player_location) || world.isVictory(player_location))
return true;
else
return false;
}
void Game::printDescription () const
{
assert(invariant ());
world.printDescription(player_location);
}
int Game::getScore () const
{
assert(invariant ());
return itemmanager.getScore();
}
void Game::printInventory () const
{
assert(invariant ());
itemmanager.printInventory ();
}
void Game::moveNorth ()
{
assert(invariant ());
if(world.canGoNorth(player_location) == true)
{
player_location = moveTo(player_location);
world.printDescription(player_location);
itemmanager.printAtLocation(player_location);
}
else
cout << "Can not reach " << endl;
assert(invariant ());
}
void Game::moveSouth ()
{
assert(invariant ());
if(world.canGoSouth(player_location) == true)
{
player_location = moveTo(player_location);
world.printDescription(player_location);
itemmanager.printAtLocation(player_location);
}
else
cout << "Can not reach " << endl;
assert(invariant ());
}
void Game::moveEast ()
{
assert(invariant ());
if(world.canGoEast(player_location) == true )
{
player_location = moveTo(player_location);
world.printDescription(player_location);
itemmanager.printAtLocation(player_location);
}
else
cout << "Can not reach " << endl;
assert(invariant ());
}
void Game::moveWest ()
{
assert(invariant ());
if(world.canGoWest(player_location) == true )
{
player_location = move(player_location);
world.printDescription(player_location);
itemmanager.printAtLocation(player_location);
}
else
cout << "Can not reach " << endl;
assert(invariant ());
}
void Game::takeItem (char id)
{
assert(invariant ());
itemmanager.take(id,player_location);
assert(invariant ());
}
void Game::leaveItem (char id)
{
assert(invariant ());
itemmanager.leave(id,player_location);
assert(invariant ());
}
void Game::reset ()
{
assert(invariant ());
itemmanager.reset();
assert(invariant ());
}
void Game::moveTo (const Location& location)
{
if(world.iObstructed(location) != true)
{
play_location = location;
printDescription();
}
else
{
if((world.isObstructed(player_location) == true))
{
player_location = location;
printDescription();
}
else
{
if(itemmanager.isInInventory(world.getRequiredItem(location)))
world.printDescriptionSuccess(location);
else
world.printDescriptionFailure(location);
}
}
}
bool Game::invariant () const
{
if(world.isValid(player_location) == true)
return true;
else
return false;
}