From 3853227d5852f45341c1ee49be4411ada78d860c Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 5 Jan 2014 13:12:05 +0100 Subject: Add new management of result. It may require to make several --- diceParser.pro | 6 ++++-- diceparser.cpp | 3 ++- diceresult.cpp | 16 +++------------- diceresult.h | 10 ++++++---- die.cpp | 34 ++++++++++++++++++++++++++++++++++ die.h | 24 ++++++++++++++++++++++++ main.cpp | 2 +- node/dicerollernode.cpp | 6 +++++- node/rerolldicenode.cpp | 20 +++++++++++++------- node/rerolldicenode.h | 6 +++--- node/sortresult.cpp | 4 ++-- 11 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 die.cpp create mode 100644 die.h diff --git a/diceParser.pro b/diceParser.pro index 112fb9a..caf19fb 100644 --- a/diceParser.pro +++ b/diceParser.pro @@ -22,7 +22,8 @@ SOURCES += main.cpp \ diceresult.cpp \ range.cpp \ booleancondition.cpp \ - validator.cpp + validator.cpp \ + die.cpp HEADERS += \ @@ -30,7 +31,8 @@ HEADERS += \ diceresult.h \ range.h \ booleancondition.h \ - validator.h + validator.h \ + die.h OTHER_FILES += README.md diff --git a/diceparser.cpp b/diceparser.cpp index f44f6af..81fc1e7 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -48,6 +48,7 @@ void DiceParser::setCurrentNode(ExecutionNode* node) void DiceParser::parseLine(QString str) { + QString command = str; m_start = new StartingNode(); m_current = m_start; bool keepParsing = true; @@ -71,7 +72,7 @@ void DiceParser::parseLine(QString str) { next = next->getNextNode(); } - qDebug() << "list:" <getResult()->getResultList() << "sum" <getResult()->getSum() ; + qDebug() << "list:" <getResult()->getResultList() << " sum:" <getResult()->getSum() << " command:" << command; } bool DiceParser::readNumber(QString& str, int& myNumber) { diff --git a/diceresult.cpp b/diceresult.cpp index 619ccbd..6d84ce1 100644 --- a/diceresult.cpp +++ b/diceresult.cpp @@ -4,25 +4,15 @@ DiceResult::DiceResult() { } -void DiceResult::insertResult(qint64 die) +void DiceResult::insertResult(Die die) { m_diceValues.append(die); } -QList& DiceResult::getResultList() +QList& DiceResult::getResultList() { return m_diceValues; } -qint64 DiceResult::getSum() -{ - qint64 sum=0; - foreach (qint64 tmp, m_diceValues) - { - sum+=tmp; - } - return sum; -} - -void DiceResult::setResultList(QList list) +void DiceResult::setResultList(QList list) { m_diceValues.clear(); m_diceValues << list; diff --git a/diceresult.h b/diceresult.h index 9212233..7cb5169 100644 --- a/diceresult.h +++ b/diceresult.h @@ -2,20 +2,22 @@ #define DICERESULT_H #include +#include "die.h" + class DiceResult { public: DiceResult(); qint64 getSum(); - QList& getResultList(); - void insertResult(qint64); + QList& getResultList(); + void insertResult(Die); - void setResultList(QList list); + void setResultList(QList list); private: - QList m_diceValues; + QList m_diceValues; }; #endif // DICERESULT_H diff --git a/die.cpp b/die.cpp new file mode 100644 index 0000000..7dde880 --- /dev/null +++ b/die.cpp @@ -0,0 +1,34 @@ +#include "die.h" + +Die::Die() +{ +} + +void Die::setValue(qint64 r) +{ + m_value = r; +} + +void Die::insertRollValue(qint64 r) +{ + m_rollResult.insert(r); +} + +void Die::setSelected(bool b) +{ + m_selected = b; +} + + +bool Die::isSelected() const +{ + return m_selected; +} +qint64 Die::getValue() const +{ + return m_value; +} +QList Die::getListValue() const +{ + return m_rollResult; +} diff --git a/die.h b/die.h new file mode 100644 index 0000000..22ce48e --- /dev/null +++ b/die.h @@ -0,0 +1,24 @@ +#ifndef DIE_H +#define DIE_H + +#include + +class Die +{ +public: + Die(); + + void setValue(qint64 r); + void insertRollValue(qint64 r); + void setSelected(bool b); + + bool isSelected() const; + qint64 getValue() const; + QList getListValue() const; +private: + qint64 m_value; + QList m_rollResult; + bool m_selected; +}; + +#endif // DIE_H diff --git a/main.cpp b/main.cpp index a42115e..daaf364 100644 --- a/main.cpp +++ b/main.cpp @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) myParser->parseLine("D25"); myParser->parseLine("8+8"); myParser->parseLine("88-1D20"); - myParser->parseLine("100*1D20"); + myParser->parseLine("100*1D20*2D6"); myParser->parseLine("100/28"); myParser->parseLine("100/8"); myParser->parseLine("100*3"); diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index c21b53c..f068f4b 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -1,4 +1,6 @@ #include "dicerollernode.h" +#include "die.h" + #include #include @@ -16,7 +18,9 @@ void DiceRollerNode::run(ExecutionNode* previous) m_diceCount = previous->getResult()->getSum(); for(quint64 i=0; i < m_diceCount ; ++i) { - m_result.insertResult(rollDice()); + Die die; + die.setValue(rollDice()); + m_result.insertResult(die); } if(NULL!=m_nextNode) { diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index 45f27de..6bc9467 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -1,21 +1,27 @@ #include "rerolldicenode.h" +#include "dicerollernode.h" -RerollDiceNode::RerollDiceNode(ExecutionNode* previous) - : m_previous(previous) +RerollDiceNode::RerollDiceNode() { } -void RerollDiceNode::run() +void RerollDiceNode::run(ExecutionNode* previous) { - if((NULL!=m_previous)&&(NULL!=m_previous->getResult())) + if((NULL!=previous)&&(NULL!=previous->getResult())) { - QList list = m_previous->getResult()->getResultList(); + QList list = previous->getResult()->getResultList(); - for(qint64 i=0; i < list.size() ; ++i) + foreach(Die die, list) { - // m_result.insertResult(rollDice()); + if(m_value == die.getValue()) + { +/* + DiceRollerNode roller; + roller.run(this);*/ + } } + if(NULL!=m_nextNode) { m_nextNode->run(this); diff --git a/node/rerolldicenode.h b/node/rerolldicenode.h index 90dc81a..609fcf3 100644 --- a/node/rerolldicenode.h +++ b/node/rerolldicenode.h @@ -12,11 +12,11 @@ class RerollDiceNode : public ExecutionNode public: enum ReRollMode {EQUAL,LESSER,GREATER}; - RerollDiceNode(ExecutionNode* previous); + RerollDiceNode(); - virtual void run(); + virtual void run(ExecutionNode* previous); private: - ExecutionNode* m_previous; + qint64 m_value; }; #endif // REROLLDICENODE_H diff --git a/node/sortresult.cpp b/node/sortresult.cpp index f4364fa..f842fcc 100644 --- a/node/sortresult.cpp +++ b/node/sortresult.cpp @@ -12,8 +12,8 @@ void SortResultNode::run(ExecutionNode* node) { return; } - QList diceList=node->getResult()->getResultList(); - QList diceList2=m_result.getResultList(); + QList diceList=node->getResult()->getResultList(); + QList diceList2=m_result.getResultList(); diceList2 = diceList; if(!m_ascending) -- cgit v1.2.3-70-g09d2