您好,登錄后才能下訂單哦!
這篇文章主要講解了python實現分發撲克牌的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
52張撲克牌發個4個玩家,每人13張。
要求:
自動生成一幅撲克牌組;洗牌;發牌到玩家手中;將玩家手中撲克牌按花色大小整理好。
思路一
import random import operator def auto(): pokers=[] poker=[] for i in ['♥','♠','♦','♣']: for j in ['A','2','3','4','5','6','7','8','9','10','J','Q','K']: poker.append(i) poker.append(j) pokers.append(poker) 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=1 for i in A: for j in B: pokers.append((n,(i+j))) n=n+1 print("開始洗牌....") random.shuffle(pokers) def xipai(x): for i in x: pokers.remove(i) return pokers def fapai(y): for i in y: print(i[1],',',end=" ") def paixu(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))
看完上述內容,是不是對python實現分發撲克牌的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。