Python Forum
Rock Paper Scissor GAME
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock Paper Scissor GAME
#4
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 wins
Note 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 wins
The 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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
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,239 Jun-30-2020, 07:56 PM
Last Post: Yoriz
  The tkinter version of Rock Paper Scissors menator01 3 3,266 Jun-28-2020, 07:15 AM
Last Post: ndc85430
  My version of Rock Paper Scissors menator01 12 6,258 Jun-27-2020, 10:25 PM
Last Post: menator01
  PyQt5 Version of Rock, Paper, & Scissors menator01 8 3,773 Jun-06-2020, 12:15 PM
Last Post: pyzyx3qwerty
  Rock, Paper, Scissors foksikrasa 11 4,478 May-28-2020, 05:58 PM
Last Post: BitPythoner
  A CLI based Rock,Paper or Scissor game. Ablazesphere 7 4,628 Oct-28-2018, 07:25 AM
Last Post: Ablazesphere
  A basic Rock-paper-scissors game by me... Unlimiter 0 2,526 Dec-25-2017, 03:41 PM
Last Post: Unlimiter
  Basic Rock, Paper, Scissors CinnamonBeard 1 3,605 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