-
Notifications
You must be signed in to change notification settings - Fork 0
/
invader.cpp
146 lines (138 loc) · 4.32 KB
/
invader.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
#include "stdcpp.h"
#include "invader.h"
#include "lib.h"
#include "frame.h"
using namespace std;
bool Army::are_all_dead() {
// Returns if all the invaders are dead
bool all_dead = true;
for (Invader invader: army) {
if (invader.stat == alive) {
all_dead = false;
break;
}
}
return all_dead;
}
bool Army::reached_bottom() {
// Returns if the army has reached the player's starship
for (Invader invader: army) {
if (invader.stat == alive && invader.y >= NUM_ROWS-1) {
return true;
}
}
return false;
}
bool Army::reached_left_wall() {
// Returns if the army has reached the left edge of the screen
for (Invader invader: army) {
if (invader.stat == alive && invader.x <= 0) {
return true;
}
}
return false;
}
bool Army::reached_right_wall() {
// Returns if the army has reached the right edge of the screen
for (Invader invader: army) {
if (invader.stat == alive && invader.x >= NUM_COLS-1) {
return true;
}
}
return false;
}
Army::Army(int NUM_INVADERS) {
srand(time(NULL));
for (int y=1; y<9; y++) {
for (int x=1; x<NUM_COLS-2; x++) {
if (army.size() >= NUM_INVADERS) {break;} // If army size is already at maximum, don't add new invaders
int decision = rand()%2; // Randomly decide if there should be an invader here at this coordinate
if (decision) {
army.push_back(Invader{x,y,Health::alive}); // Add an invader to the army
}
}
}
vec = Direction::left; // Go left first
sym = Symbol::plus; // Army symbol is a plus sign at first
rows_descended = 0;
}
Status Army::update() {
if (are_all_dead()) {return Status::win;} // If army all dead, trigger win
vector<int> dead_invaders = {};
for (int index: dead_invaders) {
army.erase(army.begin()+index);
}
if (vec == Direction::left) {
// Move to the left by one
// If to the left wall set next move as down
for (Invader &invader: army) {
if (invader.x-1 >= 0 && invader.stat==Health::alive) {
invader.x -= 1;
}
}
if (reached_left_wall()) {
prev_vec = vec;
vec = Direction::down;
}
} else if (vec == Direction::right) {
// Move to the right by one
// If to the right wall set next move as down
for (Invader &invader: army) {
if (invader.x+1 <= NUM_COLS-1 && invader.stat==Health::alive) {
invader.x += 1;
}
}
if (reached_right_wall()) {
prev_vec = vec;
vec = Direction::down;
}
} else if (vec == Direction::down) {
// Move down by one
// If at left wall set next move as right
// If at right wall set next move as left
// If reached the bottom, trigger lose
for (Invader &invader: army) {
if (invader.stat == Health::alive) {
invader.y += 1;
}
}
rows_descended += 1;
if (reached_bottom()) {return Status::lose;} // Invaders have reached the starship
if (prev_vec == Direction::left) { // Switch directions after going down
prev_vec = vec;
vec = Direction::right;
}
else if (prev_vec == Direction::right) {
prev_vec = vec;
vec = Direction::left;
}
}
switch (sym) { // Change symbol of the army
case Symbol::plus:
sym = Symbol::cross;
break;
case Symbol::cross:
sym = Symbol::plus;
break;
}
if (reached_bottom()) {return Status::lose;}
return Status::cont;
}
void Army::draw(Frame &f) {
switch (sym) {
case Symbol::plus:
for (Invader invader: army) {
if (invader.stat == Health::alive) {
f.content[invader.x][invader.y] = "+";
}
}
break;
case Symbol::cross:
for (Invader invader: army) {
if (invader.stat == Health::alive) {
f.content[invader.x][invader.y] = "x";
}
}
break;
}
}