Created
July 27, 2011 02:54
-
-
Save seratch/1108560 to your computer and use it in GitHub Desktop.
Daimon.scala #8 やってみよう
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 #8 やってみよう | |
* http://d.hatena.ne.jp/seratch2/20110507/1304777166 | |
* 以下のPlayer、Gameをそれぞれアクターとして定義して山手線ゲームをコメント例のように実行できるようにしてください。 | |
*/ | |
import actors.Actor | |
case object Get | |
case class IsUsed(station: Station) | |
case class MarkAsUsed(station: Station) | |
case class MarkAsUsedResponse(isMarkedAsUsed: Boolean) | |
case class Station(name: String) | |
class Yamanotesen extends Actor { | |
start() | |
def act() = loop { | |
react { | |
case Get => sender ! randomStation | |
case IsUsed(station) => sender ! used.contains(station) | |
case MarkAsUsed(station) => { | |
used += station | |
sender ! MarkAsUsedResponse(used.contains(station)) | |
} | |
case _ => throw new IllegalArgumentException | |
} | |
} | |
def randomStation: Station = { | |
stations(new java.util.Random().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("神田") | |
) | |
import collection.mutable.{Set,HashSet} | |
private var used: Set[Station] = new HashSet[Station] | |
} | |
case object Start | |
case object End | |
case class Game(players: Player*) extends Actor { | |
start() | |
def act() = loop { | |
react { | |
case Start => this ! players(0) | |
case player: Player => { | |
println("パンパン!") | |
player ! yamanote | |
} | |
case (player: Player, station: Station) => { | |
val isUsed = (yamanote !? IsUsed(station)).asInstanceOf[Boolean] | |
if ( !isUsed ) { | |
println(player.name + "「" + station.name + "」") | |
yamanote !? MarkAsUsed(station) | |
this ! next(player) | |
} else { | |
println(player.name + "「" + station.name + "・・あっ!」") | |
this ! End | |
} | |
} | |
case End => System.exit(0) | |
case _ => throw new IllegalArgumentException | |
} | |
} | |
val yamanote = new Yamanotesen | |
def next(player: Player): Player = { | |
val idx = players.indexOf(player) | |
if (idx == players.length-1) players(0) else players(idx+1) | |
} | |
} | |
case class Player(name: String) extends Actor { | |
start() | |
def act() = loop { | |
react { | |
case yamanote: Yamanotesen => sender ! (this, yamanote !? Get) | |
case _ => throw new IllegalArgumentException | |
} | |
} | |
} | |
val Andy = new Player("Andy") | |
val Brian = new Player("Brian") | |
new Game(Andy,Brian) ! Start | |
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