小游戲程序業(yè)務(wù):
由于我不會(huì)玩21點(diǎn),寫出來的程序有點(diǎn)瑕疵~ 謝謝理解
21點(diǎn)游戲規(guī)則與流程:
- 玩家共兩個(gè)角色:電腦和人類底哗,電腦是莊家
- 判斷雙方底牌是否直接為21點(diǎn),如果其中一方為21點(diǎn)則直接判勝利
- 如果人類牌面的總點(diǎn)數(shù)超過了21點(diǎn)前标,那就直接判輸
- 如果玩家停止要牌了,并且沒有超過21點(diǎn)的情況下距潘,則電腦根據(jù)概率要牌炼列。
- 電腦要完牌之后,直接判斷人類玩家和電腦玩家的牌面大小判斷勝負(fù)音比。
- 完成一輪游戲的時(shí)候俭尖,可由人類玩家決定,是否繼續(xù)玩下一輪
- 牌堆中剩余的牌數(shù)不夠玩一輪游戲的時(shí)候洞翩,由玩家決定是否重新取牌稽犁。
- 計(jì)算規(guī)則: 2、3骚亿、4已亥、5、6来屠、7陷猫、8秫舌、9、10分別是正常的點(diǎn)數(shù)绣檬,J足陨、Q、K都是10點(diǎn)
- A比較特殊娇未,A可以做為1或11墨缘。
代碼展示
import time
import random
def welcome_game():
print('歡迎光臨21點(diǎn)小游戲')
time.sleep(1) # 休眠1秒
print('祝您玩的愉快')
game_cmd()
# 判斷是否游戲
def game_cmd():
f_command = input('是否進(jìn)入游戲?y(進(jìn)入)/n(退出)')
if f_command == 'y':
game_start()
elif f_command == 'n':
print('期待您的下次光臨')
quit()
else:
print('輸入錯(cuò)誤,請(qǐng)查證后再次輸入')
game_cmd()
# 新?lián)淇伺屏斜?def card_list():
f_list = ['J','Q','K','A']*4
card_list =[i for i in range(2,11)]*4
card_list.extend(f_list)
# 打亂撲克牌順序
random.shuffle(card_list)
return card_list
# 概率控制
def computer_p(score):
# 前面的數(shù)占總數(shù)的百分比
p = (21-score)/13
# 如果符合這些情況零抬,就繼續(xù)要牌镊讼,否則返回0
if p > 1/4 and score < 13 or p > 1/3 and score <16 or p > 1/2 and score <17 or p > 3/4 and score <18:
x = 1
else :
x = 0
return x
# 計(jì)算得分
def sorce(score_list):
score = 0
num = score_list.count('A')
for i in score_list:
if i == 'J' or i =='Q' or i == 'K':
score += 10
elif i == 'A':
score += 1
else:
score += i
# 21點(diǎn)最多加1個(gè)10分
if num >= 1 and score < 12:
return score+10
else:
return score
# 判斷函數(shù)
def pd_fun(new_card_list):
if len(new_card_list) > 8:
again_cmd = input('請(qǐng)選擇是否繼續(xù)?y/n')
if again_cmd == 'y':
x = True
person = []
computer = []
return x, person, computer
else:
quit()
else:
again_cmd = input('請(qǐng)選擇是否繼續(xù)平夜?y/n')
print('我要重新取牌了')
if again_cmd == 'y':
game_start()
else:
quit()
# 開始游戲
def game_start():
person = []
computer = []
# 取出一副順序隨機(jī)的撲克牌
new_card_list = card_list()
person_score = 0
computer_score = 0
x = 1
while x:
# 選擇是否要牌
Pcmd = input('請(qǐng)選擇是否要牌?:y/n')
Ccmd = '1'
if Pcmd =='y':
# 取出的牌和剩余牌數(shù)
card= new_card_list.pop(-1)
person.append(card)
person_score = sorce(person)
print('你的牌為:', person)
print('點(diǎn)數(shù)和為:', person_score)
if person_score > 21:
print('你輸了蝶棋!')
# 調(diào)用函數(shù)判斷是否繼續(xù)玩
x,person,computer = pd_fun(new_card_list)
else:
Pcmd = 'n'
if computer_p(sorce(computer)) == 1:
card = new_card_list.pop(-1)
computer.append(card)
computer_score = sorce(computer)
#print('電腦牌為:', computer)
#print('點(diǎn)數(shù)和為:', computer_score)
if computer_score > 21:
print('電腦輸了')
x,person,computer = pd_fun(new_card_list)
else:
Ccmd = 'n'
if Pcmd == 'n' and Ccmd == 'n':
if person_score > computer_score:
print('你的牌面:{},電腦牌面{},你贏了!'.format(person,computer))
elif person_score == computer_score:
print('你的牌面:{},電腦牌面{},平局!'.format(person,computer))
else:
print('你的牌面:{},電腦牌面{},電腦贏!'.format(person,computer))
x,person,computer = pd_fun(new_card_list)
if __name__ == '__main__':
welcome_game()
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者