1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
import random class GameItem: def __eq__( self , other): return type (other) = = type ( self ) class Rock(GameItem): name = 'Rock' win = 'Rock blunts Scissors' def __gt__( self , other): return isinstance (other, Scissors) class Paper(GameItem): name = 'Paper' win = 'Paper covers Rock' def __gt__( self , other): return isinstance (other, Rock) class Scissors(GameItem): name = 'Scissors' win = 'Scissors cuts Paper' def __gt__( self , other): return isinstance (other, Paper) items_dict = { 'R' : Rock, 'P' : Paper, 'S' : Scissors} class Player: def __init__( self , name): self .name = name self .wins = 0 self .choice = None def choice_str( self ): return f '{self.name} choose: {self.choice.name}' def score_str( self ): return f '{self.name}\'s score: {self.wins}' def win_str( self ): return f '{self.name} won because {self.choice.win}' class HumanPlayer(Player): def make_choice( self ): choices = ', ' .join(item.name for item in items_dict.values()) letter_choices = ', ' .join(items_dict.keys()) text = ( f '{self.name} please choose from {choices}\n' f 'Enter ({letter_choices}) ' ) while True : result = input (text).upper() item_choice = items_dict.get(result) if item_choice: self .choice = item_choice() return print ( 'Incorrect choice!' ) class ComputerPlayer(Player): def make_choice( self ): choice = random.choice( tuple (items_dict.values())) self .choice = choice() class Game: def __init__( self , player1, player2): self .player1 = player1 self .player2 = player2 self .ties = 0 self .last_winner = None def check_result( self ): if self .player1.choice = = self .player2.choice: self .last_winner = None self .ties + = 1 return elif self .player1.choice > self .player2.choice: self .last_winner = self .player1 else : self .last_winner = self .player2 self .last_winner.wins + = 1 def output_result( self ): print ( f '{self.player1.choice_str()}, {self.player2.choice_str()}' ) if self .last_winner: print ( self .last_winner.win_str()) else : print ( 'It was a tie!' ) print (( f '{self.player1.score_str()}\n' f '{self.player2.score_str()}\n' f 'Ties: {self.ties}\n' )) def play( self ): self .player1.make_choice() self .player2.make_choice() self .check_result() self .output_result() def main_loop( self ): while True : self .play() result = input ( 'Another round, Enter \'y\' ' ).upper() if result ! = 'Y' : break class GameMenu: def __init__( self ): self .game = None self .mode_choices = tuple (number for number in range ( 4 )) self .modes_text = ( f '\nPlease choose a game mode:\n' '1 for human Vs computer\n' '2 for computer VS computer\n' '3 for human Vs human\n' 'or 0 to exit game' f 'Please choose from {self.mode_choices} ' ) print ( 'Welcome to Rock, Paper, Scissors' ) def mode1( self , name): self .game = Game( HumanPlayer(name), ComputerPlayer( 'Computer1' )) def mode2( self ): self .game = Game( ComputerPlayer( 'Computer1' ), ComputerPlayer( 'Computer2' )) def mode3( self , name1, name2): self .game = Game( HumanPlayer(name1), HumanPlayer(name2)) def main_loop( self ): while True : result = input ( self .modes_text) if result not in ( str (number) for number in self .mode_choices): print ( '\nIncorrect choice!' ) continue if result = = '0' : print ( 'Thank you, goodbye' ) break elif result = = '1' : self .mode1( input ( '\nPlease enter your name Player 1 ' ).capitalize()) elif result = = '2' : self .mode2() elif result = = '3' : self .mode3( input ( '\nPlease enter your name Player 1 ' ).capitalize(), input ( 'Please enter your name player 2 ' ).capitalize()) self .game.main_loop() if __name__ = = "__main__" : GameMenu().main_loop() |
Added another mainloop to enable the choice of another mode