Commit b9b3edfe authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Modernize code

parent 9af456a0
Pipeline #110813 failed with stage
in 1 minute and 47 seconds
......@@ -32,8 +32,7 @@
/** namespace for AI stuff */
namespace ai {
Ai::~Ai() {
}
Ai::~Ai() = default;
} /* namespace ai */
......
......@@ -39,8 +39,7 @@ AiFactory::AiFactory() {
m_ai = 0;
}
AiFactory::~AiFactory() {
}
AiFactory::~AiFactory() = default;
Ai* AiFactory::createAi(const Dimension& dimension, KgDifficultyLevel::StandardLevel skill,
Player player, DemoMode demoMode) const {
......@@ -53,7 +52,7 @@ Ai* AiFactory::createAi(const Dimension& dimension, KgDifficultyLevel::StandardL
return new AiAron(dimension, skill, player);
} else {
qFatal("Invalid AI!");
return 0;
return nullptr;
}
}
}
......
......@@ -27,10 +27,10 @@
#include <ctime>
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
#include "aisquare.h"
#include "coord.h"
......@@ -167,12 +167,12 @@ Coord AiBoard::evaluate() const {
std::vector<std::pair<uli, Coord> > v, v2, v3;
for (int x = 0; x < m_dimension->width(); ++x) {
for (int y = 0; y < m_dimension->height(); ++y) {
v.push_back(make_pair(points(Coord(x, y)), Coord(x, y)));
v.emplace_back(points(Coord(x, y)), Coord(x, y));
}
}
sort(v.begin(), v.end(), cmp);
uli max = v.begin()->first;
for (vector<pair<uli, Coord> >::const_iterator it = v.begin();
for (auto it = v.begin();
it != v.end(); ++it) {
bool doBreak = false;
switch (m_skill) {
......@@ -228,9 +228,9 @@ Coord AiBoard::evaluate() const {
} else if (v2.size() == 1) {
return v2.begin()->second;
}
for (vector<pair<uli, Coord> >::const_iterator it = v2.begin();
for (auto it = v2.begin();
it != v2.end(); ++it) {
v3.push_back(make_pair(value2(it->second), it->second));
v3.emplace_back(value2(it->second), it->second);
}
sort(v3.begin(), v3.end(), cmp);
if (v3.size() > 1) {
......
......@@ -23,11 +23,11 @@
#include "ai_impl.h"
#include "node.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <memory.h>
#include <assert.h>
#include <ctime>
#include <stdio.h>
#include <memory.h>
// hash table
static NodeHashData hashData[nodeHashSize];
......@@ -40,7 +40,7 @@ bool rand_inited = false;
AiImpl::AiImpl() : table_size_x(20), table_size_y(20),
start_depth(6), max_depth(6), depth_increment(2),
force_thinking(false), heur_seed(normal_heur_seed), print_info(true), max_branch(100),
timeOver(NULL), rememberedStanding(table_size_x, table_size_y)
timeOver(nullptr), rememberedStanding(table_size_x, table_size_y)
{
if (!rand_inited) {
rand_inited = true;
......@@ -49,8 +49,7 @@ AiImpl::AiImpl() : table_size_x(20), table_size_y(20),
memset(hashData, 0, sizeof(hashData));
}
AiImpl::~AiImpl() {
}
AiImpl::~AiImpl() = default;
void AiImpl::newGame() {
Standing::initRefresh();
......@@ -111,10 +110,10 @@ Field AiImpl::think() {
suggestedX = root->steps.front()->lastx;
suggestedY = root->steps.front()->lasty;
rootValue = 0;
act = NULL;
act = nullptr;
}
while (act && (timeOver == NULL || !timeOver->isTimeOver())) {
while (act && (timeOver == nullptr || !timeOver->isTimeOver())) {
// if this is a parent whose child has just been evaluated
if (act->child) {
if (act->signum > 0) {
......@@ -152,7 +151,7 @@ Field AiImpl::think() {
}
delete act->child;
act->child = NULL;
act->child = nullptr;
}
// if this parent has no more children to process
......@@ -241,10 +240,10 @@ Field AiImpl::think() {
} else {
depth_limit += depth_increment;
}
} while (depth_limit <= max_depth && (timeOver == NULL || !timeOver->isTimeOver()));
} while (depth_limit <= max_depth && (timeOver == nullptr || !timeOver->isTimeOver()));
assert((timeOver == NULL || !timeOver->isTimeOver()) || !rememberedStanding.table[bestX][bestY]);
return Field(bestX, bestY);
assert((timeOver == nullptr || !timeOver->isTimeOver()) || !rememberedStanding.table[bestX][bestY]);
return {bestX, bestY};
}
Field AiImpl::openingBook() {
......@@ -255,7 +254,7 @@ Field AiImpl::openingBook() {
x += qrand() % 5 - 2;
y += qrand() % 5 - 2;
while (rememberedStanding.table[x][y]) x++;
return Field(x, y);
return {x, y};
} else if (rememberedStanding.stepCount == 1) {
pos_T x, y;
x = rememberedStanding.lastx;
......@@ -275,14 +274,14 @@ Field AiImpl::openingBook() {
y--;
}
}
return Field(x, y);
return {x, y};
} else if (rememberedStanding.stepCount == 2) {
pos_T x1, y1, x2, y2;
int dx, dy;
x1 = previousStandings.last().lastx;
y1 = previousStandings.last().lasty;
if (!(1 <= x1 && x1 < table_size_x - 1 && 1 <= y1 && y1 < table_size_y - 1)) {
return Field(max_table_size, max_table_size);
return {max_table_size, max_table_size};
}
x2 = rememberedStanding.lastx;
y2 = rememberedStanding.lasty;
......@@ -290,25 +289,25 @@ Field AiImpl::openingBook() {
dy = (int) y1 - (int) y2;
if (-1 <= dx && dx <= 1 && -1 <= dy && dy <= 1) {
if (dx == 0) {
return Field((int) x1 + (qrand() % 2) * 2 - 1, (int) y1 + qrand() % 3 - 1);
return {static_cast<pos_T>((int) x1 + (qrand() % 2) * 2 - 1), static_cast<pos_T>((int) y1 + qrand() % 3 - 1)};
}
if (dy == 0) {
return Field((int) x1 + qrand() % 3 - 1, (int) y1 + (qrand() % 2) * 2 - 1);
return {static_cast<pos_T>((int) x1 + qrand() % 3 - 1), static_cast<pos_T>((int) y1 + (qrand() % 2) * 2 - 1)};
}
if (qrand() % 2) {
if (qrand() % 2) {
return Field((int) x1 + dx, y1);
return {static_cast<pos_T>((int) x1 + dx), y1};
} else {
return Field(x1, (int) y1 + dy);
return {x1, static_cast<pos_T>((int) y1 + dy)};
}
} else {
if (qrand() % 2) {
return Field((int) x1 - dx, (int) y1 + dy);
return {static_cast<pos_T>((int) x1 - dx), static_cast<pos_T>((int) y1 + dy)};
} else {
return Field((int) x1 + dx, (int) y1 - dy);
return {static_cast<pos_T>((int) x1 + dx), static_cast<pos_T>((int) y1 - dy)};
}
}
}
}
return Field(max_table_size, max_table_size);
return {max_table_size, max_table_size};
}
......@@ -28,7 +28,7 @@
#include "ai_interface.h"
#include "standing.h"
typedef QLinkedList<Standing> previous_standings_T;
using previous_standings_T = QLinkedList<Standing>;
class AiImpl {
public:
......
......@@ -23,7 +23,7 @@
#include "ai_interface.h"
#include "ai_impl.h"
#include <assert.h>
#include <cassert>
AiInterface::AiInterface() {
aiImpl = new AiImpl();
......
......@@ -24,7 +24,7 @@
#define BOVO_AI_INTERFACE_H
// a coordinate of the table
typedef unsigned char pos_T;
using pos_T = unsigned char;
// memory allocated for a standing
const pos_T max_table_size = 22;
......@@ -41,7 +41,7 @@ class AiImpl;
class AiTimeOver {
public:
virtual ~AiTimeOver() {}
virtual ~AiTimeOver() = default;
virtual bool isTimeOver() = 0;
};
......
......@@ -23,11 +23,11 @@
#include "node.h"
#include "ai_impl.h"
#include <assert.h>
#include <cassert>
Node::Node(Standing* _standing, AiImpl* ai):
standing(_standing),
parent(NULL), child(NULL), depth(0),
parent(nullptr), child(nullptr), depth(0),
signum(_standing->current == 0 ? 1 : -1), alpha(MinHeur - 1), beta(MaxHeur + 1), is_exact(false),
evaluated(false), depth_limit(ai->depth_limit), max_branch(ai->max_branch)
{
......@@ -36,7 +36,7 @@ Node::Node(Standing* _standing, AiImpl* ai):
Node::Node(Standing* _standing, Node* _parent):
standing(_standing),
parent(_parent), child(NULL), depth(_parent->depth + 1),
parent(_parent), child(nullptr), depth(_parent->depth + 1),
signum(-(_parent->signum)), alpha(_parent->alpha), beta(_parent->beta), is_exact(false),
evaluated(false), depth_limit(_parent->depth_limit), max_branch(_parent->max_branch)
{
......@@ -69,7 +69,7 @@ void Node::generateSteps() {
pos_T x = s->x;
pos_T y = s->y;
assert(!standing->table[x][y]);
Standing* p = new Standing(*this->standing);
auto p = new Standing(*this->standing);
p->step(x, y);
heur_T pv = p->hval * signum;
......
......@@ -29,12 +29,12 @@
#include "standing.h"
// list of following steps
typedef QLinkedList<Standing*> steps_T;
using steps_T = QLinkedList<Standing *>;
// type of hash value
typedef unsigned long long hash_T;
using hash_T = unsigned long long;
// hash table entry type
typedef index_T entry_type_T;
using entry_type_T = index_T;
enum { exact = 1, lower_bound = 2, upper_bound = 3 };
//pragmas are not in the C++ standard and Sun Studio does not support the pack(x, y) one
......
......@@ -21,12 +21,12 @@
#include "standing.h"
#include <QString>
#include <cassert>
#include <cstdlib>
#include <memory.h>
#include <assert.h>
#include <QString>
typedef QString string;
using string = QString;
// these are used by refresh() and getSuggestions()
const static PatternCount suggestValues = { {0, 8, 8, 6, 5, 3}, {0, 7, 7, 4, 2, 1} };
......@@ -284,7 +284,7 @@ void Standing::refresh(sample_T& sample_vect, PatternCount& local, int inv, posf
PatternCount newCount;
memset(newCount, 0, sizeof(newCount));
pos_T sample_size = (pos_T) sample_vect.size();
auto sample_size = (pos_T) sample_vect.size();
mark_T sample[2 * max_table_size - 1];
for (pos_T i = 0; i < sample_size; ++i) {
sample[i] = sample_vect[i];
......
......@@ -29,11 +29,11 @@
#include "ai_interface.h"
// a mark on the table
typedef unsigned char mark_T;
using mark_T = unsigned char;
// occurrences of a certain position
typedef unsigned char count_T;
using count_T = unsigned char;
// small index for a constant array
typedef unsigned char index_T;
using index_T = unsigned char;
// table symbols indexed with player number
const mark_T mark[] = {'o', 'x', '!', '\0'};
......@@ -44,10 +44,10 @@ typedef mark_T table_T[max_table_size][max_table_size];
typedef count_T suggest_T[2][max_table_size][max_table_size];
// heuristic type
typedef signed short heur_T;
using heur_T = short;
// type for total number of marks on the table
typedef unsigned short stepCount_T;
using stepCount_T = unsigned short;
// maximal and minimal value of the heuristic function
const heur_T MaxHeur = 10000;
......@@ -67,13 +67,13 @@ const index_T heurLevels = 6;
typedef count_T PatternCount[2][heurLevels];
// a row, column or diagonal of the table
typedef QVector<mark_T> sample_T;
using sample_T = QVector<mark_T>;
// interesting fields for the two players for the next step
typedef QLinkedList<Field> suggestions_T;
using suggestions_T = QLinkedList<Field>;
class Standing;
// callback function to convert a position of the sample into coordinates, and update suggestions accordingly
typedef void (Standing::*posf_T)(pos_T pos, int inv, count_T value0, count_T value1);
using posf_T = void (Standing::*)(pos_T, int, count_T, count_T);
// game state class
class Standing {
......
......@@ -31,10 +31,10 @@
namespace bovo {
/* a very short positive natural number, such as a X or Y coordinate */
typedef unsigned short int usi;
using usi = unsigned short;
/* a very long positive natural number, such as score for a certain square */
typedef unsigned long int uli;
using uli = unsigned long;
/**
* @brief Exception for a busy square
......
......@@ -29,8 +29,7 @@ Dimension::Dimension(usi width, usi height)
}
Dimension::Dimension(const Dimension& dimension)
: m_height(dimension.m_height), m_width(dimension.m_width) {
}
= default;
usi Dimension::height() const {
return m_height;
......
......@@ -165,7 +165,7 @@ QStringList Game::saveLast() const {
}
void Game::setSkill(KgDifficultyLevel::StandardLevel skill) {
if (m_ai!=0)
if (m_ai!=nullptr)
m_ai->setSkill(skill);
}
......@@ -386,7 +386,7 @@ short Game::win(const Coord& c) const {
QList<Move> Game::winningMoves() const {
if (m_winDir == -1) {
return QList<Move>();
return {};
}
QList<Move> moves;
short dy, dx;
......
......@@ -38,11 +38,9 @@ Move::Move(Player player, const Coord& coord)
}
Move::Move(const Move &m)
: m_coord(m.m_coord), m_player(m.m_player) {
}
= default;
Move::~Move() {
}
Move::~Move() = default;
Coord Move::coord() const {
return m_coord;
......
......@@ -42,7 +42,7 @@ HintItem::HintItem(Scene* scene, const Move& hint, bool animate, qreal fill)
.arg(QString::number(qrand() % 5 + 1)));
m_tick = 16;
m_tickUp = true;
m_ticker = 0;
m_ticker = nullptr;
if (animate) {
m_ticker = new QTimer(this);
m_opacity = 0.0;
......@@ -57,7 +57,7 @@ HintItem::HintItem(Scene* scene, const Move& hint, bool animate, qreal fill)
HintItem::~HintItem() {
if (m_ticker) {
disconnect(m_ticker, 0, this, 0);
disconnect(m_ticker, nullptr, this, nullptr);
m_ticker->stop();
m_ticker->deleteLater();
}
......@@ -67,17 +67,17 @@ QRectF HintItem::boundingRect() const {
qreal width = m_scene->squareSize();
qreal height = width;
qreal margin = (1.0-m_fill) * width / 2.0;
return QRectF( -width / 2.0 + margin,
return { -width / 2.0 + margin,
-height / 2.0 + margin,
width - 2.0*margin,
height - 2.0*margin);
height - 2.0*margin};
}
void HintItem::killAnimation() {
if (m_ticker) {
m_ticker->stop();
disconnect(m_ticker, 0, this, 0);
disconnect(m_ticker, nullptr, this, nullptr);
m_opacity = 0.4;
update();
}
......
......@@ -55,7 +55,7 @@ Q_SIGNALS:
void killed();
protected:
void paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget* widget=0) Q_DECL_OVERRIDE;
void paint(QPainter* p, const QStyleOptionGraphicsItem*, QWidget* widget=nullptr) Q_DECL_OVERRIDE;
private:
Scene* m_scene;
......
......@@ -65,7 +65,7 @@ int main(int argc, char **argv) {
if( app.isSessionRestored() ) {
kRestoreMainWindows<MainWindow>();
} else {
MainWindow *mainWin = new MainWindow();
auto mainWin = new MainWindow();
mainWin->show();
}
......
......@@ -61,9 +61,9 @@ using namespace ai;
namespace gui {
MainWindow::MainWindow(QWidget* parent)
: KXmlGuiWindow(parent), m_scene(0), m_game(0), m_wins(0),
m_losses(0), m_computerStarts(false), m_demoAi(0),
m_aiFactory(0), m_animate(true),
: KXmlGuiWindow(parent), m_scene(nullptr), m_game(nullptr), m_wins(0),
m_losses(0), m_computerStarts(false), m_demoAi(nullptr),
m_aiFactory(nullptr), m_animate(true),
m_winsLabel (new QLabel(i18n("Wins: %1", m_wins))),
m_lossesLabel (new QLabel(i18n("Losses: %1", m_losses))) {
statusBar()->insertPermanentWidget(0, m_winsLabel);
......@@ -106,7 +106,7 @@ MainWindow::~MainWindow() {
}
void MainWindow::save() const {
if (m_game != 0) {
if (m_game != nullptr) {
m_scene->activate(false);
QString rc = QStandardPaths::locate(QStandardPaths::ConfigLocation, QStringLiteral("bovorc"));
KConfig savegame(rc);
......@@ -186,7 +186,7 @@ void MainWindow::setupActions() {
KStandardGameAction::gameNew(this, SLOT(slotNewGame()), actionCollection());
KStandardGameAction::quit(this, SLOT(close()), actionCollection());
QAction *replayAct = new QAction(QIcon::fromTheme( QStringLiteral( "media-playback-start" )),
auto replayAct = new QAction(QIcon::fromTheme( QStringLiteral( "media-playback-start" )),
i18n("&Replay"), this);
actionCollection()->addAction( QStringLiteral( "replay" ), replayAct);
replayAct->setToolTip(i18n("Replay game"));
......@@ -196,7 +196,7 @@ void MainWindow::setupActions() {
m_hintAct = KStandardGameAction::hint(this, SLOT(hint()), actionCollection());
m_hintAct->setEnabled(false);
KToggleAction *animAct = new KToggleAction(i18n("&Animation"),this);
auto animAct = new KToggleAction(i18n("&Animation"),this);
actionCollection()->addAction( QStringLiteral( "animation" ), animAct);
animAct->setChecked(m_animate);
connect(animAct, &QAction::toggled, this, &MainWindow::setAnimation);
......@@ -252,7 +252,7 @@ void MainWindow::slotNewGame() {
if (m_game != nullptr) {
m_game->cancelAndWait();
if (m_scene != nullptr) {
disconnect(m_game, nullptr, m_scene, 0);
disconnect(m_game, nullptr, m_scene, nullptr);
}
if (!m_game->isGameOver() && m_game->history().size() > 1) {
m_lossesLabel->setText(i18n("Losses: %1",++m_losses));
......
......@@ -55,7 +55,7 @@ class MainWindow : public KXmlGuiWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget* parent=0);
explicit MainWindow(QWidget* parent=nullptr);
~MainWindow() override;
public Q_SLOTS:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment