Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

refactor: ♻️ refactor almost everything #1

Merged
merged 8 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ project(robots VERSION 0.1.0)
include(CTest)
enable_testing()

add_executable(robots src/main.cpp src/utils.cpp src/entities.cpp)
add_executable(robots src/main.cpp
src/game.cpp src/files.cpp src/info.cpp
src/keyboard.cpp src/logic.cpp src/mazes.cpp
src/screen.cpp src/timer.cpp src/utf8.cpp
)

include_directories(robots include/)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
Expand Down
10 changes: 0 additions & 10 deletions MAZE_01.TXT

This file was deleted.

33 changes: 0 additions & 33 deletions include/entities.h

This file was deleted.

64 changes: 64 additions & 0 deletions include/files.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef FILES_H
#define FILES_H

#include <fstream>
#include "mazes.h"

namespace files {

/**
* @brief Returns the name of the file containing the given maze.
*
* @param number The number of the maze.
* @return The name of the file containing the given maze.
*/
string get_maze_file_name(unsigned int number);

/**
* @brief Returns the name of the file containing the given maze's winners.
*
* @param number The number of the maze.
* @return The name of the file containing the given maze's winners.
*/
string get_maze_winners_file_name(unsigned int number);

/**
* @brief Opens the given file for reading.
* If the file doesn't exist, an error is thrown.
*
* @param file_name The name of the file.
* @return An open stream to the given file.
*/
ifstream open_file_reader(string file_name);

/**
* @brief Opens the given file for writing.
* If the file doesn't exist and it couldn't be created, an error is thrown.
*
* @param file_name The name of the file.
* @return An open stream to the given file.
*/
ofstream open_file_writer(string file_name);

/**
* @brief Returns the requested maze's data.
* If the given maze file doesn't exist, can't be read or doesn't fulfill the expected format
* exceptions are thrown.
*
* @param maze The number of the maze.
* @return The requested maze's data, if it exists.
*/
mazes::Maze read_maze(unsigned int maze);

/**
* @brief Writes the player's name and score in the winner's file. The entries are sorted in ascending score order.
* If the file doesn't exist or doesn't fulfill the expected format, exceptions are thrown.
*
* @param maze The number of the maze.
* @param player_name The given player's name.
* @param player_score The player's score.
*/
void save_maze_score(unsigned int maze, string player_name, time_t player_score);
} // namespace files

#endif
31 changes: 31 additions & 0 deletions include/game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef GAME_H
#define GAME_H

/**
* @brief Asks the user for the map he/she wants to play on.
*
* @return The chosen maze's number.
*/
mazes::Maze ask_maze();

/**
* @brief Asks the user for a move to play.
*
* @param maze The maze the player is playing on.
* @return A letter corresponding to the move.
*/
char ask_move(const mazes::Maze &maze);

/**
* @brief Asks the user for his/her name.
*
* @return The name provided by the user.
*/
string ask_name();

/**
* @brief Plays the game.
*/
void play_game();

#endif
22 changes: 22 additions & 0 deletions include/info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef INFO_H
#define INFO_H

namespace info {

/**
* @brief Displays the game's main menu.
*/
void menu();

/**
* @brief Displays the game's rules.
*/
void rules();

/**
* @brief Displays the game's exit message.
*/
void bye();
} // namespace info

#endif
70 changes: 70 additions & 0 deletions include/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H

#include <string>
#include <functional>

namespace keyboard {

struct Name {
std::string data;
};

/**
* @brief Blocks execution until the user presses the RETURN key.
*/
void wait_for_enter();

/**
* @brief Prompts the user to provide a value through console input.
* On interactive terminals, it will be done in a "fancy" fashion and in a single line.
* Otherwise, it will be done in a more "traditional" manner.
*
* @param prompt The text that will be used to prompt the user for a value.
* @param warning The error message that will be presented to the user if the value could not be parsed or if the validator function returns false.
* @param result The variable where the result will be stored.
* @param validator An optional function that determines if the value is valid.
* This function is only executed after the parsing of the value was successful.
* If this function throws a string, it will be presented to the user as an error message.
* If this function returns false, the warning message will be presented to the user.
*
* @return true, if the input is valid, or false, if cin has reached EOF.
*/
bool read_value(const std::string prompt, const std::string warning, unsigned int &result, const std::function<bool(unsigned int)> validator = [](unsigned int res) { return true; });

/**
* @brief Prompts the user to provide a value through console input.
* On interactive terminals, it will be done in a "fancy" fashion and in a single line.
* Otherwise, it will be done in a more "traditional" manner.
*
* @param prompt The text that will be used to prompt the user for a value.
* @param warning The error message that will be presented to the user if the value could not be parsed or if the validator function returns false.
* @param result The variable where the result will be stored.
* @param validator An optional function that determines if the value is valid.
* This function is only executed after the parsing of the value was successful.
* If this function throws a string, it will be presented to the user as an error message.
* If this function returns false, the warning message will be presented to the user.
*
* @return true, if the input is valid, or false, if cin has reached EOF.
*/
bool read_value(const std::string prompt, const std::string warning, char &result, const std::function<bool(char)> validator = [](char res) { return true; });

/**
* @brief Prompts the user to provide a value through console input.
* On interactive terminals, it will be done in a "fancy" fashion and in a single line.
* Otherwise, it will be done in a more "traditional" manner.
*
* @param prompt The text that will be used to prompt the user for a value.
* @param warning The error message that will be presented to the user if the value could not be parsed or if the validator function returns false.
* @param result The variable where the result will be stored.
* @param validator An optional function that determines if the value is valid.
* This function is only executed after the parsing of the value was successful.
* If this function throws a string, it will be presented to the user as an error message.
* If this function returns false, the warning message will be presented to the user.
*
* @return true, if the input is valid, or false, if cin has reached EOF.
*/
bool read_value(const std::string prompt, const std::string warning, Name &result, const std::function<bool(Name)> validator = [](Name res) { return true; });
} // namespace keyboard

#endif
72 changes: 72 additions & 0 deletions include/logic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "mazes.h"

namespace logic {

/**
* @brief Translates the given direction to horizontal and a vertical variations.
*
* @param direction The player's move.
* @param dx The variable to store the variation on the horizontal direction associated with the provided direction on.
* @param dy The variable to store the variation on the vertcal direction associated with the provided direction on.
*/
void get_deltas_from_direction(const char direction, int &dx, int &dy);

namespace player {
/**
* @brief Checks whether the given move is valid.
*
* @param maze The maze the player is playing on.
* @param dx The change in the player's horizontal position.
* @param dy The change in the player's vertical position.
* @return true, if the given move is valid, or false, otherwise.
*/
bool is_move_valid(const mazes::Maze &maze, const int dx, const int dy);

/**
* @brief Checks whether the given move is valid.
*
* @param maze The maze the player is playing on.
* @param direction The player's move.
* @return true, if the move is valid, or false, otherwise.
*/
bool is_move_valid(const mazes::Maze &maze, char direction);

/**
* @brief Moves the player.
*
* @param maze The maze the player is playing on.
* @param dx The change in the player's horizontal position.
* @param dy The change in the player's vertical position.
*/
void move(mazes::Maze &maze, int dx, int dy);

/**
* @brief Moves the player.
*
* @param maze The maze the player is playing on.
* @param direction The player's move.
*/
void move(mazes::Maze &maze, char direction);
} // namespace player

namespace robot {

/**
* @brief Provides the horizontal and vertical variations that get the robot the closest to the player.
*
* @param maze The maze the player is playing on.
* @param robot A robot.
* @param dx The change in the robot's horizontal position.
* @param dy The change in the robot's vertical position.
*/
void get_suggested_deltas(const mazes::Maze &maze, const mazes::Robot &robot, int &dx, int &dy);

/**
* @brief Moves the robot.
*
* @param maze The maze the player is playing on.
* @param robot A robot.
*/
void move(mazes::Maze &maze, mazes::Robot &robot);
} // namespace robot
} // namespace logic
Loading