-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpiece.cpp
More file actions
executable file
·103 lines (81 loc) · 3.28 KB
/
piece.cpp
File metadata and controls
executable file
·103 lines (81 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "piece.h"
// name field.
const std::string Piece::name = "piece";
const std::string Queen::name = "queen";
const std::string King::name = "king";
const std::string Tower::name = "tower";
const std::string Knight::name = "knight";
const std::string Bishop::name = "bishop";
const std::string Pawn::name = "pawn";
std::string Piece::get_name() const {return name;}
std::string King::get_name() const {return name;}
std::string Queen::get_name() const {return name;}
std::string Bishop::get_name() const {return name;}
std::string Knight::get_name() const {return name;}
std::string Tower::get_name() const {return name;}
std::string Pawn::get_name() const {return name;}
// Management of subclass builders
King::King(Case c, int col) : Piece(c, col){}
Queen::Queen(Case c, int col) : Piece(c, col){}
Knight::Knight(Case c, int col) : Piece(c, col){}
Bishop::Bishop(Case c, int col) : Piece(c, col){}
Tower::Tower(Case c, int col) : Piece(c, col){}
Pawn::Pawn(Case c, int col) : Piece(c, col){}
/******************************
* Constructors of the pieces *
******************************/
// Piece::Piece()=default;
Piece::Piece(Case case_depart, int col){
color = col;
c = case_depart;
}
/****************************************************
****************** Move ********************
***************************************************/
void Piece::move(Case end_of_move_case){
c=end_of_move_case;
}
void Pawn::move(Case end_of_move_case){
c=end_of_move_case;
}
void King::move(Case end_of_move_case){
c=end_of_move_case;
}
void Tower::move(Case end_of_move_case){
c=end_of_move_case;
}
/****************************************************
***************** permission_move ******************
***************************************************/
// Management of possible movements
bool King::permission_move(Case arrival_case) const {
return arrival_case.distance(c) <= 1;
}
bool Queen::permission_move(Case arrival_case) const { // The queen has the movements of a bishop or a tower
return (arrival_case.get(0) == c.get(0) || arrival_case.get(1) == c.get(1)) || (abs(arrival_case.get(0) - c.get(0)) == abs(arrival_case.get(1) - c.get(1)));
}
bool Knight::permission_move(Case arrival_case) const {
return abs(arrival_case.get(0) - c.get(0)) * abs(arrival_case.get(1) - c.get(1)) == 2;
}
bool Bishop::permission_move(Case arrival_case) const {
return abs(arrival_case.get(0) - c.get(0)) == abs(arrival_case.get(1) - c.get(1));
}
bool Tower::permission_move(Case arrival_case) const {
return arrival_case.get(0) == c.get(0) || arrival_case.get(1) == c.get(1);
}
bool Pawn::permission_move(Case arrival_case) const { // black=0 on top, white=1 on bottom
if (std::abs(arrival_case.get(0) - c.get(0)) > 1) return false;
if (color == 1){
if (arrival_case.get(1) - c.get(1) == 1){
return true;
}
else return arrival_case.get(1) - c.get(1) == 2 && arrival_case.get(0) == c.get(0) && c.get(1) == 1;
}
else if (color == 0){
if (arrival_case.get(1) - c.get(1) == -1){
return true;
}
else return arrival_case.get(1) - c.get(1) == -2 && arrival_case.get(0) == c.get(0) && c.get(1) == 6;
}
else return false; // the function should never reach this point (in theory)
}