zoukankan      html  css  js  c++  java
  • 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() 
      
      
        

    上面是实现结果。

    浅闻陋见,还望指正
  • 相关阅读:
    MySQL锁之三:MySQL的共享锁与排它锁编码演示
    服务链路追踪(Spring Cloud Sleuth)
    服务网关zuul之四:zuul网关配置
    hdu 1505 City Game (hdu1506加强版)
    PHP设计模式——訪问者模式
    极客互联网电视不是噱头,用户体验成创维G7200核心竞争力
    深入理解JavaScript系列(23):JavaScript与DOM(上)——也适用于新手
    使用php分页类实现简单分类
    管理之路(四)
    poj 2485 Highways (最小生成树)
  • 原文地址:https://www.cnblogs.com/upright/p/4013326.html
Copyright © 2011-2022 走看看