From c063fe626b8d83cf38a2e0831475bbdb98cb42de Mon Sep 17 00:00:00 2001 From: moonleay Date: Fri, 6 Dec 2024 11:18:18 +0100 Subject: [PATCH] feat: changed functions around, improved separation of concerns --- main.cpp | 143 +++++++++++++++++++++++++++---------------------------- 1 file changed, 69 insertions(+), 74 deletions(-) diff --git a/main.cpp b/main.cpp index 88622af..05a64e8 100644 --- a/main.cpp +++ b/main.cpp @@ -2,8 +2,8 @@ // A program which allows you to wander across a maze using 'w','a','s' and 'd'. -const vector> play_field = { - // playspace[y][x] +const vector> kMaze = { + // kMaze[y][x] {'#', '.', '.', '.', '.'}, {'#', '.', '#', '.', '.'}, {'.', 'Z', '#', '.', '.'}, @@ -12,6 +12,8 @@ const vector> play_field = { // ^ player starts here (4,0) }; +const vector kPlayerStartPosition = {4,0}; + /* legend * S - player * Z - goal @@ -20,21 +22,24 @@ const vector> play_field = { */ /// Use inputs to move the player. Don't move the player, if move is illegal -vector move_player(vector player_pos, char input); - -/// Check if the player could be moved to a certain space -bool can_move_player(vector player_pos, int move_x, int move_y); +/// @param player_pos The current player position +/// @param move_vector The requested move vector +/// @return The new position of the player +vector move_player(vector player_pos, vector move_vector); /// Check if a certain space can be moved to -bool is_pos_free(int x, int y); +/// @param target The position to check +/// @return If the position can be moved to +bool is_pos_free(vector target); -/// Render the current playfield to the console +/// Check if a certain space can be moved to +/// @param player_pos The current player position void render_play_field(vector player_pos); int main() { // Create player position formatted as (y, x) - vector player_pos = {4, 0}; + vector player_pos = kPlayerStartPosition; // Create input variable char input; @@ -45,103 +50,93 @@ int main() // Render the play field render_play_field(player_pos); - // Get input - cin >> input; - // Vaildate input - 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); - // Check if the goal has been reached yet - if (play_field[player_pos[0]][player_pos[1]] == 'Z') + if (kMaze[player_pos[0]][player_pos[1]] == 'Z') { - // Render the play field one last time to display the completed maze - render_play_field(player_pos); - // Goal reached! Let's celebrate! cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n"; break; } + + // Get input + cin >> input; + + // Create an empty vector to store the requested movement in + vector 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"; + break; + } + + // Check & apply the requested movement + player_pos = move_player(player_pos, target_movement_vector); } // End the program return 0; } -vector move_player(vector player_pos, char input) +vector move_player(vector player_pos, vector move_vector) { - switch (input) + // Calculate the position the player wants to move to + const vector 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)) { - 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]; - 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; + player_pos[0] += move_vector[0]; + player_pos[1] += move_vector[1]; } + else + cout << "Bewegung nicht moeglich!\n"; + + // Return the player position return player_pos; } -bool can_move_player(const vector player_pos, const int move_x, const int move_y) +bool is_pos_free(vector target) { - // 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; - - // Check if the position can be moved to - const bool result = is_pos_free(target_x, target_y); - if (!result) // Complain if not - cout << "Bewegung nicht moeglich!\n"; - return result; // Return result -} - -bool is_pos_free(const int x, const int y) -{ - if (x < 0 || y < 0 || x > 4 || y > 4) + if (target[1] < 0 || target[0] < 0 || target[1] > kMaze.size() - 1 || target[0] > kMaze[target[1]].size() - 1) return false; // Target pos out of bounds of area - if (play_field[y][x] == '#') + if (kMaze[target[0]][target[1]] == '#') 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 vector player_pos) { - for (int i = 0; i < play_field.size(); ++i) + for (int i = 0; i < kMaze.size(); ++i) { // For every row... - for (int j = 0; j < play_field[i].size(); ++j) + for (int j = 0; j < kMaze[i].size(); ++j) { // ... render ... if (i == player_pos[0] && j == player_pos[1]) cout << "S"; // ... 'S' if it is the entry where the player is currently else - cout << play_field[i][j]; // ... the actual entry of the field + cout << kMaze[i][j]; // ... the actual entry of the field + cout << " "; // Add space between entries } cout << "\n"; // Finish the row by ending the actively drawn line }