Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add power die type

  • Loading branch information...
commit f936e77d14d54e072303e74624499f41ad4385d3 1 parent b348477
unknown authored
View
11 src/com/droiddice/model/DiceSet.scala
@@ -94,7 +94,7 @@ class DiceSet(var dice: RandomAccessSeq[Die], var strategy: Strategy, newName: S
class DiceSetHelper {}
object DiceSetHelper {
- val diePattern = Pattern.compile("([ds+])?(-?\\d+|F|S)")
+ val diePattern = Pattern.compile("([dsp+])?(-?\\d+|F|S)(?:\\:(\\d+))?")
val leadingNumber = Pattern.compile("""^(\d*)(.*)$""")
def split(spec: String, regex: Pattern): Tuple2[String,String] = {
@@ -102,12 +102,19 @@ object DiceSetHelper {
if (matcher.matches) (matcher.group(1), matcher.group(2)) else ("","")
}
+ def split3(spec: String, regex: Pattern): Tuple3[String,String,String] = {
+ val matcher = regex.matcher(spec)
+ if (matcher.matches) (matcher.group(1), matcher.group(2), matcher.group(3)) else ("","", "")
+ }
+
def singleDieFactory(spec: String): Die = {
try {
- val (t, size) = split(spec, diePattern)
+ val (t, size, degree) = split3(spec, diePattern)
+ System.err.println("extracted die parameters t=" + t + "; size=" + size + "; degree=" + degree)
if (t == "d" && size == "F") new FudgeDie
else if (t == "d" && size == "S") new SignDie()
else if (t == "s") new SavageDie(size.toInt)
+ else if (t == "p") new PowerDie(size.toInt, degree.toInt)
else if (t == "d") new SimpleDie(size.toInt)
else new AdjustmentDie(size.toInt)
} catch {
View
20 src/com/droiddice/model/PowerDie.scala
@@ -0,0 +1,20 @@
+package com.droiddice.model
+
+import scala.util.Random
+
+class PowerDie(val size: Int, val power: Int) extends Die {
+
+ val spec = "p" + size + ":" + power
+ override val imageId = "d" + size
+
+ val min: Int = 0
+ val max: Int = (size - 1) * power
+ val random: Random = new Random
+ var value = min
+
+ def roll: Int = {
+ value = min + random.nextInt(size) * power
+ value
+ }
+
+}
View
6 test/src/com/droiddice/test/AddDiceSetSpec.scala
@@ -37,6 +37,12 @@ class AddDiceSetSpec extends Spec with ShouldMatchers {
assert(d.isInstanceOf[SignDie])
}
}
+ describe("a die created from 'p10:10' string") {
+ var d = DiceSetHelper.singleDieFactory("p10:10")
+ it("should create Power die") {
+ assert(d.isInstanceOf[PowerDie])
+ }
+ }
describe("A dice set created from '2d4+d6-1' string") {
val d = new DiceSet("2d4+d6-1")
View
35 test/src/com/droiddice/test/PowerDieSpec.scala
@@ -0,0 +1,35 @@
+package com.droiddice.test
+
+import org.scalatest.junit.JUnitRunner
+import org.junit.runner.RunWith
+import org.scalatest.Spec
+import org.scalatest.matchers.ShouldMatchers
+
+import com.droiddice.model._
+
+@RunWith(classOf[JUnitRunner])
+class PowerDieSpec extends Spec with ShouldMatchers {
+
+ describe("A PowerDie of size 6,10") {
+ val d = new PowerDie(6,10)
+
+ val dist = new Distribution(d)
+ dist.min should be(0)
+ dist.max should be(50)
+
+ it("should return type of 'd6:10'") {
+ d.spec should be("p6:10")
+ }
+ it("should display as value of last roll") {
+ val r = d.roll
+ d.display should be(r.toString)
+ }
+ }
+ describe("A PowerDie of size 8:1") {
+ val d = new PowerDie(8, 1)
+
+ val dist = new Distribution(d)
+ dist.min should be(0)
+ dist.max should be(7)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.