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

commited on 20240508

parent e43bc2c3
Branches
No related merge requests found
#include "../system/coordinates.h"
#include "../system/utilities.h"
#include "piece.h"
#include "king.h"
#include <vector>
#include <iostream>
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param dest destination coordinates
/// @return a vector containing all coordinates to be verified
std::vector<coordinates> king::needed_space(coordinates dest){
// The king doesn't need space
return {};
}
/// @brief print the piece to the standard output
void king::print_piece(){
if(piece::white){
std::cout << "\u2654";
}else{
std::cout << "\u265A";
}
}
\ No newline at end of file
#ifndef ROOK
#define ROOK
#include "../system/coordinates.h"
#include "../system/utilities.h"
#include "piece.h"
#include <vector>
/// @brief the king piece class.
class king : public piece{
public:
king(bool _white):
piece(_white)
{
legals.push_back(coordinates(0,1));
legals.push_back(coordinates(0,-1));
legals.push_back(coordinates(1,0));
legals.push_back(coordinates(1,1));
legals.push_back(coordinates(1,-1));
legals.push_back(coordinates(-1,0));
legals.push_back(coordinates(-1,1));
legals.push_back(coordinates(-1,-1));
}
std::vector<coordinates> needed_space(coordinates dest) override;
void print_piece() override;
};
#endif
\ No newline at end of file
#include "../system/coordinates.h"
#include "../system/utilities.h"
#include "piece.h"
#include "knight.h"
#include <vector>
#include <iostream>
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param dest destination coordinates
/// @return a vector containing all coordinates to be verified
std::vector<coordinates> knight::needed_space(coordinates dest){
// The knight doesn't need space
return {};
}
/// @brief print the piece to the standard output
void knight::print_piece(){
if(piece::white){
std::cout << "\u2658";
}else{
std::cout << "\u265E";
}
}
\ No newline at end of file
#ifndef ROOK
#define ROOK
#include "../system/coordinates.h"
#include "../system/utilities.h"
#include "piece.h"
#include <vector>
/// @brief the knight piece class.
class knight : public piece{
public:
knight(bool _white):
piece(_white)
{
legals.push_back(coordinates(2,1));
legals.push_back(coordinates(2,-1));
legals.push_back(coordinates(-2,1));
legals.push_back(coordinates(-2,-1));
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;
void print_piece() override;
};
#endif
\ No newline at end of file
#include "../system/coordinates.h"
#include "../system/utilities.h"
#include "piece.h"
#include "pawn.h"
#include <vector>
#include <iostream>
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param start starting coordinates
/// @param end destination coordinates
/// @return a vector containing all coordinates to be verified
std::vector<coordinates> pawn::needed_space(coordinates start, coordinates end){
std::vector<coordinates> result = {};
int diff_x = end.get_x() - start.get_x();
int diff_y = end.get_y() - start.get_y();
if(diff_x == 0){
//Normal pawn move, destination case verified
result.push_back(end);
// When moving two squares, also verify the intermediate one
if(abs(diff_y) == 2){
if(piece::white){
result.push_back(coordinates(start.get_x(),start.get_y() + 1));
}else{
result.push_back(coordinates(start.get_x(),start.get_y() - 1));
}
}
return result;
}else{
//Capture pawn move, don't need any space
return {};
}
}
/// @brief print the piece to the standard output
void pawn::print_piece(){
if(piece::white){
std::cout << "\u2659";
}else{
std::cout << "\u265F";
}
}
/// @brief called when the pawn move to indicates that it has already moved, removing the legality of the 2 cases forward move
void pawn::moved(){
if(!has_moved){
legals.erase(legals.begin());
has_moved = true;
}
}
int main(){
pawn hello(true);
hello.print_piece();
if(hello.is_possible(coordinates(0,0),coordinates(0,2))){
std::cout << "ok2" << std::endl;
}
hello.moved();
if(hello.is_possible(coordinates(0,0),coordinates(0,2))){
std::cout << "ok1" << std::endl;
}
}
\ No newline at end of file
#ifndef ROOK
#define ROOK
#include "../system/coordinates.h"
#include "../system/utilities.h"
#include "piece.h"
#include <vector>
/// @brief the pawn piece class.
class pawn : public piece{
public:
pawn(bool _white):
piece(_white)
{
int direction = 0;
if(piece::white){
direction = 1;
}else{
direction = -1;
}
legals.push_back(coordinates(0,2*direction));
legals.push_back(coordinates(0,direction));
legals.push_back(coordinates(1,direction));
legals.push_back(coordinates(-1,direction));
}
std::vector<coordinates> needed_space(coordinates start, coordinates end) override;
void print_piece() override;
void moved() override;
};
#endif
\ No newline at end of file
#include "../system/coordinates.h"
#include "../system/utilities.h"
#include "piece.h"
#include "queen.h"
#include <vector>
#include <iostream>
/// @brief calculate a list of empty space coordinates needed for the move to be legal, assuming the move is possible
/// @param start starting coordinates
/// @param end destination coordinates
/// @return a vector containing all coordinates to be verified
std::vector<coordinates> queen::needed_space(coordinates start, coordinates end){
std::vector<coordinates> result = {};
int diff_x = end.get_x() - start.get_x();
int diff_y = end.get_y() - start.get_y();
int coord_x = -1;
int coord_y = -1;
if(diff_x != 0 && diff_y != 0){
//When the queen moves like a bishop
for(int i = 1; i < abs(diff_x); i++){
if(diff_x > 0){
coord_x = start.get_x() + i;
}else{
coord_x = start.get_x() - i;
}
if(diff_y > 0){
coord_y = start.get_y() + i;
}else{
coord_y = start.get_y() - i;
}
result.push_back(coordinates(coord_x,coord_y));
}
return result;
}else{
//When the queen moves like a rook
if(end.get_x() > start.get_x()){
//Moving right
for(int i = start.get_x() + 1; i < end.get_x(); i++){
result.push_back(coordinates(i,start.get_y()));
}
return result;
}
if(end.get_x() < start.get_x()){
//Moving left
for(int i = start.get_x() - 1; i > end.get_x(); i--){
result.push_back(coordinates(i,start.get_y()));
}
return result;
}
if(end.get_y() > start.get_y()){
//Moving right
for(int i = start.get_y() + 1; i < end.get_y(); i++){
result.push_back(coordinates(start.get_x(),i));
}
return result;
}
if(end.get_y() < start.get_y()){
//Moving left
for(int i = start.get_y() - 1; i > end.get_y(); i--){
result.push_back(coordinates(start.get_x(),i));
}
return result;
}
}
return {};
}
/// @brief print the piece to the standard output
void queen::print_piece(){
if(piece::white){
std::cout << "\u2655";
}else{
std::cout << "\u265B";
}
}
\ No newline at end of file
#ifndef QUEEN
#define QUEEN
#include "../system/coordinates.h"
#include "../system/utilities.h"
#include "piece.h"
#include <vector>
/// @brief the queen piece class.
class queen : public piece{
public:
queen(bool _white):
piece(_white)
{
for(int i = -7; i < 8; i++)
if(i!=0){
// Bishop moves
legals.push_back(coordinates(i,i));
legals.push_back(coordinates(i,-i));
// Rook moves
legals.push_back(coordinates(0,i));
legals.push_back(coordinates(i,0));
}
}
std::vector<coordinates> needed_space(coordinates start, coordinates end) override;
void print_piece() override;
};
#endif
\ No newline at end of file
#include "board.h"
#include <string>
#include <regex>
// constructeur
board::board ()
{
alloc_mem_echiquier();
// allocation des pieces
// Constructeur (Couleur, nom_affiché, id, case)
pieces[Blanc] = {
new Tour (Blanc,"\u2656", 0, Square(0,0)),
new Cavalier(Blanc,"\u2658", 1, Square(0,1)),
new Fou (Blanc,"\u2657", 2, Square(0,2)),
new Dame (Blanc,"\u2655", 3, Square(0,3)),
new Roi (Blanc,"\u2654", 4, Square(0,4)),
new Fou (Blanc,"\u2657", 5, Square(0,5)),
new Cavalier(Blanc,"\u2658", 6, Square(0,6)),
new Tour (Blanc,"\u2656", 7, Square(0,7))
};
pieces[Noir] = {
new Tour (Noir, "\u265C", 8, Square(7,0)),
new Cavalier(Noir, "\u265E", 9, Square(7,1)),
new Fou (Noir, "\u265D", 10, Square(7,2)),
new Dame (Noir, "\u265B", 11, Square(7,3)),
new Roi (Noir, "\u265A", 12, Square(7,4)),
new Fou (Noir, "\u265D", 13, Square(7,5)),
new Cavalier(Noir, "\u265E", 14, Square(7,6)),
new Tour (Noir, "\u265C", 15, Square(7,7))
};
// alloc pawns
for (size_t i=0;i<8;i++) {
pieces[Blanc].push_back(new Pion(Blanc, "\u2659" + string(1,i), 16+i, Square(1,i)));
pieces[Noir].push_back(new Pion(Noir, "\u265F" + string(1,i), 24+i, Square(6,i)));
}
// Pose des pieces en position initiale sur l'echiquier
for (auto p : pieces[Blanc])
pose_piece(p, p->get_pos());
for (auto p : pieces[Noir])
pose_piece(p, p->get_pos());
}
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);
}
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