Changes by Online Editor
This commit is contained in:
parent
54a15e4cf4
commit
151fc25770
1 changed files with 100 additions and 5 deletions
105
main.cpp
105
main.cpp
|
@ -6,7 +6,7 @@ constexpr int kCols = 5;
|
|||
|
||||
// Labyrinth-Definition
|
||||
// Interpretiere als Zeilen, dann Spalten
|
||||
const vector<vector<char>> kMaze = {
|
||||
const vector<vector<char> > kMaze = {
|
||||
{'#', '.', '.', '.', '.'},
|
||||
{'#', '.', '#', '.', '.'},
|
||||
{'.', 'Z', '#', '.', '.'},
|
||||
|
@ -17,7 +17,102 @@ const vector<vector<char>> kMaze = {
|
|||
// Startposition des Spielers
|
||||
const vector<int> kPlayerStartPosition = {4, 0};
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 << ' ';
|
||||
}
|
||||
}
|
||||
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 << "Die 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