Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract parse() function, and add unittest for Diceparser #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 19 additions & 48 deletions die.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,18 @@
#include <QDebug>
#include <chrono>

Die::Die()
: m_hasValue(false),m_displayStatus(false),m_highlighted(true),m_base(1),m_color(""),m_op(Die::PLUS)//,m_mt(m_randomDevice)
{
// uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch();

// qsrand(seed);

auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
m_rng = std::mt19937(quintptr(this)+seed);
std::mt19937 DefaultRandomGenerator::m_rng = std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count());
std::shared_ptr<RandomGenerator> Die::m_rng = std::make_shared<DefaultRandomGenerator>();

void Die::setRandomGenerator(std::shared_ptr<RandomGenerator>&& r)
{
Die::m_rng = r;
}
Die::Die(const Die& die)

Die::Die(qint64 min, qint64 max)
: m_hasValue(false),m_displayStatus(false),m_highlighted(true),m_min(min),m_max(max),m_color(""),m_op(Die::PLUS)
{
m_value = die.m_value;
m_rollResult = die.m_rollResult;
m_selected = die.m_selected;
m_hasValue = die.m_hasValue;
m_displayStatus = die.m_displayStatus;
m_maxValue = die.m_maxValue;
m_highlighted = die.m_highlighted;
m_base = die.m_base;
m_color = die.getColor();
m_op = die.getOp();
assert(min<=max);
}

void Die::setValue(qint64 r)
Expand Down Expand Up @@ -134,26 +123,21 @@ void Die::replaceLastValue(qint64 value)

void Die::roll(bool adding)
{
if(m_maxValue!=0)
std::uniform_int_distribution<qint64> dist(m_min,m_max);
qint64 value = dist(*m_rng);
if((adding)||(m_rollResult.isEmpty()))
{
//quint64 value=(qrand()%m_faces)+m_base;

std::uniform_int_distribution<qint64> dist(m_base,m_maxValue);
qint64 value = dist(m_rng);
if((adding)||(m_rollResult.isEmpty()))
{
insertRollValue(value);
}
else
{
replaceLastValue(value);
}
insertRollValue(value);
}
else
{
replaceLastValue(value);
}
}

quint64 Die::getFaces() const
{
return abs(m_maxValue-m_base)+1;
return abs(m_max-m_min)+1;
}
qint64 Die::getLastRolledValue()
{
Expand Down Expand Up @@ -181,14 +165,6 @@ bool Die::isHighlighted() const
{
return m_highlighted;
}
void Die::setBase(qint64 base)
{
m_base = base;
}
qint64 Die::getBase()
{
return m_base;
}
QString Die::getColor() const
{
return m_color;
Expand All @@ -201,12 +177,7 @@ void Die::setColor(const QString &color)

qint64 Die::getMaxValue() const
{
return m_maxValue;
}

void Die::setMaxValue(const qint64 &maxValue)
{
m_maxValue = maxValue;
return m_max;
}

Die::ArithmeticOperator Die::getOp() const
Expand Down
74 changes: 57 additions & 17 deletions die.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,51 @@

#include <QList>
#include <QString>

#include <memory>
#include <deque>
#include <random>

struct RandomGenerator {
using result_type = uint_fast32_t;
virtual result_type operator() () = 0;
virtual result_type min () = 0;
virtual result_type max () = 0;
};

struct DefaultRandomGenerator: public RandomGenerator {
public:
result_type operator() () override {
return m_rng();
}
result_type min () override {
return m_rng.min();
}
result_type max () override {
return m_rng.max();
}
private:
static std::mt19937 m_rng;
};

struct CustomRandomGenerator: public RandomGenerator {
public:
CustomRandomGenerator(std::deque<result_type> values): m_values(move(values)) {}
result_type operator() () override {
auto val = m_values.front();
m_values.pop_front();
return val;
}
result_type min () override {
return INT_FAST32_MIN;
}
result_type max () override {
return INT_FAST32_MAX;
}
private:
std::deque<result_type> m_values;
};

/**
* @brief The Die class implements all methods required from a die. You must set the Faces first, then you can roll it and roll it again, to add or replace the previous result.
*/
Expand All @@ -37,12 +81,10 @@ class Die
enum ArithmeticOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION};
/**
* @brief Die
* @param max: Roll the die between min and max (inclusive)
*/
Die();
/**
* @brief Die
*/
Die(const Die& );
Die(qint64 min, qint64 max);

/**
* @brief setValue
* @param r
Expand Down Expand Up @@ -119,17 +161,10 @@ class Die
*/
bool isHighlighted() const;

/**
* @brief setBase
*/
void setBase(qint64);
qint64 getBase();

QString getColor() const;
void setColor(const QString &color);

qint64 getMaxValue() const;
void setMaxValue(const qint64 &maxValue);

Die::ArithmeticOperator getOp() const;
void setOp(const Die::ArithmeticOperator &op);
Expand All @@ -141,15 +176,20 @@ class Die
bool m_hasValue;
bool m_displayStatus;
bool m_highlighted;
qint64 m_maxValue;
qint64 m_base;
qint64 m_min;
qint64 m_max;
QString m_color;

Die::ArithmeticOperator m_op;

std::mt19937 m_rng;
static std::shared_ptr<RandomGenerator> m_rng;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is useful to put a shared pointer on a static member. Both mechanisms have quiet the same goal.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are totally right, thanks for pointing it.


private:
static void setRandomGenerator(std::shared_ptr<RandomGenerator>&& r);
public:
template<class RandomGenerator>
static void setRandomGenerator(RandomGenerator& r) {
setRandomGenerator(std::make_shared<RandomGenerator>(r));
}
};


#endif // DIE_H
4 changes: 1 addition & 3 deletions node/dicerollernode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ void DiceRollerNode::run(ExecutionNode* previous)

for(quint64 i=0; i < m_diceCount ; ++i)
{
Die* die = new Die();
Die* die = new Die(m_min,m_max);
die->setOp(m_operator);
die->setBase(m_min);
die->setMaxValue(m_max);
die->roll();
if(m_unique)
{
Expand Down
2 changes: 1 addition & 1 deletion node/explodedicenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void ExplodeDiceNode::run(ExecutionNode* previous)
{
for(Die* die: previous_result->getResultList())
{
Die* tmpdie = new Die();
Die* tmpdie = new Die(-1,-1); // yaaaaaarggl what does this block of code does?
*tmpdie=*die;
m_diceResult->insertResult(tmpdie);
die->displayed();
Expand Down
2 changes: 1 addition & 1 deletion node/filternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void FilterNode::run(ExecutionNode* previous)
{
if(m_validator->hasValid(tmp,m_eachValue))
{
Die* tmpdie = new Die();
Die* tmpdie = new Die(-1,-1); // yaaaaaarggl what does this block of code does?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All those code samples are doing the same thing, make a copy a die to do some work about this die without changing the original die.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so a copie constructor should do the trick. I will test.

*tmpdie=*tmp;
diceList2.append(tmpdie);
tmp->displayed();
Expand Down
3 changes: 1 addition & 2 deletions node/ifnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ void IfNode::run(ExecutionNode *previous)

if(m_conditionType == OnScalar)
{
Die* dice = new Die();
Die* dice = new Die(1,value);
dice->setValue(value);
dice->insertRollValue(value);
dice->setMaxValue(value);
if(m_validator->hasValid(dice,true,true))
{
nextNode=m_true;
Expand Down
2 changes: 1 addition & 1 deletion node/jumpbackwardnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void JumpBackwardNode::run(ExecutionNode* previous)
{
for(Die* die:diceResult->getResultList())
{
Die* tmpdie = new Die();
Die* tmpdie = new Die(-1,-1); // yaaaaaarggl what does this block of code does?
*tmpdie=*die;
m_diceResult->insertResult(tmpdie);
die->displayed();
Expand Down
2 changes: 1 addition & 1 deletion node/keepdiceexecnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void KeepDiceExecNode::run(ExecutionNode* previous)

for(Die* die : diceList3)
{
Die* tmpdie = new Die();
Die* tmpdie = new Die(-1,-1); // yaaaaaarggl what does this block of code does?
*tmpdie=*die;
diceList2.append(tmpdie);
die->displayed();
Expand Down
9 changes: 4 additions & 5 deletions node/listsetrollnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ void ListSetRollNode::run(ExecutionNode* previous)
QStringList rollResult;
for(quint64 i=0; i < diceCount ; ++i)
{
Die* die = new Die();
computeFacesNumber(die);
Die* die = new Die(1,computeFacesNumber());
die->roll();
m_diceResult->insertResult(die);
getValueFromDie(die,rollResult);
Expand All @@ -102,11 +101,11 @@ void ListSetRollNode::setRangeList(QList<Range>& ranges)
{
m_rangeList = ranges;
}
void ListSetRollNode::computeFacesNumber(Die* die)
qint64 ListSetRollNode::computeFacesNumber()
{
if(m_rangeList.isEmpty())
{
die->setMaxValue(m_values.size());
return m_values.size();
}
else
{
Expand All @@ -121,7 +120,7 @@ void ListSetRollNode::computeFacesNumber(Die* die)
}
++i;
}
die->setMaxValue(max);
return max;
}

}
Expand Down
2 changes: 1 addition & 1 deletion node/listsetrollnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ListSetRollNode : public ExecutionNode

private:
void getValueFromDie(Die* die,QStringList& rollResult);
void computeFacesNumber(Die* die);
qint64 computeFacesNumber();

private:
QStringList m_values;
Expand Down
2 changes: 1 addition & 1 deletion node/mergenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void MergeNode::run(ExecutionNode* previous)
{
if(!m_diceResult->getResultList().contains(die)&&(!die->hasBeenDisplayed()))
{
Die* tmpdie = new Die();
Die* tmpdie = new Die(-1,-1); // yaaaaaarggl what does this block of code does?
*tmpdie=*die;
die->displayed();
m_diceResult->getResultList().append(tmpdie);
Expand Down
2 changes: 1 addition & 1 deletion node/rerolldicenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void RerollDiceNode::run(ExecutionNode* previous)
{
for(Die* die: previous_result->getResultList())
{
Die* tmpdie = new Die();
Die* tmpdie = new Die(-1,-1); // yaaaaaarggl what does this block of code does?
*tmpdie=*die;
m_diceResult->insertResult(tmpdie);
die->displayed();
Expand Down
3 changes: 1 addition & 2 deletions node/sortresult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ void SortResultNode::run(ExecutionNode* node)
for(int i = 0; i<diceList.size();++i)
{

Die* tmp1 = new Die();
*tmp1=*diceList[i];
Die* tmp1 = new Die(*diceList[i]);
diceList[i]->displayed();

int j =0;
Expand Down
4 changes: 1 addition & 3 deletions node/splitnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ void SplitNode::run(ExecutionNode* previous)
m_diceResult->setOperator(oldDie->getOp());
for(qint64 value : oldDie->getListValue())
{
Die* tmpdie = new Die();
Die* tmpdie = new Die(*oldDie);
tmpdie->insertRollValue(value);
tmpdie->setBase(oldDie->getBase());
tmpdie->setMaxValue(oldDie->getMaxValue());
tmpdie->setValue(value);
tmpdie->setOp(oldDie->getOp());
m_diceResult->insertResult(tmpdie);
Expand Down
3 changes: 1 addition & 2 deletions operationcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ qint64 OperationCondition::hasValid(Die* b,bool recursive,bool unhighlight) cons
{
case Modulo:
{
Die die;
die.setMaxValue(b->getMaxValue());
Die die(1,b->getMaxValue());
auto valueScalar = valueToScalar();
if(valueScalar==0)
valueScalar = 1;
Expand Down