Ruby Quiz <james / grayproductions.net> writes:

> Your task is to build a strategy for playing the game of Paper Rock Scissors
> against all manner of opponents.  The question here is if you can adapt to an
> opponent's strategy and seize the advantage, while he is doing the same to you
> of course.

First, congratulations to this great quiz.  It was a great fun for me
and other people on #ruby-lang.

I tested my players in this arena:

players/cn_bias_breaker.rb   players/cn_mean_player.rb
players/cn_bias_flipper.rb   players/cn_step_ahead.rb
players/cn_bias_inverter.rb  players/jegpaperplayer.rb
players/cn_irrflug.rb        players/jegqueueplayer.rb

Here's a description of all my bots and how often they usually win in
above arena.  They are ordered in my preference, so if you chose (for
reasons whatsoever) not to run all of them, please pick the first ones
first.

# CNBiasInverter: Choose so that your bias will be the inverted
#   opponent's bias.
(4-6 wins)

# CNIrrflug: Pick a random choice.  If you win, use it again; else,
#   use a random choice.
(4-6 wins)

# CNStepAhead: Try to think a step ahead.  If you win, use the choice
#   where you'd have lost.  If you lose, you the choice where you'd
#   have won.  Use the same on draw.
(3-5 wins)

# CNBiasFlipper: Always use the choice that hits what the opponent
#   said most or second-to-most often (if the most often choice is not
#   absolutely prefered).
(4 wins)

# CNBiasBreaker: Always use the choice that hits what the opponent
#   said most often.
(3-5 wins)

# CNMeanPlayer: Pick a random choice.  If you win, use it again; else,
#   use the opponent's choice.
(1-4 wins)


---

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	cn_bias_breaker.rb
#	cn_bias_flipper.rb
#	cn_bias_inverter.rb
#	cn_irrflug.rb
#	cn_mean_player.rb
#	cn_step_ahead.rb
#
echo x - cn_bias_breaker.rb
sed 's/^X//' >cn_bias_breaker.rb << 'END-of-cn_bias_breaker.rb'
X# CNBiasBreaker: Always use the choice that hits what the opponent
X#   said most often.
X
Xclass CNBiasBreaker < Player
X  def initialize(opponent)
X    super
X    @biases = {:rock => 0, :scissors => 0, :paper => 0}
X    @hit = {:rock => :paper, :paper => :scissors, :scissors => :rock}
X  end
X  
X  def choose
X    @hit[@biases.max {|a, b| a[1] <=> b[1]}.first]
X  end
X  
X  def result( you, them, win_lose_or_draw )
X    @biases[them] += 1
X  end
Xend
END-of-cn_bias_breaker.rb
echo x - cn_bias_flipper.rb
sed 's/^X//' >cn_bias_flipper.rb << 'END-of-cn_bias_flipper.rb'
X# CNBiasFlipper: Always use the choice that hits what the opponent
X#   said most or second-to-most often (if the most often choice is not
X#   absolutely prefered).
X
Xclass CNBiasFlipper < Player
X  def initialize(opponent)
X    super
X    @biases = {:rock => 0, :scissors => 0, :paper => 0}
X    @hit = {:rock => :paper, :paper => :scissors, :scissors => :rock}
X  end
X  
X  def choose
X    b = @biases.sort_by {|k, v| -v}
X    if b[0][1] > b[1][1]*1.5
X      @hit[b[0].first]
X    else
X      @hit[b[1].first]
X    end
X  end
X  
X  def result( you, them, win_lose_or_draw )
X    @biases[them] += 1
X  end
Xend
END-of-cn_bias_flipper.rb
echo x - cn_bias_inverter.rb
sed 's/^X//' >cn_bias_inverter.rb << 'END-of-cn_bias_inverter.rb'
X# CNBiasInverter: Choose so that your bias will be the inverted
X#   opponent's bias.
X
Xclass CNBiasInverter < Player
X  def initialize(opponent)
X    super
X    @biases = {:rock => 0, :scissors => 0, :paper => 0}
X    @hit = {:rock => :paper, :paper => :scissors, :scissors => :rock}
X  end
X  
X  def choose
X    n = ::Kernel.rand(@biases[:rock] + @biases[:scissors] + @biases[:paper]).to_i
X    case n
X    when 0..@biases[:rock]
X      :paper
X    when @biases[:rock]..@biases[:rock]+@biases[:scissors]
X      :rock
X    when @biases[:rock]+@biases[:scissors]..@biases[:rock]+@biases[:scissors]+@biases[:paper]
X      :scissors
X    else
X      p @biases[:rock]+@biases[:scissors]..@biases[:paper]
X      abort n.to_s
X    end
X  end
X  
X  def result( you, them, win_lose_or_draw )
X    @biases[them] += 1
X  end
Xend
END-of-cn_bias_inverter.rb
echo x - cn_irrflug.rb
sed 's/^X//' >cn_irrflug.rb << 'END-of-cn_irrflug.rb'
X# CNIrrflug: Pick a random choice.  If you win, use it again; else,
X#   use a random choice.
X
Xclass CNIrrflug < Player
X  def initialize(opponent)
X    super
X    @last_choice = [:rock, :scissors, :paper][rand(3)]
X  end
X  
X  def choose
X    @last_choice
X  end
X  
X  def result(you, them, win_lose_or_draw)
X    if win_lose_or_draw == :win
X      @last_choice = you
X    else
X      @last_choice = [:rock, :scissors, :paper][rand(3)]
X    end
X  end
Xend
END-of-cn_irrflug.rb
echo x - cn_mean_player.rb
sed 's/^X//' >cn_mean_player.rb << 'END-of-cn_mean_player.rb'
X# CNMeanPlayer: Pick a random choice.  If you win, use it again; else,
X#   use the opponent's choice.
X
Xclass CNMeanPlayer < Player
X  def initialize(opponent)
X    super
X    @last_choice = [:rock, :scissors, :paper][rand(3)]
X  end
X  
X  def choose
X    @last_choice
X  end
X  
X  def result(you, them, win_lose_or_draw)
X    if win_lose_or_draw == :win
X      @last_choice = you
X    else
X      @last_choice = them
X    end
X  end
Xend
END-of-cn_mean_player.rb
echo x - cn_step_ahead.rb
sed 's/^X//' >cn_step_ahead.rb << 'END-of-cn_step_ahead.rb'
X# CNStepAhead: Try to think a step ahead.  If you win, use the choice
X#   where you'd have lost.  If you lose, you the choice where you'd
X#   have won.  Use the same on draw.
X
Xclass CNStepAhead < Player
X  def initialize(opponent)
X    super
X    @choice = [:rock, :scissors, :paper][rand(3)]
X  end
X  
X  def choose
X    @choice
X  end
X  
X  def result(you, them, win_lose_or_draw)
X    case win_lose_or_draw
X    when :win
X      @choice = {:rock => :paper, :paper => :scissors, :scissors => :paper}[them]
X    when :lose
X      @choice = {:rock => :scissors, :scissors => :paper, :paper => :rock}[you]
X    end
X  end
Xend
END-of-cn_step_ahead.rb
exit


-- 
Christian Neukirchen  <chneukirchen / gmail.com>  http://chneukirchen.org