-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayer.cpp
128 lines (104 loc) · 4.62 KB
/
player.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
/**
* \file player.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
* Player.
*/
#include "./player.h"
#include <QDebug>
#include <QFileInfo>
#include <QMessageBox>
#include "./opponentjs.h"
Player::Player(QWidget *pParent, const quint8 nID, const quint8 nMaxStones,
const QString &sCpuScript, QObject *pParentObj)
: m_pParent(pParent),
m_nID(nID),
m_pJsCpu(nullptr),
m_sName(tr("Player") + " " + QString::number(m_nID)),
m_sCpuScript(sCpuScript),
m_nMaxStones(nMaxStones),
m_nStonesLeft(nMaxStones),
m_nWonTowers(0) {
Q_UNUSED(pParentObj)
if (!m_sCpuScript.isEmpty()) {
QFileInfo fi(m_sCpuScript);
m_sName += " (" + fi.baseName() + ")";
}
qDebug() << "Generating " + m_sName;
}
Player::~Player() = default;
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Player::initCPU(const QJsonArray &emptyBoard, const QPoint BoardDimensions,
const quint8 nMaxTowerHeight, const quint8 nNumOfPlayers,
const QString &sOut, const QString &sPad) -> bool {
m_pJsCpu = new OpponentJS(m_pParent, m_nID, BoardDimensions, nMaxTowerHeight,
nNumOfPlayers, sOut, sPad);
connect(m_pJsCpu, &OpponentJS::actionCPU, this, &Player::actionCPU);
connect(m_pJsCpu, &OpponentJS::scriptError, this, &Player::scriptError);
return m_pJsCpu->loadAndEvalCpuScript(m_sCpuScript, emptyBoard);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Player::callCpu(const QJsonArray &board, const QJsonDocument &legalMoves,
const QJsonArray &towersNeededToWin,
const QJsonArray &stonesLeft, const QJsonArray &lastMove) {
if (nullptr == m_pJsCpu) {
qWarning() << "callCPU called for Human player P" + this->getID();
QMessageBox::warning(m_pParent, tr("Warning"), tr("Something went wrong!"));
return;
}
m_pJsCpu->callJsCpu(board, legalMoves, towersNeededToWin, stonesLeft,
lastMove);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Player::isHuman() const -> bool { return m_sCpuScript.isEmpty(); }
auto Player::getName() const -> QString { return m_sName; }
auto Player::getCpuScript() const -> QString { return m_sCpuScript; }
auto Player::getID() const -> QString { return QString::number(m_nID); }
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Player::setLegalMoves(const QJsonDocument &legalMoves) {
m_LegalMoves = legalMoves;
}
auto Player::getLegalMoves() const -> QJsonDocument { return m_LegalMoves; }
auto Player::canMove() const -> bool { return !m_LegalMoves.isEmpty(); }
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Player::setStonesLeft(const quint8 nStones) {
if (nStones <= m_nMaxStones) {
m_nStonesLeft = nStones;
} else {
m_nStonesLeft = m_nMaxStones;
qWarning() << "Stones > MaxStones!" << nStones << ">" << m_nMaxStones;
QMessageBox::warning(m_pParent, QStringLiteral("Warning"),
QStringLiteral("Something went wrong!"));
}
}
auto Player::getStonesLeft() const -> quint8 { return m_nStonesLeft; }
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Player::setWonTowers(const quint8 nWonTowers) {
m_nWonTowers = nWonTowers;
}
auto Player::getWonTowers() const -> quint8 { return m_nWonTowers; }