aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2014-01-04 16:24:54 +0100
committerRenaud G <renaud@rolisteam.org>2014-01-04 16:24:54 +0100
commit4de8cf5796446b7f8b09d776e4a6a6d6b8e95cb6 (patch)
treed0553a394e50d1de1a5185a7006fe3cb66cbfa3b
parent4114232457cbc5739872f479ef5d7772e6b5f42f (diff)
downloadOneRoll-4de8cf5796446b7f8b09d776e4a6a6d6b8e95cb6.tar.gz
OneRoll-4de8cf5796446b7f8b09d776e4a6a6d6b8e95cb6.zip
-Adding range, booleancondition and countexecutenode.
-rw-r--r--booleancondition.cpp35
-rw-r--r--booleancondition.h23
-rw-r--r--diceParser.pro9
-rw-r--r--diceparser.cpp167
-rw-r--r--diceparser.h36
-rw-r--r--diceresult.cpp5
-rw-r--r--diceresult.h2
-rw-r--r--main.cpp22
-rw-r--r--node/countexecutenode.cpp33
-rw-r--r--node/countexecutenode.h22
-rw-r--r--node/keepdiceexecnode.cpp27
-rw-r--r--node/keepdiceexecnode.h17
-rw-r--r--node/node.pri10
-rw-r--r--node/sortresult.cpp37
-rw-r--r--node/sortresult.h18
-rw-r--r--range.cpp21
-rw-r--r--range.h20
-rw-r--r--validator.cpp5
-rw-r--r--validator.h13
19 files changed, 496 insertions, 26 deletions
diff --git a/booleancondition.cpp b/booleancondition.cpp
new file mode 100644
index 0000000..c0b4299
--- /dev/null
+++ b/booleancondition.cpp
@@ -0,0 +1,35 @@
+#include "booleancondition.h"
+
+
+BooleanCondition::BooleanCondition()
+{
+}
+bool BooleanCondition::isValid(qint64 b) const
+{
+ switch(m_operator)
+ {
+ case Equal:
+ return (b==m_value);
+ case GreaterThan:
+ return (b>m_value);
+ case LesserThan:
+ return (b<m_value);
+ case GreaterOrEqual:
+ return (b>=m_value);
+ case LesserOrEqual:
+ return (b<=m_value);
+
+
+ }
+ return false;
+}
+
+void BooleanCondition::setOperator(LogicOperator m)
+{
+ m_operator = m;
+}
+
+void BooleanCondition::setValue(qint64 v)
+{
+ m_value=v;
+}
diff --git a/booleancondition.h b/booleancondition.h
new file mode 100644
index 0000000..e757a2e
--- /dev/null
+++ b/booleancondition.h
@@ -0,0 +1,23 @@
+#ifndef BOOLEANCONDITION_H
+#define BOOLEANCONDITION_H
+
+#include <Qt>
+#include "validator.h"
+
+class BooleanCondition : public Validator
+{
+public:
+ enum LogicOperator { Equal, GreaterThan, LesserThan, GreaterOrEqual, LesserOrEqual};
+ BooleanCondition();
+
+ virtual bool isValid(qint64 b) const;
+
+ void setOperator(LogicOperator m);
+ void setValue(qint64);
+
+private:
+ LogicOperator m_operator;
+ qint64 m_value;
+};
+
+#endif // BOOLEANCONDITION_H
diff --git a/diceParser.pro b/diceParser.pro
index e585d74..112fb9a 100644
--- a/diceParser.pro
+++ b/diceParser.pro
@@ -20,10 +20,19 @@ TEMPLATE = app
SOURCES += main.cpp \
diceparser.cpp \
diceresult.cpp \
+ range.cpp \
+ booleancondition.cpp \
+ validator.cpp
HEADERS += \
diceparser.h \
diceresult.h \
+ range.h \
+ booleancondition.h \
+ validator.h
OTHER_FILES += README.md
+
+
+#QMAKE_CXXFLAGS += -O0
diff --git a/diceparser.cpp b/diceparser.cpp
index 6096a4d..f44f6af 100644
--- a/diceparser.cpp
+++ b/diceparser.cpp
@@ -1,16 +1,39 @@
#include "diceparser.h"
#include <QDebug>
#include <QStringList>
+#include <QObject>
#include "node/dicerollernode.h"
#include "node/startingnode.h"
#include "node/scalaroperatornode.h"
#include "node/numbernode.h"
+#include "node/keepdiceexecnode.h"
+#include "node/sortresult.h"
+#include "node/countexecutenode.h"
DiceParser::DiceParser()
{
m_mapDiceOp = new QMap<QString,DiceOperator>();
m_mapDiceOp->insert("D",D);
+
+ m_OptionOp = new QMap<QString,OptionOperator>();
+ m_OptionOp->insert(QObject::tr("k"),keep);
+ m_OptionOp->insert(QObject::tr("K"),KeepAndReRoll);
+ m_OptionOp->insert(QObject::tr("s"),Sort);
+ m_OptionOp->insert(QObject::tr("c"),Count);
+
+
+
+ m_logicOp = new QMap<QString,BooleanCondition::LogicOperator>();
+ m_logicOp->insert("<",BooleanCondition::LesserThan);
+ m_logicOp->insert("=",BooleanCondition::Equal);
+ m_logicOp->insert("<=",BooleanCondition::LesserOrEqual);
+ m_logicOp->insert(">",BooleanCondition::GreaterThan);
+ m_logicOp->insert(">=",BooleanCondition::GreaterOrEqual);
+
+
+
+
}
void DiceParser::setCurrentNode(ExecutionNode* node)
@@ -38,20 +61,8 @@ void DiceParser::parseLine(QString str)
keepParsing =!str.isEmpty();
while(keepParsing)
{
- keepParsing = readOperator(str);
-
-// if(keepParsing)
-// {
-// keepParsing = readDiceExpression(str,execNode);
-
-// m_current->setNextNode(execNode);
-// m_current= execNode;
-
-// if(keepParsing)
-// {
-// keepParsing =!str.isEmpty();
-// }
-// }
+ keepParsing = readOperator(str);
+ keepParsing = readOption(str);
}
m_start->run();
@@ -103,7 +114,7 @@ bool DiceParser::readDice(QString& str,Dice& dice)
}
}
- return false;
+ return false;
}
bool DiceParser::readDiceOperator(QString& str,DiceOperator& op)
@@ -177,3 +188,129 @@ bool DiceParser::readOperator(QString& str)
}
return false;
}
+bool DiceParser::readOption(QString& str)
+{
+
+
+ if(str.isEmpty())
+ {
+ return false;
+ }
+
+
+
+ foreach(QString tmp, m_OptionOp->keys())
+ {
+
+ if(str.startsWith(tmp))
+ {
+
+ str=str.remove(0,tmp.size());
+ int myNumber=0;
+ switch(m_OptionOp->value(tmp))
+ {
+ case KeepAndReRoll:
+ {
+ if(readNumber(str,myNumber))
+ {
+ addSort(false);
+
+ KeepDiceExecNode* nodeK = new KeepDiceExecNode();
+ nodeK->setDiceKeepNumber(myNumber);
+
+ m_current->setNextNode(nodeK);
+ m_current= nodeK;
+ return true;
+
+ }
+ }
+ break;
+ case Sort:
+ {
+ addSort(false);
+ return true;
+ }
+ break;
+ case Count:
+ {
+ Validator* validator = readValidator(str);
+ if(NULL!=validator)
+ {
+ CountExecuteNode* node = new CountExecuteNode();
+ node->setValidator(validator);
+
+ m_current->setNextNode(node);
+ m_current = node;
+ return true;
+ }
+ }
+ break;
+ }
+ }
+ }
+ return false;
+}
+Validator* DiceParser::readValidator(QString& str)
+{
+ Validator* returnVal=NULL;
+ if(str.startsWith("["))
+ {
+ str=str.remove(0,1);
+ int start=0;
+ BooleanCondition::LogicOperator myLogicOp;
+ if(readNumber(str,start))
+ {
+ if(str.startsWith("-"))
+ {
+ str=str.remove(0,1);
+ int end=0;
+ if(readNumber(str,end))
+ {
+ if(str.startsWith("]"))
+ {
+ str=str.remove(0,1);
+ Range* range = new Range();
+ range->setValue(start,end);
+ returnVal = range;
+
+ }
+ }
+ }
+ }
+ else if(readLogicOperator(str,myLogicOp))
+ {
+ int value=0;
+ if(readNumber(str,value))
+ {
+ BooleanCondition* condition = new BooleanCondition();
+ condition->setValue(value);
+ condition->setOperator(myLogicOp);
+ returnVal = condition;
+
+ }
+ }
+ }
+ return returnVal;
+}
+bool DiceParser::readLogicOperator(QString& str,BooleanCondition::LogicOperator& op)
+{
+ foreach(QString tmp, m_logicOp->keys())
+ {
+ if(str.startsWith(tmp))
+ {
+ str=str.remove(0,tmp.size());
+ op = m_logicOp->value(tmp);
+ return true;
+ }
+ }
+ return false;
+}
+
+void DiceParser::addSort(bool b)
+{
+ SortResultNode* nodeSort = new SortResultNode();
+ nodeSort->setSortAscending(b);
+ m_current->setNextNode(nodeSort);
+ m_current = nodeSort;
+
+}
diff --git a/diceparser.h b/diceparser.h
index 6833a92..1615aaf 100644
--- a/diceparser.h
+++ b/diceparser.h
@@ -4,6 +4,9 @@
#include <QString>
#include <QMap>
#include "node/executionnode.h"
+#include "validator.h"
+#include "range.h"
+#include "booleancondition.h"
/**
* @mainpage DiceParser
@@ -28,13 +31,16 @@ class DiceParser
public:
/**
- * @brief The DiceOperator enum gathering all dice operator
+ * @brief The DiceOperator enum gathering all dice operators
*/
enum DiceOperator {D};
+
+
+
/**
- * @brief The MathOperator enum gathering all dice option/math operator such as keep k, reroll r, explode e.
+ * @brief The OptionOperator enum gathering all options availables for result.
*/
- enum MathOperator {K,k,r,e};
+ enum OptionOperator {KeepAndReRoll,keep,Reroll,Explosing,Sort,Count};
/**
* @brief DiceParser default constructor
@@ -83,8 +89,32 @@ private:
*/
void setCurrentNode(ExecutionNode* node);
+ /**
+ * @brief readOption
+ */
+ bool readOption(QString&);
+
+ /**
+ * @brief addSort
+ * @param b
+ */
+ void addSort(bool b);
+
+ /**
+ * @brief readValidator
+ * @param str
+ * @param validator
+ * @return
+ */
+ Validator* readValidator(QString& str);
+
+
+ bool readLogicOperator(QString& str,BooleanCondition::LogicOperator& condition);
+
private:
QMap<QString,DiceOperator>* m_mapDiceOp;
+ QMap<QString,OptionOperator>* m_OptionOp;
+ QMap<QString,BooleanCondition::LogicOperator>* m_logicOp;
ExecutionNode* m_start;
ExecutionNode* m_current;
};
diff --git a/diceresult.cpp b/diceresult.cpp
index dfdd765..619ccbd 100644
--- a/diceresult.cpp
+++ b/diceresult.cpp
@@ -22,3 +22,8 @@ qint64 DiceResult::getSum()
return sum;
}
+void DiceResult::setResultList(QList<qint64> list)
+{
+ m_diceValues.clear();
+ m_diceValues << list;
+}
diff --git a/diceresult.h b/diceresult.h
index a9d36a3..9212233 100644
--- a/diceresult.h
+++ b/diceresult.h
@@ -11,6 +11,8 @@ public:
QList<qint64>& getResultList();
void insertResult(qint64);
+ void setResultList(QList<qint64> list);
+
private:
QList<qint64> m_diceValues;
diff --git a/main.cpp b/main.cpp
index 479ac8a..a42115e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -9,15 +9,25 @@ int main(int argc, char *argv[])
myParser->parseLine("3D100");
myParser->parseLine("3D100");
- //myParser->parseLine("100291D66666666");
- //myParser->parseLine("10D10g3");
- //myParser->parseLine("10g3");
- myParser->parseLine("1D8");
+ // myParser->parseLine("100291D66666666");
+ myParser->parseLine("10D10K3");
+ myParser->parseLine("10k3");
+
+ /// roll 10 dice of 10 slices and sort them
+ myParser->parseLine("10D10s");
+
+ /// roll 15 dice of 10 slices and count above 7
+ myParser->parseLine("15D10c[8-10]");
+
+ /// roll 15 dice of 10 slices condition count above 7
+ myParser->parseLine("15D10c[>7]");
+
+
myParser->parseLine("1D8+2D6+7");
myParser->parseLine("D25");
myParser->parseLine("8+8");
- myParser->parseLine("8-88");
- myParser->parseLine("100*28");
+ myParser->parseLine("88-1D20");
+ myParser->parseLine("100*1D20");
myParser->parseLine("100/28");
myParser->parseLine("100/8");
myParser->parseLine("100*3");
diff --git a/node/countexecutenode.cpp b/node/countexecutenode.cpp
new file mode 100644
index 0000000..fff00f2
--- /dev/null
+++ b/node/countexecutenode.cpp
@@ -0,0 +1,33 @@
+#include "countexecutenode.h"
+
+CountExecuteNode::CountExecuteNode()
+{
+}
+void CountExecuteNode::setValidator(Validator* validator)
+{
+ m_validator = validator;
+}
+
+void CountExecuteNode::run(ExecutionNode *previous)
+{
+ if(NULL==previous)
+ {
+ return;
+ }
+ QList<qint64> diceList=previous->getResult()->getResultList();
+ qint64 sum = 0;
+ foreach(qint64 dice,diceList)
+ {
+ if(m_validator->isValid(dice))
+ {
+ ++sum;
+ }
+ }
+ m_result.insertResult(sum);
+
+
+ if(NULL!=m_nextNode)
+ {
+ m_nextNode->run(this);
+ }
+}
diff --git a/node/countexecutenode.h b/node/countexecutenode.h
new file mode 100644
index 0000000..6c971ea
--- /dev/null
+++ b/node/countexecutenode.h
@@ -0,0 +1,22 @@
+#ifndef COUNTEXECUTENODE_H
+#define COUNTEXECUTENODE_H
+
+#include "executionnode.h"
+
+#include "validator.h"
+
+class CountExecuteNode : public ExecutionNode
+{
+public:
+ CountExecuteNode();
+ virtual void run(ExecutionNode* previous);
+
+
+ virtual void setValidator(Validator* );
+
+private:
+ Validator* m_validator;
+
+};
+
+#endif // COUNTEXECUTENODE_H
diff --git a/node/keepdiceexecnode.cpp b/node/keepdiceexecnode.cpp
new file mode 100644
index 0000000..cd2b76e
--- /dev/null
+++ b/node/keepdiceexecnode.cpp
@@ -0,0 +1,27 @@
+#include "keepdiceexecnode.h"
+
+KeepDiceExecNode::KeepDiceExecNode()
+{
+}
+
+void KeepDiceExecNode::run(ExecutionNode* previous)
+{
+ if(NULL==previous)
+ {
+ return;
+ }
+ QList<qint64> diceList=previous->getResult()->getResultList();
+ QList<qint64> diceList2=m_result.getResultList();
+
+
+ diceList2 = diceList.mid(0,m_numberOfDice);
+ m_result.setResultList(diceList2);
+ if(NULL!=m_nextNode)
+ {
+ m_nextNode->run(this);
+ }
+}
+void KeepDiceExecNode::setDiceKeepNumber(quint64 n)
+{
+ m_numberOfDice = n;
+}
diff --git a/node/keepdiceexecnode.h b/node/keepdiceexecnode.h
new file mode 100644
index 0000000..28c637d
--- /dev/null
+++ b/node/keepdiceexecnode.h
@@ -0,0 +1,17 @@
+#ifndef KEEPDICEEXECNODE_H
+#define KEEPDICEEXECNODE_H
+
+#include "executionnode.h"
+
+class KeepDiceExecNode : public ExecutionNode
+{
+public:
+ KeepDiceExecNode();
+
+ virtual void run(ExecutionNode *previous);
+ virtual void setDiceKeepNumber(quint64 );
+private:
+ quint64 m_numberOfDice;
+};
+
+#endif // KEEPDICEEXECNODE_H
diff --git a/node/node.pri b/node/node.pri
index c3e1d65..fb2d9ad 100644
--- a/node/node.pri
+++ b/node/node.pri
@@ -4,7 +4,10 @@ HEADERS += \
node/rerolldicenode.h \
node/startingnode.h \
node/scalaroperatornode.h \
- node/numbernode.h
+ node/numbernode.h \
+ node/sortresult.h \
+ node/keepdiceexecnode.h \
+ node/countexecutenode.h
SOURCES += \
node/dicerollernode.cpp \
@@ -12,4 +15,7 @@ SOURCES += \
node/startingnode.cpp \
node/rerolldicenode.cpp \
node/scalaroperatornode.cpp \
- node/numbernode.cpp
+ node/numbernode.cpp \
+ node/sortresult.cpp \
+ node/keepdiceexecnode.cpp \
+ node/countexecutenode.cpp
diff --git a/node/sortresult.cpp b/node/sortresult.cpp
new file mode 100644
index 0000000..f4364fa
--- /dev/null
+++ b/node/sortresult.cpp
@@ -0,0 +1,37 @@
+#include "sortresult.h"
+
+#include <QDebug>
+
+SortResultNode::SortResultNode()
+{
+ m_ascending = true;
+}
+void SortResultNode::run(ExecutionNode* node)
+{
+ if(NULL==node)
+ {
+ return;
+ }
+ QList<qint64> diceList=node->getResult()->getResultList();
+ QList<qint64> diceList2=m_result.getResultList();
+
+ diceList2 = diceList;
+ if(!m_ascending)
+ {
+ qSort(diceList2.begin(), diceList2.end(), qGreater<int>());
+ }
+ else
+ {
+ qSort(diceList2.begin(), diceList2.end(), qLess<int>());
+ }
+ m_result.setResultList(diceList2);
+ if(NULL!=m_nextNode)
+ {
+ m_nextNode->run(this);
+ }
+
+}
+void SortResultNode::setSortAscending(bool asc)
+{
+ m_ascending = asc;
+}
diff --git a/node/sortresult.h b/node/sortresult.h
new file mode 100644
index 0000000..8433eba
--- /dev/null
+++ b/node/sortresult.h
@@ -0,0 +1,18 @@
+#ifndef SORTRESULT_H
+#define SORTRESULT_H
+
+#include "executionnode.h"
+
+class SortResultNode : public ExecutionNode
+{
+public:
+ SortResultNode();
+ virtual void run(ExecutionNode*);
+
+
+ void setSortAscending(bool asc);
+private:
+ bool m_ascending;
+};
+
+#endif // SORTRESULT_H
diff --git a/range.cpp b/range.cpp
new file mode 100644
index 0000000..0e18164
--- /dev/null
+++ b/range.cpp
@@ -0,0 +1,21 @@
+#include "range.h"
+
+Range::Range()
+{
+
+
+}
+void Range::setValue(qint64 s,qint64 e)
+{
+ m_start = s;
+ m_end=e;
+}
+
+bool Range::isValid(qint64 m) const
+{
+ if((m>=m_start)&&(m<=m_end))
+ {
+ return true;
+ }
+ return false;
+}
diff --git a/range.h b/range.h
new file mode 100644
index 0000000..73bef2b
--- /dev/null
+++ b/range.h
@@ -0,0 +1,20 @@
+#ifndef RANGE_H
+#define RANGE_H
+
+#include <Qt>
+#include "validator.h"
+
+class Range : public Validator
+{
+public:
+ Range();
+ void setValue(qint64,qint64);
+
+ virtual bool isValid(qint64 b) const;
+
+private:
+ qint64 m_start;
+ qint64 m_end;
+};
+
+#endif // RANGE_H
diff --git a/validator.cpp b/validator.cpp
new file mode 100644
index 0000000..a3cce1d
--- /dev/null
+++ b/validator.cpp
@@ -0,0 +1,5 @@
+#include "validator.h"
+
+Validator::Validator()
+{
+}
diff --git a/validator.h b/validator.h
new file mode 100644
index 0000000..1bdb977
--- /dev/null
+++ b/validator.h
@@ -0,0 +1,13 @@
+#ifndef VALIDATOR_H
+#define VALIDATOR_H
+
+#include <Qt>
+
+class Validator
+{
+public:
+ Validator();
+ virtual bool isValid(qint64 b) const = 0 ;
+};
+
+#endif // VALIDATOR_H