Created
September 22, 2012 14:27
-
-
Save akisaarinen/3766321 to your computer and use it in GitHub Desktop.
Imperative Pong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pong.imperative | |
sealed trait Event | |
class PongConnection { | |
def isConnected(): Boolean = sys.error("not implemented") | |
def readEvent(): Event = sys.error("not implemented") | |
def moveUp(): Unit = sys.error("not implemented") | |
def moveDown(): Unit = sys.error("not implemented") | |
def shootMissile(): Unit = sys.error("not implemented") | |
} | |
class Bot(connection: PongConnection) { | |
var myDirection = 0 | |
var missileInventory = 0 | |
var enemyPosition = 0 | |
def update(event: Event) { | |
if (conditionA(event)) { | |
goUp() | |
} else if (conditionB(event)) { | |
shootMissile() | |
} | |
} | |
private def goUp() { | |
myDirection = 1 | |
connection.moveUp() | |
} | |
private def shootMissile() { | |
missileInventory -= 1 | |
connection.shootMissile() | |
} | |
private def conditionA(event: Event): Boolean = sys.error("not implemented") | |
private def conditionB(event: Event): Boolean = sys.error("not implemented") | |
} | |
object Pong extends App { | |
val connection = new PongConnection | |
val bot = new Bot(connection) | |
while (connection.isConnected()) { | |
val event = connection.readEvent() | |
bot.update(event) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment