Jan-26-2019, 06:16 PM
(This post was last modified: Jan-26-2019, 06:16 PM by Clunk_Head.)
(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 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 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.