Instead of verbose dictionary one can combine Python indices and % with some math.
We order elements so that next beats previous:
0 -> 2 (Rock beat Scissors)
1 -> 0 (Paper beat Rock)
2 -> 1 (Scissors beat Paper)
As length of choices is 3 then we can use Python % to do something like that:
0 -> 2 ---> (0 - 2) % 3 -> (2 - 0) % 3 ---> 1 -> 2 ---> 1 beats 2
1 -> 0 ---> (1 - 0) % 3 -> (0 - 1) % 3 ---> 1 -> 2 ---> 1 beats 2
2 -> 1 ---> (2 - 1) % 3 -> (1 - 2) % 3 ---> 1 -> 2 ---> 1 beats 2
This means that winning side (i.e 'previous') returns 1 and loosing side (i.e 'next') returns 2. You can test it to hold true with the opposite as well (loosing combinations are 2 -> 0, 0 -> 1 and 1 -> 2)
% with negative numbers:
We order elements 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)
As length of choices is 3 then we can use Python % to do something like that:
0 -> 2 ---> (0 - 2) % 3 -> (2 - 0) % 3 ---> 1 -> 2 ---> 1 beats 2
1 -> 0 ---> (1 - 0) % 3 -> (0 - 1) % 3 ---> 1 -> 2 ---> 1 beats 2
2 -> 1 ---> (2 - 1) % 3 -> (1 - 2) % 3 ---> 1 -> 2 ---> 1 beats 2
This means that winning side (i.e 'previous') returns 1 and loosing side (i.e 'next') returns 2. You can test it to hold true with the opposite as well (loosing combinations are 2 -> 0, 0 -> 1 and 1 -> 2)
% with negative numbers:
>>> (0 - 2) % 3 1 >>> (0 - 1) % 3 2 >>> (1 - 2) % 3 2So we can do something like (no data validation applied, just to show how to use previous principle to determine winner):
>>> choices = ['Rock', 'Paper', 'Scissors'] >>> 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 >>> elif (user_choice - computer_choice) % 3 < (computer_choice - user_choice) % 3: ... # user wins ... else: ... # computer wins
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.