Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
move_translator.cpp
Go to the documentation of this file.
3#include <regex>
4#include <sstream>
5#include <string>
6
7namespace movetranslation {
8
9// AlgebraicBoardSpace implementation
10
12 : value_{value} {
13 std::string validation_string{"^[a-i](?:10|[1-9])$"};
14 std::regex validation_pattern(validation_string);
15 if (!std::regex_match(value_, validation_pattern)) {
17 "String '" + value_ + "' does not match the regex pattern '" +
18 validation_string + "'"
19 );
20 }
21}
22
24 char algebraic_column = (char)(game_board_space.file + 'a');
25 auto algebraic_row = std::to_string(10 - game_board_space.rank);
26 value_ = algebraic_column + algebraic_row;
27}
28
30 char algebraic_column = value_[0];
31 auto file = algebraic_column - 'a';
32 auto algebraic_row = value_.substr(1);
33 auto rank = 10 - std::stoi(algebraic_row);
34 return gameboard::BoardSpace{rank, file};
35}
36
37const std::string AlgebraicBoardSpace::value() { return value_; }
38
40 return value_ == other.value_;
41}
42
43// AlgebraicMove implementation
44
46 const AlgebraicBoardSpace &start,
47 const AlgebraicBoardSpace &end
48)
49 : start_{start}
50 , end_{end} {}
51
53 const std::vector<AlgebraicBoardSpace> &algebraic_board_spaces
54) {
55 if (algebraic_board_spaces.size() < 2) {
56 throw InvalidVectorSizeException("AlgebraicMove must consist of exactly 2 spaces.");
57 }
58 return AlgebraicMove{algebraic_board_spaces.at(0), algebraic_board_spaces.at(1)};
59}
60
61const AlgebraicMove AlgebraicMove::Create(const std::vector<std::string> &tokens) {
62 if (tokens.size() != 2) {
63 throw InvalidVectorSizeException("AlgebraicMove must consist of exactly 2 spaces.");
64 }
65
66 return AlgebraicMove{tokens.at(0), tokens.at(1)};
67}
68
70 auto algebraic_start = AlgebraicBoardSpace(game_board_move.start);
71 auto algebraic_end = AlgebraicBoardSpace(game_board_move.end);
72 return AlgebraicMove(algebraic_start, algebraic_end);
73}
74
76
78
81}
82
83// Free function implementations
84
85const std::string GetInput(std::istream &input_stream) {
86 std::string user_input;
87 std::getline(input_stream, user_input);
88
89 return user_input;
90}
91
92const std::string Trim(const std::string &str) {
93 if (str.empty()) {
94 return str;
95 }
96
97 auto start = std::find_if_not(str.begin(), str.end(), ::isspace);
98 auto end = std::find_if_not(str.rbegin(), str.rend(), ::isspace).base();
99
100 return std::string(start, end);
101}
102
103const std::vector<std::string> Tokenize(const std::string &input) {
104 std::vector<std::string> tokens;
105
106 std::stringstream stream(input);
107 std::string segment;
108
109 while (std::getline(stream, segment, ',')) {
110 auto trimmed_segment = Trim(segment);
111 tokens.emplace_back(trimmed_segment);
112 }
113
114 return tokens;
115}
116
117bool IsValidAlgebraicMove(const std::vector<std::string> &tokens) {
118 for (auto &token : tokens) {
119 if (!IsValidAlgebraicBoardSpace(token)) {
120 std::cerr << "Token '" + token + "' is not a valid algebraic board space."
121 << std::endl;
122 return false;
123 }
124 }
125
126 if (tokens.size() != 2) {
127 std::cerr << "AlgebraicMove consists of exactly 2 board spaces, but "
128 << tokens.size() << " values provided" << std::endl;
129 }
130
131 return true;
132}
133
134bool IsValidAlgebraicBoardSpace(const std::string &algebraic_space) {
135 std::regex pattern("^[a-i](?:10|[1-9])$");
136 return std::regex_match(algebraic_space, pattern);
137}
138
139} // namespace movetranslation
Constants, typedefs, and simple structs used by gameboard::GameBoard.
bool operator==(const AlgebraicBoardSpace &other) const
gameboard::BoardSpace ToGameBoardSpace()
AlgebraicBoardSpace(const std::string &value)
static const AlgebraicMove Create(const std::vector< AlgebraicBoardSpace > &algebraic_board_spaces)
const AlgebraicBoardSpace end()
const AlgebraicBoardSpace start()
AlgebraicMove(const AlgebraicBoardSpace &start, const AlgebraicBoardSpace &end)
const gameboard::Move ToGameBoardMove()
bool IsValidAlgebraicBoardSpace(const std::string &algebraic_space)
bool IsValidAlgebraicMove(const std::vector< std::string > &tokens)
const std::string Trim(const std::string &str)
const std::string GetInput(std::istream &input_stream)
const std::vector< std::string > Tokenize(const std::string &input)
A pair of coordinate (rank, and file) with properties determined by comparison with values of gameboa...
A gameboard::BoardSpace pair (start and end).
gameboard::BoardSpace end
gameboard::BoardSpace start