added possibility to make moves from string (with tests)
This commit is contained in:
parent
ecba8e3c6e
commit
ebf5934909
@ -24,3 +24,31 @@ int Move::score_guess(const Board& b) const {
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Move Move::from_string(std::string move) {
|
||||||
|
if (!(4 <= move.size() && move.size() <= 5))
|
||||||
|
throw std::invalid_argument("Move must be 4 or 5 characters long");
|
||||||
|
Move ret;
|
||||||
|
ret.source_square = Coords::from_algebraic(move.substr(0, 2)).to_index();
|
||||||
|
ret.target_square = Coords::from_algebraic(move.substr(2, 2)).to_index();
|
||||||
|
if (move.size() == 5)
|
||||||
|
switch (move[4]) {
|
||||||
|
case 'n':
|
||||||
|
ret.promoting_to = Knigt;
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
ret.promoting_to = Bishop;
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
ret.promoting_to = Rook;
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
ret.promoting_to = Queen;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw std::invalid_argument("Promotion piece must be one of 'nbrq'"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ret.target_square = Coords::from_algebraic(move.substr(2, 2)).to_index();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
@ -15,6 +15,8 @@ struct Move {
|
|||||||
|
|
||||||
int score_guess(const Board&) const;
|
int score_guess(const Board&) const;
|
||||||
|
|
||||||
|
static Move from_string(std::string);
|
||||||
|
|
||||||
std::string to_string() const {
|
std::string to_string() const {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << Coords::from_index(source_square)
|
ss << Coords::from_index(source_square)
|
||||||
|
13
tests/move.cpp
Normal file
13
tests/move.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include "../src/model/board/board.hpp"
|
||||||
|
#include "lib.hpp"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::string str_move = "a2a3";
|
||||||
|
ASSERT_EQUALS(str_move, Move::from_string(str_move).to_string());
|
||||||
|
|
||||||
|
str_move = "b2f4";
|
||||||
|
ASSERT_EQUALS(str_move, Move::from_string(str_move).to_string());
|
||||||
|
|
||||||
|
str_move = "a2a1r";
|
||||||
|
ASSERT_EQUALS(str_move, Move::from_string(str_move).to_string());
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user