SHOW:
|
|
- or go back to the newest paste.
1 | - | #! /usr/bin/env python3 |
1 | + | |
2 | Scissors cuts Paper | |
3 | - | ''' |
3 | + | Paper covers Rock |
4 | - | scissors cuts paper |
4 | + | Rock crushes Lizard |
5 | - | paper covers rock |
5 | + | Lizard poisons Spock |
6 | - | rock crushes lizard |
6 | + | Spock smashes Scissors |
7 | - | lizard poisons Spock |
7 | + | Scissors decapitates Lizard |
8 | - | Spock smashes scissors |
8 | + | Lizard eats Paper |
9 | - | scissors decapitates lizard |
9 | + | Paper disproves Spock |
10 | - | lizard eats paper |
10 | + | Spock vaporizes Rock |
11 | - | paper disproves Spock |
11 | + | Rock crushes Scissors |
12 | - | Spock vaporizes rock |
12 | + | |
13 | - | rock crushes scissors |
13 | + | |
14 | from random import choice | |
15 | ||
16 | def check(alpha, beta): | |
17 | - | import readline |
17 | + | for rule in RULES: |
18 | winner, verb, loser = rule | |
19 | - | RULES = list(map(str.split, __doc__.lower().strip().split('\n'))) |
19 | + | if (alpha, beta) == (winner, loser): |
20 | return alpha, ' '.join(rule) | |
21 | - | OPTIONS = ({winner for winner, verb, loser in RULES} |
21 | + | if (beta, alpha) == (winner, loser): |
22 | return beta, ' '.join(rule) | |
23 | return "", "" # no winner | |
24 | - | PROMPT = "Please select: " + ', '.join(sorted(OPTIONS)) + '\n' |
24 | + | |
25 | ||
26 | - | ################################################################################ |
26 | + | RULES = [rule.split() for rule in __doc__.strip().split('\n')] |
27 | OPTIONS = list({winner for winner, verb, loser in RULES} | |
28 | - | def test(a, b, rules=RULES): |
28 | + | |
29 | - | a, b = a.lower(), b.lower() |
29 | + | PROMPT = f"{', '.join(sorted(OPTIONS))} (return to quit): " |
30 | - | for rule in rules: |
30 | + | widest = max(len(option) for option in OPTIONS) |
31 | ||
32 | - | if (a, b) == (winner, loser): return a, ' '.join(rule) |
32 | + | print('Rules of the game:') |
33 | - | if (b, a) == (winner, loser): return b, ' '.join(rule) |
33 | + | print(__doc__) |
34 | print() | |
35 | - | ################################################################################ |
35 | + | print('Games keep going until you decide to quit by just entering return without a choice') |
36 | print('Running totals for wins, loses, and ties are printed each round.') | |
37 | - | if __name__ == '__main__': |
37 | + | print() |
38 | - | print(__doc__) |
38 | + | |
39 | - | print() |
39 | + | wins = loses = ties = 0 |
40 | - | try: |
40 | + | |
41 | - | while True: |
41 | + | while True: |
42 | - | user = None |
42 | + | user = 'to choose' |
43 | - | while user not in OPTIONS: |
43 | + | while user and user not in OPTIONS: |
44 | - | user = input(PROMPT).lower() |
44 | + | user = input(PROMPT).strip().capitalize() |
45 | - | machine = choice(list(OPTIONS)) |
45 | + | if not user: |
46 | - | try: |
46 | + | break |
47 | - | winner, rule = test(user, machine) |
47 | + | computer = choice(OPTIONS) |
48 | - | result = 'you win' if user == winner else 'you lose' |
48 | + | winner, rule = check(user, computer) |
49 | - | print(user, 'v.', machine, '->', result, '\t(%s)' % rule) |
49 | + | if not winner: |
50 | - | except: print('tie') |
50 | + | result = 'tied' |
51 | - | print() |
51 | + | ties += 1 |
52 | - | except EOFError: pass |
52 | + | elif user == winner: |
53 | result = 'win' | |
54 | wins += 1 | |
55 | else: # computer == winner | |
56 | result = 'lose' | |
57 | loses += 1 | |
58 | print(f"{user:^{widest}} v. {computer:^{widest}} - {result:4}" | |
59 | f" [{wins:3} | {loses:3} | {ties:2}]" | |
60 | f" {rule}\n" | |
61 | ) |