summaryrefslogtreecommitdiff
path: root/scala/compass.scala
diff options
context:
space:
mode:
authorGuillermo Ramos2012-05-06 14:40:34 +0200
committerGuillermo Ramos2012-05-06 14:40:34 +0200
commite17d06cbdd0eae3fcc193c345f787a28cb031cea (patch)
treee5132ca97a5016354b46c4302cb1854fda879a8d /scala/compass.scala
parent201bbad350345a63520338e74da96d4d31c8ebbc (diff)
download7l-e17d06cbdd0eae3fcc193c345f787a28cb031cea.tar.gz
[Scala] Día 1
Diffstat (limited to 'scala/compass.scala')
-rw-r--r--scala/compass.scala33
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