Commit 4235e1aa authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Port to QRandomGenerator

parent b64713cc
......@@ -8,6 +8,7 @@
*/
#include "ai.h"
#include <QRandomGenerator>
AI::AI(Sea::Player player, Sea* sea, const BattleShipsConfiguration* config)
: m_player(player)
......@@ -35,13 +36,14 @@ void AI::setShips()
// TODO: Another placing algorithm, create a list of available places and choose randomly from them.
// number of repetitions because the random place is over a previous ship = 0
bool canFinish = true;
auto *generator = QRandomGenerator::global();
do {
for (int size = m_config->longestShip(); size >= 1; size--) {
for (unsigned int j = 1; j <= m_config->numberOfShipsOfSize(size); j++) {
Ship* ship = 0;
while (ship == 0 && canFinish) {
Coord c(qrand() % m_sea->size().x, qrand() % m_sea->size().y);
Ship::Direction dir = qrand() % 2 == 0 ? Ship::LEFT_TO_RIGHT : Ship::TOP_DOWN;
Coord c(generator->bounded(m_sea->size().x), generator->bounded(m_sea->size().y));
Ship::Direction dir = generator->bounded(2) == 0 ? Ship::LEFT_TO_RIGHT : Ship::TOP_DOWN;
if (m_sea->canAddShip(m_player, c, size, dir)) {
ship = new Ship(size, dir, c);
m_sea->add(m_player, ship);
......
......@@ -10,19 +10,20 @@
#include "dummyai.h"
#include "sea.h"
#include <time.h>
#include <QRandomGenerator>
DummyAI::DummyAI(Sea::Player player, Sea* sea, const BattleShipsConfiguration* config)
: AI(player, sea, config)
{
qsrand(time(0));
}
Coord DummyAI::getMove()
{
auto *generator = QRandomGenerator::global();
if (m_sea->turn() == m_player &&
m_sea->status() == Sea::PLAYING) {
for (int i = 0; i < 10000; i++) {
Coord c(qrand() % m_sea->size().x, qrand() % m_sea->size().y);
Coord c(generator->bounded(m_sea->size().x), generator->bounded(m_sea->size().y));
if (m_sea->canHit(m_player, c)) {
return c;
}
......
......@@ -11,6 +11,7 @@
#include <algorithm>
#include <time.h>
#include <QRandomGenerator>
class Strategy
{
......@@ -147,8 +148,9 @@ public:
Coord getMove() override
{
auto *generator = QRandomGenerator::global();
for (int i = 0; i < 10000; i++) {
Coord c(qrand() % m_sea->size().x, qrand() % m_sea->size().y);
Coord c(generator->bounded(m_sea->size().x), generator->bounded(m_sea->size().y));
if (m_sea->canHit(m_player, c)) {
return c;
}
......@@ -190,7 +192,7 @@ class DiagonalStrategy : public Strategy
Coord getMoveHelper()
{
int index = qrand() % m_range;
int index = QRandomGenerator::global()->bounded(m_range);
int current = 0;
for (int y = m_offset; y < m_sea->size().y; y += m_gap) {
int diag = m_sea->size().y - y;
......@@ -222,8 +224,9 @@ class DiagonalStrategy : public Strategy
void setup()
{
auto *generator = QRandomGenerator::global();
do {
m_offset = qrand() % m_gap;
m_offset = generator->bounded(m_gap);
qDebug() << "offset =" << m_offset << " / " << m_gap;
} while (!movesAvailable());
......@@ -286,7 +289,6 @@ SmartAI::SmartAI(Sea::Player player, Sea* sea, bool random, const BattleShipsCon
: AI(player, sea, config)
, m_state(random, config)
{
qsrand(time(0));
m_strategy = std::unique_ptr<Strategy>(m_state.defaultStrategy(player, sea));
}
......
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