Python Forum
Roshambo with only 1 if switch
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Roshambo with only 1 if switch
#8
(Jan-25-2019, 08:20 AM)buran Wrote: maybe a better choice of data structure, using f-strings, random.choice

code removed for brevity

Further steps - maybe using collections.namedtuple

I really like your suggestion of random.choice.
I like f-strings for their adherence to zop "simple is better than complex" and "practicality beats purity", and they are nice and fast. However, I am so used to using % substitution and I'm a bit curmudgeonly. I should make that transition as well. I guess I need to retrain my fingers to fing some f-strings in the future. One question about f-strings, can you print a single { in an f-string?

(Jan-25-2019, 09:30 AM)perfringo Wrote: Instead of verbose dictionary one can combine Python indices and % with some math.

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
2
So 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 like where you are going with this solution. The concept of repeating anything in a program does bother me. While my solution may have its own elegance for a problem of this limited complexity it would be useless for a game with any more complexity. Even an intelligence for a game as simple as tic tac toe would overload my method.

On the other hand I have different fingers and I agree with buran on readability.
(Jan-25-2019, 09:37 AM)buran Wrote:
(Jan-25-2019, 09:30 AM)perfringo Wrote: Instead of verbose dictionary one can combine Python indices and % with some math.
Please, that would be way more complex and unreadable
Quote:Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

I'm playing with both points of view and I'm hoping to come up with a good cinergy. I'm considering combining your solution with an enumeration for readability. Does that sound readable and simple? Guess we'll see soon.

Thanks everyone for great suggestions so far.
Reply


Messages In This Thread
Roshambo with only 1 if switch - by Clunk_Head - Jan-24-2019, 10:28 PM
RE: Roshambo with only 1 if switch - by ichabod801 - Jan-24-2019, 11:55 PM
RE: Roshambo with only 1 if switch - by Clunk_Head - Jan-25-2019, 12:02 AM
RE: Roshambo with only 1 if switch - by buran - Jan-25-2019, 08:20 AM
RE: Roshambo with only 1 if switch - by Clunk_Head - Jan-26-2019, 06:16 PM
RE: Roshambo with only 1 if switch - by perfringo - Jan-25-2019, 09:30 AM
RE: Roshambo with only 1 if switch - by Clunk_Head - Jan-27-2019, 03:19 AM
RE: Roshambo with only 1 if switch - by buran - Jan-25-2019, 09:37 AM
RE: Roshambo with only 1 if switch - by perfringo - Jan-25-2019, 11:48 AM
RE: Roshambo with only 1 if switch - by buran - Jan-26-2019, 06:56 PM
RE: Roshambo with only 1 if switch - by perfringo - Jan-27-2019, 09:02 AM
RE: Roshambo with only 1 if switch - by Clunk_Head - Jan-27-2019, 10:19 PM
RE: Roshambo with only 1 if switch - by perfringo - Jan-28-2019, 08:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  switch to python3 Skaperen 0 2,195 Jul-03-2018, 12:55 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020