epr24pr2-ojanssen2/main.cpp

150 lines
4.4 KiB
C++
Raw Normal View History

2024-11-20 10:31:05 +00:00
#include "std_lib_inc.h"
// A program which allows you to wander across a maze using 'w','a','s' and 'd'.
2024-11-20 10:31:05 +00:00
const vector<vector<char>> play_field = {
// playspace[y][x]
2024-11-20 10:31:05 +00:00
{'#', '.', '.', '.', '.'},
{'#', '.', '#', '.', '.'},
{'.', 'Z', '#', '.', '.'},
{'.', '#', '#', '#', '.'},
2024-12-02 13:53:24 +00:00
{'.', '.', '.', '.', '.'}
// ^ player starts here (4,0)
2024-11-20 10:31:05 +00:00
};
2024-12-02 13:53:24 +00:00
/* legend
* S - player
* Z - goal
* . - empty space
* # - wall
*/
/// Use inputs to move the player. Don't move the player, if move is illegal
vector<int> move_player(vector<int> player_pos, char input);
2024-12-02 13:53:24 +00:00
/// Check if the player could be moved to a certain space
bool can_move_player(vector<int> player_pos, int move_x, int move_y);
2024-12-02 13:53:24 +00:00
/// 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(vector<int> player_pos);
2024-11-20 10:31:05 +00:00
2024-12-02 13:53:24 +00:00
int main()
{
// Create player position formatted as (y, x)
vector<int> player_pos = {4, 0};
2024-12-02 14:02:19 +00:00
// Create input variable
2024-12-02 13:53:24 +00:00
char input;
2024-11-25 14:40:01 +00:00
// Run the game loop
2024-12-02 13:53:24 +00:00
while (true)
{
// Render the play field
render_play_field(player_pos);
2024-12-02 14:02:19 +00:00
// Get input
2024-12-02 13:53:24 +00:00
cin >> input;
2024-12-02 14:02:19 +00:00
// Vaildate input
2024-12-02 13:53:24 +00:00
if (!cin)
{
cout << "Diese Eingabe kenne ich nicht. Gib 'h' ein, um eine Hilfe zu erhalten.\n";
continue;
}
// move player based on inputs, don't move player, if move would be illegal
player_pos = move_player(player_pos, input);
2024-11-25 14:40:01 +00:00
2024-12-02 14:02:19 +00:00
// Check if the goal has been reached yet
if (play_field[player_pos[0]][player_pos[1]] == 'Z')
2024-12-02 13:53:24 +00:00
{
// Render the play field one last time to display the completed maze
render_play_field(player_pos);
// Goal reached! Let's celebrate!
2024-12-02 13:53:24 +00:00
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
break;
}
2024-11-25 14:40:01 +00:00
}
// End the program
2024-12-02 13:53:24 +00:00
return 0;
2024-11-25 14:40:01 +00:00
}
vector<int> move_player(vector<int> player_pos, char input)
{
switch (input)
{
case 'w':
// Check if it is possible to move player to target pos
if (can_move_player(player_pos, 0, -1))
--player_pos[0];
break;
case 'a':
// Check if it is possible to move player to target pos
if (can_move_player(player_pos, -1, 0))
--player_pos[1];
// Actually move player
break;
case 's':
// Check if it is possible to move player to target pos
if (can_move_player(player_pos, 0, 1))
++player_pos[0];
break;
case 'd':
// Check if it is possible to move player to target pos
if (can_move_player(player_pos, 1, 0))
++player_pos[1];
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";
break;
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";
break;
}
return player_pos;
}
bool can_move_player(const vector<int> player_pos, const int move_x, const int move_y)
2024-12-02 13:53:24 +00:00
{
2024-12-02 14:02:19 +00:00
// Calculate the position we want to be at
const int target_x = player_pos[1] + move_x;
const int target_y = player_pos[0] + move_y;
2024-11-25 14:40:01 +00:00
2024-12-02 14:02:19 +00:00
// Check if the position can be moved to
2024-12-02 13:53:24 +00:00
const bool result = is_pos_free(target_x, target_y);
2024-12-02 14:02:19 +00:00
if (!result) // Complain if not
2024-12-02 13:53:24 +00:00
cout << "Bewegung nicht moeglich!\n";
2024-12-02 14:02:19 +00:00
return result; // Return result
2024-12-02 13:53:24 +00:00
}
2024-11-25 14:40:01 +00:00
2024-12-02 13:53:24 +00:00
bool is_pos_free(const int x, const int y)
{
if (x < 0 || y < 0 || x > 4 || y > 4)
2024-12-02 14:02:19 +00:00
return false; // Target pos out of bounds of area
2024-12-02 13:53:24 +00:00
if (play_field[y][x] == '#')
2024-12-02 14:02:19 +00:00
return false; // Target pos is a wall
return true; // Target pos is movable to (is not wall and not out of bounds)
2024-12-02 13:53:24 +00:00
}
2024-11-25 14:40:01 +00:00
void render_play_field(const vector<int> player_pos)
2024-12-02 13:53:24 +00:00
{
for (int i = 0; i < play_field.size(); ++i)
{
// For every row...
2024-12-02 13:53:24 +00:00
for (int j = 0; j < play_field[i].size(); ++j)
{
// ... render ...
if (i == player_pos[0] && j == player_pos[1])
2024-12-02 14:02:19 +00:00
cout << "S "; // ... 'S' if it is the entry where the player is currently
2024-12-02 13:53:24 +00:00
else
2024-12-02 14:02:19 +00:00
cout << play_field[i][j]; // ... the actual entry of the field
2024-11-25 14:40:01 +00:00
}
2024-12-02 14:02:19 +00:00
cout << "\n"; // Finish the row by ending the actively drawn line
2024-11-25 14:40:01 +00:00
}
}