From 70129a32196981875a94dee3a8062a323166fb7f Mon Sep 17 00:00:00 2001 From: Tait Hoyem <44244401+TTWNO@users.noreply.github.com> Date: Sat, 27 Apr 2019 16:15:43 +0000 Subject: [PATCH] Working on bug...TBC --- src/chess.cpp | 63 ++++++++++++++++++++++++++++++++++++++++----- src/functions.cpp | 15 +++++++++++ src/functions.h | 4 +++ tests/main.cpp | 9 +++++++ tests/valid_moves.h | 57 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 6 deletions(-) diff --git a/src/chess.cpp b/src/chess.cpp index e7a4b43..319ad24 100644 --- a/src/chess.cpp +++ b/src/chess.cpp @@ -16,11 +16,8 @@ void print_board(array const& board){ int ix = (i*10) + j; int piece = board[ix]; string piece_string = FANCY_CHESS_CHARS[piece]; - if (is_white(piece)){ - cout << color::rize(piece_string, "White", "Default"); - } else { - cout << color::rize(piece_string, "Black", "Default"); - } + string color = is_white(piece)?"White":"Black"; + cout << color::rize(piece_string, color, "Default"); cout << " "; } cout << endl; @@ -30,7 +27,61 @@ void print_board(array const& board){ } +// TODO: allow algebraic notation. int main(){ - print_board(DEFAULT_BOARD); + Color whos_turn = Color::WHITE; + array my_board; + copy(DEFAULT_BOARD.begin(), DEFAULT_BOARD.end(), + my_board.begin()); + + vector all_moves = {}; + vector all_moves_notation = {}; + + while (true){ + all_moves = {}; + all_moves_notation = {}; + + // Gets all moves for color who's turn it is. + if (whos_turn == Color::WHITE){ + get_all_white_moves(&my_board, &all_moves); + } else { + get_all_black_moves(&my_board, &all_moves); + } + + print_board(my_board); + // Gets a string from cin called input + string input; + getline(cin, input); + // Quits + if (input == "q"){ + break; + } + + // Gets all moves and stores them in a notation list + // TODO make into own function + bool move_exec = false; + int move_to_exec = 0; + for (int move : all_moves){ + string move_notation = to_notation(move, &my_board); + all_moves_notation.push_back(move_notation); + if (move_notation == input){ + move_exec = true; + move_to_exec = move; + break; + } + } + // If the input did not match any legal move. + if (!move_exec){ + cout << "These are the only valid moves: "; + for (string notation : all_moves_notation){ + cout << notation << " "; + } + cout << endl; + // If the input did match a legal move. + } else { + my_board = dumb_move(move_to_exec, my_board); + whos_turn = rev_color(whos_turn); + } + } return 0; } diff --git a/src/functions.cpp b/src/functions.cpp index a3aee78..2534926 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -375,3 +375,18 @@ std::string to_notation(int move, std::array *board){ } return ss.str(); } + +void get_all_white_moves(std::array *board, std::vector *moves){ + for (PieceType pt : Pieces::WHITE){ + for (int pos_of : get_poss_of(pt, board)){ + get_all_moves_as_if(pos_of, pt, board, moves); + } + } +} +void get_all_black_moves(std::array *board, std::vector *moves){ + for (PieceType pt : Pieces::BLACK){ + for (int pos_of : get_poss_of(pt, board)){ + get_all_moves_as_if(pos_of, pt, board, moves); + } + } +} diff --git a/src/functions.h b/src/functions.h index ba970f1..cde7a69 100644 --- a/src/functions.h +++ b/src/functions.h @@ -62,3 +62,7 @@ bool is_checked(int pos, std::array board); // Convert move (and board becuase notation needs more info than the move itself) // ...into algbraic notation. std::string to_notation(int move, std::array *board); + +// These functions are just for printing (for now) so they are not included in the tests. +void get_all_white_moves(std::array *board, std::vector *moves); +void get_all_black_moves(std::array *board, std::vector *moves); diff --git a/tests/main.cpp b/tests/main.cpp index 9d76e6b..ac6312f 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -262,3 +262,12 @@ TEST_CASE("Test for disambiguating moves, and checkmate mark (#)", "[to_notation CHECK(get_notations(check_rook_moves, CHECKMATE_ROOK_BOARD) == CHECKMATE_ROOK_NOTATION); } + +TEST_CASE("Do extra (random board tests) on notation and move generation", "[to_notation][get_all_moves]"){ + auto knight_moves = get_all_moves(KNIGHT_EXTRA1_POS, EXTRA1_BOARD); + auto knight_moves2 = get_all_moves(KNIGHT_EXTRA2_POS, EXTRA2_BOARD); + CHECK(get_notations(knight_moves, EXTRA1_BOARD) == KNIGHT_EXTRA1_NOTATION); + CHECK(get_notations(knight_moves2, EXTRA2_BOARD) == KNIGHT_EXTRA2_NOTATION); + CHECK(get_to_squares(knight_moves2) == KNIGHT_EXTRA2_MOVES); + +} diff --git a/tests/valid_moves.h b/tests/valid_moves.h index ff35dd6..c46544c 100644 --- a/tests/valid_moves.h +++ b/tests/valid_moves.h @@ -718,3 +718,60 @@ const std::vector CHECKMATE_ROOK_NOTATION = { "Rd7", "Rd6", "Rd5", "Rd4", "Rd3", "Rdd2", "Rd1#" }; +// Extra checks on valid moves +const int KNIGHT_EXTRA1_POS = G5; +const std::array EXTRA1_BOARD = { + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, + INV, B_ROOK, NONE, B_BISHOP, B_QUEEN, B_KING, B_BISHOP, B_KNIGHT, B_ROOK, INV, + INV, NONE, B_PAWN, B_PAWN, B_PAWN, NONE, NONE, B_PAWN, B_PAWN, INV, + INV, NONE, NONE, W_KNIGHT, NONE, NONE, B_PAWN, NONE, NONE, INV, + INV, B_PAWN, NONE, NONE, NONE, B_PAWN, W_PAWN, W_KNIGHT, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, NONE, W_PAWN, W_PAWN, INV, + INV, W_ROOK, W_KNIGHT, W_BISHOP, W_QUEEN, W_KING, W_BISHOP, NONE, W_ROOK, INV, + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV, + INV, INV, INV, INV, INV, INV, INV, INV, INV, INV +}; +const std::vector KNIGHT_EXTRA1_NOTATION = { + "Nh3", "Nf3", "Ne4", "Ne6", "Nf7", "Nxh7" +}; + +const int KNIGHT_EXTRA2_POS = D4; +const std::array EXTRA2_BOARD = { + INV,INV,INV,INV,INV,INV,INV,INV,INV,INV, + INV,INV,INV,INV,INV,INV,INV,INV,INV,INV, + INV, B_ROOK, B_KNIGHT, B_BISHOP, B_QUEEN, B_KING, B_BISHOP, B_KNIGHT, B_ROOK, INV, + INV, B_PAWN, B_PAWN, B_PAWN, B_PAWN, NONE, B_PAWN, B_PAWN, B_PAWN, INV, + INV, NONE, NONE, NONE, NONE, B_PAWN, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, W_KNIGHT, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, INV, + INV, W_ROOK, W_KNIGHT, W_BISHOP, W_QUEEN, W_KING, W_BISHOP, W_KNIGHT, W_ROOK, INV, + INV,INV,INV,INV,INV,INV,INV,INV,INV,INV, + INV,INV,INV,INV,INV,INV,INV,INV,INV,INV +}; +const std::vector KNIGHT_EXTRA2_MOVES = { + B3, F3, F5, E6, C6, B5 +}; +const std::vector KNIGHT_EXTRA2_NOTATION = { + +}; + +const std::array EXTRA3_BOARD = { + INV,INV,INV,INV,INV,INV,INV,INV,INV,INV, + INV,INV,INV,INV,INV,INV,INV,INV,INV,INV, + INV, B_ROOK, B_KNIGHT, B_BISHOP, B_QUEEN, B_KING, B_BISHOP, B_KNIGHT, B_ROOK, INV, + INV, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, B_PAWN, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, W_KNIGHT, NONE, NONE, NONE, NONE, INV, + INV, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, INV, + INV, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, W_PAWN, INV, + INV, W_ROOK, W_KNIGHT, W_BISHOP, W_QUEEN, W_KING, W_BISHOP, W_KNIGHT, W_ROOK, INV, + INV,INV,INV,INV,INV,INV,INV,INV,INV,INV, + INV,INV,INV,INV,INV,INV,INV,INV,INV,INV +}; +