Skip to content
Snippets Groups Projects
Commit e49b2623 authored by Clément Desberg's avatar Clément Desberg
Browse files

commited on 20240509

parent 8a3eaf1e
Branches
No related merge requests found
No preview for this file type
#include "board.h"
#include <string>
#include <regex>
board::board(){
board::board():
last_move(0,0)
{
last_move_en_passant_eligible = false;
white_pieces = {};
for(int i = 0; i < 8; i++){
white_pieces.push_back(new pawn(true,coordinates(i,1)));
......@@ -180,16 +182,35 @@ void board::move(coordinates move_from, coordinates move_to){
board_data[move_to.get_x()][move_to.get_y()] = board_data[move_from.get_x()][move_from.get_y()];
board_data[move_to.get_x()][move_to.get_y()]->move(move_to,false);
board_data[move_from.get_x()][move_from.get_y()] = nullptr;
last_move.move(move_to);
// last_move_en_passant_eligible attribute update if the piece moved is a pawn and it moves 2 cases.
last_move_en_passant_eligible = (board_data[move_to.get_x()][move_to.get_y()]->get_type() == pawn_type)&&
(abs(move_to.get_y() - move_from.get_y()) == 2);
}
/// @brief Check if an input is conform
/// @param cmd the input string
/// @return a boolean indicating if the input is conform
bool correct_input(std::string const & cmd){
std::regex mouvmtpattern("[a-h][1-8][a-h][1-8]");
std::regex kingcastlemouvmtpattern("(O|o|0)-(O|o|0)");
std::regex queencastlemouvmtpattern("(O|o|0)-(O|o|0)-(O|o|0)");
return regex_match(cmd,mouvmtpattern) || regex_match(cmd,kingcastlemouvmtpattern) || regex_match(cmd,queencastlemouvmtpattern);
bool board::is_en_passant_legal(coordinates move_from, coordinates move_to, bool white_move){
// We successively check all conditions
// First, checking the moving piece type
if(board_data[move_from.get_x()][move_from.get_y()]->get_type() != pawn_type){
return false;
}
// The en passant move is the same as the attack one
if(!board_data[move_from.get_x()][move_from.get_y()]->is_possible_attack(move_to)){
return false;
}
// The captured piece need to be the last played one, and be en_passant eligible
if((last_move.get_x() != move_to.get_x()) || (last_move.get_y() != move_from.get_y()) || !last_move_en_passant_eligible){
return false;
}
// The captured piece need to be one the same x of the destination, and on the same y of the starting point, and the opposite color
if(board_data[move_to.get_x()][move_from.get_y()]->get_color() == white_move){
return false;
}
// The captured piece need to be one the same x of the destination, and on the same y of the starting point, and a pawn
if(board_data[move_to.get_x()][move_from.get_y()]->get_type() != pawn_type){
return false;
}
return true;
}
/// @brief Check if a move is legal
......@@ -210,11 +231,20 @@ bool board::is_legal(coordinates move_from, coordinates move_to, bool white_move
std::vector<coordinates> empty_check = {};
if(board_data[move_to.get_x()][move_to.get_y()] == nullptr){
//Destination case is empty
if(!board_data[move_from.get_x()][move_from.get_y()]->is_possible(move_to)){
// The normal move isn't possible
return false;
if(is_en_passant_legal(move_from,move_to,white_move)){
// The en-passant special case, we capture the corresponding pawn
erase_piece(coordinates(move_to.get_x(),move_from.get_y()));
board_data[move_to.get_x()][move_from.get_y()] = nullptr;
}else{
if(!board_data[move_from.get_x()][move_from.get_y()]->is_possible(move_to)){
// The normal move isn't possible
return false;
}
}
board_data[move_from.get_x()][move_from.get_y()]->needed_space(move_to, &empty_check);
}else{
if(board_data[move_to.get_x()][move_to.get_y()]->get_color() == white_move){
//Destination case support a piece of the same color, wrong move
......@@ -251,6 +281,9 @@ coordinates get_coord_from_string(std::string move){
return coordinates(result_x,result_y);
}
/// @brief retrieve the king coordinates
/// @param white_king if the selected king is white
/// @return the coordinates of the selected king
coordinates board::get_king_coord(bool white_king){
coordinates result (0,0);
if(white_king){
......
......@@ -24,6 +24,8 @@ class board{
piece * board_data[8][8];
std::vector<piece*> white_pieces;
std::vector<piece*> black_pieces;
coordinates last_move;
bool last_move_en_passant_eligible;
public:
board();
void print();
......@@ -34,10 +36,10 @@ class board{
void erase_piece(coordinates coord);
coordinates get_king_coord(bool white_king);
bool is_check(bool white_king);
bool is_en_passant_legal(coordinates move_from, coordinates move_to, bool white_move);
};
void print_neutral_endl();
bool correct_input(std::string const & cmd);
void print_line_separator(bool inverted);
coordinates get_coord_from_string(std::string move);
......
......@@ -78,4 +78,38 @@ void terminal_set_background_color(terminal_color color){
/// @brief reset all terminal attributes
void terminal_reset_all(){
std::cout << "\033[0m";
}
/// @brief Check if an input is conform
/// @param cmd the input string
/// @return a boolean indicating if the input is conform
bool correct_input(std::string const & cmd){
std::regex mouvmtpattern("[a-h][1-8][a-h][1-8]");
std::regex kingcastlemouvmtpattern("(O|o|0)-(O|o|0)");
std::regex queencastlemouvmtpattern("(O|o|0)-(O|o|0)-(O|o|0)");
return regex_match(cmd,mouvmtpattern) || regex_match(cmd,kingcastlemouvmtpattern) || regex_match(cmd,queencastlemouvmtpattern);
}
/// @brief Check if an input corresponds to a standard move
/// @param cmd the input string
/// @return a boolean indicating if the input is a standard move
bool standard_input(std::string const & cmd){
std::regex mouvmtpattern("[a-h][1-8][a-h][1-8]");
return regex_match(cmd,mouvmtpattern);
}
/// @brief Check if an input correponds to a king castling move
/// @param cmd the input string
/// @return a boolean indicating if the input is a king castling move
bool king_castle_input(std::string const & cmd){
std::regex kingcastlemouvmtpattern("(O|o|0)-(O|o|0)");
return regex_match(cmd,kingcastlemouvmtpattern);
}
/// @brief Check if an input corresponds to a queen castling move
/// @param cmd the input string
/// @return a boolean indicating if the input is a queen castling move
bool queen_castle_input(std::string const & cmd){
std::regex queencastlemouvmtpattern("(O|o|0)-(O|o|0)-(O|o|0)");
return regex_match(cmd,queencastlemouvmtpattern);
}
\ No newline at end of file
#ifndef UTILITIES
#define UTILITIES
#include "coordinates.h"
#include <regex>
/// @brief a type for terminal colors, used in the print functions.
enum terminal_color { terminal_black, terminal_red, terminal_green, terminal_yellow, terminal_blue, terminal_magenta, terminal_cyan, terminal_white };
......@@ -9,5 +10,9 @@ bool is_corresponding(coordinates start, coordinates end, coordinates movement);
void terminal_set_text_color(terminal_color color);
void terminal_set_background_color(terminal_color color);
void terminal_reset_all();
bool correct_input(std::string const & cmd);
bool standard_input(std::string const & cmd);
bool king_castle_input(std::string const & cmd);
bool queen_castle_input(std::string const & cmd);
#endif
\ No newline at end of file
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment