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

commited on 20240509

parent 30fbef51
Branches
No related merge requests found
......@@ -11,6 +11,7 @@ class bishop : public piece{
bishop(bool _white, coordinates _coord):
piece(_white,_coord)
{
type = bishop_type;
for(int i = -7; i < 8; i++)
if(i!=0){
legals.push_back(coordinates(i,i));
......
......@@ -11,6 +11,7 @@ class king : public piece{
king(bool _white, coordinates _coord):
piece(_white,_coord)
{
type = king_type;
legals.push_back(coordinates(0,1));
legals.push_back(coordinates(0,-1));
legals.push_back(coordinates(1,0));
......
......@@ -11,6 +11,7 @@ class knight : public piece{
knight(bool _white, coordinates _coord):
piece(_white,_coord)
{
type = knight_type;
legals.push_back(coordinates(2,1));
legals.push_back(coordinates(2,-1));
legals.push_back(coordinates(-2,1));
......
......@@ -11,6 +11,7 @@ class pawn : public piece{
pawn(bool _white, coordinates _coord):
piece(_white,_coord)
{
type = pawn_type;
int direction = 0;
if(piece::white){
direction = 1;
......
......@@ -78,4 +78,10 @@ coordinates piece::get_coord(){
/// @return the color of the piece
bool piece::get_color(){
return white;
}
/// @brief retrieve the piece's type
/// @return the type of the piece
piece_type piece::get_type(){
return type;
}
\ No newline at end of file
......@@ -4,6 +4,9 @@
#include "../system/utilities.h"
#include <vector>
/// @brief Represent a piece type. Undefined for default piece
enum piece_type {pawn_type, bishop_type, knight_type, rook_type, queen_type, king_type};
/// @brief a generic piece class. white is a boolean indicating the piece's color. legals is a vector indicating all legal movements. has_moved indicate if the piece has already move during this game
class piece{
protected:
......@@ -11,6 +14,7 @@ class piece{
coordinates coord;
std::vector<coordinates> legals;
bool has_moved;
piece_type type;
public:
piece(bool _white, coordinates _coord);
bool is_possible(coordinates dest);
......@@ -21,6 +25,7 @@ class piece{
virtual void move(coordinates dest,bool debug_info);
coordinates get_coord();
bool get_color();
piece_type get_type();
};
#endif
\ No newline at end of file
......@@ -11,6 +11,7 @@ class queen : public piece{
queen(bool _white, coordinates _coord):
piece(_white,_coord)
{
type = queen_type;
for(int i = -7; i < 8; i++)
if(i!=0){
// Bishop moves
......
......@@ -11,6 +11,7 @@ class rook : public piece{
rook(bool _white, coordinates _coord):
piece(_white,_coord)
{
type = rook_type;
for(int i = -7; i < 8; i++)
if(i!=0){
legals.push_back(coordinates(0,i));
......
No preview for this file type
......@@ -138,6 +138,7 @@ void board::print(){
std::cout << " a b c d e f g h " << std::endl;
}
/// @brief print the board in canonical form
void board::canonical_print(){
for(int y = 0; y < 8; y++){
for(int x = 0; x < 8; x++){
......@@ -149,10 +150,33 @@ void board::canonical_print(){
}
}
/// @brief Erase a piece from its corresponding vector according to its coordinates
/// @param coord the coordinates of the piece to erase
void board::erase_piece(coordinates coord){
bool piece_found = false;
for(int i = 0; i < white_pieces.size() && !piece_found; i++){
if(white_pieces[i]->get_coord().get_x() == coord.get_x() && white_pieces[i]->get_coord().get_y() == coord.get_y()){
piece_found = true;
white_pieces.erase(white_pieces.begin() + i);
}
}
for(int i = 0; i < black_pieces.size() && !piece_found; i++){
if(black_pieces[i]->get_coord().get_x() == coord.get_x() && black_pieces[i]->get_coord().get_y() == coord.get_y()){
piece_found = true;
std::cout << "piece erased" << std::endl;
black_pieces.erase(black_pieces.begin() + i);
}
}
}
/// @brief Move a piece from one position to another, supposing that the move is legal
/// @param move_from the initial coordinates of the piece
/// @param move_to the final coordinates of the piece
void board::move(coordinates move_from, coordinates move_to){
if(board_data[move_to.get_x()][move_to.get_y()] != nullptr){
//Attack case : remove the other piece
erase_piece(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;
......@@ -225,4 +249,14 @@ coordinates get_coord_from_string(std::string move){
}
}
return coordinates(result_x,result_y);
}
/// @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);
}
}
\ No newline at end of file
......@@ -16,6 +16,7 @@
#define BACKGROUND_COLOR1 terminal_white
#define BACKGROUND_COLOR2 terminal_yellow
#define PIECES_COLOR terminal_black
#define HIGHLIGHT_TEXT_COLOR terminal_red
/// @brief A class representing the chess board
class board{
......@@ -30,6 +31,8 @@ class board{
void put(piece* to_put);
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);
bool is_check(bool white_king);
};
void print_neutral_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