Created
July 15, 2011 20:01
-
-
Save alecperkins/1085437 to your computer and use it in GitHub Desktop.
3-player ROCKPAPERSCISSORSLIZARDSPOCK, a Pad Hacker test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1 ROCKPAPERSCISSORSLIZARDSPOCK | |
%button GO | |
%table | |
%thead | |
%tbody |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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