added: input and its subclasses
This commit is contained in:
42
src/file_input.cc
Normal file
42
src/file_input.cc
Normal file
@ -0,0 +1,42 @@
|
||||
#include "file_input.h"
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
||||
file_input::file_input(std::ifstream &&ifs):
|
||||
in{std::move(ifs)} {}
|
||||
|
||||
inline game_command get_direction(std::string &str, const char *COMMANDS[],
|
||||
const int COMMANDS_CNT) {
|
||||
for (int i = 0; i < COMMANDS_CNT; ++i)
|
||||
if (str == COMMANDS[i] &&
|
||||
i >= game_command::move_north &&
|
||||
i <= game_command::move_southeast)
|
||||
return (game_command)i;
|
||||
|
||||
return game_command_panic;
|
||||
}
|
||||
|
||||
game_command file_input::get_command() {
|
||||
const char *COMMANDS[] = {
|
||||
"q", "no", "so", "ea", "we", "ne", "nw", "se", "sw", "u", "a"
|
||||
};
|
||||
const int COMMANDS_CNT = 11;
|
||||
std::string cmd;
|
||||
in >> cmd;
|
||||
game_command tmp;
|
||||
|
||||
if (cmd == "q")
|
||||
return game_command_terminate;
|
||||
else if (cmd == "u")
|
||||
return (game_command)((tmp = get_direction(cmd, COMMANDS,
|
||||
COMMANDS_CNT)) == game_command_panic
|
||||
? tmp : tmp - move_north + apply_north);
|
||||
else if (cmd == "a")
|
||||
return (game_command)((tmp = get_direction(cmd, COMMANDS,
|
||||
COMMANDS_CNT)) == game_command_panic
|
||||
? tmp : tmp - move_north + attack_north);
|
||||
else // is just moving
|
||||
return get_direction(cmd, COMMANDS, COMMANDS_CNT);
|
||||
|
||||
return game_command_pass;
|
||||
}
|
Reference in New Issue
Block a user