模擬分發撲克牌(python實現)

2021-08-19 17:15:26 字數 2214 閱讀 7470

52張撲克牌發個4個玩家,每人13張。

要求:

自動生成一幅撲克牌組;洗牌;發牌到玩家手中;將玩家手中撲克牌按花色大小整理好。

思路一

import random

import operator

defauto

(): pokers=

poker=

for i in ['♥','♠','♦','♣']:

for j in ['a','2','3','4','5','6','7','8','9','10','j','q','k']:

poker=

return pokers

poker=auto()

random.shuffle(poker)

li={}

for k in ['player1','player2','player3','player4']:

b=random.sample(poker,13)

for s in b:

poker.remove(s)

li.setdefault(k,b)

print('player1:',sorted(li['player1'],key=operator.itemgetter(0,1)))

print('player2:',sorted(li['player2'],key=operator.itemgetter(0,1)))

print('player3:',sorted(li['player3'],key=operator.itemgetter(0,1)))

print('player4:',sorted(li['player4'],key=operator.itemgetter(0,1)))

思路二

import random

import time

a=['♥','♠','♦','♣']

b=['a','2','3','4','5','6','7','8','9','10','j','q','k']

poker=

pokers=

n=1for i in a:

for j in b:

n=n+1

print("開始洗牌....")

random.shuffle(pokers)

defxipai

(x):

for i in x:

pokers.remove(i)

return pokers

deffapai

(y):

for i in y:

print(i[1],',',end=" ")

defpaixu

(z):

for i in z:

print(i[1],',',end=" ")

time.sleep(3)

a=random.sample(pokers,13)

pokers=xipai(a)

print("開始給player1發牌:\n")

print(fapai(a))

b=random.sample(pokers,13)

pokers=xipai(b)

print("開始給player2發牌:\n")

print(fapai(b))

c=random.sample(pokers,13)

pokers=xipai(c)

print("開始給player3發牌:\n")

print(fapai(c))

d=random.sample(pokers,13)

pokers=xipai(d)

print("開始給player4發牌:\n")

print(fapai(d))

a.sort()

b.sort()

c.sort()

d.sort()

time.sleep(3)

print("player1的牌:\n")

print(paixu(a))

print("player2的牌:\n")

print(paixu(b))

print("player3的牌:\n")

print(paixu(c))

print("player4的牌:\n")

print(paixu(d))

撲克牌模擬

題意 東東有 a b 張撲克牌。每張撲克牌有乙個大小 整數,記為a,範圍區間是 0 到 a 1 和乙個花色 整數,記為b,範圍區間是 0 到 b 1 撲克牌是互異的,也就是獨一無二的,也就是說沒有兩張牌大小和花色都相同。一手牌 的意思是你手裡有5張不同的牌,這 5 張牌沒有誰在前誰在後的順序之分,它...

撲克牌排序 tkinter模擬撲克牌和狼人殺發牌

今天我們用tkinter的canvas畫布實現撲克牌的隨機發牌 將54張牌隨機發給四位牌手,在螢幕上顯示每位牌手的牌,利用canvas將發牌結果顯示出來。需要準備的是54張撲克牌的 gif或者png,jpg的話要經過額外的轉換 為了保證最終的牌序是按照由小到大排列的,編號要按照aaaa 2222 k...

python3模擬撲克牌

python3.6環境 import collections from random import choice card collections.namedtuple card rank suit class frenchdeck ranks str n for n in range 2,11 l...