-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboardsnapshot.cpp
More file actions
125 lines (116 loc) · 4.7 KB
/
Copy pathboardsnapshot.cpp
File metadata and controls
125 lines (116 loc) · 4.7 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
#include "boardsnapshot.h"
#include <QJsonArray>
#include <QtGlobal>
BoardSnapshot snapshotFromModel(const GameModel& m, int player)
{
BoardSnapshot s;
s.rows = m.rows();
s.cols = m.cols();
s.currentPlayer = m.currentPlayer();
s.winner = m.winner();
for (int r = 0; r < m.rows(); r++) s.rowHints.append(m.rowHint(r));
for (int c = 0; c < m.cols(); c++) s.colHints.append(m.colHint(c));
s.view = QVector<QVector<int>>(m.rows(), QVector<int>(m.cols(), 0));
for (int r = 0; r < m.rows(); r++)
for (int c = 0; c < m.cols(); c++) {
const int idx = m.shipAt(r, c);
if (idx != -1 && m.ship(idx).owner == player)
s.view[r][c] = 2; // own
else if (m.isRevealed(player, r, c))
s.view[r][c] = (idx != -1) ? 3 : 1; // enemy / water
else
s.view[r][c] = 0; // fog
}
// 己方舰船(按模型索引序)
for (int i = 0; i < m.shipCount(); i++) {
const GameModel::Ship& sh = m.ship(i);
if (sh.owner != player || sh.hp <= 0 || sh.cells.isEmpty())
continue;
ShipView v;
v.index = i; v.size = sh.size; v.hp = sh.hp; v.maxHp = sh.maxHp;
v.dir = sh.dir; v.cells = sh.cells; v.mine = true;
s.ships.append(v);
}
// 敌方舰船(仅全揭示才绘制)
for (int i = 0; i < m.shipCount(); i++) {
const GameModel::Ship& sh = m.ship(i);
if (sh.owner == player || sh.hp <= 0 || sh.cells.isEmpty())
continue;
bool all = true;
for (const QPoint& p : sh.cells)
if (!m.isRevealed(player, p.x(), p.y())) { all = false; break; }
if (!all)
continue;
ShipView v;
v.index = -1; v.size = sh.size; v.hp = sh.hp; v.maxHp = sh.maxHp;
v.dir = sh.dir; v.cells = sh.cells; v.mine = false;
s.ships.append(v);
}
return s;
}
BoardSnapshot snapshotFromJson(const QJsonObject& state)
{
BoardSnapshot s;
s.rows = state[QStringLiteral("rows")].toInt(9);
s.cols = state[QStringLiteral("cols")].toInt(9);
s.currentPlayer = state[QStringLiteral("currentPlayer")].toInt(0);
s.winner = state[QStringLiteral("winner")].toInt(-1);
for (const QJsonValue& v : state[QStringLiteral("rowHints")].toArray()) s.rowHints.append(v.toInt());
for (const QJsonValue& v : state[QStringLiteral("colHints")].toArray()) s.colHints.append(v.toInt());
s.view = QVector<QVector<int>>(s.rows, QVector<int>(s.cols, 0));
const QJsonArray view = state[QStringLiteral("view")].toArray();
for (int r = 0; r < s.rows; r++) {
const QJsonArray row = view[r].toArray();
for (int c = 0; c < s.cols; c++) {
const QString v = row[c].toString();
s.view[r][c] = (v == QLatin1String("own")) ? 2
: (v == QLatin1String("enemy")) ? 3
: (v == QLatin1String("water")) ? 1 : 0;
}
}
for (const QJsonValue& v : state[QStringLiteral("myShips")].toArray()) {
const QJsonObject o = v.toObject();
ShipView sv;
sv.index = o[QStringLiteral("index")].toInt(-1);
sv.size = o[QStringLiteral("size")].toInt();
sv.hp = o[QStringLiteral("hp")].toInt();
sv.maxHp = o[QStringLiteral("maxHp")].toInt();
sv.dir = o[QStringLiteral("dir")].toInt();
sv.mine = true;
for (const QJsonValue& cc : o[QStringLiteral("cells")].toArray())
sv.cells.append(QPoint(cc.toArray()[0].toInt(), cc.toArray()[1].toInt()));
s.ships.append(sv);
}
for (const QJsonValue& v : state[QStringLiteral("enemyShips")].toArray()) {
const QJsonObject o = v.toObject();
ShipView sv;
sv.index = -1;
sv.size = o[QStringLiteral("size")].toInt();
sv.maxHp = o[QStringLiteral("maxHp")].toInt(sv.size);
sv.hp = o[QStringLiteral("hp")].toInt(-1);
sv.dir = o[QStringLiteral("dir")].toInt();
sv.mine = false;
for (const QJsonValue& cc : o[QStringLiteral("cells")].toArray())
sv.cells.append(QPoint(cc.toArray()[0].toInt(), cc.toArray()[1].toInt()));
// 仅全揭示才绘制舰船
if (sv.cells.size() == sv.size)
s.ships.append(sv);
}
return s;
}
QVector<QPoint> attackRangeCells(const ShipView& s, int rows, int cols)
{
QVector<QPoint> out;
if (s.cells.isEmpty())
return out;
int m = s.size / 2;
if (m >= s.cells.size())
m = s.cells.size() - 1;
const QPoint center = s.cells[m];
const int range = s.size * s.size;
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
if (qMax(qAbs(r - center.x()), qAbs(c - center.y())) <= range)
out.append(QPoint(r, c));
return out;
}