epr24pr2-ojanssen2/main.cpp

144 lines
4.3 KiB
C++
Raw Normal View History

2024-11-20 11:31:05 +01: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 11:31:05 +01:00
const vector<vector<char>> kMaze = {
// kMaze[y][x]
2024-11-20 11:31:05 +01:00
{'#', '.', '.', '.', '.'},
{'#', '.', '#', '.', '.'},
{'.', 'Z', '#', '.', '.'},
{'.', '#', '#', '#', '.'},
2024-12-02 14:53:24 +01:00
{'.', '.', '.', '.', '.'}
// ^ player starts here (4,0)
2024-11-20 11:31:05 +01:00
};
const vector<int> kPlayerStartPosition = {4,0};
2024-12-02 14:53:24 +01:00
/* legend
* S - player
* Z - goal
* . - empty space
* # - wall
*/
/// Use inputs to move the player. Don't move the player, if move is illegal
/// @param player_pos The current player position
/// @param move_vector The requested move vector
/// @return The new position of the player
vector<int> move_player(vector<int> player_pos, vector<int> move_vector);
2024-12-02 14:53:24 +01:00
/// Check if a certain space can be moved to
/// @param target The position to check
/// @return If the position can be moved to
bool is_pos_free(vector<int> target);
2024-12-02 14:53:24 +01:00
/// Check if a certain space can be moved to
/// @param player_pos The current player position
void render_play_field(vector<int> player_pos);
2024-11-20 11:31:05 +01:00
2024-12-02 14:53:24 +01:00
int main()
{
// Create player position formatted as (y, x)
vector<int> player_pos = kPlayerStartPosition;
2024-12-02 15:02:19 +01:00
// Create input variable
2024-12-02 14:53:24 +01:00
char input;
2024-11-25 15:40:01 +01:00
// Run the game loop
2024-12-02 14:53:24 +01:00
while (true)
{
// Render the play field
render_play_field(player_pos);
2024-12-02 15:02:19 +01:00
// Check if the goal has been reached yet
if (kMaze[player_pos[0]][player_pos[1]] == 'Z')
2024-12-02 14:53:24 +01:00
{
// Goal reached! Let's celebrate!
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
break;
2024-12-02 14:53:24 +01:00
}
// Get input
cin >> input;
2024-11-25 15:40:01 +01:00
// Create an empty vector to store the requested movement in
vector<int> target_movement_vector = {0, 0};
// Check what the player wants to do. Store the requested movement in the vector
// print messages to screen if help was requested or an unknown input was entered
switch (input)
{
case 'w':
target_movement_vector = {-1, 0};
break;
case 'a':
target_movement_vector = {0, -1};
break;
case 's':
target_movement_vector = {1, 0};
break;
case 'd':
target_movement_vector = {0, 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";
2024-12-02 14:53:24 +01:00
break;
}
// Check & apply the requested movement
player_pos = move_player(player_pos, target_movement_vector);
2024-11-25 15:40:01 +01:00
}
// End the program
2024-12-02 14:53:24 +01:00
return 0;
2024-11-25 15:40:01 +01:00
}
vector<int> move_player(vector<int> player_pos, vector<int> move_vector)
{
// Calculate the position the player wants to move to
const vector<int> target_position = { player_pos[0] + move_vector[0], player_pos[1] + move_vector[1] };
// Move player to requested position in case position is free
if (is_pos_free(target_position))
{
player_pos[0] += move_vector[0];
player_pos[1] += move_vector[1];
}
else
2024-12-02 14:53:24 +01:00
cout << "Bewegung nicht moeglich!\n";
// Return the player position
return player_pos;
2024-12-02 14:53:24 +01:00
}
2024-11-25 15:40:01 +01:00
bool is_pos_free(vector<int> target)
2024-12-02 14:53:24 +01:00
{
if (target[1] < 0 || target[0] < 0 || target[1] > kMaze.size() - 1 || target[0] > kMaze[target[1]].size() - 1)
2024-12-02 15:02:19 +01:00
return false; // Target pos out of bounds of area
if (kMaze[target[0]][target[1]] == '#')
2024-12-02 15:02:19 +01: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 14:53:24 +01:00
}
2024-11-25 15:40:01 +01:00
void render_play_field(const vector<int> player_pos)
2024-12-02 14:53:24 +01:00
{
for (int i = 0; i < kMaze.size(); ++i)
{
// For every row...
for (int j = 0; j < kMaze[i].size(); ++j)
{
// ... render ...
if (i == player_pos[0] && j == player_pos[1])
2024-12-03 13:46:50 +01:00
cout << "S"; // ... 'S' if it is the entry where the player is currently
2024-12-02 14:53:24 +01:00
else
cout << kMaze[i][j]; // ... the actual entry of the field
cout << " "; // Add space between entries
2024-11-25 15:40:01 +01:00
}
2024-12-02 15:02:19 +01:00
cout << "\n"; // Finish the row by ending the actively drawn line
2024-11-25 15:40:01 +01:00
}
}