-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.h
149 lines (112 loc) · 4.12 KB
/
piece.h
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once
#include <vector>
#include <string>
#include "position.h"
#include "color.h"
#include "Chessboard.h"
class Chessboard;
// Abstract representation of a chess piece
class Piece {
protected:
// Position on the chessboard
Position position_;
// Color of the piece
Color color_;
// Has this piece moved yet
bool moved_;
public:
/**
* Creates a new chess piece
*
* @param startPosition Starting position
* @param isWhite Is the piece part of the white team?
*/
Piece(Position startPosition, Color color);
// Checks if this piece is part of the white team
Color color() const;
Position position() const;
bool moved() const;
// Full piece name, to be set in the constructor of an inheriting piece
virtual std::string full_name() const = 0;
// Short, 1 char representation (for display purposes in the << operator), to be set in the constructor of an inheriting piece
virtual char chessboard_representation() const = 0;
/**
* Gets the possible moves of this piece on a chessboard
*
* @param chessboard Chessboard the piece will move on.
* @return Vector of possible new positions to take
*/
virtual std::vector<Position> possible_moves(const Chessboard& chessboard) const = 0;
/**
* Moves this piece to a new position on the chessboard. If move will cause an enemy piece to be captured, it will be removed from the chessboard
*
* @param chessboard Chessboard the piece will move on.
* @param newPosition New position for this piece
*/
void move_to(Chessboard& chessboard, Position newPosition);
/**
* Compares two chess pieces by name, color and position
*
* @param other Piece to compare to
*/
bool operator==(const Piece& other) const;
/**
* Compares two chess pieces by name, color and position
*
* @param other Piece to compare to
*/
bool operator!=(const Piece& other) const;
/**
* Tries to add move to a vector of positions. Checks if move is on the chessboard and doesn't overwrite another piece from the same team
*
* @param pos Position to add
* @param positions Vector of moves to add to
* @param chessboard Game state
* @returns True if the placement of moves can continue (if the space is empty), false if not
*/
bool try_add_movement_option(Position pos, std::vector<Position>& positions, const Chessboard& chessboard) const;
// Appends the short char representation to the stream
friend std::ostream& operator<<(std::ostream& out, const Piece& piece);
};
class Pawn : public Piece {
public:
Pawn(Position startPosition, Color color);
std::string full_name() const override;
char chessboard_representation() const override;
std::vector<Position> possible_moves(const Chessboard& chessboard) const override;
};
class Knight : public Piece {
public:
Knight(Position startPosition, Color color) : Piece(startPosition, color) {}
std::string full_name() const override;
char chessboard_representation() const override;
std::vector<Position> possible_moves(const Chessboard& chessboard) const override;
};
class Bishop : public Piece {
public:
Bishop(Position startPosition, Color color) : Piece(startPosition, color) {}
std::string full_name() const override;
char chessboard_representation() const override;
std::vector<Position> possible_moves(const Chessboard& chessboard) const override;
};
class Rook : public Piece {
public:
Rook(Position startPosition, Color color) : Piece(startPosition, color) {}
std::string full_name() const override;
char chessboard_representation() const override;
std::vector<Position> possible_moves(const Chessboard& chessboard) const override;
};
class Queen : public Piece {
public:
Queen(Position startPosition, Color color) : Piece(startPosition, color) {}
std::string full_name() const override;
char chessboard_representation() const override;
std::vector<Position> possible_moves(const Chessboard& chessboard) const override;
};
class King : public Piece {
public:
King(Position startPosition, Color color) : Piece(startPosition, color) {}
std::string full_name() const override;
char chessboard_representation() const override;
std::vector<Position> possible_moves(const Chessboard& chessboard) const override;
};