Created
July 1, 2010 04:24
-
-
Save quandyfactory/459573 to your computer and use it in GitHub Desktop.
Simple command-line Rock, Paper, Scissors game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Simple command-line based Rock Paper Scissors game in Python. | |
""" | |
import random | |
vals = 'R P S'.split(' ') | |
msg_win = "You win this round." | |
msg_lose = "You lose this round." | |
msg_tie = "Tie! You both picked " | |
help = "Enter R for rock, P for paper, S for scissors, H for help, or Q to quit." | |
def fight(userval): | |
""" | |
Function to run a fight. | |
""" | |
compsel = random.randint(0, len(vals)-1) | |
compval = vals[compsel] | |
print "Computer picked %s." % (compval) | |
if compval == userval: | |
return 0, '%s%s.' % (msg_tie, compval) | |
elif (userval == 'R' and compval == 'P') or (userval == 'P' and compval == 'S') or (userval == 'S' and compval == 'R'): | |
return -1, '%s %s beats %s.' % (msg_lose, compval, userval) | |
else: | |
return 1, '%s %s beats %s.' % (msg_win, userval, compval) | |
def loop(): | |
""" | |
Function to run a game loop. | |
""" | |
score = [0, 0] | |
print "" | |
print "" | |
print "Rock Paper Scissors" | |
print "-------------------" | |
print "" | |
print help | |
print "" | |
print "Let's fight!" | |
while 1: | |
userval = raw_input("-->") | |
userval = userval.upper() | |
if userval == 'Q': | |
print "Final Score: %s to %s" % (score[0], score[1]) | |
if score[0] > score[1]: | |
print "You win!" | |
elif score[0] < score[1]: | |
print "You Lose!" | |
else: | |
print "You tied." | |
break | |
elif userval == 'H': | |
print help | |
print "" | |
elif userval in vals: | |
result, message = fight(userval) | |
print message | |
if result == 1: score[0] += 1 | |
elif result == -1: score[1] += 1 | |
print "Score: %s to %s." % (score[0], score[1]) | |
print "" | |
else: | |
print "%s is not a valid command." % (userval) | |
print "" | |
if __name__ == '__main__': | |
loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment