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 6 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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.

48 changes: 48 additions & 0 deletions include/files.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#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);

mazes::Maze read_maze(unsigned int maze);

void save_maze_score(unsigned int maze, string player_name, time_t player_score);
} // namespace files

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

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
55 changes: 55 additions & 0 deletions include/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#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" fashing 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" fashing 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; });

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
22 changes: 22 additions & 0 deletions include/logic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "mazes.h"

namespace logic {
void get_deltas_from_direction(const char direction, int &dx, int &dy);

namespace player {
bool is_move_valid(const mazes::Maze &maze, const int dx, const int dy);

bool is_move_valid(const mazes::Maze &maze, char direction);

void move(mazes::Maze &maze, int dx, int dy);

void move(mazes::Maze &maze, char direction);
} // namespace player

namespace robot {

void get_suggested_deltas(const mazes::Maze &maze, const mazes::Robot &robot, int &dx, int &dy);

void move(mazes::Maze &maze, mazes::Robot &robot);
} // namespace robot
} // namespace logic
138 changes: 138 additions & 0 deletions include/mazes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#ifndef MAZES_H
#define MAZES_H

#include <vector>

using namespace std;

namespace mazes {
namespace masks {

/**
* @brief Bitmask that indicates whether a cell has a barrier in it.
*/
const unsigned int BARRIER = 1;

/**
* @brief Bitmask that indicates whether a cell has a human in it.
*/
const unsigned int HUMAN = 2;

/**
* @brief Bitmask that indicates whether a cell has a robot in it.
*/
const unsigned int ROBOT = 4;

/**
* @brief Bitmask that indicates whether a cell has something dead in it.
*/
const unsigned int DEAD = 8;
} // namespace masks

/**
* @brief Default value for a cell.
*/
const unsigned int EMPTY = 0;

/**
* @brief Represents a player.
*
*/
struct Player {
size_t x;
size_t y;
};

/**
* @brief Represents a robot.
*/
struct Robot {
size_t id;
size_t x;
size_t y;
};

/**
* @brief Represents a game maze.
*/
struct Maze {
unsigned int id;

size_t width;
size_t height;
unsigned int* cells;

Player player;
vector<mazes::Robot> robots;
};

/**
* @brief Returns whether or not a cell has something dead in it.
*
* @param value The value of a cell.
* @return true, if the cell has something dead in it, or false, otherwise.
*/
bool is_cell_dead(unsigned int value);

/**
* @brief Returns whether or not a cell has a human in it.
*
* @param value The value of a cell.
* @return true, if the cell has a human in it, or false, otherwise.
*/
bool is_cell_human(unsigned int value);

/**
* @brief Returns whether or not a cell has a robot in it.
*
* @param value The value of a cell.
* @return true, if the cell has a robot in it, or false, otherwise.
*/
bool is_cell_robot(unsigned int value);

/**
* @brief Returns whether or not a cell has a barrier in it.
*
* @param value The value of a cell.
* @return true, if the cell has a barrier in it, or false, otherwise.
*/
bool is_cell_barrier(unsigned int value);

/**
* @brief Returns a character representing the value present in a given cell.
* If a value is not recognized, the character '?' is returned.
*
* @param value The value of a cell.
* @return The character representing the value present in a given cell.
*/
char translate_from_cell_value(unsigned int value);

/**
* @brief Returns the value that is represented by the given character.
* If the character is not recognized, an error is thrown.
*
* @param value The value of a cell.
* @return The character representing the value present in a given cell.
*/
unsigned int translate_to_cell_value(char value);

size_t get_cell_index(size_t width, size_t height, size_t x, size_t y);

size_t get_cell_index(const mazes::Maze &maze, size_t x, size_t y);

unsigned int& get_cell_value_at(const mazes::Maze &maze, size_t x, size_t y);

unsigned int& get_cell_value_at_player_position(const mazes::Maze &maze);

/**
* @brief Returns whether or not a maze number is valid.
*
* @param maze A maze number.
* @return true, if the maze number is valid, or false, otherwise.
*/
bool is_maze_number_valid(unsigned int maze);

void show_maze(const mazes::Maze &maze);
} // namespace mazes

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

namespace screen {

/**
* @brief Clears the terminal window.
*/
void clear();
} // namespace screen

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

#include "time.h"

namespace timer {

/**
* @brief Starts the game timer.
*/
void start();

/**
* @brief Returns the number of seconds since the game timer started.
*
* @return The number of seconds since the game timer started.
*/
time_t stop();
} // namespace timer

#endif
Loading