feat: seperated move functionality in to separate function

This commit is contained in:
moonleay 2024-12-02 16:12:57 +01:00
parent d73dac0e67
commit bea200b9a1
Signed by: moonleay
GPG key ID: 82667543CCD715FB

103
main.cpp
View file

@ -2,7 +2,8 @@
// A program which allows you to wander across a maze using 'w','a','s' and 'd'.
const vector<vector<char>> play_field = { // playspace[y][x]
const vector<vector<char>> play_field = {
// playspace[y][x]
{'#', '.', '.', '.', '.'},
{'#', '.', '#', '.', '.'},
{'.', 'Z', '#', '.', '.'},
@ -18,6 +19,9 @@ const vector<vector<char>> play_field = { // playspace[y][x]
* # - 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);
/// 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);
@ -29,16 +33,16 @@ void render_play_field(vector<int> player_pos);
int main()
{
// Player position formatted as (y, x)
// Create player position formatted as (y, x)
vector<int> player_pos = {4, 0};
// Create input variable
char input;
// Run the game
// Run the game loop
while (true)
{
// Render the playfield
// Render the play field
render_play_field(player_pos);
// Get input
@ -49,59 +53,60 @@ int main()
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(player_pos, 0, -1))
continue;
// Actually move player
--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))
continue;
// Actually move player
--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))
continue;
// Actually move player
++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))
continue;
// Actually move player
++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";
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;
}
// 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')
{
// Goal reached! Lets celebrate!
// 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;
}
}
// End the program
return 0;
}
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";
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";
}
return player_pos;
}
bool can_move_player(const vector<int> player_pos, const int move_x, const int move_y)
{
// Calculate the position we want to be at
@ -127,9 +132,11 @@ bool is_pos_free(const int x, const int y)
void render_play_field(const vector<int> player_pos)
{
for (int i = 0; i < play_field.size(); ++i)
{ // For every row...
{
// For every row...
for (int j = 0; j < play_field[i].size(); ++j)
{ // ... render ...
{
// ... render ...
if (i == player_pos[0] && j == player_pos[1])
cout << "S "; // ... 'S' if it is the entry where the player is currently
else