Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Another Rock, Paper, Scissors
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): = name
        self.wins = 0
        self.choice = None

    def choice_str(self):
        return f'{} choose: {}'

    def score_str(self):
        return f'{}\'s score: {self.wins}'

    def win_str(self):
        return f'{} won because {}'

class HumanPlayer(Player):

    def make_choice(self):
        choices = ', '.join( for item in items_dict.values())
        letter_choices = ', '.join(items_dict.keys())
        text = (f'{} 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()

            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
        elif self.player1.choice > self.player2.choice:
            self.last_winner = self.player1
            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('It was a tie!')

               f'Ties: {self.ties}\n'))

    def play(self):

    def main_loop(self):
        while True:
            result = input('Another round, Enter \'y\' ').upper()
            if result != 'Y':

class GameMenu:
    def __init__(self): = 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): = Game(
            HumanPlayer(name), ComputerPlayer('Computer1'))

    def mode2(self): = Game(
            ComputerPlayer('Computer1'), ComputerPlayer('Computer2'))

    def mode3(self, name1, name2): = 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!')
            if result == '0':
                print('Thank you, goodbye')
            elif result == '1':
                    input('\nPlease enter your name Player 1 ').capitalize())
            elif result == '2':
            elif result == '3':
                    input('\nPlease enter your name Player 1 ').capitalize(),
                    input('Please enter your name player 2 ').capitalize())

if __name__ == "__main__":

Added another mainloop to enable the choice of another mode
menator01, GOTO10, pyzyx3qwerty like this post
Is there any way we get to quit to change from one mode to another? I had to quit the game to do so.
Yoriz likes this post
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Sure I could make a change to it so that when you chose not to have another round, it goes back to the game mode option with the option to quit the game from there.
Very cool, thank you for sharing this! I've learned a few new concepts just reading through the code and figuring out how it works.
Yoriz likes this post
(Jun-29-2020, 04:46 PM)pyzyx3qwerty Wrote: Is there any way we get to quit to change from one mode to another? I had to quit the game to do so.
I have added the requested functionality

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  The tkinter version of Rock Paper Scissors menator01 3 414 Jun-28-2020, 07:15 AM
Last Post: ndc85430
  My version of Rock Paper Scissors menator01 12 880 Jun-27-2020, 10:25 PM
Last Post: menator01
  PyQt5 Version of Rock, Paper, & Scissors menator01 8 359 Jun-06-2020, 12:15 PM
Last Post: pyzyx3qwerty
  Rock, Paper, Scissors foksikrasa 11 419 May-28-2020, 05:58 PM
Last Post: BitPythoner
  Rock Paper Scissor GAME inamullah9 3 688 Aug-11-2019, 12:17 PM
Last Post: ichabod801
  A CLI based Rock,Paper or Scissor game. Ablazesphere 7 1,446 Oct-28-2018, 07:25 AM
Last Post: Ablazesphere
  A basic Rock-paper-scissors game by me... Unlimiter 0 1,130 Dec-25-2017, 03:41 PM
Last Post: Unlimiter
  Basic Rock, Paper, Scissors CinnamonBeard 1 1,516 Dec-19-2017, 02:32 PM
Last Post: sparkz_alot

Forum Jump:

Users browsing this thread: 1 Guest(s)