diff --git a/src/board.cpp b/src/board.cpp index 35f38cd..77de5f5 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -78,7 +78,7 @@ auto Board::get_fen() -> std::string { return m_current_fen; } -auto Board::set_fen(const std::string &fen_string) -> bool { +auto Board::set_from_fen(const std::string &fen_string) -> bool { if (check_fen_is_valid(fen_string)) { m_current_fen = fen_string; return true; @@ -164,7 +164,7 @@ auto Board::move(const std::string ¬ation) -> bool { return false; } -auto Board::parse_move(const std::string &move) -> void { +auto Board::parse_move(const std::string &move) -> bool { // Square src; Square dst; int pad{}; @@ -179,11 +179,13 @@ auto Board::parse_move(const std::string &move) -> void { for (auto &i : m_pieces) { if (i->get_type() == 'P' && i->get_colour() == m_turn) { i->move(dst); - return; + return true; } } } else { // piece move denoted by uppercase letter R,N,B,Q,K + return true; } + return false; } auto Board::get_pieces() -> std::vector> & { diff --git a/src/board.hpp b/src/board.hpp index c14b2cd..1f2af62 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -21,14 +21,14 @@ class Board { Board(); Board(int variant); auto get_fen() -> std::string; - auto set_fen(const std::string &fen_string) -> bool; + auto set_from_fen(const std::string &fen_string) -> bool; auto static check_fen_is_valid(const std::string &fen) -> bool; auto print() -> void; auto print_fen() -> void; auto static print_position(std::string &fen) -> void; auto is_legal_move(const std::string ¬ation) -> bool; auto move(const std::string ¬ation) -> bool; - auto parse_move(const std::string &move) -> void; + auto parse_move(const std::string &move) -> bool; auto get_pieces() -> std::vector> &; }; diff --git a/tests/test.cpp b/tests/test.cpp index a80311e..b49b11a 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -10,7 +10,7 @@ * How to use the Catch2 framework following * https://github.com/catchorg/Catch2/blob/devel/docs/tutorial.md#test-cases-and-sections * - * TEST_CASE("Short Description", "[Optional tags]") { + * TEST_CASE("Short Description", "[Optional],[tags]") { * Setup the test variables, these are created new for each section * std::vector v(5); * @@ -99,19 +99,7 @@ TEST_CASE("Board print", "[board]") { REQUIRE(expected_output == ss.str()); } -TEST_CASE("Board move", "[board, piece]") { - Board chessboard{}; - chessboard.move("d4"); - chessboard.move("e5"); - chessboard.move("dxe5"); - chessboard.move("Nc6"); - chessboard.move("Nf3"); - chessboard.move("d4"); - chessboard.move("d4"); - chessboard.move("d4"); -} - -TEST_CASE("Print Position from FEN", "[board, fen]") { +TEST_CASE("Print Position from FEN", "[board],[fen]") { std::stringstream ss; // redirect cout to stringstream, save old buffer auto old_buf = std::cout.rdbuf(ss.rdbuf()); @@ -148,4 +136,56 @@ TEST_CASE("Print Position from FEN", "[board, fen]") { // require assertion REQUIRE(expected3 == ss.str()); } +} + +TEST_CASE("Parsing Moves", "[board],[piece],[move]") { + auto chessboard = Board{}; + std::stringstream ss; + // redirect cout to stringstream, save old buffer + auto old_buf = std::cout.rdbuf(ss.rdbuf()); + + SECTION("From starting position") { + std::vector moves = { + "d4", "e5", "dxe5", "Nc6", "Nf3", "Qe7", "Bf4", "Qb4+", + "Bd2", "Qxb2", "Nc3", "Bb4", "Rb1", "Qa3", "Nd5", "Bxd7+"}; + for (auto &i : moves) { + REQUIRE(chessboard.move(i)); + } + + chessboard.print(); + std::cout.rdbuf(old_buf); + + std::string expected = "r.b.k.nr\npppp.ppp\n..n.....\n...NP...\n........" + "\nq....N..\nP.PbPPPP\n.R.QKB.R"; + REQUIRE(expected == ss.str()); + } + + SECTION("From FEN, mate in one") { + std::string fen = + "rnbqkb1r/pp1np2p/2p1p3/2Pp3p/3P4/2N5/PP2BPPP/R1B1K1NR w KQkq - 0 10"; + chessboard.set_from_fen(fen); + REQUIRE(chessboard.move("Bxh5#")); + chessboard.print(); + std::cout.rdbuf(old_buf); + + std::string expected = "rnbqkb.r\npp.np..p\n..p.p...\n..Pp...p\n...P....\n." + ".N.....\nPP..BPPP\nR.B.K.NR"; + REQUIRE(expected == ss.str()); + REQUIRE(!chessboard.move("a6")); // test black cannot move after mate + } + + SECTION("From FEN, distinguish pieces") { + std::string fen = + "rnbqk2r/pppp1ppp/4pn2/8/1bPP4/5N2/PP2PPPP/RNBQKB1R w KQkq - 2 4"; + chessboard.set_from_fen(fen); + std::string move = "Nbd7"; + chessboard.move(move); + chessboard.print(); + + std::string expected = "rnbqk..r\npppp.ppp\n....pn..\n........\n.bPP....\n." + "....N..\nPP.NPPPP\nR.BQKB.R"; + + std::cout.rdbuf(old_buf); + REQUIRE(expected == ss.str()); + } } \ No newline at end of file