我對python很陌生,並且一直在嘗試在python上製作一個多人的二十一點遊戲。我遇到了很多很多問題,並想知道你們是否可以幫助我。創建一個多人的二十一點遊戲
import random
def total(hand):
aces = hand.count(11)
t = sum(hand)
if t > 21 and aces > 0:
while aces > 0 and t > 21:
t -= 10
aces -= 1
return t
Cards = ["2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "AH", "JH", "QH", "KH", "AC", "JC", "QC", "KC", "AS", "JS", "QS", "KS", "AD", "JD", "QD", "KD"]
Cards[35] = 11
Cards[36] = 10
Cards[37] = 10
Cards[38] = 10
Cards[39] = 11
Cards[40] = 10
Cards[41] = 10
Cards[42] = 10
Cards[43] = 11
Cards[44] = 10
Cards[45] = 10
Cards[46] = 10
Cards[47] = 11
Cards[48] = 10
Cards[49] = 10
Cards[50] = 10
Players = raw_input("How many players are there?")
for i in range Players:
Player i = []
Player i.append(choice(Cards))
Player i.append(choice(Cards))
tp = total(player)
print "Player" + i + "Cards: " + Player i + "," + "total: " + tp
hitorstand = raw_input("hit (h) or stand (s)?")
if hitorstand == "h":
Player i.append(choice(cards))
print ("hit (h) or stand (s)?")
elif hitorstand == "s":
break
else print "Please enter h or s"
dealer = []
While True:
dealer.append(choice(cards))
dealer.append(choice(cards))
td = total(dealer)
while td > 17:
dealer.append(choice(cards))
else:
break
if td < tp < 21:
"Player i wins"
else print "dealer wins"
這是我到目前爲止。我知道有很多亂碼和代碼不起作用。我想知道你們是否可以讓我知道代碼有什麼問題,並可能提出一些關於如何解決它的選項。
我現在主要關注的問題:
我想提出一個 「多人」 的二十一點遊戲。 我不知道我應該怎麼做一個多人二十一點遊戲循環。 在我的代碼中,我詢問有多少人在玩。如何在不知道 的情況下製作遊戲循環?
另外,如何在不知道有多少玩家在玩的情況下創建一個函數來找出勝者?
後我輸入
Players = raw_input("How many players are there?") for i in range Players:
的
Players
在for循環給我一個語法錯誤。哪裏不對?
作爲一個更新,我還以爲你說的關於製造列表 什麼,我還是真的不明白我應該如何去 作出代碼,找出贏家。
例如
即使我做一個列表,如果我不知道有多少玩家是如何其實玩,我就不能在列表中的元素進行比較。如果我知道有多少人在玩,
playerlist = [1,2,3]
我可以說
if playerlist[0] > playerlist[1], playerlist[2] and playerlist[0] < 21:
then print "player 1 wins!"
但因爲我不知道有多少人在玩,直到用戶實際輸入的類型,我迷路了至於我應該如何爲勝利者編寫代碼。
我不知道是否有方法說「如果這比其他更大」。我只知道如何說「如果這比這更大」。
python有沒有辦法說「如果這比其他更大」? 如果不是,你能給我一些建議,讓代碼找出勝利者嗎?
它應該是'我在範圍(球員)',而不是'我在範圍球員'。 – Ryan 2012-07-30 02:03:25
二十一點問題是怎麼回事? http://stackoverflow.com/questions/11724954/how-do-i-separate-each-list-for-a-player-in-the-blackjack-game#comment15557653_11724954 – Benjamin 2012-07-30 20:54:54