From 45f6d69507b2668967a5dd138a686d38de6cbf51 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 12 Feb 2019 00:02:47 -0500 Subject: [PATCH] Changed dice to a template class and updated ifdef statements --- Dice.hpp | 21 +++++++++++---------- testDice.cpp | 5 +++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Dice.hpp b/Dice.hpp index e1f29e8..999d687 100644 --- a/Dice.hpp +++ b/Dice.hpp @@ -29,7 +29,7 @@ #include //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 #endif //ifndef linux @@ -37,23 +37,24 @@ namespace mee{ +template 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 dist; //A distribution to make sure the numbers come out relatively evenly + std::uniform_int_distribution 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; } diff --git a/testDice.cpp b/testDice.cpp index 15fd2d2..1bf794e 100644 --- a/testDice.cpp +++ b/testDice.cpp @@ -22,6 +22,7 @@ #include #include +#include #include "Dice.hpp" const int LENGTH_OF_TEST = 100; //How many times the dice will get rolled * the number of sides @@ -30,7 +31,7 @@ const int LENGTH_OF_TEST = 100; //How many times the dice will get rolled * the int main(){ //Check the default constructor std::cout << "Checking the default constructor of the Dice class:\n"; - mee::Dice die1; + mee::Dice die1; if(die1.getSides() == 6){ std::cout << "Default constructor passes the test\n"; } @@ -40,7 +41,7 @@ int main(){ //Check the constructor with a high side number std::cout << "\nChecking a constructor with a high number of sides\n"; - mee::Dice die2(50); + mee::Dice die2(50); if(die2.getSides() == 50){ std::cout << "Parameterized constructor passed the test\n"; }