Skip to content

Instantly share code, notes, and snippets.

@jacoby
Created March 1, 2012 15:43
Show Gist options
  • Save jacoby/1950608 to your computer and use it in GitHub Desktop.
Save jacoby/1950608 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Rock Paper Scissors
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def rps_tournament_winner ( tourney )
# if tourney[0][0] is a string, that means that's a match
# if not
if tourney[0][0].is_a?( String )
player1 = tourney[0]
player2 = tourney[1]
puts "Player 1: " + player1.join( ',' )
puts "Player 2: " + player2.join( ',' )
round = [ player1 , player2 ]
winner = rps_game_winner( round )
puts " Winner: " + winner.join(':')
return winner
else
puts "######### ######### Bracket vvvvvv"
player1 = rps_tournament_winner( tourney[0] )
player2 = rps_tournament_winner( tourney[1] )
puts "Player 1: " + player1.join( ',' )
puts "Player 2: " + player2.join( ',' )
round = [ player1 , player2 ]
winner = rps_game_winner( round )
puts " Winner: " + winner.join(':')
puts "######### ######### Bracket ^^^^^"
return winner
end
end
def rps_game_winner ( game )
raise WrongNumberOfPlayersError unless game.length == 2
player1 = game[0]
player2 = game[1]
puts "11111111: " + player1.join( ',' )
puts "22222222: " + player2.join( ',' )
raise NoSuchStrategyError unless player1[1] == "P" || player1[1] == "S" || player1[1] == "R"
raise NoSuchStrategyError unless player2[1] == "P" || player2[1] == "S" || player2[1] == "R"
# player1 wins tie
if player1[1] == "R"
if player2[1] == "R"
return player1
elsif player2[1] == "P"
return player2
elsif player2[1] == "S"
return player1
end
elsif player1[1] == "P"
if player2[1] == "R"
return player1
elsif player2[1] == "P"
return player1
elsif player2[1] == "S"
return player2
end
elsif player1[1] == "S"
if player2[2] == "R"
return player1
elsif player2[1] == "P"
return player1
elsif player2[1] == "S"
return player1
end
end
return 'impossible','impossible'
end
tourney = [
[
[ [ "Armando" , "P" ] , [ "Dave" , "S" ] ],
[ [ "Richard" , "R" ] , [ "Michael" , "S" ] ]
],
[
[ [ "Allen" , "S" ] , [ "Omer" , "P" ] ],
[ [ "David E." , "R" ] , [ "Richard X." , "P" ] ]
]
]
puts rps_tournament_winner( tourney )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment