SHOW:
|
|
- or go back to the newest paste.
1 | import random | |
2 | ROCK=0 | |
3 | PAPER=1 | |
4 | SCISSORS=2 | |
5 | ||
6 | TIE=0 | |
7 | WIN=1 | |
8 | - | LOSE=-1 |
8 | + | LOSS=2 |
9 | ||
10 | - | def createRpsTable(): |
10 | + | def setup(): |
11 | - | rpsTable=list() |
11 | + | rpsDict=dict() |
12 | - | rpsTable.append([None, None, None]) |
12 | + | rpsDict['winLossTable']=createWinLossTable() |
13 | - | rpsTable.append([None, None, None]) |
13 | + | indexToString=[None, None, None] |
14 | - | rpsTable.append([None, None, None]) |
14 | + | indexToString[ROCK]='rock' |
15 | - | rpsTable[ROCK][ROCK]=TIE |
15 | + | indexToString[PAPER]='paper' |
16 | - | rpsTable[ROCK][PAPER]=LOSE |
16 | + | indexToString[SCISSORS]='scissors' |
17 | - | rpsTable[ROCK][SCISSORS]=WIN |
17 | + | rpsDict['indexToString']=indexToString |
18 | - | rpsTable[PAPER][ROCK]=WIN |
18 | + | stringToIndex=dict() |
19 | - | rpsTable[PAPER][PAPER]=TIE |
19 | + | stringToIndex['rock']=ROCK |
20 | - | rpsTable[PAPER][SCISSORS]=LOSE |
20 | + | stringToIndex['paper']=PAPER |
21 | - | rpsTable[SCISSORS][ROCK]=LOSE |
21 | + | stringToIndex['scissors']=SCISSORS |
22 | - | rpsTable[SCISSORS][PAPER]=WIN |
22 | + | rpsDict['stringToIndex']=stringToIndex |
23 | - | rpsTable[SCISSORS][SCISSORS]=TIE |
23 | + | results=[None, None, None] |
24 | - | return rpsTable |
24 | + | results[TIE]='It was a tie this time.' |
25 | results[WIN]='Congratulations, you win!' | |
26 | results[LOSS]='Looks like the computer beat you this round.' | |
27 | - | rpsTable=createRpsTable() |
27 | + | rpsDict['resultStrings']=results |
28 | return rpsDict | |
29 | - | wantsToContinue=runGame(rpsTable) |
29 | + | |
30 | - | if not wantsToContinue: |
30 | + | def createWinLossTable(): |
31 | - | break |
31 | + | winLossTable=list() |
32 | winLossTable.append([None, None, None]) | |
33 | - | def runGame(rpsTable) |
33 | + | winLossTable.append([None, None, None]) |
34 | - | playerChoice=raw_input("Rock, Paper, or Scissors? ").lower() |
34 | + | winLossTable.append([None, None, None]) |
35 | winLossTable[ROCK][ROCK]=TIE | |
36 | - | if playerChoice="rock": |
36 | + | winLossTable[ROCK][PAPER]=LOSS |
37 | - | playerChoice=ROCK |
37 | + | winLossTable[ROCK][SCISSORS]=WIN |
38 | - | elif playerChoice="paper": |
38 | + | winLossTable[PAPER][ROCK]=WIN |
39 | - | playerChoice=PAPER |
39 | + | winLossTable[PAPER][PAPER]=TIE |
40 | - | elif playerChoice="scissors": |
40 | + | winLossTable[PAPER][SCISSORS]=LOSS |
41 | - | playerChoice=SCISSORS |
41 | + | winLossTable[SCISSORS][ROCK]=LOSS |
42 | - | else: |
42 | + | winLossTable[SCISSORS][PAPER]=WIN |
43 | - | print "Invalid choice." |
43 | + | winLossTable[SCISSORS][SCISSORS]=TIE |
44 | return winLossTable | |
45 | ||
46 | def main(): | |
47 | rpsDict=setup() | |
48 | - | if computerChoice=ROCK: |
48 | + | |
49 | - | print "The computer chose rock." |
49 | + | runGame(rpsDict) |
50 | - | elif computerChoice=PAPER: |
50 | + | playAgain=raw_input("Do you want to play again? (yes, no) ").lower() |
51 | - | print "The computer chose paper." |
51 | + | if playAgain in ('y', 'yes'): |
52 | - | else: |
52 | + | |
53 | - | print "The computer chose scissors." |
53 | + | exit() |
54 | ||
55 | - | result = rpsTable[playerChoice][computerChoice] |
55 | + | def runGame(rpsDict): |
56 | - | if result=WIN: |
56 | + | |
57 | - | print "Congratulations, you win!" |
57 | + | playerChoice=raw_input("\nRock, Scissors, or Paper? ").lower() |
58 | - | elif result=LOSE: |
58 | + | try: |
59 | - | print "Looks like the computer beat you this round." |
59 | + | playerChoice=rpsDict['stringToIndex'][playerChoice] |
60 | - | else: |
60 | + | except: |
61 | - | print "It was a tie this time." |
61 | + | print 'That choice is invalid.' |
62 | continue | |
63 | - | playAgain=raw_input("Do you want to play again? (yes, no)").lower() |
63 | + | |
64 | - | if playAgain in ('y', 'yes'): |
64 | + | |
65 | - | return True |
65 | + | |
66 | - | return False |
66 | + | print "The computer chose %s.\n" % rpsDict['indexToString'][computerChoice] |
67 | ||
68 | result = rpsDict['winLossTable'][playerChoice][computerChoice] | |
69 | print rpsDict['resultStrings'][result] | |
70 | ||
71 | if __name__=='__main__': | |
72 | main() |