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

Port to QRandomGenerator (compile without deprecated qt5.15 method)

parent 0d60ca60
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "diamond.h" #include "diamond.h"
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QRandomGenerator>
#include <KgDifficulty> #include <KgDifficulty>
const int KDiamond::Board::MoveDuration = 100; //duration of a move animation (per coordinate unit) in milliseconds const int KDiamond::Board::MoveDuration = 100; //duration of a move animation (per coordinate unit) in milliseconds
...@@ -45,7 +46,7 @@ KDiamond::Board::Board(KGameRenderer *renderer) ...@@ -45,7 +46,7 @@ KDiamond::Board::Board(KGameRenderer *renderer)
//roll the dice to get a color, but ensure that there are not three of a color in a row from the start //roll the dice to get a color, but ensure that there are not three of a color in a row from the start
int color; int color;
while (true) { while (true) {
color = qrand() % m_colorCount + 1; // +1 because numbering of enum KDiamond::Color starts at 1 color = QRandomGenerator::global()->bounded(1, m_colorCount + 1);
//condition: no triplet in y axis (attention: only the diamonds above us are defined already) //condition: no triplet in y axis (attention: only the diamonds above us are defined already)
if (point.y() >= 2) { //no triplet possible for i = 0, 1 if (point.y() >= 2) { //no triplet possible for i = 0, 1
const int otherColor1 = diamond(point + dispY1)->color(); const int otherColor1 = diamond(point + dispY1)->color();
...@@ -279,7 +280,8 @@ void KDiamond::Board::fillGaps() ...@@ -279,7 +280,8 @@ void KDiamond::Board::fillGaps()
continue; //inside of diamond stack - no gaps to fill continue; //inside of diamond stack - no gaps to fill
} }
--yt; --yt;
diamond = spawnDiamond(qrand() % m_colorCount + 1); const quint32 randValue = QRandomGenerator::global()->bounded(1, m_colorCount + 1); //high value is excluse
diamond = spawnDiamond(randValue);
diamond->setPos(QPoint(x, yt)); diamond->setPos(QPoint(x, yt));
const MoveAnimSpec spec = { diamond, QPoint(x, yt), QPoint(x, y) }; const MoveAnimSpec spec = { diamond, QPoint(x, yt), QPoint(x, y) };
specs << spec; specs << spec;
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <cmath> #include <cmath>
#include <QPainter> #include <QPainter>
#include <QRandomGenerator>
#include <QTimerEvent> #include <QTimerEvent>
#include <KGamePopupItem> #include <KGamePopupItem>
#include <KGameRenderer> #include <KGameRenderer>
...@@ -368,7 +369,7 @@ void Game::showHint() ...@@ -368,7 +369,7 @@ void Game::showHint()
if (m_availableMoves.isEmpty() || !m_board->selections().isEmpty()) { if (m_availableMoves.isEmpty() || !m_board->selections().isEmpty()) {
return; return;
} }
const QPoint location = m_availableMoves.value(qrand() % m_availableMoves.size()); const QPoint location = m_availableMoves.value( QRandomGenerator::global()->bounded(m_availableMoves.size()));
m_board->setSelection(location, true); m_board->setSelection(location, true);
m_gameState->removePoints(3); m_gameState->removePoints(3);
} }
......
...@@ -44,7 +44,6 @@ int main(int argc, char **argv) ...@@ -44,7 +44,6 @@ int main(int argc, char **argv)
migrate.setUiFiles(QStringList() << QStringLiteral("kdiamondui.rc")); migrate.setUiFiles(QStringList() << QStringLiteral("kdiamondui.rc"));
migrate.migrate(); migrate.migrate();
qsrand(time(nullptr));
KLocalizedString::setApplicationDomain("kdiamond"); KLocalizedString::setApplicationDomain("kdiamond");
KAboutData about(QStringLiteral("kdiamond"), i18nc("The application's name", "KDiamond"), QLatin1String(KDIAMOND_VERSION_STRING), i18n(description), KAboutData about(QStringLiteral("kdiamond"), i18nc("The application's name", "KDiamond"), QLatin1String(KDIAMOND_VERSION_STRING), i18n(description),
KAboutLicense::GPL, i18n("(C) 2008-2010 Stefan Majewsky and others"), QStringLiteral("https://kde.org/applications/games/org.kde.kdiamond")); KAboutLicense::GPL, i18n("(C) 2008-2010 Stefan Majewsky and others"), QStringLiteral("https://kde.org/applications/games/org.kde.kdiamond"));
......
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