RockPaperScissorsLizardSpock Python实现

  初学python,实现一些很有意思的小游戏是很能提高编程能力的。

 

  Rock-Paper-Scissors-Lizard-Spock

  http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock  这里是关于这个小游戏的全部介绍

下面是我用python实现的代码:

#coding:utf-8
'''
Rock-Paper-Scissors-Lizard-Spock
     游戏规则:
         石头砸蜥蜴;石头敲坏剪子。
        剪子剪布;剪子斩首蜥蜴;
        布包石头;布包死斯波克;        
        蜥蜴毒死斯波克;蜥蜴吃掉布;
        斯波克踩碎剪子;斯波克融化石头;
'''
import random

def win(computer,player):
    '''
    游戏输赢判定
    根据对玩家和电脑的选择数值化,对五个选项的赋值,得出以下判定方法
    '''
    diff = (player - computer) % 5
    if (diff == 1) or (diff == 2):
        return 'Yes,you are winer !'
    elif (diff == 3) or (diff == 4):
        return 'Sorry,computer win the game ...'
    else:
        return 'God, you and computer tie !'
    
def numToStr(num):
    if num==0:
        return 'Rock'
    elif num==1:
        return 'Spock'
    elif num==2:
        return 'Paper'
    elif num==3:
        return 'Lizard'
    elif num==4:
        return 'Scissors'
    elif num==5:
        return 'Exit'
    
def strToNum(Str):
    if Str=='Rock':
        return 0
    elif Str=='Spock':
        return 1
    elif Str=='Paper':
        return 2
    elif Str=='Lizard':
        return 3
    elif Str=='Scissors':
        return 4
    elif Str=='Exit':
        return 5
    else: 
        return 6 

def rpsls(playerStr):
    playerNum=strToNum(playerStr)
    computerNum=random.randint(0,5)
    winer=win(computerNum,playerNum)
    computerStr=numToStr(computerNum)
    print 'You chose '+playerStr
    print 'The computer chose '+computerStr
    print winer
    print ''
    
def main():
    print 'Welcome to play the Rock-Paper-Scissors-Lizard-Spock !'
    print 'While you need inPut something,please chose from the following word !'
    print 'Rock,Paper,Scissors,Lizard,Spock,Exit...(Exit for exit)'
    print ''
    print 'Please inPut your choice:'
    player=raw_input()
    while(1):
        xx=strToNum(player)
        if(xx==0 or xx==1 or xx==2 or xx==3 or xx==4 or xx==5):
            if(strToNum(player) == 5): 
                print 'Game Over !'
                break
            rpsls(player)
            print 'Please inPut your choice:'
            player=raw_input()
        else :
            print 'InPut wrong ! Please chose again :'
            player=raw_input()
               
main() 
  
  
    

上面是实现结果。

浅闻陋见,还望指正
原文地址:https://www.cnblogs.com/upright/p/4013326.html