chore: added more comments
This commit is contained in:
parent
e875eba6dc
commit
d5b9ca8a7e
1 changed files with 33 additions and 12 deletions
45
main.cpp
45
main.cpp
|
@ -29,53 +29,72 @@ void render_play_field(int pos_x, int pos_y);
|
|||
|
||||
int main()
|
||||
{
|
||||
// Current player position (y, x)
|
||||
// Player (y, x)
|
||||
int pos_x = 0;
|
||||
int pos_y = 4;
|
||||
|
||||
// Create input variable
|
||||
char input;
|
||||
|
||||
// Run the game
|
||||
while (true)
|
||||
{
|
||||
// Render the playfield
|
||||
render_play_field(pos_x, pos_y);
|
||||
|
||||
// Get input
|
||||
cin >> input;
|
||||
// Vaildate input
|
||||
if (!cin)
|
||||
{
|
||||
cout << "Diese Eingabe kenne ich nicht. Gib 'h' ein, um eine Hilfe zu erhalten.\n";
|
||||
continue;
|
||||
}
|
||||
// Check what action to perform based on input
|
||||
switch (input)
|
||||
{
|
||||
case 'w':
|
||||
// Check if it is possible to move player to target pos
|
||||
if (!can_move_player(pos_x, pos_y, 0, -1))
|
||||
continue;
|
||||
// Actually move player
|
||||
--pos_y;
|
||||
break;
|
||||
case 'a':
|
||||
// Check if it is possible to move player to target pos
|
||||
if (!can_move_player(pos_x, pos_y, -1, 0))
|
||||
continue;
|
||||
// Actually move player
|
||||
--pos_x;
|
||||
break;
|
||||
case 's':
|
||||
// Check if it is possible to move player to target pos
|
||||
if (!can_move_player(pos_x, pos_y, 0, 1))
|
||||
continue;
|
||||
// Actually move player
|
||||
++pos_y;
|
||||
break;
|
||||
case 'd':
|
||||
// Check if it is possible to move player to target pos
|
||||
if (!can_move_player(pos_x, pos_y, 1, 0))
|
||||
continue;
|
||||
// Actually move player
|
||||
++pos_x;
|
||||
break;
|
||||
case 'h':
|
||||
// Print help text to console
|
||||
cout << "Gebe 'w', 'a', 's', oder 'd' ein zum bewegen. Gebe 'h' ein um diesen Text anzuzeigen\n";
|
||||
continue;
|
||||
default:
|
||||
// Target action not recognized. Print 'error' message to screen
|
||||
cout << "Diese Eingabe kenne ich nicht. Gib 'h' ein, um eine Hilfe zu erhalten.\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Check if the goal has been reached yet
|
||||
if (play_field[pos_y][pos_x] == 'Z')
|
||||
{
|
||||
// Goal reached! Lets celebrate!
|
||||
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
|
||||
break;
|
||||
}
|
||||
|
@ -86,35 +105,37 @@ int main()
|
|||
|
||||
bool can_move_player(const int pos_x, const int pos_y, const int move_x, const int move_y)
|
||||
{
|
||||
// Calculate the position we want to be at
|
||||
const int target_x = pos_x + move_x;
|
||||
const int target_y = pos_y + move_y;
|
||||
|
||||
// Check if the position can be moved to
|
||||
const bool result = is_pos_free(target_x, target_y);
|
||||
if (!result)
|
||||
if (!result) // Complain if not
|
||||
cout << "Bewegung nicht moeglich!\n";
|
||||
return result;
|
||||
return result; // 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
|
||||
return false; // Target pos out of bounds of area
|
||||
if (play_field[y][x] == '#')
|
||||
return false; // target is wall
|
||||
return true; // target is empty or goal
|
||||
return false; // Target pos is a wall
|
||||
return true; // Target pos is movable to (is not wall and not out of bounds)
|
||||
}
|
||||
|
||||
void render_play_field(const int pos_x, const int pos_y)
|
||||
{
|
||||
for (int i = 0; i < play_field.size(); ++i)
|
||||
{ // every row
|
||||
{ // For every row...
|
||||
for (int j = 0; j < play_field[i].size(); ++j)
|
||||
{ // every entry in row
|
||||
{ // ... render ...
|
||||
if (i == pos_y && j == pos_x)
|
||||
cout << "S ";
|
||||
cout << "S "; // ... 'S' if it is the entry where the player is currently
|
||||
else
|
||||
cout << play_field[i][j];
|
||||
cout << play_field[i][j]; // ... the actual entry of the field
|
||||
}
|
||||
cout << "\n";
|
||||
cout << "\n"; // Finish the row by ending the actively drawn line
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue