Rock Paper Scissor GAME - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Rock Paper Scissor GAME (/thread-20353.html) |
Rock Paper Scissor GAME - inamullah9 - Aug-06-2019 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. Here is the code import random print ('WELCOME TO ROCK PAPER SCISSOR GAME\n\t\tDEVELOPED BY INAMULLAH9\n') cscore=0 pscore=0 def score(): global cscore global pscore global name print (name,'score :',pscore) print ('computer score: ',cscore) print('PLEASE ENTER YOUR NAME') name=input() print(name, 'VS computer') while True: if pscore == 5: print (name,'WIN THE MATCH') input() break elif cscore == 5: print ('Computer WINS THE MATCH') input() break print('Enter\n1 for ROCK \n2 for PAPER \n3 for SCISSOR') x=int(input()) if x <1 or x>3: print('invalid choice\ntry again') continue p=random.randint(1,3) if p==1 and x==1: print ('rock vs rock') if p==2 and x==2: print ('paper vs paper') if p==3 and x==3: print ('scissor vs scissor') if p==1 and x==2: print ('rock vs paper') if p==2 and x==3: print ('paper vs scissor') if p==3 and x==1: print ('scissor vs rock') if p==2 and x==1: print ('paper vs rock') if p==3 and x==2: print ('scissor vs paper') if p==1 and x==3: print ('rock vs scissor') if x==1 and p==1 or x==2 and p==2 or x==3 and p==3: print('DRAW') score() continue elif x==1 and p==3 or x==2 and p == 1 or x==3 and p==2: print(name," Win") pscore+=1 score() continue elif p==1 and x==3 or p==2 and x==1 or p==3 and x==2: print('Computer Win') cscore+=1 score() continue RE: Rock Paper Scissor GAME - ThomasL - Aug-10-2019 (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() RE: Rock Paper Scissor GAME - perfringo - Aug-11-2019 I like ThomasL solution for using dictionary but at the same time I am vary of using long handwritten datastructures (matrix dictionary). There is good probability that some typo or simple error will slip in while entering this data. Therefore alternative approaches for consideration focusing on how to determine winner. Following does not provide fully functional code but just ideas how problem can be approached. If taking away all validation and output formatting it boils down to how to determine winner. Basically there can be two outcomes: (1) draw or (2) either side wins. Draw is simple - if choices are equal its draw. For winners there is actually only three winning combinations: >>> first_wins = [('Rock', 'Scissors'), ('Paper', 'Rock'), ('Scissors', 'Paper')]So it can be expressed: if user_choice == computer_choice: # draw elif (user_choice, computer_choice) in first_wins: # user wins else: # computer winsAnother approach can be considered 'too clever'. One can have possible choices ordered so that next beats previous: >>> choices = ['Rock', 'Paper', 'Scissors']The problem is, that 'previous' to 'Rock' should be 'Scissors' which is actually last in list. Winning combinations using indices are: 0,2 (Rock beat Scissors) 1,0 (Paper beat Rock) 2,1 (Scissors beat Paper) Loosing combinations using indices are: 2,0 (Scissors defeated by Rock) 0,1 (Rock defeated by Paper) 1,2 (Paper defeated by Scissors) Doing %3 on these combinations: >>> (0 - 2) % 3 1 >>> (1 - 0) % 3 1 >>> (2 - 1) % 3 1 >>> (2 - 0) % 3 2 >>> (0 - 1) % 3 2 >>> (1 - 2) % 3 2As one can see, winning combination is always 1 and loosing one is 2. This can be used something like this: choices = ['Rock', 'Paper', 'Scissors'] players = ['user', 'computer'] user_choice = # get user choice and return corresponding index in choices computer_choice = random.choice(range(3)) # or random.choice(len(choices)) if user_choice == computer_choice: # draw else: # winner = players[(user_choice - computer_choice) % 3 - 1]Of course, draw is always 3 as same indices return 0 (0-0, 1-1, 2-2) and 0 % 3 is 3. It can be used if deemed useful (a la result = ['user wins', 'computer wins', 'draw']). I would not argue that these are better options. They are alternative options RE: Rock Paper Scissor GAME - ichabod801 - Aug-11-2019 As a third way to do it, I like this dictionary for rock, paper, scissors: wins = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}It stores what beat what in a slightly different data structure than perfringo used. Again, checking for the win is very simple: if user_choice == computer_choice: # draw elif wins[user_choice] == computer_choice: # user wins else: # computer winsNote that I like this because it scales well for expanding to rock-paper-scissors-lizard-spock: wins = {'rock': ['scissors', 'lizard'], 'scissors': ['paper', 'lizard'], 'paper': ['rock', 'spock'], 'lizard': ['paper', 'spock'], 'spock': ['scissors', 'rock']}You just need to change to testing with the in operator: if user_choice == computer_choice: # draw elif computer_choice in wins[user_choice]: # user wins else: # computer winsThe lesson being that there may be two different algorithms/data structures that are basically equivalent for the problem at hand, but the choice of which to use may be driven by other parts of the application. |