Created
February 19, 2013 17:20
-
-
Save sudowork/4987931 to your computer and use it in GitHub Desktop.
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
| import scala.actors.Actor | |
| import scala.actors.Actor._ | |
| import scala.util.Random.shuffle | |
| case class Start(player1: Actor, player2: Actor) | |
| case object Stop | |
| case class Play(coordinator: Actor) | |
| case class Throw(player: Actor, move: String) | |
| class Player(val id: Int) extends Actor { | |
| val moves = List("Rock", "Paper", "Scissor") | |
| def act() { | |
| loop { | |
| react { | |
| case Play(c) => { | |
| c ! Throw(self, shuffle(moves).head) | |
| exit | |
| } | |
| } | |
| } | |
| } | |
| override def toString = id.toString | |
| } | |
| class Coordinator extends Actor { | |
| def act() { | |
| loop { | |
| react { | |
| case Start(p1, p2) => { | |
| p1 ! Play(self) | |
| p2 ! Play(self) | |
| } | |
| case Stop => { | |
| println("Game over") | |
| exit | |
| } | |
| case Throw(pa, ma) => { | |
| println("Player %s threw %s".format(pa, ma)) | |
| react { | |
| case Throw(pb, mb) => { | |
| println("Player %s threw %s".format(pb, mb)) | |
| self ! Stop | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| object RPSTest extends App { | |
| val c = new Coordinator() | |
| val p1 = new Player(1) | |
| val p2 = new Player(2) | |
| p1.start | |
| p2.start | |
| c.start | |
| c ! Start(p1, p2) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment