-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrobot.cpp
250 lines (220 loc) · 7.63 KB
/
robot.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <bits/stdc++.h>
#include <conio.h>
#include "robot.hpp"
using namespace std;
int button;
int turn;
bool attacking = false;
bool attacked = false;
bool kill = false;
Robot::Robot() {
this->health = 100;
this->power = 5;
this->range = 5;
this->position[0] = 0;
this->position[1] = 0;
}
MechaKurama::MechaKurama() {
this->health = 100;
this->power = 10;
this->range = 3;
this->bunshinNumber = 1;
}
bool Robot::isAlive() {
return health > 0;
}
bool MechaKurama::isAlive() {
return health > 0;
}
Robot r;
MechaKurama k;
void gameOver() {
cout << endl;
cout << "--------------------------" << endl;
cout << "-------- Game Over -------" << endl;
cout << "--------------------------" << endl << endl;
cout << "Press any key to go back to exit";
button = getch();
exit(0);
}
void randomMechaKuramaPosition(int size) {
srand(time(NULL));
k.position[0] = rand() % size;
k.position[1] = rand() % size;
}
void plot(int size, int rPosition[2], int kPosition[2]) {
int i; int j;
float distance = sqrt(pow(rPosition[0] - kPosition[0], 2) + pow(rPosition[1] - kPosition[1], 2));
if(attacked) {
if(r.isAlive()) {
r.health -= k.power;
}
}
system("cls");
cout << "Robot statuses: " << r.health;
if(attacked) { cout << "(-" << k.power << ")"; }
cout << " HP; " << r.power << " damage; " << r.range << " attack range" << endl;
cout << "Robot's coordinate (" << rPosition[0] << "," << rPosition[1] << ")" << endl << endl;
cout << "Mecha Kurama (" << k.bunshinNumber << ") statuses: " << k.health;
if(attacking && kill == false) { cout << "(-" << r.power << ")"; }
cout << " HP; " << k.power << " damage; " << k.range << " attack range" << endl;
cout << "Mecha Kurama (" << k.bunshinNumber << ")'s coordinate (" << kPosition[0] << "," << kPosition[1] << ")" << endl << endl;
cout << "The distance between Robot and Mecha Kurama is " << distance << " unit" << endl;
for(i = size-1; i >= 0; i--) {
for(j = 0; j < size; j++) {
if(i == rPosition[1] && j == rPosition[0]) {
cout << " R";
} else if(i == kPosition[1] && j == kPosition[0]) {
cout << " K";
} else {
cout << " *";
}
}
cout << endl;
}
cout << endl;
if(distance > r.range && button == 32) { //Space
cout << "Too far away, get closer!" << endl;
}
if(k.isAlive()) {
if(kill == false) {
if(distance <= k.range) {
if(distance <= r.range) {
cout << "You and Mecha Kurama can attack each other" << endl;
} else {
cout << "You are standing too close to the Mecha Kurama" << endl;
}
} else if(distance <= r.range) {
cout << "You can attack Mecha Kurama from here" << endl;
}
if(attacking) {
cout << "You are dealing " << r.power << " damage to Mecha Kurama" << endl;
attacking = false;
}
if(attacked) {
cout << "You've been hit " << k.power << " damage by Mecha Kurama" << endl;
attacked = false;
}
}
} else {
if(attacking) {
kill = true;
turn = 0;
k.health = 100;
k.bunshinNumber += 1;
randomMechaKuramaPosition(size);
plot(size, r.position, k.position);
cout << "You killed 1 Mecha Kurama" << endl;
cout << "Another bunshin has come to revenge" << endl;
kill = false;
attacking = false;
}
}
if(r.isAlive() == false) {
if(k.bunshinNumber > 1) {
cout << endl << "Successfully destroyed " << (k.bunshinNumber - 1) << " Mecha Kurama" << endl;
} else {
cout << endl << "None of Mecha Kurama has been destroyed. Play better next time!" << endl;
}
gameOver();
}
turn += 1;
}
void play() {
int size;
while(true) {
cout << "Enter the size of battlefield [8 .. 16]: ";
cin >> size;
if(8 <= size && size <= 16) {
break;
}
}
randomMechaKuramaPosition(size);
if(k.position[0] == 0) { k.position[0] = 1; }
if(k.position[1] == 0) { k.position[1] = 1; }
plot(size, r.position, k.position);
while(r.isAlive()) {
button = getch();
float distance = sqrt(pow(r.position[0] - k.position[0], 2) + pow(r.position[1] - k.position[1], 2));
if(turn > 0) {
if(distance <= k.range) {
attacked = true;
}
}
if(button == 119) { //W
int w = r.position[1] + 1;
if(w != size) {
if(w != k.position[1] || r.position[0] != k.position[0]) {
r.position[1] += 1;
} else { attacked = false; }
} else { attacked = false; }
} else if(button == 97) { //A
int a = r.position[0] - 1;
if(a != -1) {
if(a != k.position[0] || r.position[1] != k.position[1]) {
r.position[0] -= 1;
} else { attacked = false; }
} else { attacked = false; }
} else if(button == 115) { //S
int s = r.position[1] - 1;
if(s != -1) {
if(s != k.position[1] || r.position[0] != k.position[0]) {
r.position[1] -= 1;
} else { attacked = false; }
} else { attacked = false; }
} else if(button == 100) { //D
int d = r.position[0] + 1;
if(d != size) {
if(d != k.position[0] || r.position[1] != k.position[1]) {
r.position[0] += 1;
} else { attacked = false; }
} else { attacked = false; }
} else if(button == 32) { //Space
if(distance <= r.range) {
k.health -= r.power;
attacking = true;
}
} else if(button == 27) { //ESC
exit(0);
}
system("cls");
plot(size, r.position, k.position);
}
}
void instructions() {
cout << "Instructions" << endl;
cout << "----------------" << endl;
cout << "Avoid Mecha Kurama's attack by moving" << endl << endl;
cout << "Press 'A' to move left" << endl;
cout << "Press 'D' to move right" << endl;
cout << "Press 'W' to move up" << endl;
cout << "Press 'S' to move down" << endl;
cout << "Press 'Space' to attack" << endl;
cout << "Press 'ESC' to exit" << endl << endl;
cout << "Press any key to go back to main menu";
button = getch();
}
int main() {
do {
system("cls");
cout << "-------------------------- " << endl;
cout << "| Robot War | " << endl;
cout << "-------------------------- " << endl;
cout << "1. Start game" << endl;
cout << "2. Instruction" << endl;
cout << "Use ESC to quit the game" << endl << endl;
cout << "Select option: ";
button = getch();
if(button == 49) { //1
system("cls");
play();
} else if(button == 50) { //2
system("cls");
instructions();
} else if(button == 27) { //ESC
exit(0);
}
} while(1);
system("pause");
return 0;
}