mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Changed dice to a template class and updated ifdef statements
This commit is contained in:
21
Dice.hpp
21
Dice.hpp
@@ -29,7 +29,7 @@
|
||||
#include <cinttypes>
|
||||
//Use this for anything besides Linux. It replaces random_device
|
||||
//I know this doesn't work correctly with mingw on Windows, not sure about msbuild or mac so I don't take the chance
|
||||
#ifndef linux
|
||||
#ifndef __linux
|
||||
#include <ctime>
|
||||
#endif //ifndef linux
|
||||
|
||||
@@ -37,23 +37,24 @@
|
||||
|
||||
namespace mee{
|
||||
|
||||
template<class T>
|
||||
class Dice{
|
||||
private:
|
||||
uint64_t face; //Holds the currently rolled number
|
||||
uint64_t sides; //Holds the number of sides the dice has
|
||||
T face; //Holds the currently rolled number
|
||||
T sides; //Holds the number of sides the dice has
|
||||
std::default_random_engine generator; //The number generator that all the numbers come from
|
||||
std::uniform_int_distribution<uint64_t> dist; //A distribution to make sure the numbers come out relatively evenly
|
||||
std::uniform_int_distribution<T> dist; //A distribution to make sure the numbers come out relatively evenly
|
||||
public:
|
||||
#ifdef linux
|
||||
Dice(uint64_t sides = 6) : face(1), sides(sides), generator(std::random_device{}()), dist(1, sides) { }
|
||||
#ifdef __linux
|
||||
Dice(T sides = 6) : face(1), sides(sides), generator(std::random_device{}()), dist(1, sides) { }
|
||||
#else
|
||||
Dice(uint64_t sides = 6) : face(1), sides(sides), generator(time(0)), dist(1, sides) { }
|
||||
Dice(T sides = 6) : face(1), sides(sides), generator(time(0)), dist(1, sides) { }
|
||||
#endif //ifdef linux
|
||||
//Setup ways to get information from the class
|
||||
uint64_t getFace() const { return face; }
|
||||
uint64_t getSides() const { return sides; }
|
||||
T getFace() const { return face; }
|
||||
T getSides() const { return sides; }
|
||||
//Used to simulate rolling the dice. Returns the new number
|
||||
uint64_t roll() {
|
||||
T roll() {
|
||||
face = dist(generator);
|
||||
return face;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user