Anonymous user
Penney's game: Difference between revisions
→{{Header|Ruby}}: optimized the choice of the computer.
(→{{header|UNIX Shell}}: added zkl) |
(→{{Header|Ruby}}: optimized the choice of the computer.) |
||
Line 1,554:
=={{Header|Ruby}}==
<lang ruby>
def yourChoice
puts "Enter your choice (H/T)"
Line 1,570 ⟶ 1,568:
end
if coin == Toss[0]▼
myC = Array.new(3){Toss.sample}▼
puts "I chose #{myC.join(' ')}"▼
yC = yourChoice▼
else▼
yC = yourChoice▼
myC = Toss - [yC[1]] + yC.first(2)▼
end▼
print seq.join(' ')▼
loop do
puts "\n%s I
▲ if coin == Toss[0]
puts "\n You win!" or break if seq == yC▼
▲ yC = yourChoice
▲ else
▲ yC = yourChoice
▲ myC = Toss - [yC[1]] + yC.first(2)
▲ puts "I chose #{myC.join(' ')}"
▲ end
▲ print seq.join(' ')
loop do
puts "\n I win!" or break if seq == myC
▲ puts "\n You win!" or break if seq == yC
seq.push(Toss.sample).shift
print " #{seq[-1]}"
end
end</lang>
|