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

commited on 20240508

parent 9fd53a34
Branches
No related merge requests found
main: 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
make clean
......
No preview for this file type
......@@ -53,4 +53,8 @@ void piece::moved(){
coordinates piece::get_coord(){
return coord;
}
bool piece::get_color(){
return white;
}
\ No newline at end of file
......@@ -19,6 +19,7 @@ class piece{
virtual void print_piece();
virtual void moved();
coordinates get_coord();
bool get_color();
};
#endif
\ No newline at end of file
......@@ -5,7 +5,7 @@
board::board(){
white_pieces = {};
for(int i = 0; i < 8; i++){
white_pieces.push_back(new pawn(true,coordinates(i,0)));
white_pieces.push_back(new pawn(true,coordinates(i,1)));
}
white_pieces.push_back(new rook(true,coordinates(0,0)));
white_pieces.push_back(new knight(true,coordinates(1,0)));
......@@ -18,7 +18,7 @@ board::board(){
black_pieces = {};
for(int i = 0; i < 8; i++){
black_pieces.push_back(new pawn(false,coordinates(i,7)));
black_pieces.push_back(new pawn(false,coordinates(i,6)));
}
black_pieces.push_back(new rook(false,coordinates(0,7)));
black_pieces.push_back(new knight(false,coordinates(1,7)));
......@@ -132,6 +132,11 @@ void board::print(){
std::cout << " a b c d e f g h " << std::endl;
}
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_from.get_x()][move_from.get_y()] = nullptr;
}
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)");
......@@ -139,7 +144,38 @@ bool correct_input(std::string const & cmd){
return regex_match(cmd,mouvmtpattern) || regex_match(cmd,kingcastlemouvmtpattern) || regex_match(cmd,queencastlemouvmtpattern);
}
int main(){
board test;
test.print();
}
\ No newline at end of file
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){
//Moving from an empty space, wron move
return false;
}
if(board_data[move_from.get_x()][move_from.get_y()]->get_color() != white_move){
//Moving a piece that the player isn't owning, wrong move
return false;
}
if(board_data[move_to.get_x()][move_to.get_y()] == nullptr){
//Destination case is empty
return board_data[move_from.get_x()][move_from.get_y()]->is_possible(move_to);
}
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
return false;
}else{
//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);
}
}
coordinates get_coord_from_string(std::string move){
int result_x = atoi(&move[1]) - 1;
int result_y = 0;
std::vector<char> y_traduction = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
for(int i = 0; i < 8; i++){
if(move[0] == y_traduction[i]){
result_y = i;
}
}
return coordinates(result_x,result_y);
}
......@@ -26,10 +26,13 @@ class board{
board();
void print();
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 print_neutral_endl();
bool correct_input(std::string const & cmd);
void print_line_separator(bool inverted);
coordinates get_coord_from_string(std::string move);
#endif
\ No newline at end of file
#include "board.h"
#include <iostream>
#include <string>
int main(){
board chess_board;
std::string movement;
bool game_end = false;
bool correct_move = false;
coordinates start(0,0);
coordinates end(0,0);
bool white_move = true;
while(!game_end){
chess_board.print();
movement = "";
correct_move = false;
while(!correct_move){
std::cout << "Move ? " << std::endl;
std::cin >> movement;
if(movement == "/quit" || movement == "/resign" || movement == "/draw"){
//The movement input is correct and a game ending one
game_end = true;
correct_move = true;
}else{
if(correct_input(movement)){
//The movement input is correct
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;
}
}else{
//The movement input is wrong
std::cout << "Wrong input format. Format need to be in algrebic notation : b1c3 for example" << std::endl;
}
}
}
white_move = !white_move;
}
}
\ 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