""" Declare imports """ from random import * class Game(object): def __init__(self, smart): self.username = 'Guest' # Player's username self.user_points = 0 # Player's points self.cpu_points = 0 # CPU's points self.user_choices = { 'r': 0, 'p': 0, 's': 0 } # Player's choices self.smart_cpu = smart # If CPU is smart self.round = 0 # Round number self.full_choices = { 'r': 'Rock', 'p': 'Paper', 's': 'Scissors' } def start_game(self): print "[==============================================]" print "" print " *Rock.. Paper.. Scissors.. SHOOT!*" print "" print "" self.username = raw_input("Enter your username: ") print "" print "Welcome, " + self.username + "!" print "" print "[==============================================]" print "" tellRules = raw_input("Do you know how to play? (y/n) :").lower() while (len(tellRules) < 1): tellRules = raw_input("Invalid choice!\n" + "Do you know how to play? (y/n) :").lower() tellRules = tellRules[0] while tellRules != 'y' and tellRules != 'n': tellRules = raw_input("Invalid choice!\n" + "Do you know how to play? (y/n) :")[0].lower() print "" print "" if (tellRules == 'n'): self.tell_rules() else: self.next_round() def tell_rules(self): print "[==============================================]" print "" print "Instructions:" print "" print "1) Choose either rock, paper, or scissors" print "> by typing 'r', 'p', or 's'" print "> without the quotes" print "2) The CPU will make its choice" print "3) The winner of the round is the winner of the choices" print "> Rock > Scissors, Scissors > Paper, Paper > Rock" print "" ready = raw_input("Are you ready to play? (y/n):").lower() == 'y' if not ready: self.tell_rules() else: self.next_round() def is_valid_guess(self, guess): return guess.lower() in ('r', 'p', 's') def get_full_choice(self, choice): return self.full_choices[choice] def get_smart_cpu_choice(self): # If all values were 0, game started if max(self.user_choices.values()) == 0: return self.get_cpu_choice() else: best = self.keywithmaxval(self.user_choices) if best == 'r': return 'p' elif best == 'p': return 's' elif best == 's': return 'r' def get_cpu_choice(self): return choice(['r', 'p', 's']) def get_winner(self, player_choice, cpu_choice): if player_choice.lower() == cpu_choice.lower(): return "draw" elif player_choice == 'r' and cpu_choice == 's': return "player" elif player_choice == 'p' and cpu_choice == 'r': return "player" elif player_choice == 's' and cpu_choice == 'p': return "player" elif cpu_choice == 'r' and player_choice == 's': return "cpu" elif cpu_choice == 'p' and player_choice == 'r': return "cpu" elif cpu_choice == 's' and player_choice == 'p': return "cpu" else: return "draw" def ask_quit(self): quit = raw_input("Would you like to quit?").lower() while (len(quit) < 1): quit = raw_input("Invalid choice!\n" + "Would you like to quit?").lower() quit = quit[0] while quit != 'y' and quit != 'n': quit = raw_input("Invalid choice!\n" + "Would you like to quit? (y/n) :") return quit == 'y' def end_game(self): print "[==============================================]" print "" print "Thanks for playing!" print "" print "[==============================================]" def next_round(self): self.round += 1 print "" print "[==============================================]" print "" print "Round #" + str(self.round) + "!" print "Your points: " + str(self.user_points) print "CPU points: " + str(self.cpu_points) print "" print "" player_choice = raw_input("Choose your object (rock,papers,scissors/r,p,s)").lower() while not self.is_valid_guess(player_choice): player_choice = raw_input("Invalid choice!\n" + "Choose your object (rock,papers,scissors/r,p,s)").lower() while len(player_choice) < 1: player_choice = raw_input("Invalid choice!\n" + "Choose your object (rock,papers,scissors/r,p,s)").lower() player_choice = player_choice[0] print "" print "You chose: " + self.get_full_choice(player_choice) cpu_choice = 'N/A' if self.smart_cpu: cpu_choice = self.get_smart_cpu_choice() else: cpu_choice = self.get_cpu_choice() print "The CPU chose: " + self.get_full_choice(cpu_choice) print "" winner = self.get_winner(player_choice, cpu_choice) if winner == 'player': print "You won this round!" self.user_points += 1 self.user_choices[player_choice] += 1 elif winner == 'cpu': print "The CPU won this round!" self.cpu_points += 1 else: print "Nobody won! Round resulted in a draw!" print "" # If they want to quit, end the game if self.ask_quit(): self.end_game() # If they don't want to quit, next round else: self.next_round() def keywithmaxval(self, d): v = list(d.values()) k = list(d.keys()) return k[v.index(max(v))] game = Game(True) game.start_game()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question