Skip to content

Commit

Permalink
Use option/some/none for remove disk (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skr00b@!! authored Feb 6, 2019
1 parent f63cb09 commit 47638cf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
13 changes: 6 additions & 7 deletions src/com/seefurst/towersofhanoi/boardobjects/Board.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/com/seefurst/towersofhanoi/boardobjects/Pole.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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????
}
}

Expand Down

0 comments on commit 47638cf

Please sign in to comment.