Created
August 2, 2013 13:38
-
-
Save grimrose/6139953 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
package tddbc | |
import groovy.transform.Canonical | |
enum Hand { | |
ROCK, | |
PAPER, | |
SCISSORS | |
} | |
@Canonical | |
class Player { | |
Hand hand | |
Result result | |
} | |
enum Result { | |
WIN, | |
ROSE, | |
EVEN | |
} | |
@Canonical | |
class Game { | |
Player defender | |
Player challenger | |
Player getWinner() { | |
if (this.defender.result.is(Result.WIN)) return this.defender | |
if (this.defender.result.is(Result.EVEN)) return this.defender | |
this.challenger | |
} | |
} | |
class Observer { | |
def notify = { Game game -> | |
game.defender.result = judge(game.defender.hand, game.challenger.hand) | |
switch (game.defender.result) { | |
case Result.WIN: | |
game.challenger.result = Result.ROSE | |
break | |
case Result.ROSE: | |
game.challenger.result = Result.WIN | |
break | |
default: | |
game.challenger.result = Result.EVEN | |
break | |
} | |
game | |
} | |
def judge = { Hand defender, Hand challenger -> | |
switch (defender) { | |
case Hand.ROCK: | |
if (challenger.is(Hand.SCISSORS)) return Result.WIN | |
if (challenger.is(Hand.PAPER)) return Result.ROSE | |
return Result.EVEN | |
case Hand.PAPER: | |
if (challenger.is(Hand.ROCK)) return Result.WIN | |
if (challenger.is(Hand.SCISSORS)) return Result.ROSE | |
return Result.EVEN | |
case Hand.SCISSORS: | |
if (challenger.is(Hand.PAPER)) return Result.WIN | |
if (challenger.is(Hand.ROCK)) return Result.ROSE | |
return Result.EVEN | |
} | |
} | |
} | |
def challenger = new Player(hand: Hand.ROCK) | |
def defender = new Player(hand: Hand.PAPER) | |
def game = new Game(challenger: challenger, defender: defender) | |
def result = new Observer().notify game | |
assert result.winner == defender |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment