用Python写剪刀石头布游戏

猿友 2021-01-04 16:40:28 浏览数 (3168)
反馈

相信很多的小伙伴小时候都有玩过剪刀石头布的游戏吧那么你和电脑玩过吗?以下是小编用Python脚本写的剪刀石头布的小游戏。

1、电脑赢的情况

电脑(computer) 玩家(player)
石头 (stone) 剪刀(scissors)
剪刀 (scissor) 布(cloth)
布 (cloth) 石头(stone)

2、玩家赢的情况

玩家 (player) 电脑(computer)
石头 (stone) 剪刀(scissors)
剪刀 (scissor) 布(cloth)
布 (cloth) 石头(stone)

根据以上规则我们可以用 if 的形式来做到,代码如下:

import random

C_G = ['stone','scissor','cloth']

computer = random.choice(C_G)

player = input('please enter your choice(stone,scissor,cloth):')

if computer == 'stone':

    if player == 'stone':

        print('\033[33mdraw\033[0m')

    elif player == 'scissor':

        print('\033[32myou lose!!!\033[0m')

    else:

        print('\033[31myou win!!!\033[0m')

if computer == 'scissor':

    if player == 'scissor':

        print('\033[33mdraw\033[0m')

    elif player == 'stone':

        print('\033[31myou win!!!\033[0m')

    else:

        print('\033[32myou lose!!!\033[0m')

if computer == 'cloth':

    if player == 'cloth':

        print('\033[33mdraw\033[0m')

    elif player == 'scissor':

        print('\033[31myou win!!!\033[0m')

    else:

        print('\033[32myou lose!!!\033[0m')

print('your choice:%s' % player,'computer choice:%s' % computer )

进阶一

可以把赢的情况写在一个列表中这样可以让上面的脚本更精简些

import random

C_G = ['stone','scissor','cloth']

computer = random.choice(C_G)

WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]

player = input('please enter your choice(stone,scissor,cloth):')

if computer == player:

    print('\033[33mdraw\033[0m')

elif [player,computer] in WinList:

    print('\033[33myou win!!\033[0m')

else:

    print('\033[32myou lose!!!\033[0m')

print('your choice:%s' % player,'computer choice:%s' % computer )

进阶二

为了更加方便玩我们分别用数字 0, 1, 2 代替:石头(stone)、剪刀(scissor)、布(cloth)

import random

C_G = ['stone','scissor','cloth']

computer = random.choice(C_G)

WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]

choice_memu = '''

(0)stone

(1)scissor

(2)cloth

please choice(0/1/2):'''

number = int(input(choice_memu))

player = C_G[number]

if computer == player:

    print('\033[33mdraw\033[0m')

elif [player,computer] in WinList:

    print('\033[33myou win!!\033[0m')

else:

    print('\033[32myou lose!!!\033[0m')

print('your choice:%s' % player,'computer choice:%s' % computer )

进阶三

我们用三局两胜的手段来决出最后的冠军如果是平局就继续猜直至电脑赢了两局或者玩家赢了两局才得出最后的冠军。

import random

C_G = ['stone','scissor','cloth']

computer = random.choice(C_G)

WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]

choice_memu = '''

(0)stone

(1)scissor

(2)cloth

please choice(0/1/2):'''

c_win = 0

p_win = 0

d_win = 1

while c_win < 2 and p_win < 2:

  number = int(input(choice_memu))

  player = C_G[number]

  if computer == player:

    d_win+=1

  elif [player,computer] in WinList:

    p_win+=1

    if p_win == 2:

      print('\033[31myou win!!!\033[0m')

      break

  else:

    c_win+=1

    if c_win == 2:

      print('\033[32myou lose!!!\033[0m')

      break

total_time = p_win + c_win + d_win

print('you guesse: %s times' % total_time,'you lost: %s times' % c_win,'you win: %s times' % p_win,'draw: %s times' % d_win)

以上就是小编对剪刀石头布小游戏的编写思路了,希望小伙伴们能够有所收获。

推荐好课:Python Flask建站框架Python Django框架




1 人点赞