-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessBoardCore.cpp
181 lines (167 loc) · 4.98 KB
/
chessBoardCore.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
#include "chessboardcore.h"
#include <QDebug>
#include <QFileDialog>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <cctype>
#define FOR(i, j, k) for (int i = (j); i < (k); i++)
// 共享的全局变量定义
ChessBoardCore chessBoard;
ChessBoardCore::ChessBoardCore() { init(); }
ChessBoardCore::~ChessBoardCore() {}
void ChessBoardCore::dataPrint()
{
FOR(i, 0, 15)
{
FOR(j, 0, 15) { qDebug("%d", static_cast<int>(m_data[i][j])); }
qDebug() << endl;
}
}
void ChessBoardCore::clearData()
{
FOR(i, 0, 15)
FOR(j, 0, 15) { m_data[i][j] = DataType::none; }
}
bool ChessBoardCore::searchWin(ChessBoardCore::DataType chess)
{
FOR(i, 0, 11)
FOR(j, 0, 15)
if (m_data[i][j] == chess && m_data[i + 1][j] == chess && m_data[i + 2][j] == chess &&
m_data[i + 3][j] == chess && m_data[i + 4][j] == chess)
return true;
FOR(i, 0, 15)
FOR(j, 0, 11)
if (m_data[i][j] == chess && m_data[i][j + 1] == chess && m_data[i][j + 2] == chess &&
m_data[i][j + 3] == chess && m_data[i][j + 4] == chess)
return true;
FOR(i, 0, 11)
FOR(j, 0, 11)
if (m_data[i][j] == chess && m_data[i + 1][j + 1] == chess &&
m_data[i + 2][j + 2] == chess && m_data[i + 3][j + 3] == chess &&
m_data[i + 4][j + 4] == chess)
return true;
FOR(i, 0, 11)
FOR(j, 4, 15)
if (m_data[i][j] == chess && m_data[i + 1][j - 1] == chess &&
m_data[i + 2][j - 2] == chess && m_data[i + 3][j - 3] == chess &&
m_data[i + 4][j - 4] == chess)
return true;
return false;
}
bool ChessBoardCore::saveBoard()
{
QString fileName =
QFileDialog::getSaveFileName(nullptr, tr("Save Chess Board File"), QString("."),
tr("ChessBoardFile(*.chessbrd)"));
if (!fileName.isNull())
{
QFile file(fileName);
if (!file.open(QFile::Text | QFile::WriteOnly))
{
qDebug() << file.errorString() << endl;
return false;
}
QJsonObject jsonObj;
jsonObj.insert("usedTime", m_usedTime);
jsonObj.insert("flag", m_flag);
QString tempString;
FOR(i, 0, 15)
{
FOR(j, 0, 15) { tempString.append(static_cast<int>(m_data[i][j]) + 48); }
}
jsonObj.insert("chessBoard", tempString);
QJsonDocument jsonDoc;
jsonDoc.setObject(jsonObj);
qDebug() << jsonDoc.toJson();
file.write(jsonDoc.toJson());
file.close();
}
qDebug() << fileName << endl;
return true;
}
bool ChessBoardCore::loadBoard()
{
QString filename =
QFileDialog::getOpenFileName(nullptr, tr("Open Chess Board File"), QString("."),
tr("ChessBoardFile(*.chessbrd)"));
if (!filename.isNull())
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << file.errorString() << endl;
return false;
}
QByteArray ba = file.readAll();
// qDebug() << ba;
QJsonDocument jsonDoc;
jsonDoc = QJsonDocument::fromJson(ba);
QJsonObject jsonObj = jsonDoc.object();
qDebug() << jsonDoc.toJson();
m_usedTime = jsonObj["usedTime"].toInt();
m_flag = jsonDoc["flag"].toInt();
qDebug() << m_usedTime;
qDebug() << m_flag;
QString tempStr = jsonObj["chessBoard"].toString();
FOR(i, 0, 15)
{
FOR(j, 0, 15)
{
int tempData = tempStr[i * 15 + j].toLatin1();
qDebug() << tempData;
if (!std::isdigit(tempData))
{
file.close();
init();
return false;
}
m_data[i][j] = static_cast<DataType>(tempData - 48);
}
}
// dataPrint();
file.close();
return true;
}
else
{
return false;
}
}
void ChessBoardCore::init()
{
clearData();
m_opt = ChessBoardCore::PaintOptType::onlyChessBoard;
m_flag = 0;
m_usedTime = 0;
}
bool ChessBoardCore::setPointData(int x, int y, DataType d, int s = 1)
{
if (x < 0 || y < 0 || x >= 15 || y >= 15)
return false;
else
{
QMutexLocker locker(&mutex);
m_data[x][y] = d;
emit needRepaint();
if (s == 1) emit dataChanged(x, y, d);
return true;
}
}
void ChessBoardCore::setAChess(int x, int y, ChessBoardCore::DataType d)
{
if (chessBoard.getPointData(x, y) == ChessBoardCore::DataType::none)
{
chessBoard.setOpt(ChessBoardCore::PaintOptType::chess);
chessBoard.setPointData(x, y, d, 1);
if (chessBoard.searchWin(d))
{
if (m_flag) { chessBoard.setOpt(ChessBoardCore::PaintOptType::whiteWin); }
else
{
chessBoard.setOpt(ChessBoardCore::PaintOptType::blackWin);
}
}
m_flag ^= 1;
}
}