diff options
| author | 2017-04-27 18:17:03 +0200 | |
|---|---|---|
| committer | 2017-04-27 18:17:03 +0200 | |
| commit | 9b2839b6f09177bda6b3654c56a750f84173123a (patch) | |
| tree | 259cbab92ad580b11ddd4a01bd8fa53b151a237c /result/diceresult.cpp | |
| parent | 026ca5cb4829cd4c4ad6b945e27479303e96fe82 (diff) | |
| download | OneRoll-9b2839b6f09177bda6b3654c56a750f84173123a.tar.gz OneRoll-9b2839b6f09177bda6b3654c56a750f84173123a.zip | |
-management of arithmetic operator for scalar result of dice.
Diffstat (limited to 'result/diceresult.cpp')
| -rw-r--r-- | result/diceresult.cpp | 56 |
1 files changed, 48 insertions, 8 deletions
diff --git a/result/diceresult.cpp b/result/diceresult.cpp index aa39524..2825eec 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -87,16 +87,56 @@ qreal DiceResult::getScalarResult() } else { - qint64 scalarSum = 0; - foreach(Die* die,m_diceValues) - { - scalarSum+=die->getValue(); - } - return scalarSum; + qint64 scalar=0; + int i = 0; + for(auto tmp : m_diceValues) + { + if(i>0) + { + switch(m_operator) + { + case Die::PLUS: + scalar+=tmp->getValue(); + break; + case Die::MULTIPLICATION: + scalar*=tmp->getValue(); + break; + case Die::MINUS: + scalar-=tmp->getValue(); + break; + case Die::DIVIDE: + if(tmp!=0) + { + scalar/=tmp->getValue(); + } + else + { + //error(); + } + break; + } + } + else + { + scalar=tmp; + } + ++i; + } + return scalar; } return 0; } + +Die::ArithmeticOperator DiceResult::getOperator() const +{ + return m_operator; +} + +void DiceResult::setOperator(const Die::ArithmeticOperator& dieOperator) +{ + m_operator = dieOperator; +} QString DiceResult::toString(bool wl) { QStringList scalarSum; @@ -104,8 +144,8 @@ QString DiceResult::toString(bool wl) { scalarSum << QString::number(die->getValue()); } - if(wl) - { + if(wl) + { return QStringLiteral("%3 [label=\"DiceResult Value %1 dice %2\"]").arg(getScalarResult()).arg(scalarSum.join('_')).arg(m_id); } else |