Aug-11-2019, 08:27 AM
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:
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:
I would not argue that these are better options. They are alternative options
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.