Wir haben dieses Spiel mit Arrays und Hashtabellen implementiert:
$gewinn = {'Papier' => 'Stein',
'Stein' => 'Schere', 'Schere' => 'Papier'}
$erlaubt = $gewinn.keys + ['Ende']
$score = {'computer' => 0, 'mensch' => 0}
def auswerten(menschAuswahl, computerAuswahl)
if menschAuswahl == computerAuswahl
print "Unentschieden "
elsif $gewinn[menschAuswahl] == computerAuswahl
$score['mensch'] += 1
print "Gewonnen "
else
$score['computer'] += 1
print "Verloren "
end
end
def status(m, c)
print "Es steht #{m} : #{c} "
print ((m>c) ? ':-)' : ':-('), "\n\n"
end
def input
puts "Auswahl: (#{$erlaubt.join(',')}) "
print "Deine Wahl? "
return STDIN.gets.chomp.capitalize
end
while eingabe = input
next unless $erlaubt.include?(eingabe)
break if eingabe == 'Ende'
computerAuswahl = $erlaubt[rand(3)]
auswerten(eingabe, computerAuswahl)
status($score['mensch'], $score['computer'])
end