1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:47:45 +00:00

Snake: Use AK::get_random_uniform() instead of rand()

This commit is contained in:
Andreas Kling 2021-08-30 18:19:24 +02:00
parent 4c8fe01bff
commit d3d170851d

View file

@ -6,13 +6,12 @@
*/ */
#include "SnakeGame.h" #include "SnakeGame.h"
#include <AK/Random.h>
#include <LibConfig/Client.h> #include <LibConfig/Client.h>
#include <LibGUI/Painter.h> #include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h> #include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h> #include <LibGfx/FontDatabase.h>
#include <stdlib.h>
#include <time.h>
SnakeGame::SnakeGame() SnakeGame::SnakeGame()
{ {
@ -21,7 +20,6 @@ SnakeGame::SnakeGame()
m_fruit_bitmaps.append(*Gfx::Bitmap::try_load_from_file("/res/icons/snake/eggplant.png")); m_fruit_bitmaps.append(*Gfx::Bitmap::try_load_from_file("/res/icons/snake/eggplant.png"));
m_fruit_bitmaps.append(*Gfx::Bitmap::try_load_from_file("/res/icons/snake/cauliflower.png")); m_fruit_bitmaps.append(*Gfx::Bitmap::try_load_from_file("/res/icons/snake/cauliflower.png"));
m_fruit_bitmaps.append(*Gfx::Bitmap::try_load_from_file("/res/icons/snake/tomato.png")); m_fruit_bitmaps.append(*Gfx::Bitmap::try_load_from_file("/res/icons/snake/tomato.png"));
srand(time(nullptr));
reset(); reset();
m_high_score = Config::read_i32("Snake", "Snake", "HighScore", 0); m_high_score = Config::read_i32("Snake", "Snake", "HighScore", 0);
@ -63,13 +61,13 @@ void SnakeGame::spawn_fruit()
{ {
Coordinate coord; Coordinate coord;
for (;;) { for (;;) {
coord.row = rand() % m_rows; coord.row = get_random_uniform(m_rows);
coord.column = rand() % m_columns; coord.column = get_random_uniform(m_columns);
if (is_available(coord)) if (is_available(coord))
break; break;
} }
m_fruit = coord; m_fruit = coord;
m_fruit_type = rand() % m_fruit_bitmaps.size(); m_fruit_type = get_random_uniform(m_fruit_bitmaps.size());
} }
Gfx::IntRect SnakeGame::score_rect() const Gfx::IntRect SnakeGame::score_rect() const