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

2024/05/09level2done

parent 27dd9ab1
Branches
No related merge requests found
chess: system/game.o system/board.o pieces/pawn.o pieces/king.o pieces/queen.o pieces/bishop.o pieces/knight.o pieces/rook.o pieces pieces/piece.o system/coordinates.o system/utilities.o
g++ pieces/*.o system/*.o -o src/echecs
g++ pieces/*.o system/*.o -o src/echecs -Wall -Wextra
make clean
%.o: %.cc
......
No preview for this file type
......@@ -251,12 +251,37 @@ coordinates get_coord_from_string(std::string move){
return coordinates(result_x,result_y);
}
coordinates board::get_king_coord(bool white_king){
coordinates result (0,0);
if(white_king){
for(int i = 0; i < white_pieces.size(); i++){
if(white_pieces[i]->get_type() == king_type){
result = white_pieces[i]->get_coord();
}
}
}else{
for(int i = 0; i < black_pieces.size(); i++){
if(black_pieces[i]->get_type() == king_type){
result = black_pieces[i]->get_coord();
}
}
}
return result;
}
/// @brief check if the selected player is in check state
/// @param white_king indicate if the king checked is white
/// @return a boolean indicating if a check is present
bool board::is_check(bool white_king){
bool result = false;
for(int i = 0; i < white_pieces.size(); i++){
result = result || is_legal(white_pieces[i]->get_coord(),,false);
if(white_king){
for(int i = 0; i < black_pieces.size(); i++){
result = result || is_legal(black_pieces[i]->get_coord(),get_king_coord(white_king),!white_king);
}
}else{
for(int i = 0; i < white_pieces.size(); i++){
result = result || is_legal(white_pieces[i]->get_coord(),get_king_coord(white_king),!white_king);
}
}
return result;
}
\ No newline at end of file
......@@ -16,7 +16,7 @@
#define BACKGROUND_COLOR1 terminal_white
#define BACKGROUND_COLOR2 terminal_yellow
#define PIECES_COLOR terminal_black
#define HIGHLIGHT_TEXT_COLOR terminal_red
#define WARNING_TEXT_COLOR terminal_red
/// @brief A class representing the chess board
class board{
......@@ -32,6 +32,7 @@ class board{
void move(coordinates move_from, coordinates move_to);
bool is_legal(coordinates move_from, coordinates move_to, bool white_move);
void erase_piece(coordinates coord);
coordinates get_king_coord(bool white_king);
bool is_check(bool white_king);
};
......
......@@ -34,7 +34,13 @@ int main(){
terminal_reset_all();
std::cout << " turn. ";
}
// Check warning for current player king
if(chess_board.is_check(white_move)){
std::cout << std::endl;
terminal_set_text_color(WARNING_TEXT_COLOR);
std::cout << "Warning : Check state" << std::endl;
terminal_reset_all();
}
std::cout << "What move ? " << std::endl;
std::cin >> movement;
if(movement == "/quit" || movement == "/resign" || movement == "/draw"){
......@@ -60,8 +66,16 @@ int main(){
coordinates start = get_coord_from_string(movement.substr(0, 2));
coordinates end = get_coord_from_string(movement.substr(2, 2));
if(chess_board.is_legal(start,end,white_move)){
chess_board.move(start,end);
correct_move = true;
//Apply the movement on a chess board copy and verify the check state of the current player's king
board chess_board_copy(chess_board);
chess_board_copy.move(start,end);
if(chess_board_copy.is_check(white_move)){
//The move is making the active player in check state, illegal
std::cout << "Illegal move. Your king will be checked" << std::endl;
}else{
chess_board.move(start,end);
correct_move = true;
}
}else{
std::cout << "Illegal move." << std::endl;
}
......
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