溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用Python寫剪刀石頭布游戲

發(fā)布時間:2022-02-22 16:45:18 來源:億速云 閱讀:230 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“怎么用Python寫剪刀石頭布游戲”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用Python寫剪刀石頭布游戲”吧!

1、電腦贏的情況

電腦(computer)玩家(player)
石頭 (stone)剪刀(scissors)
剪刀 (scissor)布(cloth)
布 (cloth)石頭(stone)

2、玩家贏的情況

玩家 (player)電腦(computer)
石頭 (stone)剪刀(scissors)
剪刀 (scissor)布(cloth)
布 (cloth)石頭(stone)

根據(jù)以上規(guī)則我們可以用 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 )

進(jìn)階一

可以把贏的情況寫在一個列表中這樣可以讓上面的腳本更精簡些

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 )

進(jìn)階二

為了更加方便玩我們分別用數(shù)字 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 )

進(jìn)階三

我們用三局兩勝的手段來決出最后的冠軍如果是平局就繼續(xù)猜直至電腦贏了兩局或者玩家贏了兩局才得出最后的冠軍。

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寫剪刀石頭布游戲”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI