Rock-paper-scissors: Difference between revisions

→‎{{header|Python}}: Less than half the length of the old program. New version more Pythonic, old version felt Java-focused.
m (→‎{{header|Sidef}}: code simplifications)
(→‎{{header|Python}}: Less than half the length of the old program. New version more Pythonic, old version felt Java-focused.)
Line 3,277:
 
=={{header|Python}}==
A light version with limited support for additional rules. New weapons can be introduced, but each can only be beaten by one other weapon.
<lang python>#!/usr/bin/python
The <code>rules</code> dictionary is of the form <code>'this': beaten by 'that', etc</code> as opposed to <code>'this': beats 'that'</code>.
from random import choice, randrange
from bisect import bisect
from collections import defaultdict
 
<lang python>from random import choice, randrange
WHATBEATS = { 'paper' : 'scissors',
'scissors' : 'rock',
'rock' : 'paper' }
 
ORDERrules = ({'rock',: 'paper', 'scissors'): 'rock', 'paper': 'scissors'}
WHATBEATSprevious = { ['rock', 'paper' :, 'scissors',]
 
while True:
CHOICEFREQUENCY = defaultdict(int)
human = input('\nchoose your weapon: ')
 
computer = rules[choice(previous)] # choose the weapon which beats a randomly chosen weapon from "previous"
def probChoice(choices, probabilities):
print('the computer played', computer, end='; ')
total = sum(probabilities)
prob_accumulator = 0
accumulator = []
for p in probabilities:
prob_accumulator += p
accumulator.append(prob_accumulator)
r = randrange(total)
bsct = bisect(accumulator, r)
chc = choices[bsct]
return chc
 
if human in ('quit', 'exit'): break
def checkWinner(a, b):
if b == WHATBEATS[a]:
return b
elif a == WHATBEATS[b]:
return a
 
elif human in rules:
return None
previous.append(human) # add human's choice to "previous"
if rules[computer] == human: # if what beats the computer's choice is the human's choice...
print('yay you win!')
elif rules[human] == computer: # if what beats the human's choice is the computer's choice...
print('the computer beat you... :(')
return belse:
'scissorsprint("it's :a 'rock',tie!")
 
else: print("that's not a valid choice")</lang>
def sanitizeChoice(a):
# Drop it to lower-case
return a.lower()
 
Output, where player always chooses Rock:
def registerPlayerChoice(choice):
CHOICEFREQUENCY[choice] += 1
 
<pre>choose your weapon: rock
def getRandomChoice():
the computer played rock; it's a tie!
if len(CHOICEFREQUENCY) == 0:
return choice(ORDER)
choices = CHOICEFREQUENCY.keys()
probabilities = CHOICEFREQUENCY.values()
return WHATBEATS[probChoice(choices, probabilities)]
 
choose your weapon: rock
while True:
the computer played scissors; yay you win!
humanChoice = raw_input()
humanChoice = sanitizeChoice(humanChoice)
if humanChoice not in ORDER:
continue
 
choose your weapon: rock
compChoice = getRandomChoice()
the computer played paper; the computer beat you... :(
print "Computer picked", compChoice+",",
 
choose your weapon: rock
# Don't register the player choice until after the computer has made
the computer played paper; the computer beat you... :(
# its choice.
registerPlayerChoice(humanChoice)
 
winner = checkWinner(humanChoice, compChoice)
 
if winner == None:
winner = "nobody"
 
print winner, "wins!"</lang>
 
Output, where player always chooses Rock:
 
choose your weapon: rock
<pre>!504 #5 j0 ?0 $ ./rps.py
the computer played paper; the computer beat you... :(</pre>
rock
Computer picked scissors, rock wins!
rock
Computer picked paper, paper wins!
rock
Computer picked paper, paper wins!
rock
Computer picked paper, paper wins!
rock
Computer picked paper, paper wins!</pre>
 
=={{header|Rascal}}==