Updated for uniformity and random now working

This commit is contained in:
2021-07-03 02:39:50 -04:00
parent a0a89d510f
commit d579c1663b
3 changed files with 28 additions and 31 deletions

View File

@@ -1,11 +1,10 @@
//myClasses/Dice.hpp //myClasses/headers/mee/Dice.hpp
//Matthew Ellison //Matthew Ellison
// Created: 1-26-19 // Created: 01-26-19
//Modified: 1-26-19 //Modified: 07-02-21
//This is a simple class to simulate a dice for games //This is a simple class to simulate a dice for games
///This file has to be modified slightly to work with windows because the random_device does not work correctly
/* /*
Copyright (C) 2018 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -20,19 +19,11 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef MEE_DICE_HPP
#ifndef DICE_HPP #define MEE_DICE_HPP
#define DICE_HPP
#include <random> #include <random>
#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
#include <ctime>
#endif //ifndef linux
namespace mee{ namespace mee{
@@ -41,25 +32,28 @@ template<class T>
class Dice{ class Dice{
private: private:
T face; //Holds the currently rolled number T face; //Holds the currently rolled number
T sides; //Holds the number of sides the dice has 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::default_random_engine generator; //The number generator that all the numbers come from
std::uniform_int_distribution<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: public:
#ifdef __linux Dice(T sides = 6) : face(1), sides(sides), generator(std::random_device{}()), dist(1, sides){
Dice(T sides = 6) : face(1), sides(sides), generator(std::random_device{}()), dist(1, sides) { } }
#else
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 //Setup ways to get information from the class
T getFace() const { return face; } T getFace() const{
T getSides() const { return sides; } return face;
}
T getSides() const{
return sides;
}
//Used to simulate rolling the dice. Returns the new number //Used to simulate rolling the dice. Returns the new number
T roll() { T roll(){
face = dist(generator); face = dist(generator);
return face; return face;
} }
}; };
} //namespace mee } //namespace mee
#endif //DICE_HPP
#endif //MEE_DICE_HPP

View File

@@ -19,17 +19,18 @@
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef MEE_STOPWATCH_HPP
#define MEE_STOPWATCH_HPP
#ifndef STOPWATCH_HPP
#define STOPWATCH_HPP
#include <chrono> #include <chrono>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
namespace mee{ namespace mee{
class Stopwatch{ class Stopwatch{
public: public:
//Create an error class //Create an error class
@@ -202,6 +203,8 @@ std::ostream& operator<<(std::ostream& out, Stopwatch& timer){
bool num = timer.hasStopped; bool num = timer.hasStopped;
return out; return out;
} }
} //end namespace mee } //end namespace mee
#endif //end STOPWATCH_HPP #endif //end MEE_STOPWATCH_HPP

View File

@@ -25,7 +25,7 @@
#include <cinttypes> #include <cinttypes>
#include "Dice.hpp" #include "Dice.hpp"
const uint64_t LENGTH_OF_TEST = 100; //How many times the dice will get rolled * the number of sides const uint64_t LENGTH_OF_TEST = 100; //How many times the dice will get rolled * the number of sides
int main(){ int main(){