diff options
Diffstat (limited to 'node')
| -rw-r--r-- | node/dicerollernode.cpp | 30 | ||||
| -rw-r--r-- | node/dicerollernode.h | 25 | ||||
| -rw-r--r-- | node/executionnode.cpp | 24 | ||||
| -rw-r--r-- | node/executionnode.h | 20 | ||||
| -rw-r--r-- | node/node.pri | 15 | ||||
| -rw-r--r-- | node/numbernode.cpp | 19 | ||||
| -rw-r--r-- | node/numbernode.h | 17 | ||||
| -rw-r--r-- | node/rerolldicenode.cpp | 25 | ||||
| -rw-r--r-- | node/rerolldicenode.h | 22 | ||||
| -rw-r--r-- | node/scalaroperatornode.cpp | 91 | ||||
| -rw-r--r-- | node/scalaroperatornode.h | 32 | ||||
| -rw-r--r-- | node/startingnode.cpp | 14 | ||||
| -rw-r--r-- | node/startingnode.h | 14 |
13 files changed, 348 insertions, 0 deletions
diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp new file mode 100644 index 0000000..c21b53c --- /dev/null +++ b/node/dicerollernode.cpp @@ -0,0 +1,30 @@ +#include "dicerollernode.h" + +#include <QDateTime> +#include <QDebug> + +DiceRollerNode::DiceRollerNode(quint64 faces) + : m_faces(faces) +{ + uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch(); + qsrand(seed); +} +void DiceRollerNode::run(ExecutionNode* previous) +{ + if(NULL!=previous) + { + m_diceCount = previous->getResult()->getSum(); + for(quint64 i=0; i < m_diceCount ; ++i) + { + m_result.insertResult(rollDice()); + } + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } + } +} +quint64 DiceRollerNode::rollDice() +{ + return (qrand()%m_faces)+1; +} diff --git a/node/dicerollernode.h b/node/dicerollernode.h new file mode 100644 index 0000000..172d09d --- /dev/null +++ b/node/dicerollernode.h @@ -0,0 +1,25 @@ +#ifndef DICEROLLERNODE_H +#define DICEROLLERNODE_H + +#include "executionnode.h" + +class DiceRollerNode : public ExecutionNode +{ +public: + DiceRollerNode(quint64 faces); + + virtual void run(ExecutionNode*); + + //private method +private: + quint64 rollDice(); + + +//private members +private: + quint64 m_diceCount; + quint64 m_faces; /// faces + +}; + +#endif // DICEROLLERNODE_H diff --git a/node/executionnode.cpp b/node/executionnode.cpp new file mode 100644 index 0000000..cf1826d --- /dev/null +++ b/node/executionnode.cpp @@ -0,0 +1,24 @@ +#include "executionnode.h" + +ExecutionNode::ExecutionNode() + : m_nextNode(NULL) +{ + +} +ExecutionNode::~ExecutionNode() +{ + +} + +DiceResult* ExecutionNode::getResult() +{ + return &m_result; +} +void ExecutionNode::setNextNode(ExecutionNode* node) +{ + m_nextNode = node; +} +ExecutionNode* ExecutionNode::getNextNode() +{ + return m_nextNode; +} diff --git a/node/executionnode.h b/node/executionnode.h new file mode 100644 index 0000000..2d81556 --- /dev/null +++ b/node/executionnode.h @@ -0,0 +1,20 @@ +#ifndef EXECUTIONNODE_H +#define EXECUTIONNODE_H + +#include "diceresult.h" + +class ExecutionNode +{ +public: + ExecutionNode(); + virtual ~ExecutionNode(); + virtual void run(ExecutionNode* previous = NULL)=0; + DiceResult* getResult(); + void setNextNode(ExecutionNode*); + ExecutionNode* getNextNode(); +protected: + DiceResult m_result; + ExecutionNode* m_nextNode; +}; + +#endif // EXECUTIONNODE_H diff --git a/node/node.pri b/node/node.pri new file mode 100644 index 0000000..c3e1d65 --- /dev/null +++ b/node/node.pri @@ -0,0 +1,15 @@ +HEADERS += \ + node/dicerollernode.h \ + node/executionnode.h \ + node/rerolldicenode.h \ + node/startingnode.h \ + node/scalaroperatornode.h \ + node/numbernode.h + +SOURCES += \ + node/dicerollernode.cpp \ + node/executionnode.cpp \ + node/startingnode.cpp \ + node/rerolldicenode.cpp \ + node/scalaroperatornode.cpp \ + node/numbernode.cpp diff --git a/node/numbernode.cpp b/node/numbernode.cpp new file mode 100644 index 0000000..690918a --- /dev/null +++ b/node/numbernode.cpp @@ -0,0 +1,19 @@ +#include "numbernode.h" + +NumberNode::NumberNode() +{ + +} +void NumberNode::run(ExecutionNode* ) +{ + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } +} + +void NumberNode::setNumber(qint64 a) +{ + m_result.insertResult(a); + m_number = a; +} diff --git a/node/numbernode.h b/node/numbernode.h new file mode 100644 index 0000000..ad472c6 --- /dev/null +++ b/node/numbernode.h @@ -0,0 +1,17 @@ +#ifndef NUMBERNODE_H +#define NUMBERNODE_H +#include "node/executionnode.h" + +class NumberNode : public ExecutionNode +{ +public: + NumberNode(); + void run(ExecutionNode* previous); + void setNumber(qint64); + +private: + qint64 m_number; + +}; + +#endif // NUMBERNODE_H diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp new file mode 100644 index 0000000..45f27de --- /dev/null +++ b/node/rerolldicenode.cpp @@ -0,0 +1,25 @@ +#include "rerolldicenode.h" + +RerollDiceNode::RerollDiceNode(ExecutionNode* previous) + : m_previous(previous) +{ + +} +void RerollDiceNode::run() +{ + if((NULL!=m_previous)&&(NULL!=m_previous->getResult())) + { + QList<qint64> list = m_previous->getResult()->getResultList(); + + + for(qint64 i=0; i < list.size() ; ++i) + { + // m_result.insertResult(rollDice()); + } + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } + } +} + diff --git a/node/rerolldicenode.h b/node/rerolldicenode.h new file mode 100644 index 0000000..90dc81a --- /dev/null +++ b/node/rerolldicenode.h @@ -0,0 +1,22 @@ +#ifndef REROLLDICENODE_H +#define REROLLDICENODE_H + + +#include "executionnode.h" + +/** + * @brief The RerollDiceNode class reroll dice given a condition and replace(or add) the result. + */ +class RerollDiceNode : public ExecutionNode +{ + +public: + enum ReRollMode {EQUAL,LESSER,GREATER}; + RerollDiceNode(ExecutionNode* previous); + + virtual void run(); +private: + ExecutionNode* m_previous; +}; + +#endif // REROLLDICENODE_H diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp new file mode 100644 index 0000000..e8a2ec0 --- /dev/null +++ b/node/scalaroperatornode.cpp @@ -0,0 +1,91 @@ +#include "scalaroperatornode.h" + +#include <QDebug> + +ScalarOperatorNode::ScalarOperatorNode() + : m_internalNode(NULL) +{ + m_scalarOperationList.insert('+',PLUS); + m_scalarOperationList.insert('-',MINUS); + m_scalarOperationList.insert('x',MULTIPLICATION); + m_scalarOperationList.insert('*',MULTIPLICATION); + m_scalarOperationList.insert('/',DIVIDE); + m_scalarOperationList.insert('÷',DIVIDE); +} + +void ScalarOperatorNode::run(ExecutionNode* previous) +{ + if(NULL!=m_internalNode) + { + m_internalNode->run(this); + } + if(NULL!=previous) + { + DiceResult* previousResult = previous->getResult(); + ExecutionNode* internal = m_internalNode; + while(NULL != internal->getNextNode() ) + { + internal = internal->getNextNode(); + } + DiceResult* internalResult = internal->getResult(); + + switch(m_myOperator) + { + case PLUS: + m_result.insertResult(add(previousResult->getSum(),internalResult->getSum())); + break; + case MINUS: + m_result.insertResult(substract(previousResult->getSum(),internalResult->getSum())); + break; + case MULTIPLICATION: + m_result.insertResult(multiple(previousResult->getSum(),internalResult->getSum())); + break; + case DIVIDE: + m_result.insertResult(divide(previousResult->getSum(),internalResult->getSum())); + break; + default: + break; + + } + + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } + } + +} +bool ScalarOperatorNode::setOperatorChar(QChar c) +{ + if(m_scalarOperationList.contains(c)) + { + m_myOperator = m_scalarOperationList.value(c); + return true; + } + return false; +} + + +void ScalarOperatorNode::setInternalNode(ExecutionNode* node) +{ + m_internalNode = node; +} +qint64 ScalarOperatorNode::add(qint64 a,qint64 b) +{ + return a+b; +} + +qint64 ScalarOperatorNode::substract(qint64 a,qint64 b) +{ + return a-b; +} + +qint64 ScalarOperatorNode::divide(qint64 a,qint64 b) +{ + return a/b; +} + +qint64 ScalarOperatorNode::multiple(qint64 a,qint64 b) +{ + return a*b; +} diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h new file mode 100644 index 0000000..96324ba --- /dev/null +++ b/node/scalaroperatornode.h @@ -0,0 +1,32 @@ +#ifndef SCALAROPERATORNODE_H +#define SCALAROPERATORNODE_H + +#include <QMap> +#include <QChar> + +#include "executionnode.h" + + +class ScalarOperatorNode : public ExecutionNode +{ +public: + enum ScalarOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION}; + ScalarOperatorNode(); + virtual void run(ExecutionNode*); + bool setOperatorChar(QChar c); + void setInternalNode(ExecutionNode* node); + + +private: + qint64 add(qint64,qint64); + qint64 substract(qint64,qint64); + qint64 divide(qint64,qint64); + qint64 multiple(qint64,qint64); + +private: + ScalarOperator m_myOperator; + ExecutionNode* m_internalNode; + QMap<QChar,ScalarOperator> m_scalarOperationList; +}; + +#endif // SCALAROPERATORNODE_H diff --git a/node/startingnode.cpp b/node/startingnode.cpp new file mode 100644 index 0000000..602fec0 --- /dev/null +++ b/node/startingnode.cpp @@ -0,0 +1,14 @@ +#include "startingnode.h" +#include <QDebug> + +StartingNode::StartingNode() +{ + +} +void StartingNode::run(ExecutionNode*) +{ + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } +} diff --git a/node/startingnode.h b/node/startingnode.h new file mode 100644 index 0000000..08134e7 --- /dev/null +++ b/node/startingnode.h @@ -0,0 +1,14 @@ +#ifndef STARTINGNODE_H +#define STARTINGNODE_H + +#include "executionnode.h" + +class StartingNode : public ExecutionNode +{ +public: + StartingNode(); + + virtual void run(ExecutionNode*); +}; + +#endif // STARTINGNODE_H |