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

commited on 20240509

parent 02249be2
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 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 g++ pieces/*.o system/*.o -o src/echecs
make clean make clean
%.o: %.cc %.o: %.cc
......
File deleted
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible /// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param dest destination coordinates /// @param dest destination coordinates
/// @return a vector containing all coordinates to be verified /// @return a vector containing all coordinates to be verified
std::vector<coordinates> bishop::needed_space(coordinates dest){ std::vector<coordinates> *bishop::needed_space(coordinates dest){
std::vector<coordinates> result = {}; std::vector<coordinates> *result = new std::vector<coordinates>();
int diff_x = dest.get_x() - piece::coord.get_x(); int diff_x = dest.get_x() - piece::coord.get_x();
int diff_y = dest.get_y() - piece::coord.get_y(); int diff_y = dest.get_y() - piece::coord.get_y();
int coord_x = -1; int coord_x = -1;
...@@ -25,7 +25,7 @@ std::vector<coordinates> bishop::needed_space(coordinates dest){ ...@@ -25,7 +25,7 @@ std::vector<coordinates> bishop::needed_space(coordinates dest){
}else{ }else{
coord_y = piece::coord.get_y() - i; coord_y = piece::coord.get_y() - i;
} }
result.push_back(coordinates(coord_x,coord_y)); result->push_back(coordinates(coord_x,coord_y));
} }
return result; return result;
} }
...@@ -37,4 +37,13 @@ void bishop::print_piece(){ ...@@ -37,4 +37,13 @@ void bishop::print_piece(){
}else{ }else{
std::cout << "\u265D"; std::cout << "\u265D";
} }
}
/// @brief print the piece to the standard output in canonical form
void bishop::canonical_print_piece(){
if(piece::white){
std::cout << "wB";
}else{
std::cout << "bB";
}
} }
\ No newline at end of file
...@@ -17,8 +17,9 @@ class bishop : public piece{ ...@@ -17,8 +17,9 @@ class bishop : public piece{
legals.push_back(coordinates(i,-i)); legals.push_back(coordinates(i,-i));
} }
} }
std::vector<coordinates> needed_space(coordinates dest) override; std::vector<coordinates> *needed_space(coordinates dest) override;
void print_piece() override; void print_piece() override;
void canonical_print_piece() override;
}; };
#endif #endif
\ No newline at end of file
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible /// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param dest destination coordinates /// @param dest destination coordinates
/// @return a vector containing all coordinates to be verified /// @return a vector containing all coordinates to be verified
std::vector<coordinates> king::needed_space(coordinates dest){ std::vector<coordinates> *king::needed_space(coordinates dest){
// The king doesn't need space // The king doesn't need space
return {}; std::vector<coordinates> *result = new std::vector<coordinates>();
return result;
} }
/// @brief print the piece to the standard output /// @brief print the piece to the standard output
...@@ -20,4 +21,13 @@ void king::print_piece(){ ...@@ -20,4 +21,13 @@ void king::print_piece(){
}else{ }else{
std::cout << "\u265A"; std::cout << "\u265A";
} }
}
/// @brief print the piece to the standard output in canonical form
void king::canonical_print_piece(){
if(piece::white){
std::cout << "wK";
}else{
std::cout << "bK";
}
} }
\ No newline at end of file
...@@ -20,8 +20,9 @@ class king : public piece{ ...@@ -20,8 +20,9 @@ class king : public piece{
legals.push_back(coordinates(-1,1)); legals.push_back(coordinates(-1,1));
legals.push_back(coordinates(-1,-1)); legals.push_back(coordinates(-1,-1));
} }
std::vector<coordinates> needed_space(coordinates dest) override; std::vector<coordinates> *needed_space(coordinates dest) override;
void print_piece() override; void print_piece() override;
void canonical_print_piece() override;
}; };
#endif #endif
\ No newline at end of file
...@@ -8,9 +8,10 @@ ...@@ -8,9 +8,10 @@
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible /// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param dest destination coordinates /// @param dest destination coordinates
/// @return a vector containing all coordinates to be verified /// @return a vector containing all coordinates to be verified
std::vector<coordinates> knight::needed_space(coordinates dest){ std::vector<coordinates> *knight::needed_space(coordinates dest){
// The knight doesn't need space // The knight doesn't need space
return {}; std::vector<coordinates> *result = new std::vector<coordinates>();
return result;
} }
/// @brief print the piece to the standard output /// @brief print the piece to the standard output
...@@ -20,4 +21,13 @@ void knight::print_piece(){ ...@@ -20,4 +21,13 @@ void knight::print_piece(){
}else{ }else{
std::cout << "\u265E"; std::cout << "\u265E";
} }
}
/// @brief print the piece to the standard output in canonical form
void knight::canonical_print_piece(){
if(piece::white){
std::cout << "wN";
}else{
std::cout << "bN";
}
} }
\ No newline at end of file
...@@ -20,8 +20,9 @@ class knight : public piece{ ...@@ -20,8 +20,9 @@ class knight : public piece{
legals.push_back(coordinates(1,-2)); legals.push_back(coordinates(1,-2));
legals.push_back(coordinates(-1,-2)); legals.push_back(coordinates(-1,-2));
} }
std::vector<coordinates> needed_space(coordinates dest) override; std::vector<coordinates> *needed_space(coordinates dest) override;
void print_piece() override; void print_piece() override;
void canonical_print_piece() override;
}; };
#endif #endif
\ No newline at end of file
...@@ -8,25 +8,25 @@ ...@@ -8,25 +8,25 @@
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible /// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param dest destination coordinates /// @param dest destination coordinates
/// @return a vector containing all coordinates to be verified /// @return a vector containing all coordinates to be verified
std::vector<coordinates> pawn::needed_space(coordinates dest){ std::vector<coordinates> *pawn::needed_space(coordinates dest){
std::vector<coordinates> result = {}; std::vector<coordinates> *result = new std::vector<coordinates>();
int diff_x = dest.get_x() - piece::coord.get_x(); int diff_x = dest.get_x() - piece::coord.get_x();
int diff_y = dest.get_y() - piece::coord.get_y(); int diff_y = dest.get_y() - piece::coord.get_y();
if(diff_x == 0){ if(diff_x == 0){
//Normal pawn move, destination case verified //Normal pawn move, destination case verified
result.push_back(dest); result->push_back(dest);
// When moving two squares, also verify the intermediate one // When moving two squares, also verify the intermediate one
if(abs(diff_y) == 2){ if(abs(diff_y) == 2){
if(piece::white){ if(piece::white){
result.push_back(coordinates(piece::coord.get_x(),piece::coord.get_y() + 1)); result->push_back(coordinates(piece::coord.get_x(),piece::coord.get_y() + 1));
}else{ }else{
result.push_back(coordinates(piece::coord.get_x(),piece::coord.get_y() - 1)); result->push_back(coordinates(piece::coord.get_x(),piece::coord.get_y() - 1));
} }
} }
return result; return result;
}else{ }else{
//Capture pawn move, don't need any space //Capture pawn move, don't need any space
return {}; return result;
} }
} }
...@@ -39,13 +39,32 @@ void pawn::print_piece(){ ...@@ -39,13 +39,32 @@ void pawn::print_piece(){
} }
} }
/// @brief called when the pawn move to indicates that it has already moved, removing the legality of the 2 cases forward move /// @brief print the piece to the standard output in canonical form
void pawn::move(coordinates dest){ void pawn::canonical_print_piece(){
if(piece::white){
std::cout << "wP";
}else{
std::cout << "bP";
}
}
/// @brief called to move the pawn
/// @param dest the destination coordinates of the piece
/// @param debug_info print debug info to output when true
void pawn::move(coordinates dest, bool debug_info){
if(!has_moved){ if(!has_moved){
legals.erase(legals.begin()); legals.erase(legals.begin());
has_moved = true; has_moved = true;
} }
coord.move(dest); coord.move(dest);
if(debug_info){
std::cout << "Piece move from : x=" << coord.get_x() << " y=" << coord.get_y() << "to : x=" << coord.get_x() << " y=" << coord.get_y() << std::endl;
std::cout << "Piece info : white=" << white << std::endl;
std::cout << "Legal moves :" << std::endl;
for(int i = 0; i < legals.size(); i++){
std::cout << "x=" << legals[i].get_x() << " y=" << legals[i].get_y() << std::endl;
}
}
} }
/// @brief verify if the dest position is a valid attack move. The pawn need a special overrided function. /// @brief verify if the dest position is a valid attack move. The pawn need a special overrided function.
......
...@@ -17,13 +17,14 @@ class pawn : public piece{ ...@@ -17,13 +17,14 @@ class pawn : public piece{
}else{ }else{
direction = -1; direction = -1;
} }
// The attack moves list is managed in the overrided is_possible_attack function. // The attack moves list is managed in the overrided is_possible_attack function.
legals.push_back(coordinates(0,2*direction)); legals.push_back(coordinates(0,2*direction));
legals.push_back(coordinates(0,direction)); legals.push_back(coordinates(0,direction));
} }
std::vector<coordinates> needed_space(coordinates dest) override; std::vector<coordinates> *needed_space(coordinates dest) override;
void print_piece() override; void print_piece() override;
void move(coordinates dest) override; void canonical_print_piece() override;
void move(coordinates dest, bool debug_info) override;
bool is_possible_attack(coordinates dest) override; bool is_possible_attack(coordinates dest) override;
}; };
......
...@@ -37,8 +37,8 @@ bool piece::is_possible_attack(coordinates dest){ ...@@ -37,8 +37,8 @@ bool piece::is_possible_attack(coordinates dest){
/// @brief calculate the space needed for the piece to move /// @brief calculate the space needed for the piece to move
/// @param dest destination point /// @param dest destination point
/// @return a vector containing all spaces to be verified /// @return a vector containing all spaces to be verified
std::vector<coordinates> piece::needed_space(coordinates dest){ std::vector<coordinates> *piece::needed_space(coordinates dest){
return {}; std::vector<coordinates> *result = new std::vector<coordinates>();
} }
/// @brief print the piece to the standard output /// @brief print the piece to the standard output
...@@ -46,17 +46,36 @@ void piece::print_piece(){ ...@@ -46,17 +46,36 @@ void piece::print_piece(){
std::cout << "undefined piece"; std::cout << "undefined piece";
} }
/// @brief print the piece to the standard output in canonical form
void piece::canonical_print_piece(){
std::cout << "undefined piece";
}
void piece::move(coordinates dest){ /// @brief called to move the piece
/// @param dest the destination coordinates of the piece
/// @param debug_info print debug info to output when true
void piece::move(coordinates dest,bool debug_info){
//By default, only update has_moved //By default, only update has_moved
has_moved = true; has_moved = true;
coord.move(dest); coord.move(dest);
if(debug_info){
std::cout << "Piece move from : x=" << coord.get_x() << " y=" << coord.get_y() << "to : x=" << coord.get_x() << " y=" << coord.get_y() << std::endl;
std::cout << "Piece info : white=" << white << std::endl;
std::cout << "Legal moves :" << std::endl;
for(int i = 0; i < legals.size(); i++){
std::cout << "x=" << legals[i].get_x() << " y=" << legals[i].get_y() << std::endl;
}
}
} }
/// @brief retrieve the piece's coordinates
/// @return the coordinates of the piece
coordinates piece::get_coord(){ coordinates piece::get_coord(){
return coord; return coord;
} }
/// @brief retrieve the piece's color
/// @return the color of the piece
bool piece::get_color(){ bool piece::get_color(){
return white; return white;
} }
\ No newline at end of file
...@@ -15,9 +15,10 @@ class piece{ ...@@ -15,9 +15,10 @@ class piece{
piece(bool _white, coordinates _coord); piece(bool _white, coordinates _coord);
bool is_possible(coordinates dest); bool is_possible(coordinates dest);
virtual bool is_possible_attack(coordinates dest); virtual bool is_possible_attack(coordinates dest);
virtual std::vector<coordinates> needed_space(coordinates dest); virtual std::vector<coordinates> *needed_space(coordinates dest);
virtual void print_piece(); virtual void print_piece();
virtual void move(coordinates dest); virtual void canonical_print_piece();
virtual void move(coordinates dest,bool debug_info);
coordinates get_coord(); coordinates get_coord();
bool get_color(); bool get_color();
}; };
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible /// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param dest destination coordinates /// @param dest destination coordinates
/// @return a vector containing all coordinates to be verified /// @return a vector containing all coordinates to be verified
std::vector<coordinates> queen::needed_space(coordinates dest){ std::vector<coordinates> *queen::needed_space(coordinates dest){
std::vector<coordinates> result = {}; std::vector<coordinates> *result = new std::vector<coordinates>();
int diff_x = dest.get_x() - piece::coord.get_x(); int diff_x = dest.get_x() - piece::coord.get_x();
int diff_y = dest.get_y() - piece::coord.get_y(); int diff_y = dest.get_y() - piece::coord.get_y();
int coord_x = -1; int coord_x = -1;
...@@ -27,7 +27,7 @@ std::vector<coordinates> queen::needed_space(coordinates dest){ ...@@ -27,7 +27,7 @@ std::vector<coordinates> queen::needed_space(coordinates dest){
}else{ }else{
coord_y = piece::coord.get_y() - i; coord_y = piece::coord.get_y() - i;
} }
result.push_back(coordinates(coord_x,coord_y)); result->push_back(coordinates(coord_x,coord_y));
} }
return result; return result;
}else{ }else{
...@@ -35,33 +35,33 @@ std::vector<coordinates> queen::needed_space(coordinates dest){ ...@@ -35,33 +35,33 @@ std::vector<coordinates> queen::needed_space(coordinates dest){
if(dest.get_x() > piece::coord.get_x()){ if(dest.get_x() > piece::coord.get_x()){
//Moving right //Moving right
for(int i = piece::coord.get_x() + 1; i < dest.get_x(); i++){ for(int i = piece::coord.get_x() + 1; i < dest.get_x(); i++){
result.push_back(coordinates(i,piece::coord.get_y())); result->push_back(coordinates(i,piece::coord.get_y()));
} }
return result; return result;
} }
if(dest.get_x() < piece::coord.get_x()){ if(dest.get_x() < piece::coord.get_x()){
//Moving left //Moving left
for(int i = piece::coord.get_x() - 1; i > dest.get_x(); i--){ for(int i = piece::coord.get_x() - 1; i > dest.get_x(); i--){
result.push_back(coordinates(i,piece::coord.get_y())); result->push_back(coordinates(i,piece::coord.get_y()));
} }
return result; return result;
} }
if(dest.get_y() > piece::coord.get_y()){ if(dest.get_y() > piece::coord.get_y()){
//Moving right //Moving right
for(int i = piece::coord.get_y() + 1; i < dest.get_y(); i++){ for(int i = piece::coord.get_y() + 1; i < dest.get_y(); i++){
result.push_back(coordinates(piece::coord.get_x(),i)); result->push_back(coordinates(piece::coord.get_x(),i));
} }
return result; return result;
} }
if(dest.get_y() < piece::coord.get_y()){ if(dest.get_y() < piece::coord.get_y()){
//Moving left //Moving left
for(int i = piece::coord.get_y() - 1; i > dest.get_y(); i--){ for(int i = piece::coord.get_y() - 1; i > dest.get_y(); i--){
result.push_back(coordinates(piece::coord.get_x(),i)); result->push_back(coordinates(piece::coord.get_x(),i));
} }
return result; return result;
} }
} }
return {}; return result;
} }
/// @brief print the piece to the standard output /// @brief print the piece to the standard output
...@@ -71,4 +71,13 @@ void queen::print_piece(){ ...@@ -71,4 +71,13 @@ void queen::print_piece(){
}else{ }else{
std::cout << "\u265B"; std::cout << "\u265B";
} }
}
/// @brief print the piece to the standard output in canonical form
void queen::canonical_print_piece(){
if(piece::white){
std::cout << "wQ";
}else{
std::cout << "bQ";
}
} }
\ No newline at end of file
...@@ -21,8 +21,9 @@ class queen : public piece{ ...@@ -21,8 +21,9 @@ class queen : public piece{
legals.push_back(coordinates(i,0)); legals.push_back(coordinates(i,0));
} }
} }
std::vector<coordinates> needed_space(coordinates dest) override; std::vector<coordinates> *needed_space(coordinates dest) override;
void print_piece() override; void print_piece() override;
void canonical_print_piece() override;
}; };
#endif #endif
\ No newline at end of file
...@@ -8,33 +8,33 @@ ...@@ -8,33 +8,33 @@
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible /// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param dest destination coordinates /// @param dest destination coordinates
/// @return a vector containing all coordinates to be verified /// @return a vector containing all coordinates to be verified
std::vector<coordinates> rook::needed_space(coordinates dest){ std::vector<coordinates> *rook::needed_space(coordinates dest){
std::vector<coordinates> result = {}; std::vector<coordinates> *result = new std::vector<coordinates>();
if(dest.get_x() > piece::coord.get_x()){ if(dest.get_x() > piece::coord.get_x()){
//Moving right //Moving right
for(int i = piece::coord.get_x() + 1; i < dest.get_x(); i++){ for(int i = piece::coord.get_x() + 1; i < dest.get_x(); i++){
result.push_back(coordinates(i,piece::coord.get_y())); result->push_back(coordinates(i,piece::coord.get_y()));
} }
return result; return result;
} }
if(dest.get_x() < piece::coord.get_x()){ if(dest.get_x() < piece::coord.get_x()){
//Moving left //Moving left
for(int i = piece::coord.get_x() - 1; i > dest.get_x(); i--){ for(int i = piece::coord.get_x() - 1; i > dest.get_x(); i--){
result.push_back(coordinates(i,piece::coord.get_y())); result->push_back(coordinates(i,piece::coord.get_y()));
} }
return result; return result;
} }
if(dest.get_y() > piece::coord.get_y()){ if(dest.get_y() > piece::coord.get_y()){
//Moving right //Moving right
for(int i = piece::coord.get_y() + 1; i < dest.get_y(); i++){ for(int i = piece::coord.get_y() + 1; i < dest.get_y(); i++){
result.push_back(coordinates(piece::coord.get_x(),i)); result->push_back(coordinates(piece::coord.get_x(),i));
} }
return result; return result;
} }
if(dest.get_y() < piece::coord.get_y()){ if(dest.get_y() < piece::coord.get_y()){
//Moving left //Moving left
for(int i = piece::coord.get_y() - 1; i > dest.get_y(); i--){ for(int i = piece::coord.get_y() - 1; i > dest.get_y(); i--){
result.push_back(coordinates(piece::coord.get_x(),i)); result->push_back(coordinates(piece::coord.get_x(),i));
} }
return result; return result;
} }
...@@ -48,4 +48,13 @@ void rook::print_piece(){ ...@@ -48,4 +48,13 @@ void rook::print_piece(){
}else{ }else{
std::cout << "\u265C"; std::cout << "\u265C";
} }
}
/// @brief print the piece to the standard output in canonical form
void rook::canonical_print_piece(){
if(piece::white){
std::cout << "wR";
}else{
std::cout << "bR";
}
} }
\ No newline at end of file
...@@ -17,8 +17,9 @@ class rook : public piece{ ...@@ -17,8 +17,9 @@ class rook : public piece{
legals.push_back(coordinates(i,0)); legals.push_back(coordinates(i,0));
} }
} }
std::vector<coordinates> needed_space(coordinates dest) override; std::vector<coordinates> *needed_space(coordinates dest) override;
void print_piece() override; void print_piece() override;
void canonical_print_piece() override;
}; };
#endif #endif
\ No newline at end of file
File added
...@@ -45,16 +45,21 @@ board::board(){ ...@@ -45,16 +45,21 @@ board::board(){
} }
} }
/// @brief put a piece onto the board according to its coordinates
/// @param to_put
void board::put(piece* to_put){ void board::put(piece* to_put){
coordinates coord = to_put->get_coord(); coordinates coord = to_put->get_coord();
board_data[coord.get_x()][coord.get_y()] = to_put; board_data[coord.get_x()][coord.get_y()] = to_put;
} }
/// @brief print a neutral font color and background color new line
void print_neutral_endl(){ void print_neutral_endl(){
terminal_reset_all(); terminal_reset_all();
std::cout << std::endl; std::cout << std::endl;
} }
/// @brief print a line separator with board matching background colors
/// @param inverted indicate if the line is inverted or not. Every even line is inverted to create the chess board
void print_line_separator(bool inverted){ void print_line_separator(bool inverted){
bool switcher = false; bool switcher = false;
for(int i = 0; i < 8; i++){ for(int i = 0; i < 8; i++){
...@@ -69,6 +74,7 @@ void print_line_separator(bool inverted){ ...@@ -69,6 +74,7 @@ void print_line_separator(bool inverted){
} }
} }
/// @brief print the board
void board::print(){ void board::print(){
std::cout << " a b c d e f g h "; std::cout << " a b c d e f g h ";
print_neutral_endl(); print_neutral_endl();
...@@ -132,11 +138,29 @@ void board::print(){ ...@@ -132,11 +138,29 @@ void board::print(){
std::cout << " a b c d e f g h " << std::endl; std::cout << " a b c d e f g h " << std::endl;
} }
void board::canonical_print(){
for(int y = 0; y < 8; y++){
for(int x = 0; x < 8; x++){
if(board_data[x][y]!=nullptr){
board_data[x][y]->canonical_print_piece();
}
std::cout << ",";
}
}
}
/// @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){ 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()] = 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; board_data[move_from.get_x()][move_from.get_y()] = nullptr;
} }
/// @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){ bool correct_input(std::string const & cmd){
std::regex mouvmtpattern("[a-h][1-8][a-h][1-8]"); std::regex mouvmtpattern("[a-h][1-8][a-h][1-8]");
std::regex kingcastlemouvmtpattern("(O|o|0)-(O|o|0)"); std::regex kingcastlemouvmtpattern("(O|o|0)-(O|o|0)");
...@@ -144,9 +168,14 @@ bool correct_input(std::string const & cmd){ ...@@ -144,9 +168,14 @@ bool correct_input(std::string const & cmd){
return regex_match(cmd,mouvmtpattern) || regex_match(cmd,kingcastlemouvmtpattern) || regex_match(cmd,queencastlemouvmtpattern); return regex_match(cmd,mouvmtpattern) || regex_match(cmd,kingcastlemouvmtpattern) || regex_match(cmd,queencastlemouvmtpattern);
} }
/// @brief Check if a move is legal
/// @param move_from the initial position
/// @param move_to the final position
/// @param white_move indicate if the current active player is White
/// @return a boolean indicating if the move is legal
bool board::is_legal(coordinates move_from, coordinates move_to, bool white_move){ bool board::is_legal(coordinates move_from, coordinates move_to, bool white_move){
if(board_data[move_from.get_x()][move_from.get_y()] == nullptr){ if(board_data[move_from.get_x()][move_from.get_y()] == nullptr){
//Moving from an empty space, wron move //Moving from an empty space, wrong move
return false; return false;
} }
...@@ -154,20 +183,37 @@ bool board::is_legal(coordinates move_from, coordinates move_to, bool white_move ...@@ -154,20 +183,37 @@ bool board::is_legal(coordinates move_from, coordinates move_to, bool white_move
//Moving a piece that the player isn't owning, wrong move //Moving a piece that the player isn't owning, wrong move
return false; return false;
} }
std::vector<coordinates> empty_check = {};
if(board_data[move_to.get_x()][move_to.get_y()] == nullptr){ if(board_data[move_to.get_x()][move_to.get_y()] == nullptr){
//Destination case is empty //Destination case is empty
return board_data[move_from.get_x()][move_from.get_y()]->is_possible(move_to); if(!board_data[move_from.get_x()][move_from.get_y()]->is_possible(move_to)){
// The normal move isn't possible
return false;
}
empty_check = board_data[move_from.get_x()][move_from.get_y()]->needed_space(move_to);
} }
if(board_data[move_to.get_x()][move_to.get_y()]->get_color() == white_move){ 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 //Destination case support a piece of the same color, wrong move
return false; return false;
}else{ }else{
//Destination case support a piece of a different color //Destination case support a piece of a different color
return board_data[move_from.get_x()][move_from.get_y()]->is_possible_attack(move_to); if(!board_data[move_from.get_x()][move_from.get_y()]->is_possible_attack(move_to)){
// The attack move isn't possible
return false;
}
empty_check = board_data[move_from.get_x()][move_from.get_y()]->needed_space(move_to);
} }
bool result = true;
for(int i = 0; i < empty_check.size(); i++){
result = result && (board_data[empty_check[i].get_x()][empty_check[i].get_y()] == nullptr);
}
return result;
} }
/// @brief Retrieve coordinates from a string
/// @param move a string with the pattern "a6" (half a input)
/// @return coordinates corresponding to the string
coordinates get_coord_from_string(std::string move){ coordinates get_coord_from_string(std::string move){
int result_y = atoi(&move[1]) - 1; int result_y = atoi(&move[1]) - 1;
int result_x = 0; int result_x = 0;
...@@ -178,4 +224,4 @@ coordinates get_coord_from_string(std::string move){ ...@@ -178,4 +224,4 @@ coordinates get_coord_from_string(std::string move){
} }
} }
return coordinates(result_x,result_y); return coordinates(result_x,result_y);
} }
\ No newline at end of file
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#define BACKGROUND_COLOR2 terminal_yellow #define BACKGROUND_COLOR2 terminal_yellow
#define PIECES_COLOR terminal_black #define PIECES_COLOR terminal_black
/// @brief A class representing the chess board
class board{ class board{
private: private:
piece * board_data[8][8]; piece * board_data[8][8];
...@@ -25,6 +26,7 @@ class board{ ...@@ -25,6 +26,7 @@ class board{
public: public:
board(); board();
void print(); void print();
void canonical_print();
void put(piece* to_put); void put(piece* to_put);
void move(coordinates move_from, coordinates move_to); void move(coordinates move_from, coordinates move_to);
bool is_legal(coordinates move_from, coordinates move_to, bool white_move); bool is_legal(coordinates move_from, coordinates move_to, bool white_move);
......
...@@ -2,6 +2,11 @@ ...@@ -2,6 +2,11 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
/// @brief an enumeration to represent the game result
enum winner_type {win_white, win_black, draw, interrupt};
/// @brief The main function
/// @return nothing
int main(){ int main(){
board chess_board; board chess_board;
std::string movement; std::string movement;
...@@ -10,6 +15,7 @@ int main(){ ...@@ -10,6 +15,7 @@ int main(){
coordinates start(0,0); coordinates start(0,0);
coordinates end(0,0); coordinates end(0,0);
bool white_move = true; bool white_move = true;
winner_type game_result;
while(!game_end){ while(!game_end){
chess_board.print(); chess_board.print();
movement = ""; movement = "";
...@@ -35,6 +41,19 @@ int main(){ ...@@ -35,6 +41,19 @@ int main(){
//The movement input is correct and a game ending one //The movement input is correct and a game ending one
game_end = true; game_end = true;
correct_move = true; correct_move = true;
if(movement == "/quit"){
game_result = interrupt;
}
if(movement == "/resign"){
if(white_move){
game_result = win_black;
}else{
game_result = win_white;
}
}
if(movement == "/draw"){
game_result = draw;
}
}else{ }else{
if(correct_input(movement)){ if(correct_input(movement)){
//The movement input is correct //The movement input is correct
...@@ -54,4 +73,24 @@ int main(){ ...@@ -54,4 +73,24 @@ int main(){
} }
white_move = !white_move; white_move = !white_move;
} }
chess_board.canonical_print();
switch (game_result)
{
case win_white:
std::cout << " 1-0" << std::endl;
break;
case win_black:
std::cout << " 0-1" << std::endl;
break;
case draw:
std::cout << " 1/2-1/2" << std::endl;
break;
case interrupt:
std::cout << " ?-?" << std::endl;
break;
}
} }
\ No newline at end of file
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