diff --git a/src/com/seefurst/towersofhanoi/boardobjects/Board.scala b/src/com/seefurst/towersofhanoi/boardobjects/Board.scala index f32dc33..8740ec4 100644 --- a/src/com/seefurst/towersofhanoi/boardobjects/Board.scala +++ b/src/com/seefurst/towersofhanoi/boardobjects/Board.scala @@ -25,14 +25,13 @@ class Board(numberOfDisksPerPole: Int) { def moveDisk(source: Int, dest: Int) = { //System.out.println("calling move disk, beginning pole: " + source + " dest pole: " + dest) val sourcePole = poles(source) //index - val sourceDisk = sourcePole.removeDisk() - //System.out.println("source disk: " + sourceDisk) - if (sourceDisk == null ) throw new IllegalStateException; - val destPole = poles(dest) //index - destPole.addDisk(sourceDisk) - - } + val sourceDiskOption = sourcePole.removeDisk() + sourceDiskOption match { + case Some(sourceDisk) => destPole.addDisk(sourceDisk) + case None => throw new IllegalStateException; + } + } def isFinished: Boolean = { poles(poles.length - 1).numberOfDisksOnPole == numberOfDisksPerPole diff --git a/src/com/seefurst/towersofhanoi/boardobjects/Pole.scala b/src/com/seefurst/towersofhanoi/boardobjects/Pole.scala index d7c8cf1..60709e1 100644 --- a/src/com/seefurst/towersofhanoi/boardobjects/Pole.scala +++ b/src/com/seefurst/towersofhanoi/boardobjects/Pole.scala @@ -29,11 +29,11 @@ class Pole(numberOfDisks: Int) { else throw new IllegalArgumentException("disk of size: " + disk.size + " is not allowed on this pole: " + this) } - def removeDisk(): Disk = { + def removeDisk(): Option[Disk] = { try { - disks.pop(); + Some(disks.pop()); } catch { - case e: EmptyStackException => null // use Option???? + case e: EmptyStackException => None // use Option???? } }