feat: finished basic task
This commit is contained in:
parent
50335b3ff9
commit
5da1b434cf
1 changed files with 105 additions and 103 deletions
208
main.cpp
208
main.cpp
|
@ -1,118 +1,120 @@
|
|||
#include "std_lib_inc.h"
|
||||
|
||||
// Labyrinth-Dimensionen als konstanter Ausdruck
|
||||
constexpr int kRows = 5;
|
||||
constexpr int kCols = 5;
|
||||
// A programm which allows you to wander across a maze using 'w','a','s' and 'd'.
|
||||
|
||||
// Labyrinth-Definition
|
||||
// Interpretiere als Zeilen, dann Spalten
|
||||
const vector<vector<char> > kMaze = {
|
||||
const vector<vector<char>> play_field = { // playspace[y][x]
|
||||
{'#', '.', '.', '.', '.'},
|
||||
{'#', '.', '#', '.', '.'},
|
||||
{'.', 'Z', '#', '.', '.'},
|
||||
{'.', '#', '#', '#', '.'},
|
||||
{'.', '.', '.', '.', '.'},
|
||||
{'.', '.', '.', '.', '.'}
|
||||
// ^ player starts here (4,0)
|
||||
};
|
||||
|
||||
// Startposition des Spielers
|
||||
const vector<int> kPlayerStartPosition = {4, 0};
|
||||
/* legend
|
||||
* S - player
|
||||
* Z - goal
|
||||
* . - empty space
|
||||
* # - wall
|
||||
*/
|
||||
|
||||
/// Check if the player could be moved to a certain space
|
||||
bool can_move_player(int pos_x, int pos_y, int move_x, int move_y);
|
||||
|
||||
/// Check if a certain space can be moved to
|
||||
bool is_pos_free(int x, int y);
|
||||
|
||||
/// Render the current playfield to the console
|
||||
void render_play_field(int pos_x, int pos_y);
|
||||
|
||||
int main()
|
||||
{
|
||||
// Current player position (y, x)
|
||||
int pos_x = 0;
|
||||
int pos_y = 4;
|
||||
char input;
|
||||
|
||||
while (true)
|
||||
{
|
||||
render_play_field(pos_x, pos_y);
|
||||
cin >> input;
|
||||
if (!cin)
|
||||
{
|
||||
cout << "Diese Eingabe kenne ich nicht. Gib 'h' ein, um eine Hilfe zu erhalten.\n";
|
||||
continue;
|
||||
}
|
||||
switch (input)
|
||||
{
|
||||
case 'w':
|
||||
if (!can_move_player(pos_x, pos_y, 0, -1))
|
||||
continue;
|
||||
--pos_y;
|
||||
break;
|
||||
case 'a':
|
||||
if (!can_move_player(pos_x, pos_y, -1, 0))
|
||||
continue;
|
||||
--pos_x;
|
||||
break;
|
||||
case 's':
|
||||
if (!can_move_player(pos_x, pos_y, 0, 1))
|
||||
continue;
|
||||
++pos_y;
|
||||
break;
|
||||
case 'd':
|
||||
if (!can_move_player(pos_x, pos_y, 1, 0))
|
||||
continue;
|
||||
++pos_x;
|
||||
break;
|
||||
case 'h':
|
||||
cout << "Gebe 'w', 'a', 's', oder 'd' ein zum bewegen. Gebe 'h' ein um diesen Text anzuzeigen\n";
|
||||
continue;
|
||||
default:
|
||||
cout << "Diese Eingabe kenne ich nicht. Gib 'h' ein, um eine Hilfe zu erhalten.\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
void printMaze(vector<int> &kPlayerCurrentPosition) {
|
||||
for (int current_row = 0; current_row < kMaze.size(); current_row++) {
|
||||
for (int current_column = 0; current_column < kMaze[current_row].size(); current_column++) {
|
||||
//Gebe aktuelle Spielepositon aus.
|
||||
if (kPlayerCurrentPosition[0] == current_row && kPlayerCurrentPosition[1] == current_column) {
|
||||
cout << 'S';
|
||||
} else {
|
||||
//Sonst gebe den Feldinhalt aus.d
|
||||
cout << kMaze[current_row][current_column];
|
||||
}
|
||||
// Aufeinanderfolgende Felder trennen
|
||||
if (current_column != kMaze[current_row].size() - 1) {
|
||||
cout << ' ';
|
||||
}
|
||||
if (play_field[pos_y][pos_x] == 'Z')
|
||||
{
|
||||
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool can_move_player(const int pos_x, const int pos_y, const int move_x, const int move_y)
|
||||
{
|
||||
const int target_x = pos_x + move_x;
|
||||
const int target_y = pos_y + move_y;
|
||||
|
||||
const bool result = is_pos_free(target_x, target_y);
|
||||
if (!result)
|
||||
cout << "Bewegung nicht moeglich!\n";
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_pos_free(const int x, const int y)
|
||||
{
|
||||
if (x < 0 || y < 0 || x > 4 || y > 4)
|
||||
return false; // target out of bounds of area
|
||||
if (play_field[y][x] == '#')
|
||||
return false; // target is wall
|
||||
return true; // target is empty or goal
|
||||
}
|
||||
|
||||
void render_play_field(const int pos_x, const int pos_y)
|
||||
{
|
||||
for (int i = 0; i < play_field.size(); ++i)
|
||||
{ // every row
|
||||
for (int j = 0; j < play_field[i].size(); ++j)
|
||||
{ // every entry in row
|
||||
if (i == pos_y && j == pos_x)
|
||||
cout << "S ";
|
||||
else
|
||||
cout << play_field[i][j];
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft ob die vector<int> end_postion beschriebenen Postition eine gültige Postion ist.
|
||||
* @param kPlayerNewPosition
|
||||
* @return false end_postion eine ungültige Postion beschreibt.
|
||||
*/
|
||||
bool valid_move(const vector<int> &kPlayerNewPosition) {
|
||||
//Ausserhalb des Labyrinths
|
||||
if (kPlayerNewPosition[0] < 0 || kPlayerNewPosition[0] >= kRows || kPlayerNewPosition[1] < 0 || kPlayerNewPosition[
|
||||
1] >= kCols) {
|
||||
return false;
|
||||
}
|
||||
//Endpostion ist eine Wand
|
||||
if (kMaze[kPlayerNewPosition[0]][kPlayerNewPosition[1]] == '#') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool goal_reached(const vector<int> &kPlayerCurrentPosition) {
|
||||
//Ziel erreicht
|
||||
if (kMaze[kPlayerCurrentPosition[0]][kPlayerCurrentPosition[1]] == 'Z') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool move(vector<int> &kPlayerCurrentPosition, const int rows, const int colums) {
|
||||
vector<int> kPlayerNewPosition = {kPlayerCurrentPosition[0] + rows, kPlayerCurrentPosition[1] + colums};
|
||||
if (not valid_move(kPlayerNewPosition)) {
|
||||
cout << "Bewegung nicht moeglich!\n";
|
||||
return false;
|
||||
}
|
||||
//Bewegung ist Gültig, wir setzen die neue Postion.
|
||||
kPlayerCurrentPosition = kPlayerNewPosition;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
vector<int> kPlayerCurrentPositon = kPlayerStartPosition;
|
||||
//Übergabe von Parametern als Refernz (Copy by Referenz)
|
||||
printMaze(kPlayerCurrentPositon);
|
||||
|
||||
|
||||
char input;
|
||||
//Starte Gameloop
|
||||
while (true) {
|
||||
cin >> input;
|
||||
if (!cin) {
|
||||
cout << "Eingabe beendet\n"; // Beendet das Programm bei ungültiger Eingabe
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (input) {
|
||||
case 'w':
|
||||
move(kPlayerCurrentPositon, -1, 0);
|
||||
break;
|
||||
case 'a':
|
||||
move(kPlayerCurrentPositon, 0, -1);
|
||||
break;
|
||||
case 's':
|
||||
move(kPlayerCurrentPositon, 1, 0);
|
||||
break;
|
||||
case 'd':
|
||||
move(kPlayerCurrentPositon, 0, 1);
|
||||
break;
|
||||
case 'h':
|
||||
cout << "Beliebiger Hilfetext. Good Luck!\n";
|
||||
break;
|
||||
default:
|
||||
cout << "Diese Eingabe kenne ich nicht. Gib 'h' ein, um eine Hilfe zu erhalten.\n";
|
||||
}
|
||||
printMaze(kPlayerCurrentPositon);
|
||||
if (goal_reached(kPlayerCurrentPositon)) {
|
||||
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue