Skip to content

Instantly share code, notes, and snippets.

@akisaarinen
Created September 22, 2012 14:27
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save akisaarinen/3766321 to your computer and use it in GitHub Desktop.
Imperative Pong
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