Created
December 27, 2014 03:27
-
-
Save bufferings/16ce9a07129522ab7bca to your computer and use it in GitHub Desktop.
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
package janken; | |
public class じゃんけん { | |
public enum 結果 { | |
勝ち, 負け, あいこ | |
} | |
public enum 手 { | |
グー { | |
@Override | |
public 結果 対(手 相手の手) { | |
if (相手の手 == null) { | |
throw new NullPointerException(); | |
} | |
switch (相手の手) { | |
case グー: | |
return 結果.あいこ; | |
case チョキ: | |
return 結果.勝ち; | |
case パー: | |
return 結果.負け; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
} | |
}, | |
チョキ { | |
@Override | |
public 結果 対(手 相手の手) { | |
if (相手の手 == null) { | |
throw new NullPointerException(); | |
} | |
switch (相手の手) { | |
case グー: | |
return 結果.負け; | |
case チョキ: | |
return 結果.あいこ; | |
case パー: | |
return 結果.勝ち; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
} | |
}, | |
パー { | |
@Override | |
public 結果 対(手 相手の手) { | |
if (相手の手 == null) { | |
throw new NullPointerException(); | |
} | |
switch (相手の手) { | |
case グー: | |
return 結果.勝ち; | |
case チョキ: | |
return 結果.負け; | |
case パー: | |
return 結果.あいこ; | |
default: | |
throw new IllegalArgumentException(); | |
} | |
} | |
}; | |
public abstract 結果 対(手 相手の手); | |
} | |
} |
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
package janken | |
import spock.lang.Specification | |
import spock.lang.Unroll | |
import static janken.じゃんけん.手.* | |
import static janken.じゃんけん.結果.* | |
class じゃんけんSpec extends Specification { | |
@Unroll | |
def "僕が#iで相手が#uのとき結果は#resultになる"() { | |
expect: | |
i.対(u) == result | |
where: | |
i | u || result | |
グー | グー || あいこ | |
グー | チョキ || 勝ち | |
グー | パー || 負け | |
チョキ | グー || 負け | |
チョキ | チョキ || あいこ | |
チョキ | パー || 勝ち | |
パー | グー || 勝ち | |
パー | チョキ || 負け | |
パー | パー || あいこ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment