-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbgabe4.cpp
455 lines (405 loc) · 12.7 KB
/
Abgabe4.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* Ein verbessertes Labyrinth-Spiel
* Autor: Fritz Bökler ([email protected])
* Datum: 02.12.2024
* MIT Lizenz
*
* In diesem Spiel versucht eine SpielerIn (S) das Ziel (Z) zu erreichen.
* Das Labyrinth wird ueber die Konsole (cout) ausgegeben, die Eingabe erfolgt ebenfalls
* zeilengepuffert ueber die Konsole (cin).
*
* Das Labyrinth enthält die folgenden Zeichen:
* . Leeres Feld
* # Wand (nicht begehbar)
* Z Ziel
* S SpielerIn (wird nicht im Labyrint selbst gespeichert)
* K Schluessel
* T Tür
* A Geist
*
* Eine SpielerIn hat eine Anzahl an Schlüsseln. Diese Anzahl wird beim Erreichen eines
* K-Feldes erhöht und beim Erreichen eines T-Feldes reduziert. Eine Tür kann nur durchschritten
* werden, wenn die SpielerIn mindestens einen Schluessel besitzt. Ein aufgenommener Schluessel
* verschwindet (wird zu .), ebenso wie eine durchschrittene Tuer.
*
* Die folgenden Eingaben sind gültig:
* w - nach oben bewegen
* a - nach links bewegen
* s - nach unten bewegen
* d - nach rechts bewegen
* q - Spiel beenden
*
* Das Labyrnith wird zu Beginn eingegeben.
* Syntax: <Zeilen> <Spalte> <Labyrinth-Zeichen> <Spieler Zeile> <Spieler Spalte>
* <Zeilen>: 1 bis 20
* <Spalten>: 1 bis 20
* <Labyrint-Zeichen>: Eine Folge von <Zeilen> * <Spalten> vielen Zeichen aus {., #, Z, K, T, A}
* <Spieler Zeile>: 0 bis <Zeilen> - 1
* <Spieler Spalte>: 0 bis <Spalten> - 1
*
* Ein Beispiellabyrinth: 7 7 ...#....#...#T..####Z#....##K###.#......A#.###### 0 4
*/
#include "std_lib_inc.h"
#include "Abgabe4.h"
// Exception fuer nicht erlaubte Bewegungen
class BadMovement {};
// Exception fuer unbekannte Eingaben
class UnknownInput {};
// Exception fuer eine falsche Labyrinth-Eingabe
class BadMaze {};
// Klasse, die eine SpielerIn kapselt
class Player
{
public:
int no_keys; // Anzahl der Schlüssel der SpielerIn
vector<int> position; // Aktuelle Position der SpielerIn im Labyrinth
};
// Klasse, die das Labyrinth kapselt
class Maze
{
public:
int rows; // Anzahl der Zeilen des Labyrinths
int cols; // Anzahl der Spalten des Labyrinths
vector<vector<char>> data; // Labyrinth-Daten (erst Zeilen dann Spalten)
vector<int> player_start_position; // Startposition der SpielerIn, so wie es in der Übergabe steht
};
// Fasst Labyrinth und Spieler zu einem Spiel-Status zusammen
class GameState
{
public:
Maze maze; // Das Labyrinth
Player player; // Die SpielerIn
bool exit; // Wurde 'q' gerdückt?
bool hit_ghost; // Wurde ein Geist getroffen?
bool info_mode; // Ist der Infomode aktiviert?
};
// Togglet den Info Modus an bzw. aus
void toggle_info_mode(GameState& game_state){
if(game_state.info_mode){
game_state.info_mode = false;
}else{
game_state.info_mode = true;
}
}
// Funktion zur Anzeige des Spielfeldes
void display_maze(GameState game_state)
{
const int player_row = game_state.player.position[0];
const int player_col = game_state.player.position[1];
//cout << "\033[H\033[J"; // ANSI Escape Code zum Loeschen des Bildschirms
for(int i = 0; i < game_state.maze.rows; i++)
{
for(int j = 0; j < game_state.maze.cols; j++)
{
if(i == player_row && j == player_col)
{
cout << 'S';
}
else
{
cout << game_state.maze.data[i][j];
}
cout << " ";
}
// Printet Infomode nach der ersten Zeile falls aktiviert
if(game_state.info_mode && (i == 0)){
if(steps_til_goal(game_state, game_state.player.position) != -1){
cout << steps_til_goal(game_state, game_state.player.position) << " Schritte bis zum Ziel";
}
};
cout << '\n';
}
}
// Funktion zur Umrechnung eines Kommandos zu einer neuen Position
// Vorbedingung: direction muss aus {w, s, a, d} kommen.
vector<int> new_position_by_direction(vector<int> player_position, char direction)
{
const int row = player_position[0];
const int col = player_position[1];
switch(direction)
{
case 'w':
return {row - 1, col};
case 's':
return {row + 1, col};
case 'a':
return {row, col - 1};
case 'd':
return {row, col + 1};
default:
assert(false, "new_position_by_direction: invalid direction, assumes direction is one of {w, s, a, d}.");
return {};
}
}
// Fuehrt Aktionen des Spieler-Feldes aus
// Vorbedingung: Wenn das Feld eine Tuer ist, muss mindestens ein Schluessel zur Verfuegung stehen
GameState process_tile_action(GameState game_state)
{
const int row = game_state.player.position[0];
const int col = game_state.player.position[1];
assert(game_state.maze.data[row][col] != 'T' || game_state.player.no_keys > 0,
"process_tile_action(...) assumes enough keys are there when approaching a door.");
if(game_state.maze.data[row][col] == 'K')
{
++game_state.player.no_keys;
game_state.maze.data[row][col] = '.';
}
else if(game_state.maze.data[row][col] == 'T')
{
--game_state.player.no_keys;
game_state.maze.data[row][col] = '.';
}
else if(game_state.maze.data[row][col] == 'A')
{
game_state.hit_ghost = true;
}
return game_state;
}
// Gibt true zurueck gdw. die Position begehbar ist
bool position_is_walkable(vector<int> position, GameState game_state)
{
const int row = position[0];
const int col = position[1];
if(row < 0 || col < 0)
{
return false;
}
if(row >= game_state.maze.rows || col >= game_state.maze.cols)
{
return false;
}
if(game_state.maze.data[row][col] == '#')
{
return false;
}
if(game_state.maze.data[row][col] == 'T' && game_state.player.no_keys == 0)
{
return false;
}
return true;
}
// Gibt die benoetigte Schritte bis zum Ziel an
// nach vorgegebenen Agorithmus
int steps_til_goal(GameState game_state, vector<int> position, int steps){
if(!position_is_walkable(position, game_state)){
return -1;
}else if(game_state.maze.data[position[0]][position[1]] == 'T'){
return -1;
}else if(game_state.maze.data[position[0]][position[1]] == 'Z'){
return 0;
}else if(steps == 0){
return -1;
}else{
vector <int> possible_ways;
possible_ways.push_back(steps_til_goal(game_state, {position[0]+1,position[1]}, steps -1));
possible_ways.push_back(steps_til_goal(game_state, {position[0]-1,position[1]}, steps -1));
possible_ways.push_back(steps_til_goal(game_state, {position[0],position[1]+1}, steps -1));
possible_ways.push_back(steps_til_goal(game_state, {position[0],position[1]-1}, steps -1));
int min = -1;
for(int i : possible_ways){
if(i == -1){
continue;
}
if((min == -1) || (i < min)){
min = i;
}
}
if(min == -1){
return -1;
}else{
return 1 + min;
}
}
return -1;
}
// Funktion zur Bewegung der SpielerIn
GameState move_player(GameState game_state, char direction)
{
vector<int> potential_new_position = new_position_by_direction(game_state.player.position, direction);
if(!position_is_walkable(potential_new_position, game_state))
{
throw BadMovement {};
}
game_state.player.position = potential_new_position;
return process_tile_action(game_state);
}
// Gibt eine kurze Hilfe aus
void display_help()
{
cout << "Willkommen zum Labyrinth-Spiel!\n";
cout << "Ziel des Spiels: Finden Sie den Weg vom Startpunkt (S) zum Ziel (Z).\n";
cout << "Spielfeld-Erklaerung:\n";
cout << "S - Startpunkt: Hier beginnt die SpielerIn.\n";
cout << "Z - Ziel: Erreichen Sie diesen Punkt, um das Spiel zu gewinnen.\n";
cout << "# - Wand: Diese Felder sind nicht begehbar.\n";
cout << "K - Schluessel: Hier können Sie einen Schluessel aufsammeln, um eine Tuer zu oeffnen.\n";
cout << "T - Tuer: Unbegehbares Feld, ausser, Sie haben einen Schluessel. Beim Durchschreiten wird ein Schluessel verbraucht.\n";
cout << "A - Geist: Ein Geist im Labyrinth. Stehen die SpielerIn auf dem selben Feld, verlieren Sie das Spiel!\n";
cout << ". - Leeres Feld: Diese Felder koennen betreten werden.\n";
cout << "\nSteuerung:\n";
cout << "w - Nach oben bewegen\n";
cout << "a - Nach links bewegen\n";
cout << "s - Nach unten bewegen\n";
cout << "d - Nach rechts bewegen\n";
cout << "q - Spiel beenden\n";
cout << "Nach jeder Befehlseingabe muss die Eingabetaste (Enter) gedrueckt werden, um sich zu bewegen.\n";
cout << "\nViel Erfolg im Labyrinth!\n";
}
// Reagiert auf das eingegebene Kommando und gibt an die jeweilige Funktion
// ab, die sich um genau dieses Kommando kuemmert.
GameState process_input(GameState game_state, char input)
{
switch(input)
{
case 'w':
case 's':
case 'a':
case 'd':
return move_player(game_state, input);
case 'i':
toggle_info_mode(game_state);
break;
case 'h':
case 'H':
display_help();
break;
case 'q':
game_state.exit = true;
return game_state;
default:
throw UnknownInput{};
}
return game_state;
}
// Gibt true zurueck, wenn das Ziel erreicht wurde
bool reached_goal(GameState game_state)
{
return game_state.maze.data[game_state.player.position[0]][game_state.player.position[1]] == 'Z';
}
// Gibt true zurueck gdw der Geist getroffen wurde
bool hit_ghost(GameState game_state)
{
return game_state.hit_ghost;
}
// Gibt true zurueck gdw. das Spiel zuende ist
bool is_end_condition(GameState game_state)
{
return reached_goal(game_state) || hit_ghost(game_state) || game_state.exit;
}
// Die Hauptschleife des Spiels
GameState game_loop(GameState game_state)
{
char input;
while(cin && !is_end_condition(game_state))
{
assert(game_state.player.no_keys >= 0,
"Player has a negative number of keys.");
display_maze(game_state);
cin >> input;
if(cin)
{
try
{
game_state = process_input(game_state, input);
}
catch(BadMovement&)
{
cout << "Bewegung nicht moeglich!\n";
}
catch(UnknownInput&)
{
cout << "Diese Eingabe kenne ich nicht. Gib 'h' ein, um eine Hilfe zu erhalten.\n";
}
}
}
return game_state;
}
// Liest ein integer von der Eingabe.
// Vorbedingung: cin ist in Ordnung
int read_int()
{
int integer;
cin >> integer;
if(!cin) {throw BadMaze{};}
return integer;
}
// Liest die Labyrinth-Daten ein.
// Vorbedingung: cin ist ok.
vector<vector<char>> read_maze_data(int rows, int cols)
{
vector<vector<char>> maze_data(rows);
char ch;
for(int i = 0; i < rows * cols; ++i)
{
cin >> ch;
if(!cin) {throw BadMaze {};}
if(!(ch == '#' || ch == 'T' || ch == 'A' || ch == '.' || ch == 'K' || ch == 'Z'))
{
throw BadMaze {};
}
maze_data[i / cols].push_back(ch);
}
return maze_data;
}
// Liest das Labyrinth von der Konsole nach der Formatdefinition aus der Aufgabe
Maze read_maze()
{
int rows = read_int();
int cols = read_int();
if(rows < 1 || cols < 1 || rows > 20 || cols > 20)
{
throw BadMaze {};
}
vector<vector<char>> labyrinth_data = read_maze_data(rows, cols);
int player_row = read_int();
int player_col = read_int();
if(player_row < 0 || player_row >= rows || player_col < 0 || player_col >= cols)
{
throw BadMaze {};
}
if(labyrinth_data[player_row][player_col] != '.')
{
throw BadMaze {};
}
return {rows, cols, labyrinth_data, {player_row, player_col}};
}
// Initialisiert das Labyrinth-Objekt
GameState initialize()
{
Maze maze = read_maze();
Player player{0, maze.player_start_position};
return GameState {maze, player, false, false, false};
}
int main()
{
activateAssertions();
try
{
GameState game_state = initialize();
game_state = game_loop(game_state);
if(reached_goal(game_state))
{
display_maze(game_state);
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
}
else if(hit_ghost(game_state))
{
cout << "Sie haben einen Geist getroffen! Game Over!\n";
}
else
{
cout << "Schoenen Tag noch!\n";
}
return 0;
}
catch(BadMaze&)
{
cout << "Fehler beim Einlesen des Labyrinths.\n";
return 1;
}
catch(...)
{
cout << "Unbekannter Fehler!\n";
return 1;
}
}