Rock-paper-scissors: Difference between revisions
Content added Content deleted
(Added PicoLisp) |
(add Ruby) |
||
Line 910: | Line 910: | ||
rock |
rock |
||
Computer picked paper, paper wins!</pre> |
Computer picked paper, paper wins!</pre> |
||
=={{header|Ruby}}== |
|||
<lang ruby>class RockPaperScissorsGame |
|||
def initialize() |
|||
@pieces = %w[rock paper scissors] |
|||
@beats = { |
|||
'rock' => 'paper', |
|||
'paper' => 'scissors', |
|||
'scissors' => 'rock', |
|||
} |
|||
@plays = { |
|||
'rock' => 1, |
|||
'paper' => 1, |
|||
'scissors' => 1, |
|||
} |
|||
@score = [0, 0] |
|||
play |
|||
end |
|||
def humanPlay() |
|||
answer = nil |
|||
loop do |
|||
print "\nYour choice: #@pieces? " |
|||
answer = STDIN.gets.strip.downcase |
|||
next if answer.empty? |
|||
if idx = @pieces.find_index {|piece| piece.match(/^#{answer}/)} |
|||
answer = @pieces[idx] |
|||
break |
|||
else |
|||
puts "invalid answer, try again" |
|||
end |
|||
end |
|||
print "H: #{answer}, " |
|||
answer |
|||
end |
|||
def computerPlay() |
|||
total = @plays.values.reduce(:+) |
|||
r = rand(total) + 1 |
|||
sum = 0 |
|||
humans_choice = nil |
|||
@pieces.each do |piece| |
|||
sum += @plays[piece] |
|||
if r <= sum |
|||
humans_choice = piece |
|||
break |
|||
end |
|||
end |
|||
choice = @beats[humans_choice] |
|||
print "C: #{choice} => " |
|||
choice |
|||
end |
|||
def play |
|||
loop do |
|||
h = humanPlay |
|||
c = computerPlay |
|||
# only update the human player's history after the computer has chosen |
|||
@plays[h] += 1 |
|||
if h == c |
|||
puts "draw" |
|||
elsif h == @beats[c] |
|||
puts "Human wins" |
|||
@score[0] += 1 |
|||
else |
|||
puts "Computer wins" |
|||
@score[1] += 1 |
|||
end |
|||
puts "score: human=#{@score[0]}, computer=#{@score[1]}" |
|||
end |
|||
end |
|||
end |
|||
game = RockPaperScissorsGame.new</lang> |
|||
sample game where human always chooses rock: |
|||
<pre style="height: 40ex; overflow: scroll">Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: rock => draw |
|||
score: human=0, computer=0 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=1 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: rock => draw |
|||
score: human=0, computer=1 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=2 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=3 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=4 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=5 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=6 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=7 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=8 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=9 |
|||
Your choice: ["rock", "paper", "scissors"]? r |
|||
H: rock, C: paper => Computer wins |
|||
score: human=0, computer=10</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |