-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.cpp
156 lines (120 loc) · 4.16 KB
/
Piece.cpp
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
150
151
152
153
154
155
#include "Piece.h"
Piece::Piece(int x, int y, int type, std::shared_ptr<Player> owner)
: x(x), y(y), pieceType(type), owner(owner) {}
void Piece::setPosition(int newX, int newY) {
x = newX;
y = newY;
}
int Piece::getPieceType() const {
return pieceType;
}
std::pair<int, int> Piece::getPosition() const {
return std::make_pair(x, y);
}
std::shared_ptr<Player> Piece::getOwner() const {
return owner;
}
void Piece::setOwner(std::shared_ptr<Player> newOwner) {
owner = newOwner;
}
void Piece::setPieceType(int newPieceType) {
pieceType = newPieceType;
}
std::string Piece::getName() const {
return "Unknown Piece";
}
bool Piece::isValidMove(int toX, int toY, const ChessBoard& board) const {
// 默认的移动验证逻辑
return toX >= 0 && toY >= 0 && toX < board.getWidth() && toY < board.getHeight();
}
// 各个子类的实现...
// QueenBee类的实现
QueenBee::QueenBee(int x, int y, std::shared_ptr<Player> owner)
: Piece(x, y, PieceType::QueenBee_id, owner) {}
bool QueenBee::isValidMove(int toX, int toY, const ChessBoard& board) const {
int dx = abs(toX - x);
int dy = abs(toY - y);
return (dx + dy == 1 || (dx == 1 && dy == 1)) && board.hasAdjacentPiece(toX, toY);
}
std::string QueenBee::getName() const {
return "QueenBee";
}
// Beetle类的实现
Beetle::Beetle(int x, int y, std::shared_ptr<Player> owner)
: Piece(x, y, PieceType::Beetle_id, owner) {}
bool Beetle::isValidMove(int toX, int toY, const ChessBoard& board) const {
return (abs(toX - x) <= 1 && abs(toY - y) <= 1) && board.hasAdjacentPiece(toX, toY);
}
std::string Beetle::getName() const {
return "Beetle";
}
// Grasshopper类的实现
Grasshopper::Grasshopper(int x, int y, std::shared_ptr<Player> owner)
: Piece(x, y, PieceType::Grasshopper_id, owner) {}
bool Grasshopper::isValidMove(int toX, int toY, const ChessBoard& board) const {
if (!board.hasAdjacentPiece(toX, toY)) return false;
if (x == toX) {
int step = (toY > y) ? 1 : -1;
for (int j = y + step; j != toY; j += step) {
if (board.getBoard()[x][j].empty()) return false;
}
} else if (y == toY) {
int step = (toX > x) ? 1 : -1;
for (int i = x + step; i != toX; i += step) {
if (board.getBoard()[i][y].empty()) return false;
}
} else {
return false;
}
return true;
}
std::string Grasshopper::getName() const {
return "Grasshopper";
}
// Spider类的实现
Spider::Spider(int x, int y, std::shared_ptr<Player> owner)
: Piece(x, y, PieceType::Spider_id, owner) {}
bool Spider::isValidMove(int toX, int toY, const ChessBoard& board) const {
int distance = abs(toX - x) + abs(toY - y);
return distance == 4 && board.hasAdjacentPiece(toX, toY);
}
std::string Spider::getName() const {
return "Spider";
}
// Ant类的实现
Ant::Ant(int x, int y, std::shared_ptr<Player> owner)
: Piece(x, y, PieceType::Ant_id, owner) {}
bool Ant::isValidMove(int toX, int toY, const ChessBoard& board) const {
return board.hasAdjacentPiece(toX, toY);
}
std::string Ant::getName() const {
return "Ant";
}
// Ladybug类的实现
Ladybug::Ladybug(int x, int y, std::shared_ptr<Player> owner)
: Piece(x, y, PieceType::Ladybug_id, owner) {}
bool Ladybug::isValidMove(int toX, int toY, const ChessBoard& board) const {
int distance = abs(toX - x) + abs(toY - y);
return distance == 4 && board.hasAdjacentPiece(toX, toY);
}
std::string Ladybug::getName() const {
return "Ladybug";
}
// Mosquito类的实现
Mosquito::Mosquito(int x, int y, std::shared_ptr<Player> owner)
: Piece(x, y, PieceType::Mosquito_id, owner) {}
bool Mosquito::isValidMove(int toX, int toY, const ChessBoard& board) const {
return board.hasAdjacentPiece(toX, toY);
}
std::string Mosquito::getName() const {
return "Mosquito";
}
// Pillbug类的实现
Pillbug::Pillbug(int x, int y, std::shared_ptr<Player> owner)
: Piece(x, y, PieceType::Pillbug_id, owner) {}
bool Pillbug::isValidMove(int toX, int toY, const ChessBoard& board) const {
return (abs(toX - x) <= 1 && abs(toY - y) <= 1) && board.hasAdjacentPiece(toX, toY);
}
std::string Pillbug::getName() const {
return "Pillbug";
}