Python Forum
Another Rock, Paper, Scissors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another Rock, Paper, Scissors
#1
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
Reply
#2
Is there any way we get to quit to change from one mode to another? I had to quit the game to do so.
pyzyx3qwerty
"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
Reply
#3
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.
Reply
#4
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.
Reply
#5
(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
Reply


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

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020