Skip to content

Instantly share code, notes, and snippets.

@alecperkins
Created July 15, 2011 20:01
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 alecperkins/1085437 to your computer and use it in GitHub Desktop.
Save alecperkins/1085437 to your computer and use it in GitHub Desktop.
3-player ROCKPAPERSCISSORSLIZARDSPOCK, a Pad Hacker test
choices = [
{ name: 'rock', beats: ['scissors', 'lizard'] }
{ name: 'paper', beats: ['rock', 'spock'] }
{ name: 'scissors', beats: ['paper', 'lizard'] }
{ name: 'lizard', beats: ['paper', 'spock'] }
{ name: 'spock', beats: ['rock', 'scissors'] }
]
tbody = $('tbody')
thead = $('thead')
players = (0 for i in [1..3])
num_plays = 0
runRound = ->
num_plays += 1
plays = []
for player in players
choice_index = Math.floor(Math.random() * choices.length)
choice = _.clone(choices[choice_index])
plays.push(choice)
round_wins = (0 for i in [1..3])
_.each plays, (play, i) ->
_.each _.without(plays, play), (other, k) ->
if _.detect(play.beats, (item) -> item is other.name)?
players[i] += 1
round_wins[i] += 1
header = '<tr><td></td>'
_.each players, (wins, i) ->
header += "<td>#{ wins }</td>"
header += '</tr>'
thead.html(header)
row = "<tr><td>#{ num_plays }.</td>"
_.each plays, (play, i) ->
row += "<td class='wins-#{ round_wins[i] }'>#{ play.name }</td>"
row += '</tr>'
tbody.prepend(row)
$('button').click ->
runRound()
%h1 ROCKPAPERSCISSORSLIZARDSPOCK
%button GO
%table
%thead
%tbody
td
:padding 10px
:text-align center
.wins-0
:background rgba(255,0,0,0.1)
.wins-1
:background rgba(0,0,255,0.1)
.wins-2
:background rgba(0,255,0,0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment