-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
185 lines (167 loc) · 6.42 KB
/
Board.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "Board.h"
#include <iostream>
// 静态成员变量初始化
std::shared_ptr<ChessBoard> ChessBoard::instance = nullptr;
// 私有构造函数
ChessBoard::ChessBoard() {
board.resize(size, std::vector<std::vector<std::shared_ptr<Piece>>>(size));
}
// 获取唯一实例
std::shared_ptr<ChessBoard> ChessBoard::getInstance() {
if (!instance) {
instance = std::shared_ptr<ChessBoard>(new ChessBoard());
}
return instance;
}
// 检查坐标是否在棋盘范围内
bool ChessBoard::isWithinBounds(int x, int y) const {
return x >= 0 && x < size && y >= 0 && y < size;
}
// 放置棋子
void ChessBoard::placePiece(int x, int y, uint8_t pieceType, std::shared_ptr<Player> player) {
if (!isWithinBounds(x, y)) {
throw std::out_of_range("Coordinates out of bounds.");
}
std::shared_ptr<Piece> newPiece;
int pieceId = pieceType;
switch (pieceId) {
case PieceType::QueenBee_id:
newPiece = std::make_shared<QueenBee>(x, y, player);
break;
case PieceType::Beetle_id:
newPiece = std::make_shared<Beetle>(x, y, player);
break;
case PieceType::Grasshopper_id:
newPiece = std::make_shared<Grasshopper>(x, y, player);
break;
case PieceType::Spider_id:
newPiece = std::make_shared<Spider>(x, y, player);
break;
case PieceType::Ant_id:
newPiece = std::make_shared<Ant>(x, y, player);
break;
case PieceType::Ladybug_id:
newPiece = std::make_shared<Ladybug>(x, y, player);
break;
case PieceType::Mosquito_id:
newPiece = std::make_shared<Mosquito>(x, y, player);
break;
case PieceType::Pillbug_id:
newPiece = std::make_shared<Pillbug>(x, y, player);
break;
default:
throw std::invalid_argument("Invalid piece type.");
}
// 如果是放置潮虫,将潮虫放置于vecter开头
if(newPiece->getPieceType() == PieceType::Pillbug_id){
board[x][y].emplace(board[x][y].begin(), newPiece);
return;
}
// 如果是空位置或者放置的是 Beetle,可以放置棋子
if (board[x][y].empty() || newPiece->getPieceType() == PieceType::Beetle_id) {
board[x][y].push_back(newPiece);
} else {
throw std::runtime_error("Cannot place piece here.");
}
}
// 移动棋子
void ChessBoard::movePiece(int fromX, int fromY, int toX, int toY, const std::string& pieceType, std::shared_ptr<Player> player) {
if (!isWithinBounds(fromX, fromY) || !isWithinBounds(toX, toY)) {
throw std::out_of_range("Coordinates out of bounds.");
}
if (board[fromX][fromY].empty() || board[fromX][fromY].back()->getName() != pieceType) {
throw std::invalid_argument("No piece of specified type at the given position.");
}
auto piece = board[fromX][fromY].back();
if (!piece->isValidMove(toX, toY, *this)) {
throw std::runtime_error("Invalid move for this piece type.");
}
board[fromX][fromY].pop_back();
placePiece(toX, toY, piece->getPieceType(), player);
}
// 显示棋盘
void ChessBoard::displayBoard() const {
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (!board[i][j].empty() && board[i][j].back()) {
std::cout << board[i][j].back()->getName() << " ";
} else {
std::cout << ". ";
}
}
std::cout << std::endl;
}
}
// 检查某个位置是否有相邻棋子
bool ChessBoard::hasAdjacentPiece(int x, int y) const {
const int directions[6][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, -1}, {-1, 1}};
for (const auto& dir : directions) {
int adjX = x + dir[0];
int adjY = y + dir[1];
// 如果相邻的位置在棋盘范围内,并且有棋子,则返回 true
if (isWithinBounds(adjX, adjY) && !board[adjX][adjY].empty()) {
return true;
}
}
return false;
}
// 查看棋子周围所有棋子
std::vector<std::shared_ptr<Piece>> ChessBoard::getAdjacentPieces(int x, int y) const {
std::vector<std::shared_ptr<Piece>> adjacentPieces;
const int directions[6][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, -1}, {-1, 1}};
for (const auto& dir : directions) {
int adjX = x + dir[0];
int adjY = y + dir[1];
if (isWithinBounds(adjX, adjY) && !board[adjX][adjY].empty()) {
adjacentPieces.push_back(board[adjX][adjY].back());
}
}
return adjacentPieces;
}
// 检查某个位置是否被包围
bool ChessBoard::isPositionSurrounded(int x, int y, bool considerEdge) const {
const int directions_ji[6][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}};
const int directions_ou[6][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, -1}, {-1, -1}};
if (y%2 == 0){
for (const auto& dir : directions_ou) {
int adjX = x + dir[0];
int adjY = y + dir[1];
if (isWithinBounds(adjX, adjY)) {
if (board[adjX][adjY].empty()) {
return false; // 如果某个方向没有棋子,则位置未被完全包围
}
} else if (!considerEdge) {
return false; // 如果不考虑边缘,边缘视为未被包围
}
}
}
else{
for (const auto& dir : directions_ji) {
int adjX = x + dir[0];
int adjY = y + dir[1];
if (isWithinBounds(adjX, adjY)) {
if (board[adjX][adjY].empty()) {
return false; // 如果某个方向没有棋子,则位置未被完全包围
}
} else if (!considerEdge) {
return false; // 如果不考虑边缘,边缘视为未被包围
}
}
}
return true; // 所有方向都有棋子,或者边缘被视为封闭,位置被完全包围
}
// 清空棋盘
void ChessBoard::clearBoard() {
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
board[i][j].clear(); // 清空每个位置的棋子堆栈
}
}
std::cout << "The board has been cleared." << std::endl;
}
std::shared_ptr<Piece> ChessBoard::getPieceAt(int x, int y) const {
if (!isWithinBounds(x, y) || board[x][y].empty()) {
return nullptr; // 如果位置不在边界内或没有棋子,返回空指针
}
return board[x][y].back(); // 返回堆栈顶部的棋子
}