Penney's game: Difference between revisions
Content added Content deleted
(→{{header|UNIX Shell}}: added zkl) |
(→{{Header|Ruby}}: optimized the choice of the computer.) |
||
Line 1,554: | Line 1,554: | ||
=={{Header|Ruby}}== |
=={{Header|Ruby}}== |
||
<lang ruby> |
<lang ruby>Toss = [:Heads, :Tails] |
||
Toss = [:Heads, :Tails] |
|||
def yourChoice |
def yourChoice |
||
puts "Enter your choice (H/T)" |
puts "Enter your choice (H/T)" |
||
Line 1,570: | Line 1,568: | ||
end |
end |
||
puts "%s I start, %s you start ..... #{coin = Toss.sample}" % Toss |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
puts "I chose #{myC.join(' ')}" |
|||
⚫ | |||
seq = Array.new(3){Toss.sample} |
|||
⚫ | |||
loop do |
loop do |
||
puts "\n I |
puts "\n%s I start, %s you start ..... %s" % [*Toss, coin = Toss.sample] |
||
⚫ | |||
⚫ | |||
myC = Toss.shuffle << Toss.sample |
|||
puts "I chose #{myC.join(' ')}" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
loop do |
|||
puts "\n I win!" or break if seq == myC |
|||
⚫ | |||
seq.push(Toss.sample).shift |
|||
print " #{seq[-1]}" |
|||
end |
|||
end</lang> |
end</lang> |
||