-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopponentjs.cpp
More file actions
180 lines (154 loc) · 6.64 KB
/
opponentjs.cpp
File metadata and controls
180 lines (154 loc) · 6.64 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
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
// SPDX-FileCopyrightText: 2017-2024 Thorsten Roth
// SPDX-License-Identifier: GPL-3.0-or-later
#include "./opponentjs.h"
#include <QDebug>
#include <QFile>
#include <QJSEngine>
#include <QMessageBox>
OpponentJS::OpponentJS(QWidget *pParent, const quint8 nID,
const QPoint BoardDimensions,
const quint8 nHeightTowerWin, const quint8 nNumOfPlayers,
const QString &sOut, const QString &sPad,
QObject *pParentObj)
: m_pParent(pParent),
m_nID(nID),
m_BoardDimensions(BoardDimensions),
m_nHeightTowerWin(nHeightTowerWin),
m_nNumOfPlayers(nNumOfPlayers),
m_sOut(sOut),
m_sPad(sPad),
m_jsEngine(new QJSEngine(pParentObj)) {
m_obj = m_jsEngine->globalObject();
m_obj.setProperty(QStringLiteral("game"), m_jsEngine->newQObject(this));
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto OpponentJS::loadAndEvalCpuScript(const QString &sFilepath,
const QJsonArray &emptyBoard) -> bool {
QFile f(sFilepath);
if (!f.open(QFile::ReadOnly)) {
qWarning() << "Couldn't open JS file:" << sFilepath;
return false;
}
QString source = QString::fromUtf8(f.readAll());
f.close();
this->log("Script: " + sFilepath);
QJSValue result(m_jsEngine->evaluate(source, sFilepath));
if (result.isError()) {
qCritical().noquote()
<< "Error in CPU P" + QString::number(m_nID) + "script at line " +
result.property(QStringLiteral("lineNumber")).toString() +
"\n " + result.toString();
emit scriptError();
return false;
}
// Check if callCPU() is available for calling the script
if (!m_obj.hasProperty(QStringLiteral("callCPU")) ||
!m_obj.property(QStringLiteral("callCPU")).isCallable()) {
qCritical() << "Error in CPU P" + QString::number(m_nID) +
"script - function callCPU() not found or not callable!";
emit scriptError();
return false;
}
// Call (optional) init() function if available
if (m_obj.hasProperty(QStringLiteral("initCPU")) &&
m_obj.property(QStringLiteral("initCPU")).isCallable()) {
QJsonDocument jsdoc(emptyBoard);
QString sJsonEmptyBoard(
QString::fromLatin1(jsdoc.toJson(QJsonDocument::Compact)));
QJSValueList arguments;
arguments << sJsonEmptyBoard;
result = m_obj.property(QStringLiteral("initCPU")).call(arguments);
if (result.isError()) {
qCritical().noquote()
<< "CPU P" + QString::number(m_nID) +
"- Error calling \"initCPU\" function at line: " +
result.property(QStringLiteral("lineNumber")).toString() +
"\n " + result.toString();
QMessageBox::warning(m_pParent, tr("Warning"),
tr("CPU script execution error! "
"Please check the debug log."));
emit scriptError();
return false;
}
}
return true;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void OpponentJS::callJsCpu(const QJsonArray &board,
const QJsonDocument &legalMoves,
const QJsonArray &towersNeededToWin,
const QJsonArray &stonesLeft,
const QJsonArray &lastMove) {
QJsonDocument jsdoc(board);
QString sJsonBoard(QString::fromLatin1(jsdoc.toJson(QJsonDocument::Compact)));
QJSValueList arguments;
arguments << sJsonBoard;
m_legalMoves = legalMoves;
m_TowersNeededToWin = towersNeededToWin;
m_StonesLeft = stonesLeft;
m_LastMove = lastMove;
QJSValue result = m_obj.property(QStringLiteral("callCPU")).call(arguments);
// qDebug() << "Result of callCPU(): " + result.toString();
if (result.isError()) {
qCritical().noquote()
<< "CPU P" + QString::number(m_nID) +
"- Error calling \"callCPU\" function at line: " +
result.property(QStringLiteral("lineNumber")).toString() +
"\n " + result.toString();
QMessageBox::warning(m_pParent, tr("Warning"),
tr("CPU script execution error! "
"Please check the debug log."));
emit scriptError();
return;
}
// CPU has to return an int array with length 3
QJsonArray move;
if (result.isArray()) {
if (3 == result.property(QStringLiteral("length")).toInt()) {
if (result.property(0).isNumber() && // From (-1 --> set stone at "To")
result.property(1).isNumber() && // Number of stones
result.property(2).isNumber()) { // To
move << result.property(0).toInt() << result.property(1).toInt()
<< result.property(2).toInt();
if (move.at(0).toInt() >= -1 && move.at(0).toInt() < board.size() &&
move.at(1).toInt() > 0 && move.at(2).toInt() >= 0 &&
move.at(2).toInt() < board.size()) {
emit actionCPU(move);
return;
}
}
}
}
qCritical() << "CPU P" + QString::number(m_nID) +
"script invalid return from callCPU(): " +
result.toString();
QMessageBox::warning(m_pParent, tr("Warning"),
tr("CPU script execution error! "
"Please check the debug log."));
emit scriptError();
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Functions accessible by external JavaScript
auto OpponentJS::getID() -> quint8 { return m_nID; }
auto OpponentJS::getNumOfPlayers() -> quint8 { return m_nNumOfPlayers; }
auto OpponentJS::getHeightToWin() -> quint8 { return m_nHeightTowerWin; }
auto OpponentJS::getTowersNeededToWin() -> QJsonArray {
return m_TowersNeededToWin;
}
auto OpponentJS::getNumberOfStones() -> QJsonArray { return m_StonesLeft; }
auto OpponentJS::getLastMove() -> QJsonArray { return m_LastMove; }
auto OpponentJS::getLegalMoves() -> QString {
QString sJsonMoves(
QString::fromLatin1(m_legalMoves.toJson(QJsonDocument::Compact)));
return sJsonMoves;
}
auto OpponentJS::getBoardDimensionX() -> int { return m_BoardDimensions.x(); }
auto OpponentJS::getBoardDimensionY() -> int { return m_BoardDimensions.y(); }
auto OpponentJS::getOutside() -> QString { return m_sOut; }
auto OpponentJS::getPadding() -> QString { return m_sPad; }
void OpponentJS::log(const QString &sMsg) {
qDebug() << "CPU P" + QString::number(m_nID) + " - " + sMsg;
}