Skip to content

Instantly share code, notes, and snippets.

@bufferings
Created December 27, 2014 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bufferings/16ce9a07129522ab7bca to your computer and use it in GitHub Desktop.
Save bufferings/16ce9a07129522ab7bca to your computer and use it in GitHub Desktop.
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 結果 対(手 相手の手);
}
}
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