Skip to content

Commit

Permalink
Add operator T
Browse files Browse the repository at this point in the history
  • Loading branch information
Renaud G committed Sep 26, 2019
1 parent 69a238f commit d94207b
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SET( dice_sources
${CMAKE_CURRENT_SOURCE_DIR}/node/executionnode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/explodedicenode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/helpnode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/allsamenode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/mergenode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/jumpbackwardnode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/keepdiceexecnode.cpp
Expand Down
16 changes: 16 additions & 0 deletions HelpMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* [Group](#group)
* [Spread](#spread)
* [Unique](#unique)
* [All The same](#allsame)
* [Value list](#Value-list)
* [Comment (\#)](#comment-)
* [Arithmetic](#arithmetic)
Expand Down Expand Up @@ -304,6 +305,21 @@ First Result: `10 [6, 4], 3, 3, 2`
Result after spead: `6, 4, 3, 2`
Final result: `6+4+3 = 13`

### All the same

This operator is temporary. It is dedicated to answer issue about Tunnels and Trolls system. It is why the marker operator is `t`.
Dice explode when all dice has the same value.

> 2d6t
```
# Explode twice because 2,1
Result: 12 - details:[2d6t (5 [2,1,2] 7 [2,1,4])]
# Nothing happened
Result: 10 - details:[2d6t (4 6)]
```

### Unique

It makes exploded dice as new dice.
Expand Down
1 change: 1 addition & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SET( dice_sources
../node/jumpbackwardnode.cpp
../node/keepdiceexecnode.cpp
../node/listaliasnode.cpp
../node/allsamenode.cpp
../node/listsetrollnode.cpp
../node/numbernode.cpp
../node/parenthesesnode.cpp
Expand Down
10 changes: 10 additions & 0 deletions diceparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QStringList>
#include <functional>

#include "node/allsamenode.h"
#include "node/bind.h"
#include "node/countexecutenode.h"
#include "node/dicerollernode.h"
Expand Down Expand Up @@ -86,6 +87,7 @@ DiceParser::DiceParser()
m_OptionOp->insert(QStringLiteral("f"), Filter);
m_OptionOp->insert(QStringLiteral("y"), Split);
m_OptionOp->insert(QStringLiteral("u"), Unique);
m_OptionOp->insert(QStringLiteral("t"), AllSameExplode);
m_OptionOp->insert(QStringLiteral("g"), Group);
m_OptionOp->insert(QStringLiteral("b"), Bind);
m_OptionOp->insert(QStringLiteral("o"), Occurences);
Expand Down Expand Up @@ -1192,6 +1194,14 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //,
found= true;
}
break;
case AllSameExplode:
{
AllSameNode* allSame = new AllSameNode();
previous->setNextNode(allSame);
node=allSame;
found = true;
}
break;
case Bind:
{
BindNode* bindNode= new BindNode();
Expand Down
2 changes: 2 additions & 0 deletions diceparser.pri
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SOURCES += $$PWD/diceparser.cpp \
$$PWD/operationcondition.cpp \
$$PWD/node/stringnode.cpp \
$$PWD/node/filternode.cpp \
$$PWD/node/allsamenode.cpp \
$$PWD/node/groupnode.cpp \
$$PWD/node/dicerollernode.cpp \
$$PWD/node/executionnode.cpp \
Expand Down Expand Up @@ -66,6 +67,7 @@ HEADERS += \
$$PWD/operationcondition.h \
$$PWD/node/stringnode.h \
$$PWD/node/filternode.h\
$$PWD/node/allsamenode.h\
$$PWD/node/groupnode.h \
$$PWD/node/variablenode.h\
$$PWD/node/dicerollernode.h \
Expand Down
3 changes: 2 additions & 1 deletion include/diceparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class DiceParser
Group,
Occurences,
Unique,
Bind
Bind,
AllSameExplode
};
/**
* @brief The CommandOperator enum
Expand Down
82 changes: 82 additions & 0 deletions node/allsamenode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "allsamenode.h"


AllSameNode::AllSameNode() : m_diceResult(new DiceResult())
{
m_result= m_diceResult;
}

void AllSameNode::run(ExecutionNode* previous)
{
m_previousNode= previous;
if(nullptr != previous)
{
DiceResult* previous_result= dynamic_cast<DiceResult*>(previous->getResult());
if(nullptr != previous_result)
{
m_result->setPrevious(previous_result);
bool allSame=true;
int i=0;
quint64 previousValue;
for(auto& die : previous_result->getResultList())
{
if(i == 0)
previousValue=die->getValue();
Die* tmpdie= new Die(*die);
m_diceResult->insertResult(tmpdie);
die->displayed();
if(previousValue != die->getValue())
allSame=false;
++i;
}

while(allSame)
{
QList<Die*> list= m_diceResult->getResultList();
qint64 pValue;
int i =0;
for(auto& die: list)
{
die->roll(true);
if(i==0)
pValue=die->getValue();
if(pValue != die->getValue())
allSame = false;
++i;
}
}
}
}
if(nullptr != m_nextNode)
{
m_nextNode->run(this);
}
}


QString AllSameNode::toString(bool withLabel) const
{
if(withLabel)
{
return QString("%1 [label=\"AllSameNode\"]").arg(m_id);
}
else
{
return m_id;
}
}

qint64 AllSameNode::getPriority() const
{
qint64 priority= 0;
if(nullptr != m_nextNode)
{
priority= m_nextNode->getPriority();
}
return priority;
}

ExecutionNode* AllSameNode::getCopy() const
{
return new AllSameNode();
}
33 changes: 33 additions & 0 deletions node/allsamenode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef ALLSAMENODE_H
#define ALLSAMENODE_H

#include "executionnode.h"

#include "result/diceresult.h"
#include "validator.h"

class AllSameNode : public ExecutionNode
{
public:
AllSameNode();
// virtual ~AllSameNode();

virtual void run(ExecutionNode* previous);
/**
* @brief toString
* @return
*/
virtual QString toString(bool withLabel) const;
/**
* @brief getPriority
* @return
*/
virtual qint64 getPriority() const;

virtual ExecutionNode* getCopy() const;

private:
DiceResult* m_diceResult;
};

#endif // FILTERNODE_H

0 comments on commit d94207b

Please sign in to comment.