[Tutor] rock, paper, scissors (ro sham bo) part deux
ahimsa
ahimsa@onetel.net.uk
Mon Feb 3 17:40:09 2003
--=-1dowTb3laGaVs1+b9qz5
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Sean, Magnus, Alan, Bob and Gregor
At the risk of belabouring this thread, I wanted to update you with
developments on my earlier requests for input and since you all so
graciously contributed something to helping me, I wanted to share where
it is at (to date).
I utilised Magnus's idea regarding turning the rock, paper, scissors
combination into a dictionary, and calling the key/value pair to
ascertain which wins.
I also wanted to allow the user to keep the game going rather than just
play once and have to press F5 again to start it off, which is why I
created the game as a module itself that can just be called.
Finally, I wanted to provide a little more feedback to the user - hence
all the stuff about the round summary.
I was hoping that I might be able to carry a round winner's total over
from one game to the next to give a series summary upon quitting, but
haven't figured out how to do that yet.
Anyway, have attached the code, and would naturally welcome comments.
It is probably too many lines for what the program actually does, so if
you have any ideas how parsimony can be achieved, please share them with
me, but I'd also appreciate it if you could explain what it is that you
are actually doing too - that would aid my learning.
Thanks folks.
Kind regards
Andrew
--
ahimsa <ahimsa@onetel.net.uk>
--=-1dowTb3laGaVs1+b9qz5
Content-Disposition: attachment; filename=rock2.py
Content-Type: text/x-python; name=rock2.py; charset=UTF-8
Content-Transfer-Encoding: 7bit
# Rock Game v.2
print
print "Rock & Paper & Scissors, v.2"
print "----------------------------"
print
# Initialisation:
import random
def item( a ): # Convert int to string
if a == 1:
return "Rock"
elif a == 2:
return "Paper"
elif a == 3:
return "Scissors"
def whoWins( b, c ): # Dictionary calling module
if options[ b ] == c:
print c, "beats", b, "- You lost!"
elif options[ c ] == b:
print b, "beats", c, "- You win!!"
else:
print "A tie - play again"
def playGame( ): # The game module itself
counter = 0
compWin = 0
playWin = 0
totComp = 0
totPlay = 0
while counter < 3: # 3 turns only
x = random.randrange( 1, 4 )
print
print "Rock (1), Paper (2), Scissors (3)"
print
y = int( raw_input( "Make a selection: " ) )
whoWins( item( y ), item( x ))
counter += 1 # counter controlled loop
if options[ item( y ) ] == item( x ): # keep score
compWin += 1
elif options[ item( x ) ] == item( y ):
playWin += 1
print
print "You won", playWin # output results
print "The Computer won", compWin
if playWin > compWin:
print "You won that round"
elif compWin > playWin:
print "The Computer won that round"
else:
print "That round was a tie"
print
print "--------%%%%%%------------"
print "Do you want to play again?"
doPlay = int( raw_input( "Enter 1 for 'YES' or 0 for 'NO': " )) # keep the game rolling
if doPlay == 1: # sentinel value control
playGame()
else:
doPlay != 1
print "Hit ENTER to return to the prompt" # graceful exit
print
# Processing Phase
options = { "Rock" : "Paper", "Scissors" : "Rock", "Paper" : "Scissors" } # Dict template
print "Do you want to play?"
doPlay = int( raw_input( "Enter 1 for 'YES' or 0 for 'NO': " )) # User gets a choice
if doPlay == 1:
playGame() # Play ball!!!!
else:
print "Hit ENTER to return to the prompt"
--=-1dowTb3laGaVs1+b9qz5--