diff options
Diffstat (limited to 'scala/compass.scala')
-rw-r--r-- | scala/compass.scala | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/scala/compass.scala b/scala/compass.scala new file mode 100644 index 0000000..cd3af78 --- /dev/null +++ b/scala/compass.scala @@ -0,0 +1,33 @@ +class Compass { + val directions = List("north", "east", "south", "west") + var bearing = 0 + + print("Initial bearing: ") + println(direction) + + def direction() = directions(bearing) + def inform(turnDirection : String) { + println("Turning " + turnDirection + ". Now bearing " + direction) + } + + def turnRight() { + bearing = (bearing + 1) % directions.size + inform("right") + } + + def turnLeft() { + bearing = (bearing + (directions.size - 1)) % directions.size + inform("left") + } +} + +val myCompass = new Compass + +myCompass.turnRight +myCompass.turnRight + +myCompass.turnLeft +myCompass.turnLeft +myCompass.turnLeft +myCompass.turnLeft +myCompass.turnLeft |