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

commited on 20240508

parent 6cc6b4b3
Branches
No related merge requests found
......@@ -8,8 +8,8 @@
/// @brief the bishop piece class.
class bishop : public piece{
public:
bishop(bool _white):
piece(_white)
bishop(bool _white, coordinates _coord):
piece(_white,_coord)
{
for(int i = -7; i < 8; i++)
if(i!=0){
......
......@@ -8,8 +8,8 @@
/// @brief the king piece class.
class king : public piece{
public:
king(bool _white):
piece(_white)
king(bool _white, coordinates _coord):
piece(_white,_coord)
{
legals.push_back(coordinates(0,1));
legals.push_back(coordinates(0,-1));
......
......@@ -8,8 +8,8 @@
/// @brief the knight piece class.
class knight : public piece{
public:
knight(bool _white):
piece(_white)
knight(bool _white, coordinates _coord):
piece(_white,_coord)
{
legals.push_back(coordinates(2,1));
legals.push_back(coordinates(2,-1));
......
......@@ -9,19 +9,19 @@
/// @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> pawn::needed_space(coordinates dest){
std::vector<coordinates> result = {};
int diff_x = end.get_x() - start.get_x();
int diff_y = end.get_y() - start.get_y();
int diff_x = dest.get_x() - piece::coord.get_x();
int diff_y = dest.get_y() - piece::coord.get_y();
if(diff_x == 0){
//Normal pawn move, destination case verified
result.push_back(end);
result.push_back(dest);
// 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));
result.push_back(coordinates(piece::coord.get_x(),piece::coord.get_y() + 1));
}else{
result.push_back(coordinates(start.get_x(),start.get_y() - 1));
result.push_back(coordinates(piece::coord.get_x(),piece::coord.get_y() - 1));
}
}
return result;
......@@ -51,11 +51,11 @@ void pawn::moved(){
int main(){
pawn hello(true);
hello.print_piece();
if(hello.is_possible(coordinates(0,0),coordinates(0,2))){
if(hello.is_possible(coordinates(0,2))){
std::cout << "ok2" << std::endl;
}
hello.moved();
if(hello.is_possible(coordinates(0,0),coordinates(0,2))){
if(hello.is_possible(coordinates(0,2))){
std::cout << "ok1" << std::endl;
}
}
\ No newline at end of file
......@@ -8,8 +8,8 @@
/// @brief the pawn piece class.
class pawn : public piece{
public:
pawn(bool _white):
piece(_white)
pawn(bool _white, coordinates _coord):
piece(_white,_coord)
{
int direction = 0;
if(piece::white){
......@@ -22,7 +22,7 @@ class pawn : public piece{
legals.push_back(coordinates(1,direction));
legals.push_back(coordinates(-1,direction));
}
std::vector<coordinates> needed_space(coordinates start, coordinates end) override;
std::vector<coordinates> needed_space(coordinates dest) override;
void print_piece() override;
void moved() override;
};
......
......@@ -5,8 +5,8 @@
/// @brief the constructor for the piece class
/// @param _white indicates if the piece is white
piece::piece(bool _white):
white(_white)
piece::piece(bool _white, coordinates _coord):
white(_white), coord(_coord)
{
legals = {};
has_moved = false;
......
......@@ -12,7 +12,7 @@ class piece{
std::vector<coordinates> legals;
bool has_moved;
public:
piece(bool _white);
piece(bool _white, coordinates _coord);
bool is_possible(coordinates dest);
virtual std::vector<coordinates> needed_space(coordinates dest);
virtual void print_piece();
......
......@@ -9,55 +9,55 @@
/// @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> queen::needed_space(coordinates dest){
std::vector<coordinates> result = {};
int diff_x = end.get_x() - start.get_x();
int diff_y = end.get_y() - start.get_y();
int diff_x = dest.get_x() - piece::coord.get_x();
int diff_y = dest.get_y() - piece::coord.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;
coord_x = piece::coord.get_x() + i;
}else{
coord_x = start.get_x() - i;
coord_x = piece::coord.get_x() - i;
}
if(diff_y > 0){
coord_y = start.get_y() + i;
coord_y = piece::coord.get_y() + i;
}else{
coord_y = start.get_y() - i;
coord_y = piece::coord.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()){
if(dest.get_x() > piece::coord.get_x()){
//Moving right
for(int i = start.get_x() + 1; i < end.get_x(); i++){
result.push_back(coordinates(i,start.get_y()));
for(int i = piece::coord.get_x() + 1; i < dest.get_x(); i++){
result.push_back(coordinates(i,piece::coord.get_y()));
}
return result;
}
if(end.get_x() < start.get_x()){
if(dest.get_x() < piece::coord.get_x()){
//Moving left
for(int i = start.get_x() - 1; i > end.get_x(); i--){
result.push_back(coordinates(i,start.get_y()));
for(int i = piece::coord.get_x() - 1; i > dest.get_x(); i--){
result.push_back(coordinates(i,piece::coord.get_y()));
}
return result;
}
if(end.get_y() > start.get_y()){
if(dest.get_y() > piece::coord.get_y()){
//Moving right
for(int i = start.get_y() + 1; i < end.get_y(); i++){
result.push_back(coordinates(start.get_x(),i));
for(int i = piece::coord.get_y() + 1; i < dest.get_y(); i++){
result.push_back(coordinates(piece::coord.get_x(),i));
}
return result;
}
if(end.get_y() < start.get_y()){
if(dest.get_y() < piece::coord.get_y()){
//Moving left
for(int i = start.get_y() - 1; i > end.get_y(); i--){
result.push_back(coordinates(start.get_x(),i));
for(int i = piece::coord.get_y() - 1; i > dest.get_y(); i--){
result.push_back(coordinates(piece::coord.get_x(),i));
}
return result;
}
......
......@@ -8,8 +8,8 @@
/// @brief the queen piece class.
class queen : public piece{
public:
queen(bool _white):
piece(_white)
queen(bool _white, coordinates _coord):
piece(_white,_coord)
{
for(int i = -7; i < 8; i++)
if(i!=0){
......@@ -21,7 +21,7 @@ class queen : public piece{
legals.push_back(coordinates(i,0));
}
}
std::vector<coordinates> needed_space(coordinates start, coordinates end) override;
std::vector<coordinates> needed_space(coordinates dest) override;
void print_piece() override;
};
......
......@@ -9,33 +9,33 @@
/// @param start starting coordinates
/// @param end destination coordinates
/// @return a vector containing all coordinates to be verified
std::vector<coordinates> rook::needed_space(coordinates start, coordinates end){
std::vector<coordinates> rook::needed_space(coordinates dest){
std::vector<coordinates> result = {};
if(end.get_x() > start.get_x()){
if(dest.get_x() > piece::coord.get_x()){
//Moving right
for(int i = start.get_x() + 1; i < end.get_x(); i++){
result.push_back(coordinates(i,start.get_y()));
for(int i = piece::coord.get_x() + 1; i < dest.get_x(); i++){
result.push_back(coordinates(i,piece::coord.get_y()));
}
return result;
}
if(end.get_x() < start.get_x()){
if(dest.get_x() < piece::coord.get_x()){
//Moving left
for(int i = start.get_x() - 1; i > end.get_x(); i--){
result.push_back(coordinates(i,start.get_y()));
for(int i = piece::coord.get_x() - 1; i > dest.get_x(); i--){
result.push_back(coordinates(i,piece::coord.get_y()));
}
return result;
}
if(end.get_y() > start.get_y()){
if(dest.get_y() > piece::coord.get_y()){
//Moving right
for(int i = start.get_y() + 1; i < end.get_y(); i++){
result.push_back(coordinates(start.get_x(),i));
for(int i = piece::coord.get_y() + 1; i < dest.get_y(); i++){
result.push_back(coordinates(piece::coord.get_x(),i));
}
return result;
}
if(end.get_y() < start.get_y()){
if(dest.get_y() < piece::coord.get_y()){
//Moving left
for(int i = start.get_y() - 1; i > end.get_y(); i--){
result.push_back(coordinates(start.get_x(),i));
for(int i = piece::coord.get_y() - 1; i > dest.get_y(); i--){
result.push_back(coordinates(piece::coord.get_x(),i));
}
return result;
}
......
......@@ -8,8 +8,8 @@
/// @brief the rook piece class.
class rook : public piece{
public:
rook(bool _white):
piece(_white)
rook(bool _white, coordinates _coord):
piece(_white,_coord)
{
for(int i = -7; i < 8; i++)
if(i!=0){
......@@ -17,7 +17,7 @@ class rook : public piece{
legals.push_back(coordinates(i,0));
}
}
std::vector<coordinates> needed_space(coordinates start, coordinates end) override;
std::vector<coordinates> needed_space(coordinates dest) override;
void print_piece() override;
};
......
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