門
昨天不是星期天嗎?
你們在家放松的話一般做什么?
周末購物,出去看電影。這是你們的周末。
程序員的周末就是在家躺尸唐詩躺尸,偶爾加班加班加班,或者跟著幾個朋友在家消遣時間打打麻將,撲克牌玩一下!
??尤其是放長假【ps:也沒啥假,長假就是過年】在老家的時候,親戚尤其多,七大姑八大姨的一年好不容易聚一次,打打麻將跟撲克這是常有的事兒,聯(lián)絡下感情這是最快的方式~
??說起打撲克,我們經常就是玩兒的二百四、炸金花、三個打一個那就是叫啥名字來著,容我想想......
?話說真詞窮,我們那都是方言撒,我翻譯不過來普通話是叫什么了,我估計240你們也沒聽懂是啥,23333~
??今天的話小編是帶大家做一款21點的撲克游戲!
有大佬可優(yōu)化一下這個代碼,做一個精致豪華的界面就好了~~
正文
游戲規(guī)則:21點又名黑杰克,該游戲由2到6個人玩,使用除大小王之外的52張牌,游戲者的目標是使手中的牌的點數(shù)之和不超過21點且盡量大。當使用1副牌時,以下每種牌各一張(沒有大小王):
(1)初始化玩家數(shù):
<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def iniGame():
global playerCount, cards
while(True):
try:
playerCount = int(input('輸入玩家數(shù):'))
except ValueError:
print('無效輸入!')
continue
if playerCount < 2:
print('玩家必須大于1!')
continue
else:
break
try:
decks = int(input('輸入牌副數(shù):(默認等于玩家數(shù))'))
except ValueError:
print('已使用默認值!')
decks = playerCount
print('玩家數(shù):', playerCount, ',牌副數(shù):', decks)
cards = getCards(decks) # 洗牌</span></span></span>
(2)建立了玩家列表,電腦跟玩家對戰(zhàn)。
<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def createPlayerList():
global playerList
playerList = []
for i in range(playerCount):
playerList += [{'id': '', 'cards': [], 'score': 0}].copy()
playerList[i]['id'] = '電腦' + str(i+1)
playerList[playerCount-1]['id'] = '玩家'
random.shuffle(playerList) # 為各玩家隨機排序</span></span></span>
(3)開始會設置2張明牌玩法都可以看到點數(shù)。
<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def gameStart():
print('為各玩家分2張明牌:')
for i in range(playerCount): # 為每個玩家分2張明牌
deal(playerList[i]['cards'], cards, 2)
playerList[i]['score'] = getScore(playerList[i]['cards']) # 計算初始得分
print(playerList[i]['id'], ' ', getCardName(playerList[i]['cards']),
' 得分 ', playerList[i]['score'])
)</span></span></span>
(4)游戲為電腦跟玩家依次分發(fā)第三張暗牌,這是別人看不到的。
<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def gamePlay():
for i in range(playerCount):
print('當前', playerList[i]['id'])
if playerList[i]['id'] == '玩家': # 玩家
while(True):
print('當前手牌:', getCardName(playerList[i]['cards']))
_isDeal = input('是否要牌?(y/n)')
if _isDeal == 'y':
deal(playerList[i]['cards'], cards)
print('新牌:', getCardName(playerList[i]['cards'][-1]))
# 重新計算得分:
playerList[i]['score'] = getScore(playerList[i]['cards'])
elif _isDeal == 'n':
break
else:
print('請重新輸入!')
else: # 電腦
while(True):
if isDeal(playerList[i]['score']) == 1: # 為電腦玩家判斷是否要牌
deal(playerList[i]['cards'], cards)
print('要牌。')
# 重新計算得分:
playerList[i]['score'] = getScore(playerList[i]['cards'])
else:
print('不要了。')
break
)</span></span></span>
(5)隨機洗牌:
<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def getCards(decksNum):
cardsList = ['Aa', 'Ab', 'Ac', 'Ad',
'Ka', 'Kb', 'Kc', 'Kd',
'Qa', 'Qb', 'Qc', 'Qd',
'Ja', 'Jb', 'Jc', 'Jd',
'0a', '0b', '0c', '0d',
'9a', '9b', '9c', '9d',
'8a', '8b', '8c', '8d',
'7a', '7b', '7c', '7d',
'6a', '6b', '6c', '6d',
'5a', '5b', '5c', '5d',
'4a', '4b', '4c', '4d',
'3a', '3b', '3c', '3d',
'2a', '2b', '2c', '2d']
cardsList *= decksNum # 牌副數(shù)
random.shuffle(cardsList) # 隨機洗牌
return cardsList</span></span></span>
(6)?設置牌名字典:
<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">cardNameDict = {'Aa': '黑桃A', 'Ab': '紅桃A', 'Ac': '梅花A', 'Ad': '方片A',
'Ka': '黑桃K', 'Kb': '紅桃K', 'Kc': '梅花K', 'Kd': '方片K',
'Qa': '黑桃Q', 'Qb': '紅桃Q', 'Qc': '梅花Q', 'Qd': '方片Q',
'Ja': '黑桃J', 'Jb': '紅桃J', 'Jc': '梅花J', 'Jd': '方片J',
'0a': '黑桃10', '0b': '紅桃10', '0c': '梅花10', '0d': '方片10',
'9a': '黑桃9', '9b': '紅桃9', '9c': '梅花9', '9d': '方片9',
'8a': '黑桃8', '8b': '紅桃8', '8c': '梅花8', '8d': '方片8',
'7a': '黑桃7', '7b': '紅桃7', '7c': '梅花7', '7d': '方片7',
'6a': '黑桃6', '6b': '紅桃6', '6c': '梅花6', '6d': '方片6',
'5a': '黑桃5', '5b': '紅桃5', '5c': '梅花5', '5d': '方片5',
'4a': '黑桃4', '4b': '紅桃4', '4c': '梅花4', '4d': '方片4',
'3a': '黑桃3', '3b': '紅桃3', '3c': '梅花3', '3d': '方片3',
'2a': '黑桃2', '2b': '紅桃2', '2c': '梅花2', '2d': '方片2'}</span></span></span>
?(7)判斷勝負:
<span style="color:#333333"><span style="background-color:#ffffff"><span style="background-color:#f8f8f8">def showWinAndLose():
loserList = [] # [['id', score], ['id', score], ...]
winnerList = [] # [['id', score], ['id', score], ...]
winnerCount = 0
loserCount = 0
for i in range(playerCount):
if playerList[i]['score'] > 21: # 爆牌直接進入敗者列表
lo([playerList[i]['id'], playerList[i]['score']])
else: # 臨時勝者列表
winnerLi([playerList[i]['id'], playerList[i]['score']])
if len(winnerList) == 0: # 極端情況:全部爆牌
print('全部玩家爆牌:')
for i in range(len(loserList)):
print(loserList[i][0], loserList[i][1])
elif len(loserList) == 0: # 特殊情況:無人爆牌
winnerLi(key=lambda x: x[1], reverse=True) # 根據分數(shù)值排序勝者列表
for i in range(len(winnerList)): # 計算最低分玩家數(shù)量
if i != len(winnerList)-1:
if winnerList[-i-1][1] == winnerList[-i-2][1]:
loserCount = (i+2)
else:
if loserCount == 0:
loserCount = 1
break
else:
loserCount = len(loserList)
if loserCount == 1:
lo())
else:
while(len(loserList) != loserCount):
lo())
for i in range(len(winnerList)): # 計算最高分玩家數(shù)量
if i != len(winnerList)-1:
if winnerList[i][1] == winnerList[i+1][1]:
winnerCount = (i+2)
else:
if winnerCount == 0:
winnerCount = 1
break
else:
winnerCount = len(winnerList)
while(len(winnerList) != winnerCount):
winnerLi()
print('獲勝:')
for i in range(len(winnerList)):
print(winnerList[i][0], winnerList[i][1])
print('失?。?#39;)
for i in range(len(loserList)):
print(loserList[i][0], loserList[i][1])
else: # 一般情況:有人爆牌
winnerLi(key=lambda x: x[1], reverse=True) # 根據分數(shù)值排序勝者列表
for i in range(len(winnerList)): # 計算最高分玩家數(shù)量
if i != len(winnerList)-1:
if winnerList[i][1] == winnerList[i+1][1]:
winnerCount = (i+2)
else:
if winnerCount == 0:
winnerCount = 1
break
else:
winnerCount = len(winnerList)
while(len(winnerList) != winnerCount):
winnerLi()
print('獲勝:')
for i in range(len(winnerList)):
print(winnerList[i][0], winnerList[i][1])
print('失?。?#39;)
for i in range(len(loserList)):
print(loserList[i][0], loserList[i][1])</span></span></span>
?游戲效果:咳咳咳.......感覺這游戲看運氣也看膽量!!
?總結?
??哈哈哈!小編玩游戲比較廢,你們要來試試嘛?無聊的時候可以摸摸魚,打打醬油~
制作不易,記得一鍵三連哦!! 如果需要本文完整的代碼+圖片素材。
Python新手安裝包、免費激活碼、等等更多Python資料 【私信小編06】即可免費領取哦??!
1.《關于21點玩法我想說打撲克嘛?Python教你“經典紙牌游戲21點”玩法》援引自互聯(lián)網,旨在傳遞更多網絡信息知識,僅代表作者本人觀點,與本網站無關,侵刪請聯(lián)系頁腳下方聯(lián)系方式。
2.《關于21點玩法我想說打撲克嘛?Python教你“經典紙牌游戲21點”玩法》僅供讀者參考,本網站未對該內容進行證實,對其原創(chuàng)性、真實性、完整性、及時性不作任何保證。
3.文章轉載時請保留本站內容來源地址,http://f99ss.com/yule/2073710.html