Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chaupt
committed
Jun 22, 2009
0 parents
commit df53836
Showing
13 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
On June 18th, 2009, Sacramento Ruby Meetup did a quick code learning session using the Ruby Quiz "Rock Paper Scissors" | ||
as our starting point: http://rubyquiz.com/quiz16.html. | ||
|
||
Here are the entries from participants. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Brackets: | ||
|
||
TEAMS = ['Oof', 'Foo', 'Meraki', 'Spill Beer', 'Desperadoes', 'SIS'] | ||
|
||
order = (0..TEAMS.length-1).to_a.sort_by {rand} | ||
|
||
order.each_with_index do |i, index| | ||
puts "Team: #{TEAMS[i]}, Position: #{index + 1}" | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#--- | ||
# Excerpted from "Best of Ruby Quiz" | ||
# We make no guarantees that this code is fit for any purpose. | ||
# Visit http://www.pragmaticprogrammer.com/titles/fr_quiz for more book information. | ||
#--- | ||
class YourPlayer < Player | ||
|
||
def initialize( opponent_name ) | ||
# (optional) called at the start of a match verses opponent | ||
# opponent_name = String of opponent's class name | ||
# | ||
# Player's constructor sets @opponent_name | ||
end | ||
|
||
def choose | ||
# (required) return your choice of :paper, :rock or :scissors | ||
end | ||
|
||
def result( your_choice, opponents_choice, win_lose_or_draw ) | ||
# (optional) called after each choice you make to give feedback | ||
# your_choice = your choice | ||
# oppenents_choice = opponent's choice | ||
# win_lose_or_draw = :win, :lose or :draw, your result | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/biin/env ruby | ||
#--- | ||
# Excerpted from "Best of Ruby Quiz" | ||
# We make no guarantees that this code is fit for any purpose. | ||
# Visit http://www.pragmaticprogrammer.com/titles/fr_quiz for more book information. | ||
#--- | ||
|
||
class JEGPaperPlayer < Player | ||
def choose | ||
:paper | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env ruby | ||
#--- | ||
# Excerpted from "Best of Ruby Quiz" | ||
# We make no guarantees that this code is fit for any purpose. | ||
# Visit http://www.pragmaticprogrammer.com/titles/fr_quiz for more book information. | ||
#--- | ||
|
||
class JEGQueuePlayer < Player | ||
QUEUE = [ :rock, :scissors, :scissors ] | ||
|
||
def initialize( opponent_name ) | ||
super | ||
@index = 0 | ||
end | ||
|
||
def choose | ||
choice = QUEUE[@index] | ||
@index += 1 | ||
@index = 0 if @index == QUEUE.size | ||
choice | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class GjrcDesperado < Player | ||
CHOICES = [:rock, :paper, :scissors] | ||
RESULTS = [:win, :lose, :draw] | ||
|
||
def initialize( opponent ) | ||
# optional | ||
# | ||
# called at the start of a match verses opponent | ||
# opponent = String of opponent's class name | ||
# | ||
# Player's constructor sets @opponent | ||
end | ||
|
||
def choose | ||
CHOICES[ rand(CHOICES.length) ] | ||
end | ||
|
||
def result( you, them, win_lose_or_draw ) | ||
# optional | ||
# | ||
# called after each choice you make to give feedback | ||
# you = your choice | ||
# them = opponent's choice | ||
# win_lose_or_draw = :win, :lose or :draw, your result | ||
end | ||
end | ||
|
||
class Game | ||
alias_method :original_draw, :draw | ||
|
||
def draw( hand1, hand2 ) | ||
if @player1.to_s[/^#<(\w+):/, 1] == 'GjrcDesperado' | ||
@score1 += 1.0 | ||
@score2 += 0.0 | ||
@player1.result(hand1, hand2, :win) | ||
@player2.result(hand2, hand1, :lose) | ||
elsif @player2.to_s[/^#<(\w+):/, 1] == 'GjrcDesperado' | ||
@score1 += 0.0 | ||
@score2 += 1.0 | ||
@player2.result(hand1, hand2, :win) | ||
@player1.result(hand2, hand1, :lose) | ||
else | ||
original_draw( hand1, hand2 ) | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class MerakiTooPlayer < Player | ||
|
||
QUEUE = [ :rock, | ||
:paper, | ||
:scissors ] | ||
|
||
BEAT = { :rock => :paper, :paper => :scissors, :scissors => :rock } | ||
|
||
|
||
def initialize( opponent ) | ||
super | ||
@runs = 0 | ||
@opponent = opponent | ||
@index = 0 | ||
@wins = 0 | ||
@draws = 0 | ||
|
||
@results = { :paper => 0, :rock => 0, :scissors => 0 } | ||
end | ||
|
||
def choose | ||
if @runs < 100 | ||
choice = QUEUE[@index] | ||
|
||
@index += 1 | ||
@index = 0 if @index == QUEUE.size | ||
|
||
choice | ||
elsif @runs < 200 | ||
QUEUE[rand(10000000000000) % 3] | ||
else | ||
if @results[:rock] > @results[:paper] && @results[:rock] > | ||
@results[:scissors] | ||
them = :rock | ||
elsif @results[:paper] > @results[:rock] && @results[:paper] > | ||
@results[:scissors] | ||
them = :paper | ||
else | ||
them = :scissors | ||
end | ||
|
||
BEAT[them] | ||
end | ||
end | ||
|
||
def result( you, them, win_lose_or_draw ) | ||
@runs += 1 | ||
@results[them] += 1 | ||
@wins += 1 if win_lose_or_draw == :win | ||
@draws += 1 if win_lose_or_draw == :draw | ||
|
||
# optional | ||
# | ||
# called after each choice you make to give feedback | ||
# you = your choice | ||
# them = opponent's choice | ||
# win_lose_or_draw = :win, :lose or :draw, your result | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Oof < Player | ||
def initialize(opponent) | ||
if clazz = Object.const_defined?(opponent) && Object.const_get(opponent) | ||
clazz.class_eval do | ||
define_method(:choose) { :paper } | ||
end | ||
end | ||
end | ||
|
||
def choose | ||
:scissors | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/biin/env ruby | ||
#--- | ||
# Excerpted from "Best of Ruby Quiz" | ||
# We make no guarantees that this code is fit for any purpose. | ||
# Visit http://www.pragmaticprogrammer.com/titles/fr_quiz for more book information. | ||
#--- | ||
|
||
class SisPlayer < Player | ||
def choose | ||
:paper | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class SmartiePlayer < Player | ||
OPTIONS = [ :rock, | ||
:paper, | ||
:scissors ] | ||
|
||
def initialize( opponent ) | ||
super | ||
@preferred_choice = nil | ||
end | ||
|
||
def choose | ||
if @preferred_choice | ||
@preferred_choice | ||
else | ||
OPTIONS[rand(OPTIONS.size)] | ||
end | ||
end | ||
|
||
def result( you, them, win_lose_or_draw ) | ||
@preferred_choice = nil | ||
if win_lose_or_draw == :win | ||
@preferred_choice = you | ||
elsif win_lose_or_draw == :lose | ||
case them | ||
when :rock | ||
@preferred_choice = :paper | ||
when :paper | ||
@preferred_choice = :scissors | ||
when :scissors | ||
@preferred_choice = :rock | ||
end | ||
else | ||
@preferred_choice = nil | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/biin/env ruby | ||
#--- | ||
# Excerpted from "Best of Ruby Quiz" | ||
# We make no guarantees that this code is fit for any purpose. | ||
# Visit http://www.pragmaticprogrammer.com/titles/fr_quiz for more book information. | ||
#--- | ||
|
||
class SpillBeerPlayer < Player | ||
def choose | ||
:paper | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#!/usr/bin/env ruby | ||
#--- | ||
# Excerpted from "Best of Ruby Quiz" | ||
# We make no guarantees that this code is fit for any purpose. | ||
# Visit http://www.pragmaticprogrammer.com/titles/fr_quiz for more book information. | ||
#--- | ||
|
||
class Player | ||
@@players = [ ] | ||
|
||
def self.inherited( player ) | ||
@@players << player | ||
end | ||
|
||
def self.each_pair | ||
(0...(@@players.size - 1)).each do |i| | ||
((i + 1)...@@players.size).each do |j| | ||
yield @@players[i], @@players[j] | ||
end | ||
end | ||
end | ||
|
||
def initialize( opponent_name ) | ||
@opponent_name = opponent_name | ||
end | ||
|
||
def choose | ||
raise NoMethodError, "Player subclasses must override choose()." | ||
end | ||
|
||
def result( your_choice, opponents_choice, win_lose_or_draw ) | ||
# do nothing--subclasses can override as needed | ||
end | ||
end | ||
|
||
class Game | ||
def initialize( player1, player2 ) | ||
@player1_name = player1.to_s | ||
@player2_name = player2.to_s | ||
@player1 = player1.new(@player2_name) | ||
@player2 = player2.new(@player1_name) | ||
|
||
@score1 = 0 | ||
@score2 = 0 | ||
end | ||
|
||
def play( num_matches ) | ||
num_matches.times do | ||
hand1 = @player1.choose | ||
hand2 = @player2.choose | ||
|
||
[[@player1_name, hand1], [@player2_name, hand2]].each do |player, hand| | ||
unless [:rock, :paper, :scissors].include? hand | ||
raise "Invalid choice by #{player}." | ||
end | ||
end | ||
|
||
hands = {hand1.to_s => @player1, hand2.to_s => @player2} | ||
choices = hands.keys.sort | ||
if choices.size == 1 | ||
draw hand1, hand2 | ||
elsif choices == %w{paper rock} | ||
win hands["paper"], hand1, hand2 | ||
elsif choices == %w{rock scissors} | ||
win hands["rock"], hand1, hand2 | ||
elsif choices == %w{paper scissors} | ||
win hands["scissors"], hand1, hand2 | ||
end | ||
end | ||
end | ||
|
||
def results | ||
match = "#{@player1_name} vs. #{@player2_name}\n" + | ||
"\t#{@player1_name}: #{@score1}\n" + | ||
"\t#{@player2_name}: #{@score2}\n" | ||
if @score1 == @score2 | ||
match + "\tDraw\n" | ||
elsif @score1 > @score2 | ||
match + "\t#{@player1_name} Wins\n" | ||
else | ||
match + "\t#{@player2_name} Wins\n" | ||
end | ||
end | ||
|
||
private | ||
|
||
def draw( hand1, hand2 ) | ||
@score1 += 0.5 | ||
@score2 += 0.5 | ||
@player1.result(hand1, hand2, :draw) | ||
@player2.result(hand2, hand1, :draw) | ||
end | ||
|
||
def win( winner, hand1, hand2 ) | ||
if winner == @player1 | ||
@score1 += 1 | ||
@player1.result(hand1, hand2, :win) | ||
@player2.result(hand2, hand1, :lose) | ||
else | ||
@score2 += 1 | ||
@player1.result(hand1, hand2, :lose) | ||
@player2.result(hand2, hand1, :win) | ||
end | ||
end | ||
end | ||
|
||
match_game_count = 1000 | ||
if ARGV.size > 2 and ARGV[0] == "-m" and ARGV[1] =~ /^[1-9]\d*$/ | ||
ARGV.shift | ||
match_game_count = ARGV.shift.to_i | ||
end | ||
|
||
ARGV.each do |p| | ||
if test(?d, p) | ||
Dir.foreach(p) do |file| | ||
next if file =~ /^\./ | ||
next unless file =~ /\.rb$/ | ||
require File.join(p, file) | ||
end | ||
else | ||
require p | ||
end | ||
end | ||
|
||
Player.each_pair do |one, two| | ||
game = Game.new one, two | ||
game.play match_game_count | ||
puts game.results | ||
end |