-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arena.cpp
210 lines (186 loc) · 4.96 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
//
// Arena.cpp
// Project1
//
// Created by Atibhav Mittal on 1/6/16.
// Copyright © 2016 ati. All rights reserved.
//
#include "Arena.h"
#include "Player.h"
#include "Robot.h"
#include "globals.h"
#include "History.h"
#include <iostream>
using namespace std;
///////////////////////////////////////////////////////////////////////////
// Arena implementations
///////////////////////////////////////////////////////////////////////////
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_nRobots = 0;
}
Arena::~Arena()
{
for (int k = 0; k < m_nRobots; k++)
delete m_robots[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::robotCount() const
{
return m_nRobots;
}
int Arena::nRobotsAt(int r, int c) const
{
int count = 0;
for (int k = 0; k < m_nRobots; k++)
{
const Robot* rp = m_robots[k];
if (rp->row() == r && rp->col() == c)
count++;
}
return count;
}
bool Arena::determineNewPosition(int& r, int& c, int dir) const
{
switch (dir)
{
case UP: if (r <= 1) return false; else r--; break;
case DOWN: if (r >= rows()) return false; else r++; break;
case LEFT: if (c <= 1) return false; else c--; break;
case RIGHT: if (c >= cols()) return false; else c++; break;
default: return false;
}
return true;
}
void Arena::display() const
{
// Position (row,col) in the arena coordinate system is represented in
// the array element grid[row-1][col-1]
char grid[MAXROWS][MAXCOLS];
int r, c;
// Fill the grid with dots
for (r = 0; r < rows(); r++)
for (c = 0; c < cols(); c++)
grid[r][c] = '.';
// Indicate each robot's position
for (int k = 0; k < m_nRobots; k++)
{
const Robot* rp = m_robots[k];
char& gridChar = grid[rp->row()-1][rp->col()-1];
switch (gridChar)
{
case '.': gridChar = 'R'; break;
case 'R': gridChar = '2'; break;
case '9': break;
default: gridChar++; break; // '2' through '8'
}
}
// Indicate player's position
if (m_player != nullptr)
{
// Set the char to '@', unless there's also a robot there,
// in which case set it to '*'.
char& gridChar = grid[m_player->row()-1][m_player->col()-1];
if (gridChar == '.')
gridChar = '@';
else
gridChar = '*';
}
// Draw the grid
clearScreen();
for (r = 0; r < rows(); r++)
{
for (c = 0; c < cols(); c++)
cout << grid[r][c];
cout << endl;
}
cout << endl;
// Write message, robot, and player info
cout << endl;
cout << "There are " << robotCount() << " robots remaining." << endl;
if (m_player == nullptr)
cout << "There is no player." << endl;
else
{
if (m_player->age() > 0)
cout << "The player has lasted " << m_player->age() << " steps." << endl;
if (m_player->isDead())
cout << "The player is dead." << endl;
}
}
bool Arena::addRobot(int r, int c)
{
// Dynamically allocate a new Robot and add it to the arena
if (m_nRobots == MAXROBOTS)
return false;
m_robots[m_nRobots] = new Robot(this, r, c);
m_nRobots++;
return true;
}
bool Arena::addPlayer(int r, int c)
{
// Don't add a player if one already exists
if (m_player != nullptr)
return false;
// Dynamically allocate a new Player and add it to the arena
m_player = new Player(this, r, c);
return true;
}
bool Arena::attackRobotAt(int r, int c, int dir)
{
// Attack one robot. Returns true if a robot was attacked and destroyed,
// false otherwise (no robot there, or the attack did not destroy the
// robot).
int k = 0;
for ( ; k < m_nRobots; k++)
{
if (m_robots[k]->row() == r && m_robots[k]->col() == c)
break;
}
if (k < m_nRobots && m_robots[k]->getAttacked(dir)) // robot dies
{
delete m_robots[k];
m_robots[k] = m_robots[m_nRobots-1];
m_nRobots--;
m_history.record(r - 1, c - 1);
return true;
}
return false;
}
bool Arena::moveRobots()
{
for (int k = 0; k < m_nRobots; k++)
{
Robot* rp = m_robots[k];
rp->move();
if (rp->row() == m_player->row() && rp->col() == m_player->col())
m_player->setDead();
}
// return true if the player is still alive, false otherwise
return ! m_player->isDead();
}
History& Arena::history()
{
return m_history;
}