用python写剪刀石头布游戏

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

1、电脑赢的情况

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

2、玩家赢的情况

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

3因此我们可以用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 )

进阶二
为了更加方便玩我们可以用数字代替、剪刀(scissor)、石头(stone)、布(cloth)

1、电脑赢的情况

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

2、玩家赢的情况

玩家 (player) 电脑(computer)
石头 (stone)(0) 剪刀(scissors)(1)
剪刀 (scissor)(1) 布(cloth)(2)
布 (cloth)(2) 石头(stone)(0)
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)

以上是我对剪刀石头布的看法,如果你们认为我还有改善的地方欢迎留言

你可能感兴趣的:(笔记)