自動斗地主是一種能自動模擬出真實的斗地主場景的程序。Python作為一種功能強大且易學(xué)易用的編程語言,可以很好地實現(xiàn)這一目標(biāo)。
# -*- coding: utf-8 -*-
import random
card_type = ["?", "?", "?", "?"]
card_num = ["A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"]
cards = []
for ctype in card_type:
for cnum in card_num:
cards.append(ctype + cnum)
#print(cards)
random.shuffle(cards)
landlord = []
player1 = []
player2 = []
temp = []
for i in range(3):
landlord.append(cards[i])
for i in range(3, 20):
if i % 2 == 1:
player1.append(cards[i])
else:
player2.append(cards[i])
print("地主的牌:")
print(landlord)
print("玩家1的牌:")
print(player1)
print("玩家2的牌:")
print(player2)
temp.extend(landlord)
temp.extend(player1)
temp.extend(player2)
temp.sort()
sum_card = []
sum_card.append(temp)
sum_card.append(landlord)
sum_card.append(player1)
sum_card.append(player2)
for i in range(1, 4):
print("第", i, "個玩家出牌:")
if i == 1:
player = player1
elif i == 2:
player = player2
else:
player = landlord
for c in player:
print(c)
print("\n")
上述代碼實現(xiàn)了斗地主的初始化、發(fā)牌、顯示牌等基本功能。當(dāng)然,這只是一個初步的版本,如果想要實現(xiàn)更加智能化的撲克牌游戲,我們還需要投入更多精力。