-
Notifications
You must be signed in to change notification settings - Fork 3
/
generateboard.cpp
281 lines (251 loc) · 9.05 KB
/
generateboard.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* \file generateboard.cpp
*
* \section LICENSE
*
* Copyright (C) 2015-present Thorsten Roth
*
* This file is part of StackAndConquer.
*
* StackAndConquer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* StackAndConquer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with StackAndConquer. If not, see <https://www.gnu.org/licenses/>.
*
* \section DESCRIPTION
* Generate json .stackboard file from txt input files.
*/
#include "./generateboard.h"
#include <QDebug>
#include <QDirIterator>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
void GenerateBoard::startGeneration(const QStringList &cmdArgs,
const QString &sBoardInExt,
const QString &sBoardOutExt,
const QString &sIN, const QString &sOUT) {
GenerateBoard::loopFiles(GenerateBoard::checkCmdArgs(cmdArgs, sBoardInExt),
sBoardInExt, sBoardOutExt, sIN, sOUT);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto GenerateBoard::checkCmdArgs(const QStringList &cmdArgs,
const QString &sBoardInExt)
-> QPair<QFileInfo, QFileInfo> {
if (cmdArgs.isEmpty()) {
qWarning() << "Please specify input (can be file *" + sBoardInExt +
" or "
"folder) and optional the destination folder:";
qWarning() << "./stackandconquer INPUT [DESTINATION]\n";
exit(-1);
}
QFileInfo fiTest;
fiTest.setFile(cmdArgs.at(0).trimmed());
if (!fiTest.exists()) {
qWarning() << "Input file/folder" << cmdArgs.at(0) << "cannot be found!\n";
exit(-1);
}
QPair<QFileInfo, QFileInfo> fiInOut;
fiInOut.first = QFileInfo(cmdArgs.at(0).trimmed());
if (cmdArgs.size() > 1) {
fiTest.setFile(cmdArgs.at(1));
if (!fiTest.exists() || !fiTest.isDir()) {
qWarning() << "Output folder" << cmdArgs.at(1) << "cannot be found!\n";
exit(-1);
}
fiInOut.second = QFileInfo(cmdArgs.at(1).trimmed());
}
return fiInOut;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void GenerateBoard::loopFiles(const QPair<QFileInfo, QFileInfo> &fiInOut,
const QString &sBoardInExt,
const QString &sBoardOutExt, const QString &sIN,
const QString &sOUT) {
QFile input;
QFile output;
if (fiInOut.first.isFile()) {
if (QString(sBoardInExt).remove('.') != fiInOut.first.suffix()) {
qWarning() << "Input file has to use " + sBoardInExt + " extension!\n";
exit(-1);
}
input.setFileName(fiInOut.first.filePath());
if (fiInOut.second.exists()) {
output.setFileName(fiInOut.second.filePath() + "/" +
fiInOut.first.baseName() + sBoardOutExt);
} else {
QString sOut(fiInOut.first.filePath());
sOut = sOut.replace(sBoardInExt, sBoardOutExt);
output.setFileName(sOut);
}
if (!generateBoard(&input, &output, sIN, sOUT)) {
exit(-1);
}
} else { // Loop through input folder
QDirIterator it(fiInOut.first.filePath(),
QStringList() << "*" + sBoardInExt,
QDir::NoDotAndDotDot | QDir::Files);
while (it.hasNext()) {
it.next();
input.setFileName(it.filePath());
if (fiInOut.second.exists()) {
output.setFileName(fiInOut.second.filePath() + "/" +
it.fileInfo().baseName() + sBoardOutExt);
} else {
QString sOut(it.filePath());
sOut = sOut.replace(sBoardInExt, sBoardOutExt);
output.setFileName(sOut);
}
if (!generateBoard(&input, &output, sIN, sOUT)) {
qWarning() << "Looping through input folder was interrupted!\n";
exit(-1);
}
}
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto GenerateBoard::generateBoard(QFile *pInput, QFile *pOutput,
const QString &sIN,
const QString &sOUT) -> bool {
if (!pInput->open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Couldn't open input file for board generation:"
<< pInput->fileName() << "\n";
return false;
}
qDebug() << "Input:\t" << pInput->fileName();
QTextStream in(pInput);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
// Since Qt 6 UTF-8 is used by default
in.setCodec("UTF-8");
#endif
QList<QString> listBoard;
QString sLine;
bool bOk(true);
quint16 nCountAllFields(0);
int nRows(-1);
int nColumns(0);
int nTempCol(0);
QList<uint> nPlayerStones;
while (!in.atEnd()) {
nRows++;
sLine = in.readLine().trimmed();
if (sLine.isEmpty()) {
break;
}
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
const QStringList sList(sLine.split(',', QString::SkipEmptyParts));
#else
const QStringList sList(sLine.split(',', Qt::SkipEmptyParts));
#endif
if (sList.isEmpty()) {
qWarning() << "Invalid line:" << nRows + 1;
return false;
}
// FIRST LINE -------------------------------------------------------------
// Contains number of stones for two/three/more player separated by ','
// e.g. 20,15 -> two players = 20 stones, three players = 15 stones
if (0 == nRows) {
for (auto const &sNum : sList) {
nPlayerStones.append(sNum.toUInt(&bOk));
if (!bOk) {
qWarning() << "ERROR: Invalid first line (stones players):" << sNum;
return false;
}
if (nPlayerStones.last() == 0) {
qWarning() << "ERROR: Invalid number of stones:" << sNum;
return false;
}
}
if (nPlayerStones.isEmpty()) {
qWarning() << "ERROR: Invalid first line (stones players):" << sLine;
return false;
}
continue;
}
// FURTHER LINES ----------------------------------------------------------
nTempCol = 0;
for (const auto &s : sList) {
nCountAllFields++;
nTempCol++;
listBoard << s.trimmed();
if (sIN != listBoard.last() && sOUT != listBoard.last()) {
qWarning() << "ERROR: Board files contains invalid data (neither " +
sIN + " nor " + sOUT + ") in line"
<< nRows + 1 << "at field:" << nTempCol;
return false;
}
}
if (1 == nRows) {
nColumns = nTempCol;
}
// Check if all columns have the same length
if (nColumns != nTempCol) {
qWarning() << "ERROR: Line" << nRows + 1
<< "has invalid length. "
"Expected length:"
<< nColumns;
return false;
}
}
pInput->close();
// CHECKS -------------------------------------------------------------------
if (nRows * nColumns != nCountAllFields) {
qWarning() << "ERROR: Invalid number of ALL fields:\n"
"Rows x cols ="
<< nRows * nColumns
<< "\nAll counted fields =" << nCountAllFields;
return false;
}
// WRITE BOARD --------------------------------------------------------------
if (!pOutput->open(QIODevice::WriteOnly)) {
qWarning() << "ERROR: Couldn't open/write board file:"
<< pOutput->fileName();
return false;
}
qDebug() << "Output:\t" << pOutput->fileName();
// Convert board to json array
QJsonArray jsonArray;
const QList<QString> tmpBoard(listBoard);
for (const auto &s : tmpBoard) {
jsonArray << s;
}
QJsonArray stonesArray;
for (auto nNum : nPlayerStones) {
stonesArray << static_cast<int>(nNum);
}
QJsonObject jsonObj;
jsonObj[QStringLiteral("Board")] = jsonArray;
jsonObj[QStringLiteral("Rows")] = nRows;
jsonObj[QStringLiteral("Columns")] = nColumns;
jsonObj[QStringLiteral("PlayersStones")] = stonesArray;
QJsonDocument jsDoc(jsonObj);
if (-1 == pOutput->write(jsDoc.toJson())) {
qWarning() << "ERROR: Error while writing output file!";
return false;
}
pOutput->close();
// INFO ---------------------------------------------------------------------
QString sStones(QLatin1String(""));
uint nPlayers = 1;
for (auto nNum : nPlayerStones) {
nPlayers++;
sStones += " " + QString::number(nPlayers) + ":" + QString::number(nNum);
}
qDebug() << "\t Stones players:" << sStones;
qDebug() << "\t Size:" << nRows << "x" << nColumns;
qDebug() << "\t Number of all fields:" << nCountAllFields;
qDebug() << "";
return true;
}