Python Forum
Rock Paper Scissor GAME
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock Paper Scissor GAME
#2
(Aug-06-2019, 05:07 PM)inamullah9 Wrote: I learned basic python in 1 week and here is what i developed. It is a basic rock paper scissor game between user and computer. One who scores 5 points wins the game. It took me 30 minutes to complete this.
Hi inamullah9,
Well done so far. These are your first steps to become professional python programmer.
I just want to show you how i would code this nice little game to provide you with some introduction to data types and how to use them.
So it´s not meant to say "Your code is ugly and mine is better". No pun intended.
I´m sure there are a lot of pythonistas who would say to me "I can do that even better". So here is my code:
import random

def choose(text, options):
    while True:
        answer = input(text)
        if answer in options:
            return answer
        print(f'Your options are {list(options)}')

options = {1: 'Rock', 2: 'Paper', 3: 'Scissor'}

matrix = {(1,1): 0, (2,2): 0, (3,3): 0, 
          (1,3): 1, (2,1): 1, (3,2): 1, 
          (1,2): 2, (2,3): 2, (3,1): 2}

def main():
    name = input('What`s your name? : ')
    winner = {0: f'Draw! Nobody', 1: f'{name}', 2: 'Computer'}

    while True:
        print(f'\nOk, {name} plays vs the computer.')
        score = [0, 0, 0]
        while True:
            player = int(choose(f'Enter [1] for Rock, [2] for Paper, [3] for Scissor ', '123'))
            cpu = random.randint(1, 3)
            state = (player, cpu)        
            print(f'{options[player]} vs {options[cpu]}, {winner[matrix[state]]} wins!')
            score[matrix[state]] += 1
            if score[1] == 5:
                print (f'{name.upper()} WINS THE MATCH!\n')
                break                
            elif score[2] == 5:
                print('COMPUTER WINS THE MATCH!\n')
                break

            print (f'{name} score:{score[1]}, cpu score: {score[2]}')

        answer = choose(f'Do you want to play again, {name}? ', 'YNyn')
        if answer in 'Nn':
            break

if __name__ == '__main__':
    main()
Reply


Messages In This Thread
Rock Paper Scissor GAME - by inamullah9 - Aug-06-2019, 05:07 PM
RE: Rock Paper Scissor GAME - by ThomasL - Aug-10-2019, 10:14 AM
RE: Rock Paper Scissor GAME - by perfringo - Aug-11-2019, 08:27 AM
RE: Rock Paper Scissor GAME - by ichabod801 - Aug-11-2019, 12:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Another Rock, Paper, Scissors Yoriz 4 3,224 Jun-30-2020, 07:56 PM
Last Post: Yoriz
  The tkinter version of Rock Paper Scissors menator01 3 3,240 Jun-28-2020, 07:15 AM
Last Post: ndc85430
  My version of Rock Paper Scissors menator01 12 6,207 Jun-27-2020, 10:25 PM
Last Post: menator01
  PyQt5 Version of Rock, Paper, & Scissors menator01 8 3,722 Jun-06-2020, 12:15 PM
Last Post: pyzyx3qwerty
  Rock, Paper, Scissors foksikrasa 11 4,423 May-28-2020, 05:58 PM
Last Post: BitPythoner
  A CLI based Rock,Paper or Scissor game. Ablazesphere 7 4,592 Oct-28-2018, 07:25 AM
Last Post: Ablazesphere
  A basic Rock-paper-scissors game by me... Unlimiter 0 2,501 Dec-25-2017, 03:41 PM
Last Post: Unlimiter
  Basic Rock, Paper, Scissors CinnamonBeard 1 3,590 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