Skip to content

Instantly share code, notes, and snippets.

@RShergold
Last active August 29, 2015 14:07
Show Gist options
  • Save RShergold/34989e0301b84a906797 to your computer and use it in GitHub Desktop.
Save RShergold/34989e0301b84a906797 to your computer and use it in GitHub Desktop.
rock paper scissors javascript - code golf - small as possible
var moves = ['rock','paper','scissors'],
results = ['we draw','you loose','you win'],
user_move = moves.indexOf(prompt(moves+'?')),
computer_move,
result;
while (user_move > -1) {
computer_move = Math.round(Math.random()*2);
result = results[ (2*user_move+computer_move)%3 ];
alert( 'You choose: ' +moves[user_move]+ '\nI chose:' +moves[computer_move]+ '\n' +result);
user_move = moves.indexOf(prompt(moves+'?',moves[user_move]));
}
@RShergold
Copy link
Author

or even smaller in ruby!

moves = %w[rock paper scissors]
puts moves.join(' ')+'?'
yourMove = moves.index(gets.chomp).to_f
myMove = rand(3)
puts moves[myMove]
puts %w[draw lose win][(2*yourMove+myMove)%3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment