Commit b820a2df authored by Albert Astals Cid's avatar Albert Astals Cid

Port to QRandomGenerator

This sadly changes the output of a given "numbered game" so hopefully people don't have written down "nice mahjongg numbers"

Increase Qt dependency to 5.10 (the one that has QRandomGenerator) and KF5 one
to the first that depends on Qt 5.10
parent 67de0a0f
project(kmahjongg)
cmake_minimum_required (VERSION 3.5 FATAL_ERROR)
set(QT_MIN_VERSION "5.9.0")
set(KF5_MIN_VERSION "5.46.0")
set(QT_MIN_VERSION "5.10.0")
set(KF5_MIN_VERSION "5.55.0")
find_package(ECM ${KF5_MIN_VERSION} REQUIRED NO_MODULE)
......
......@@ -245,7 +245,7 @@ bool GameData::generateSolvableGame()
int position, cnt = 0;
do {
position = static_cast<int>(random.getLong(m_numTilesToGenerate));
position = random.bounded(m_numTilesToGenerate);
if (cnt++ > (m_numTilesToGenerate * m_numTilesToGenerate)) {
return false; // bail
......@@ -402,7 +402,7 @@ int GameData::selectPosition(int lastPosition)
while (!goodPosition) {
// Select a random, but free, position.
do {
position = random.getLong(m_numTilesToGenerate);
position = random.bounded(m_numTilesToGenerate);
if (cnt++ > (m_numTilesToGenerate * m_numTilesToGenerate)) {
return -1; // bail
......@@ -577,11 +577,11 @@ bool GameData::generateStartPosition2()
int p2;
if (remaining > 2) {
p2 = p1 = random.getLong(remaining - 2);
p2 = p1 = random.bounded(remaining - 2);
int bail = 0;
while (p1 == p2) {
p2 = random.getLong(remaining - 2);
p2 = random.bounded(remaining - 2);
if (bail >= 100) {
if (p1 != p2) {
......@@ -677,7 +677,7 @@ void GameData::randomiseFaces()
int to = at;
while (to == at) {
to = random.getLong(144);
to = random.bounded(144);
if ((to & 1) != 0) {
--to;
......@@ -899,8 +899,8 @@ bool GameData::findMove(POSITION & posA, POSITION & posB)
}
if (posCount >= 2) {
random.setSeed(0); // WABA: Why is the seed reset?
short pos = random.getLong(posCount) & -2; // Even value
random.seed(QRandomGenerator::global()->generate()); // WABA: Why is the seed reset?
short pos = random.bounded(posCount) & -2; // Even value
posA = m_posTable[pos];
posB = m_posTable[pos + 1];
......@@ -1077,8 +1077,8 @@ void GameData::shuffle()
// now lets randomise the faces, selecting 400 pairs at random and
// swapping the faces.
for (int ran = 0; ran < 400; ++ran) {
int pos1 = random.getLong(count);
int pos2 = random.getLong(count);
int pos1 = random.bounded(count);
int pos2 = random.bounded(count);
if (pos1 == pos2) {
continue;
......
......@@ -21,11 +21,9 @@
#define GAMEDATA_H
// Qt
#include <QRandomGenerator>
#include <QVector>
// KDE
#include <KRandomSequence>
// KMahjongg
#include "kmtypes.h"
......@@ -88,7 +86,7 @@ public:
short m_depth;
short m_maxTiles;
KRandomSequence random;
QRandomGenerator random;
private:
void putTile(POSITION & pos)
......
......@@ -17,11 +17,11 @@
// Qt
#include <QMouseEvent>
#include <QRandomGenerator>
#include <QResizeEvent>
// KDE
#include <KLocalizedString>
#include <KRandom>
// KMahjongg
#include "demoanimation.h"
......@@ -218,14 +218,14 @@ void GameView::createNewGame(long gameNumber)
// Create a random game number, if no one was given.
if (gameNumber == -1) {
m_gameNumber = KRandom::random();
m_gameNumber = QRandomGenerator::global()->generate();
} else {
m_gameNumber = gameNumber;
}
m_gameData->m_allowUndo = 0;
m_gameData->m_allowRedo = 0;
m_gameData->random.setSeed(m_gameNumber);
m_gameData->random.seed(m_gameNumber);
// Translate m_pGameData->Map to an array of POSITION data. We only need to
// do this once for each new game.
......
......@@ -406,7 +406,7 @@ private:
int yCounter, int z, int & zCount);
unsigned short m_cheatsUsed;
long m_gameNumber;
quint32 m_gameNumber;
bool m_gamePaused;
bool m_match;
bool m_gameGenerated;
......
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