Rock-paper-scissors: Difference between revisions

→‎{{header|Ruby}}: constant CHOICES,BEATS, add quit choice, output draw and chosen
(add special trampoline usage information to Clojure example)
(→‎{{header|Ruby}}: constant CHOICES,BEATS, add quit choice, output draw and chosen)
Line 3,603:
=={{header|Ruby}}==
<lang ruby>class RockPaperScissorsGame
@piecesCHOICES = %w[rock paper scissors quit]
@beatsBEATS = {
'rock' => 'paper',
'paper' => 'scissors',
'scissors' => 'rock',
}
def initialize()
@pieces = %w[rock paper scissors]
@beats = {
'rock' => 'paper',
'paper' => 'scissors',
'scissors' => 'rock',
}
@plays = {
'rock' => 1,
'paper' => 1,
'scissors' => 1,
}
@score = [0, 0, 0] # [0]:Human wins, [1]:Computer wins, [2]:draw
end
 
play
end
 
def humanPlay()
answer = nil
loop do
print "\nYour choice: #@pieces{CHOICES}? "
answer = STDIN.gets.strip.downcase
next if answer.empty?
if idx = @piecesCHOICES.find_index {|piecechoice| piecechoice.match(/^#{answer}/)}
return answer = @piecesCHOICES[idx] if idx
puts "invalid breakanswer, try again"
else
puts "invalid answer, try again"
end
end
answer
end
 
def computerPlay()
total = @plays.values.reduce(:+)
r = rand(total) + 1
sum = 0
humans_choiceCHOICES.each =do nil|choice|
sum += @plays[choice]
@pieces.each do |piece|
sumreturn += @playsBEATS[piecechoice] if r <= sum
if r <= sum
humans_choice = piece
break
end
end
@beats[humans_choice]
end
 
def play
loop do
h = humanPlay
break if rh <== sum"quit"
c = computerPlay
print "H: #{h}, C: #{c} => "
else
 
# only update the human player's history after the computer has chosen
@plays[h] += 1
break
 
if h == c
puts "draw"
elsif h == @beatsscore[c2] += 1
elsif h == BEATS[c]
puts "Human wins"
@score[0] += 1
Line 3,669 ⟶ 3,662:
@score[1] += 1
end
puts "score: human=#{@score[0]}%d, computer=#{%d, draw=%d" % [*@score[1]}"
end
@plays.each_key{|k| @plays[k] -= 1}
puts "\nhumans chose #{@plays}"
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
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: rock => draw
score: human=0, computer=0, draw=1
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paperscissors => ComputerHuman wins
score: human=01, computer=0, draw=1
 
Your choice: ["rock", "paper", "scissors"]? r
H: rock, C: rock => draw
score: human=0, computer=1
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paper => Computer wins
score: human=01, computer=21, draw=1
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paper => Computer wins
score: human=01, computer=32, draw=1
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paper => Computer wins
score: human=01, computer=43, draw=1
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paper => Computer wins
score: human=01, computer=54, draw=1
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paper => Computer wins
score: human=01, computer=65, draw=1
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paper => Computer wins
score: human=01, computer=76, draw=1
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paperrock => Computer winsdraw
score: human=01, computer=86, draw=2
 
Your choice: ["rock", "paper", "scissors", "quit"]? r
H: rock, C: paper => Computer wins
score: human=01, computer=97, draw=2
 
Your choice: ["rock", "paper", "scissors", "quit"]? rq
 
H: rock, C: paper => Computer wins
humans chose {"rock"=>10, "paper"=>0, "scissors"=>0}
score: human=0, computer=10</pre>
</pre>
 
=={{header|Run BASIC}}==
Anonymous user