-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessboard.cpp
More file actions
127 lines (120 loc) · 3.6 KB
/
Copy pathchessboard.cpp
File metadata and controls
127 lines (120 loc) · 3.6 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "chessboard.h"
#include "utils.h"
#include "printer.h"
#include "chess.h"
#include "utils.h"
// #include <iostream>
void Chessboard::add(Point p, shared_ptr<Chess> i) {
id2pnt.insert({i->getID(),p});
board.insert({p,move(i)});
set_minmax(&minx, &maxx, p.x);
set_minmax(&minz, &maxz, p.z);
}
void Chessboard::print() const { //输出棋盘
// std::cout <<minx<<maxx<<minz<<maxz<<std::endl;
Printer* printer = new Printer;
for (int z = minz; z <= maxz;z++)
{
for (int p = 0; p<z - minz;p++)
{
graph g = {-1, " ", " ", " "}; // 输出3格空气
printer->add(g);
}
for (int x = minx; x <= maxx; x++)
{
Point a;
bool foundxz=false;
for (int layer = 5; layer >= 0; layer--){
a = {x, z,layer};
if (board.count(a)>0)
{
printer->add(board.find(a)->second->to_graph());
foundxz = true;
break;
}
}
if(!foundxz){
graph g = {-1, " ", " ", " "}; // 输出6格空气
printer->add(g);
}
}
printer->print();
}
delete printer;
}
// 根据玩家号(0=全部,1,2),枚举全部的棋子,返回集合
std::set<Point> Chessboard::get_chess(int i) const
{
set<Point> ret;
for (auto it = id2pnt.begin(); it != id2pnt.end(); ++it)
{
if ((i != 0 && it->first.player != i) || it->second.layer!=0)//滤除上层棋子
{
continue;
}
ret.insert(it->second);
}
return ret;
}
std::set<Point> Chessboard::enum_mov_dest(Point p) const // 列举‘蚂蚁’全部可能到达的位置(不考虑卡位)
{
set<Point> x = get_chess(0);
x.erase(p);
set<Point> ret=enum_nearby(x);
ret.erase(p);
return ret;
}
bool Chessboard::isConnected(const Point &p) const
{ // 判断棋盘连通性
std::set<Point> Allchesses = get_chess(0);
Allchesses.erase(p);
if (bfs(Allchesses)) {
return true;
}
return false;
}
bool Chessboard::bfs(std::set<Point> &Allchesses) const {
std::queue<Point> toVisit;
Point start = *(Allchesses.begin());
toVisit.push(start);
std::set<Point>visited;
while (!toVisit.empty()) {
Point current = toVisit.front();
toVisit.pop();
visited.insert(current);
// 遍历当前棋子的邻居
for (const Point &direction : DIRECTIONS) {
Point neighbor = {current+direction};
// 如果邻居在网格中,且尚未访问过
if (Allchesses.find(neighbor) != Allchesses.end() && visited.find(neighbor) == visited.end()) {
toVisit.push(neighbor);
}
}
}
if (visited.size() == Allchesses.size()) {
return true;
}
return false;
}
// void Chessboard::remove(cid id)
// { // 删除棋子,特殊调用
// Point ori_point = id2pnt[id];
// id2pnt.erase(id);
// auto it = board.find(ori_point);
// board.erase(it);
// }
void Chessboard::move_chess(cid id,Point target){
Point origin = id2pnt[id];
id2pnt[id] = target;
auto it=board.find(origin);
board.insert({target,move(it->second)});
set_minmax(&minx, &maxx, target.x);
set_minmax(&minz, &maxz, target.z);
board.erase(it);
// 考虑到游戏棋盘一致性,移动时不回收边界
}
bool Chessboard::check_upper(Point p) const//有压在上方的棋子时返回True
{
p.layer++;
return (board.count(p) > 0);
}