From d8f8408c7cf0621eb7e06561ff113a80c2b6c94a Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sat, 26 Jan 2019 01:23:00 -0500 Subject: [PATCH] Added a simple dice class and test function --- Dice.hpp | 64 +++++++++++++++++++++++++++++++++++ testDice.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 Dice.hpp create mode 100644 testDice.cpp diff --git a/Dice.hpp b/Dice.hpp new file mode 100644 index 0000000..e1f29e8 --- /dev/null +++ b/Dice.hpp @@ -0,0 +1,64 @@ +//myClasses/Dice.hpp +//Matthew Ellison +// Created: 1-26-19 +//Modified: 1-26-19 +//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 + + 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef DICE_HPP +#define DICE_HPP + + +#include +#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 +#include +#endif //ifndef linux + + + +namespace mee{ + +class Dice{ +private: + uint64_t face; //Holds the currently rolled number + uint64_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 +public: + #ifdef linux + Dice(uint64_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) { } + #endif //ifdef linux + //Setup ways to get information from the class + uint64_t getFace() const { return face; } + uint64_t getSides() const { return sides; } + //Used to simulate rolling the dice. Returns the new number + uint64_t roll() { + face = dist(generator); + return face; + } +}; + +} //namespace mee + +#endif //DICE_HPP diff --git a/testDice.cpp b/testDice.cpp new file mode 100644 index 0000000..15fd2d2 --- /dev/null +++ b/testDice.cpp @@ -0,0 +1,94 @@ +//myClasses/testDice.cpp +//Matthew Ellison +// Created: 1-26-19 +//Modified: 1-26-19 +//This file is a simple program to test the Dice class +/* + Copyright (C) 2018 Matthew Ellison + + 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#include +#include +#include "Dice.hpp" + +const int LENGTH_OF_TEST = 100; //How many times the dice will get rolled * the number of sides + + +int main(){ + //Check the default constructor + std::cout << "Checking the default constructor of the Dice class:\n"; + mee::Dice die1; + if(die1.getSides() == 6){ + std::cout << "Default constructor passes the test\n"; + } + else{ + std::cout << "Default constructor has an incorrect number of sides\n"; + } + + //Check the constructor with a high side number + std::cout << "\nChecking a constructor with a high number of sides\n"; + mee::Dice die2(50); + if(die2.getSides() == 50){ + std::cout << "Parameterized constructor passed the test\n"; + } + else{ + std::cout << "Parameterized constructor has an incorrect number of sides\n"; + } + + //Run a long test to see how the rolls balance out for the default constructor + std::cout << "\nStarting test for the default constructor die:\n"; + //Setup an array to track how many times a number has been rolled + std::vector die1Rolls; + die1Rolls.reserve(die1.getSides()); + for(int cnt = 0;cnt < die1.getSides();++cnt){ + die1Rolls.push_back(0); + } + //A loop to roll the die and record the results + for(int cnt = 0;cnt < (LENGTH_OF_TEST * die1.getSides());++cnt){ + int num = die1.roll(); + ++die1Rolls[num - 1]; //num - 1 to account for dice starting at 1 and array starting at 0 + } + //Print out the results + for(int cnt = 0;cnt < die1Rolls.size();++cnt){ + std::cout << cnt + 1 << ". " << die1Rolls[cnt] << '\n'; + } + + //Run a long test to see how the rolls balance out for the parameterized constructor + std::cout << "\nStarting test for the parameterized constructor die:\n"; + //Setup an array to track how many times a number has been rolled + std::vector die2Rolls; + die2Rolls.reserve(die2.getSides()); + for(int cnt = 0;cnt < die2.getSides();++cnt){ + die2Rolls.push_back(0); + } + //A loop to roll the die and record the results + for(int cnt = 0;cnt < (LENGTH_OF_TEST * die2.getSides());++cnt){ + int num = die2.roll(); + ++die2Rolls[num - 1]; //num - 1 to account for dice starting at 1 and array starting at 0 + } + //Print out the results + for(int cnt = 0;cnt < die2Rolls.size();++cnt){ + std::cout << cnt + 1 << ". " << die2Rolls[cnt] << '\n'; + } + + std::cout << "Test of die class completed!" << std::endl; + + return 0; +} + +/*Results: + +*/