From bb9eeae7d5798d5c2c8579949c544b55be1bcbca Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 25 Jan 2018 01:06:07 +0100 Subject: -Add management of unique for dice and list. --- node/dicerollernode.cpp | 28 ++++++++++++++++++++++- node/dicerollernode.h | 4 ++++ node/listsetrollnode.cpp | 58 ++++++++++++++++++++++++++++++++++-------------- node/listsetrollnode.h | 1 + 4 files changed, 73 insertions(+), 18 deletions(-) (limited to 'node') diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index 5a2ea04..1f501e5 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -29,6 +29,13 @@ void DiceRollerNode::run(ExecutionNode* previous) { m_errors.insert(NO_DICE_TO_ROLL,QObject::tr("No dice to roll")); } + auto possibleValue = (m_max-m_min)+1; + //qDebug() << possibleValue; + if( possibleValue < m_diceCount && m_unique) + { + m_errors.insert(TOO_MANY_DICE,QObject::tr("More unique values asked than possible values (D operator)")); + return; + } for(quint64 i=0; i < m_diceCount ; ++i) { @@ -37,7 +44,16 @@ void DiceRollerNode::run(ExecutionNode* previous) die->setBase(m_min); die->setMaxValue(m_max); die->roll(); - //qDebug() << die->getValue() << "value"; + if(m_unique) + { + const auto& equal = [](const Die* a,const Die* b){ + return a->getValue() == b->getValue(); + }; + while(m_diceResult->contains(die,equal)) + { + die->roll(false); + } + } m_diceResult->insertResult(die); } if(nullptr!=m_nextNode) @@ -94,3 +110,13 @@ void DiceRollerNode::setOperator(const Die::ArithmeticOperator &dieOperator) m_operator = dieOperator; m_diceResult->setOperator(dieOperator); } + +bool DiceRollerNode::getUnique() const +{ + return m_unique; +} + +void DiceRollerNode::setUnique(bool unique) +{ + m_unique = unique; +} diff --git a/node/dicerollernode.h b/node/dicerollernode.h index 0857b30..2b4e3dd 100644 --- a/node/dicerollernode.h +++ b/node/dicerollernode.h @@ -47,12 +47,16 @@ public: Die::ArithmeticOperator getOperator() const; void setOperator(const Die::ArithmeticOperator & dieOperator); + bool getUnique() const; + void setUnique(bool unique); + private: quint64 m_diceCount; qint64 m_max; /// faces DiceResult* m_diceResult; qint64 m_min; Die::ArithmeticOperator m_operator; + bool m_unique; }; #endif // DICEROLLERNODE_H diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index 21d5403..d7d4566 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -65,17 +65,24 @@ void ListSetRollNode::run(ExecutionNode* previous) if(nullptr!=result) { quint64 diceCount = result->getResult(Result::SCALAR).toReal(); - m_result->setPrevious(result); - QStringList rollResult; - for(quint64 i=0; i < diceCount ; ++i) + if(diceCount > m_values.size() && m_unique) { - Die* die = new Die(); - computeFacesNumber(die); - die->roll(); - m_diceResult->insertResult(die); - getValueFromDie(die,rollResult); + m_errors.insert(TOO_MANY_DICE,QObject::tr("More unique values asked than possible values (L operator)")); + } + else + { + m_result->setPrevious(result); + QStringList rollResult; + for(quint64 i=0; i < diceCount ; ++i) + { + Die* die = new Die(); + computeFacesNumber(die); + die->roll(); + m_diceResult->insertResult(die); + getValueFromDie(die,rollResult); + } + m_stringResult->setText(rollResult.join(",")); } - m_stringResult->setText(rollResult.join(",")); if(nullptr!=m_nextNode) { m_nextNode->run(this); @@ -110,12 +117,10 @@ void ListSetRollNode::computeFacesNumber(Die* die) { if(((i==0)||(maxsetMaxValue(max); } @@ -126,20 +131,39 @@ void ListSetRollNode::getValueFromDie(Die* die,QStringList& rollResult) { if(die->getValue()-1getValue()-1]; + auto str = m_values[die->getValue()-1]; + while(m_unique && rollResult.contains(str)) + { + die->roll(false); + str = m_values[die->getValue()-1]; + } + rollResult << str; } } else { Q_ASSERT(m_values.size() == m_rangeList.size()); - int i=0; - foreach (Range range, m_rangeList) + bool found = false; + while(!found) { - if(range.hasValid(die,false)) + int i=0; + for (Range& range: m_rangeList) { - rollResult << m_values[i]; + auto it = std::find(m_rangeIndexResult.begin(),m_rangeIndexResult.end(),i); + auto isValid = range.hasValid(die,false); + if((isValid && !m_unique)|| + (isValid && it == m_rangeIndexResult.end())) + { + m_rangeIndexResult.push_back(i); + rollResult << m_values[i]; + found=true; + } + ++i; + } + if(!found) + { + die->roll(false); } - ++i; } } } diff --git a/node/listsetrollnode.h b/node/listsetrollnode.h index 2cc36f0..15d00e4 100644 --- a/node/listsetrollnode.h +++ b/node/listsetrollnode.h @@ -54,6 +54,7 @@ private: QStringList m_values; DiceResult* m_diceResult; StringResult* m_stringResult; + std::vector m_rangeIndexResult; bool m_unique; QList m_rangeList; }; -- cgit v1.2.3-70-g09d2