Instantly share code, notes, and snippets.

Embed
What would you like to do?
Groovy version do JokenpoSheldon
/*
VERSÃO PARA SERES HUMANOS
No dia 18/03/2015 a Galera do DojoRio resolveuresolver o problema o jokenpo elevado a sheldon cooper. O resultado ofi um dojo com várias risadas e um resultado bem antes das 211:00 da noite.
*/
plays = ['scissor', 'paper', 'rock', 'lizard', 'spock']
def play(play1, play2){
warning = {i, n -> (i + n) % 5}
position = plays.indexOf(play1)
play1 == play2 ? 'draw' : play2 in [plays[warning(position, 1)] , plays[warning(position, 3)]] ? 'play1' : 'play2'
}
//TESTS OK
assert play('scissor', 'scissor') == 'draw'
assert play('scissor', 'paper') == 'play1'
assert play('scissor', 'lizard') == 'play1'
assert play('lizard', 'scissor') == 'play2'
assert play('paper', 'paper') == 'draw'
assert play('paper', 'rock') == 'play1'
assert play('paper', 'spock') == 'play1'
assert play('rock', 'paper') == 'play2'
assert play('rock', 'rock') == 'draw'
assert play('rock', 'lizard') == 'play1'
assert play('rock', 'scissor') == 'play1'
assert play('scissor', 'rock') == 'play2'
assert play('lizard', 'lizard') == 'draw'
assert play('lizard', 'spock') == 'play1'
assert play('lizard', 'paper') == 'play1'
assert play('spock', 'lizard') == 'play2'
assert play('spock', 'spock') == 'draw'
assert play('spock', 'scissor') == 'play1'
assert play('spock', 'rock') == 'play1'
assert play('rock', 'spock') == 'play2'
//GROOVY E SEU MOMENTO NERD
//Versão minimificada da mesma funcionalidade
l = ['scissor', 'paper', 'rock', 'lizard', 'spock']
p={p1,p2,c={a,b->l[(l.indexOf(a)+b)%5]}->p1==p2?'eq':p2 in [c(p1, 1),c(p1,3)]?'p1':'p2'}
//TESTS OK
assert p('scissor', 'scissor') == 'eq'
assert p('scissor', 'paper') == 'p1'
assert p('scissor', 'lizard') == 'p1'
assert p('lizard', 'scissor') == 'p2'
assert p('paper', 'paper') == 'eq'
assert p('paper', 'rock') == 'p1'
assert p('paper', 'spock') == 'p1'
assert p('rock', 'paper') == 'p2'
assert p('rock', 'rock') == 'eq'
assert p('rock', 'lizard') == 'p1'
assert p('rock', 'scissor') == 'p1'
assert p('scissor', 'rock') == 'p2'
assert p('lizard', 'lizard') == 'eq'
assert p('lizard', 'spock') == 'p1'
assert p('lizard', 'paper') == 'p1'
assert p('spock', 'lizard') == 'p2'
assert p('spock', 'spock') == 'eq'
assert p('spock', 'scissor') == 'p1'
assert p('spock', 'rock') == 'p1'
assert p('rock', 'spock') == 'p2'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment