-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArena.cpp
225 lines (188 loc) · 5.28 KB
/
Arena.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// Arena.cpp
// Vampires
//
// Created by Andrew K. Sloan on 1/12/20.
// Copyright © 2020 Andrew K. Sloan. All rights reserved.
//
#include <iostream>
#include "Arena.h"
#include "globals.h"
#include "Vampire.h"
#include "Player.h"
using namespace std;
// Arena::Arena(int nRows, int nCols), adding :m_history
Arena::Arena(int nRows, int nCols):m_history(nRows, nCols)
{
if (nRows <= 0 || nCols <= 0 || nRows > MAXROWS || nCols > MAXCOLS)
{
cout << "***** Arena created with invalid size " << nRows << " by "
<< nCols << "!" << endl;
exit(1);
}
m_rows = nRows;
m_cols = nCols;
m_player = nullptr;
m_nVampires = 0;
m_turns = 0;
for (int r = 1; r <= m_rows; r++)
for (int c = 1; c <= m_cols; c++)
setCellStatus(r, c, EMPTY);
}
History& Arena::history()
{
return m_history;
}
Arena::~Arena()
{
for (int k = 0; k < m_nVampires; k++)
delete m_vampires[k];
delete m_player;
}
int Arena::rows() const
{
return m_rows;
}
int Arena::cols() const
{
return m_cols;
}
Player* Arena::player() const
{
return m_player;
}
int Arena::vampireCount() const
{
return m_nVampires;
}
int Arena::getCellStatus(int r, int c) const
{
checkPos(r, c, "Arena::getCellStatus");
return m_grid[r-1][c-1];
}
int Arena::numberOfVampiresAt(int r, int c) const
{
int count = 0;
for (int k = 0; k < m_nVampires; k++)
{
Vampire* vp = m_vampires[k];
if (vp->row() == r && vp->col() == c)
count++;
}
return count;
}
void Arena::display(string msg) const
{
char displayGrid[MAXROWS][MAXCOLS];
int r, c;
// Fill displayGrid with dots (empty) and stars (poisoned blood vials)
for (r = 1; r <= rows(); r++)
for (c = 1; c <= cols(); c++)
displayGrid[r-1][c-1] = (getCellStatus(r,c) == EMPTY ? '.' : '*');
// Indicate each vampire's position
for (int k = 0; k < m_nVampires; k++)
{
const Vampire* vp = m_vampires[k];
char& gridChar = displayGrid[vp->row()-1][vp->col()-1];
switch (gridChar)
{
case '.': gridChar = 'V'; break;
case 'V': gridChar = '2'; break;
case '9': break;
default: gridChar++; break; // '2' through '8'
}
}
// Indicate player's position
if (m_player != nullptr)
displayGrid[m_player->row()-1][m_player->col()-1] = (m_player->isDead() ? 'X' : '@');
// Draw the grid
clearScreen();
for (r = 1; r <= rows(); r++)
{
for (c = 1; c <= cols(); c++)
cout << displayGrid[r-1][c-1];
cout << endl;
}
cout << endl;
// Write message, vampire, and player info
if (msg != "")
cout << msg << endl;
cout << "There are " << vampireCount() << " vampires remaining." << endl;
if (m_player == nullptr)
cout << "There is no player!" << endl;
else if (m_player->isDead())
cout << "The player is dead." << endl;
cout << m_turns << " turns have been taken." << endl;
}
void Arena::setCellStatus(int r, int c, int status)
{
checkPos(r, c, "Arena::setCellStatus");
m_grid[r-1][c-1] = status;
}
bool Arena::addVampire(int r, int c)
{
if (! isPosInBounds(r, c))
return false;
// Don't add a vampire on a spot with a poisoned blood vial
if (getCellStatus(r, c) != EMPTY)
return false;
// Don't add a vampire on a spot with a player
if (m_player != nullptr && m_player->row() == r && m_player->col() == c)
return false;
if (m_nVampires == MAXVAMPIRES)
return false;
m_vampires[m_nVampires] = new Vampire(this, r, c);
m_nVampires++;
return true;
}
bool Arena::addPlayer(int r, int c)
{
if (! isPosInBounds(r, c))
return false;
// Don't add a player if one already exists
if (m_player != nullptr)
return false;
// Don't add a player on a spot with a vampire
if (numberOfVampiresAt(r, c) > 0)
return false;
m_player = new Player(this, r, c);
return true;
}
void Arena::moveVampires()
{
// Move all vampires
for (int k = m_nVampires-1; k >= 0; k--)
{
Vampire* vp = m_vampires[k];
vp->move();
if (m_player != nullptr &&
vp->row() == m_player->row() && vp->col() == m_player->col())
m_player->setDead();
if (vp->isDead())
{
delete vp;
// The order of Vampire pointers in the m_vampires array is
// irrelevant, so it's easiest to move the last pointer to
// replace the one pointing to the now-deleted vampire. Since
// we are traversing the array from last to first, we know this
// last pointer does not point to a dead vampire.
m_vampires[k] = m_vampires[m_nVampires-1];
m_nVampires--;
}
}
// Another turn has been taken
m_turns++;
}
bool Arena::isPosInBounds(int r, int c) const
{
return (r >= 1 && r <= m_rows && c >= 1 && c <= m_cols);
}
void Arena::checkPos(int r, int c, string functionName) const
{
if (!isPosInBounds(r, c))
{
cout << "***** " << "Invalid arena position (" << r << ","
<< c << ") in call to " << functionName << endl;
exit(1);
}
}