Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- ROCK=0
- PAPER=1
- SCISSORS=2
- TIE=0
- WIN=1
- LOSS=2
- def setup():
- rpsDict=dict()
- rpsDict['winLossTable']=createWinLossTable()
- indexToString=[None, None, None]
- indexToString[ROCK]='rock'
- indexToString[PAPER]='paper'
- indexToString[SCISSORS]='scissors'
- rpsDict['indexToString']=indexToString
- stringToIndex=dict()
- stringToIndex['rock']=ROCK
- stringToIndex['paper']=PAPER
- stringToIndex['scissors']=SCISSORS
- rpsDict['stringToIndex']=stringToIndex
- results=[None, None, None]
- results[TIE]='It was a tie this time.'
- results[WIN]='Congratulations, you win!'
- results[LOSS]='Looks like the computer beat you this round.'
- rpsDict['resultStrings']=results
- return rpsDict
- def createWinLossTable():
- winLossTable=list()
- winLossTable.append([None, None, None])
- winLossTable.append([None, None, None])
- winLossTable.append([None, None, None])
- winLossTable[ROCK][ROCK]=TIE
- winLossTable[ROCK][PAPER]=LOSS
- winLossTable[ROCK][SCISSORS]=WIN
- winLossTable[PAPER][ROCK]=WIN
- winLossTable[PAPER][PAPER]=TIE
- winLossTable[PAPER][SCISSORS]=LOSS
- winLossTable[SCISSORS][ROCK]=LOSS
- winLossTable[SCISSORS][PAPER]=WIN
- winLossTable[SCISSORS][SCISSORS]=TIE
- return winLossTable
- def main():
- rpsDict=setup()
- while True:
- runGame(rpsDict)
- playAgain=raw_input("Do you want to play again? (yes, no) ").lower()
- if playAgain in ('y', 'yes'):
- continue
- exit()
- def runGame(rpsDict):
- while True:
- playerChoice=raw_input("\nRock, Scissors, or Paper? ").lower()
- try:
- playerChoice=rpsDict['stringToIndex'][playerChoice]
- except:
- print 'That choice is invalid.'
- continue
- break
- computerChoice=random.randint(0,2)
- print "The computer chose %s.\n" % rpsDict['indexToString'][computerChoice]
- result = rpsDict['winLossTable'][playerChoice][computerChoice]
- print rpsDict['resultStrings'][result]
- if __name__=='__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment