Created
August 3, 2011 03:42
-
-
Save seratch/1121858 to your computer and use it in GitHub Desktop.
Daimon.scala #9 やってみよう
This file contains 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
/** | |
* Daimon.scala #9 やってみよう | |
* http://d.hatena.ne.jp/seratch2/20110802/1312305023 | |
* scala -cp .:akka-actor-1.1.3.jar yamanote-akka.scala | |
* 実装例: https://gist.github.com/1121859 | |
*/ | |
import akka.actor.Actor | |
import akka.actor.ActorRef | |
import akka.actor.Actor._ | |
case class Station(name: String) | |
object Yamanotesen { | |
def randomStation: Station = { | |
stations(new java.security.SecureRandom().nextInt(stations.length)) | |
} | |
private val stations: List[Station] = List( | |
Station("東京") | |
,Station("有楽町") | |
,Station("新橋") | |
,Station("浜松町") | |
,Station("田町") | |
,Station("品川") | |
,Station("大崎") | |
,Station("五反田") | |
,Station("目黒") | |
,Station("恵比寿") | |
,Station("渋谷") | |
,Station("原宿") | |
,Station("代々木") | |
,Station("新宿") | |
,Station("新大久保") | |
,Station("高田馬場") | |
,Station("目白") | |
,Station("池袋") | |
,Station("大塚") | |
,Station("巣鴨") | |
,Station("駒込") | |
,Station("田端") | |
,Station("西日暮里") | |
,Station("日暮里") | |
,Station("御徒町") | |
,Station("秋葉原") | |
,Station("神田") | |
) | |
} | |
case class StationIsUsed(station: Station) | |
case class MarkStationAsUsed(station: Station) | |
case class MarkStationAsUsedResponse(isMarkedAsUsed: Boolean) | |
class UsedStationsManager extends Actor { | |
self.start | |
def receive = { | |
case StationIsUsed(station) => | |
self.reply(used.contains(station).asInstanceOf[Boolean]) | |
case MarkStationAsUsed(station) => { | |
used += station | |
self.reply(MarkStationAsUsedResponse( | |
used.contains(station).asInstanceOf[Boolean] | |
)) | |
} | |
case _ => throw new IllegalArgumentException | |
} | |
import collection.mutable.{Set,HashSet} | |
private var used: Set[Station] = new HashSet[Station] | |
} | |
case object YourTurn | |
case class Player(name: String) extends Actor { | |
self.start | |
def receive = { | |
case YourTurn => self.reply(PlayerResponse(this, Yamanotesen.randomStation)) | |
case _ => throw new IllegalArgumentException | |
} | |
} | |
case object GameStart | |
case class PlayerResponse(player: Player, station:Station) | |
case class NextPlayer(player: Player) | |
case object GameEnd | |
case class Game(players: ActorRef*) extends Actor { | |
self.start | |
val usedStationManager = actorOf(new UsedStationsManager) | |
def receive = { | |
// TODO | |
case msg => | |
} | |
} | |
val Andy = actorOf(Player("Andy")) | |
val Brian = actorOf(Player("Brian")) | |
actorOf(Game(Andy,Brian)) ! GameStart | |
Thread.sleep(5000L) | |
System.exit(0) | |
/* | |
パンパン! | |
Andy「浜松町」 | |
パンパン! | |
Brian「日暮里」 | |
パンパン! | |
Andy「大塚」 | |
パンパン! | |
Brian「目白」 | |
パンパン! | |
Andy「品川」 | |
パンパン! | |
Brian「恵比寿」 | |
パンパン! | |
Andy「日暮里・・あっ!」 | |
*/ | |
// vim: set ts=4 sw=4 et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment