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>
|
#include <cinttypes>
|
||||||
//Use this for anything besides Linux. It replaces random_device
|
//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
|
//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>
|
#include <ctime>
|
||||||
#endif //ifndef linux
|
#endif //ifndef linux
|
||||||
|
|
||||||
@@ -37,23 +37,24 @@
|
|||||||
|
|
||||||
namespace mee{
|
namespace mee{
|
||||||
|
|
||||||
|
template<class T>
|
||||||
class Dice{
|
class Dice{
|
||||||
private:
|
private:
|
||||||
uint64_t face; //Holds the currently rolled number
|
T face; //Holds the currently rolled number
|
||||||
uint64_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<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:
|
public:
|
||||||
#ifdef linux
|
#ifdef __linux
|
||||||
Dice(uint64_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
|
#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
|
#endif //ifdef linux
|
||||||
//Setup ways to get information from the class
|
//Setup ways to get information from the class
|
||||||
uint64_t getFace() const { return face; }
|
T getFace() const { return face; }
|
||||||
uint64_t getSides() const { return sides; }
|
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
|
||||||
uint64_t roll() {
|
T roll() {
|
||||||
face = dist(generator);
|
face = dist(generator);
|
||||||
return face;
|
return face;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cinttypes>
|
||||||
#include "Dice.hpp"
|
#include "Dice.hpp"
|
||||||
|
|
||||||
const int LENGTH_OF_TEST = 100; //How many times the dice will get rolled * the number of sides
|
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(){
|
int main(){
|
||||||
//Check the default constructor
|
//Check the default constructor
|
||||||
std::cout << "Checking the default constructor of the Dice class:\n";
|
std::cout << "Checking the default constructor of the Dice class:\n";
|
||||||
mee::Dice die1;
|
mee::Dice<int> die1;
|
||||||
if(die1.getSides() == 6){
|
if(die1.getSides() == 6){
|
||||||
std::cout << "Default constructor passes the test\n";
|
std::cout << "Default constructor passes the test\n";
|
||||||
}
|
}
|
||||||
@@ -40,7 +41,7 @@ int main(){
|
|||||||
|
|
||||||
//Check the constructor with a high side number
|
//Check the constructor with a high side number
|
||||||
std::cout << "\nChecking a constructor with a high number of sides\n";
|
std::cout << "\nChecking a constructor with a high number of sides\n";
|
||||||
mee::Dice die2(50);
|
mee::Dice<uint64_t> die2(50);
|
||||||
if(die2.getSides() == 50){
|
if(die2.getSides() == 50){
|
||||||
std::cout << "Parameterized constructor passed the test\n";
|
std::cout << "Parameterized constructor passed the test\n";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user