JGGUGのイベントではおなじみの「Groovyじゃんけん」を実際に作ってみました。
import static Hands.* enum Hands{ GOO,CHOKI,PAR; public result = [:].withDefault{"$this is even with $this"} } def rule(Hands x) { [defeats:{Hands y-> x.result[y]="$x beats $y" y.result[x]="$y is beaten by $x"}] } def janken(Hands x) { [with:{Hands y-> println x.result[y]}] } rule GOO defeats CHOKI rule PAR defeats GOO rule CHOKI defeats PAR janken GOO with CHOKI // ==> GOO beats CHOKI janken CHOKI with GOO // ==> CHOKI is beaten by GOO janken GOO with PAR // ==> GOO is beaten by PAR janken GOO with GOO // ==> GOO is even with GOO
「Groovyじゃ〜んけ〜ん」と言った後に高速で上のコードを唱えて先に言い終った方が勝ちです。
Groovy 1.8以上で動作します。