这是我在python中的第一批程序之一,我真的需要一些关于如何改进的反馈。这是一个多人游戏,在每回合结束时将电脑交给下一个玩家。
import random
import time
import os
import operator
def invalid():
print('\nINVALID INPUT')
time.sleep(0.8)
#checks what the players deck adds up to
def deck_check(deck):
#No idea why but it only works if the card dictionary is also here and not if given to the function as a variable
card={"1":1, "2":2, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "Jack":10, "Queen":10, "King":10, "Ace":1}
total=0
ace_number=0
if "Ace" in deck:
#two Loops to make sure all aces are at the end of player_deck
for a in deck:
if "Ace" == a:
deck.remove(a)
ace_number=ace_number+1
for _ in range(ace_number):
deck.append("Ace")
for t in deck:
if t=="Ace":
if total <=10:
if ace_number==1:
card["Ace"]=11
else:
card["Ace"]=1
else:
card["Ace"]=1
ace_number=ace_number-1
total=total+card[t]
else:
for b in deck:
total=total+card[b]
return total
while True:
print('\n1:New Game\n2:Exit Game')
x = input("Enter(1,2):")
if x=="1":
while True:
player_list=[]
player_capital={}
try:
players = int(input("Enter number of players:"))
except ValueError:
invalid()
continue
else:
if players<2:
print('\nMinimum 2 players')
time.sleep(0.8)
continue
while True:
try:
rounds = int(input("Enter number of rounds:"))
except ValueError:
invalid()
continue
else:
if rounds<1:
print('\nMinimum 1 round')
time.sleep(0.8)
continue
break
while True:
try:
money = int(input("Enter how much money all players should start with:"))
except ValueError:
invalid()
continue
else:
if money<10:
print('\nMinimum 10')
time.sleep(0.8)
continue
break
for u in range(players):
v = input("Enter player "+str(u+1)+" name:")
player_list.append(v)
player_capital[v]=money
while True:
try:
bet = int(input("Enter how much all players bet at the beginning of each round:"))
except ValueError:
invalid()
continue
else:
if bet<1:
print('\nMinimum 1')
time.sleep(0.8)
continue
elif bet>money:
print('\nNot enough starting money')
time.sleep(0.8)
continue
break
#Loop for rounds
for r in range(rounds):
round_result={}
capital=len(player_list)*bet
for z in range(len(player_list)):
player_capital[player_list[z]] = player_capital[player_list[z]]-bet
#Rests Deck
Cards={"1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "Jack":10, "Queen":10, "King":10, "Ace":1}
Deck={"Hearts":None, "Spades":None, "Clubs":None, "Diamonds":None}
for n in Deck:
Deck[n] = Cards
#Loop for players
for p in range(len(player_list)):
if player_capital[player_list[p]] <=0:
print(f'\n{player_list[p]} is broke')
continue
player_deck=[]
print(f'\nRound: {r+1}\nPlayer {player_list[p]} your turn!\nMake sure no other player can see the screen!')
time.sleep(4)
for _ in range(2):
h = random.choice(list(Deck))
g = random.choice(list(Deck[h]))
player_deck.append(g)
c = Deck[h]
del c[g]
if set(player_deck)=="Ace":
print(f'\nMoney in the game: {capital}\nYour money: {player_capital[player_list[p]]}\nYour Cards: {player_deck}\nBLACKJACK!')
break
else:
while True:
total=deck_check(player_deck)
if total>21:
print('\nYour bust!')
time.sleep(1)
break
continue
else:
print(f'\nMoney in the game: {capital}\nYour money: {player_capital[player_list[p]]}\nYour Cards: {player_deck} Adding up to: {total}\n1:Draw additional card\n2:Increse bet\n3:End turn')
e = input("Enter(1,2,3):")
if e=="1":
h = random.choice(list(Deck))
g = random.choice(list(Deck[h]))
player_deck.append(g)
c = Deck[h]
del c[g]
elif e=="2":
try:
print("")
q = int(input("Enter by how much do you want to incresen bet:"))
except ValueError:
invalid()
continue
if q > player_capital[player_list[p]]:
print('\nYou dont have that much money!')
continue
else:
capital=capital+q
player_capital[player_list[p]] = player_capital[player_list[p]]-q
elif e=="3":
round_result[player_list[p]]=total
break
else:
invalid()
print('\nNext Player')
print ("\n" * 100)
try:
round_winner = max(round_result.items(), key=operator.itemgetter(1))[0]
except ValueError:
print('\nNext Round')
time.sleep(1)
continue
player_capital[round_winner]=player_capital[round_winner]+capital
print(f'\nRound Winner is {round_winner}\nNext Round')
time.sleep(1)
winner = max(player_capital.items(), key=operator.itemgetter(1))[0]
print(f'\n{winner} is the winner!\nGAME OVER!')
break
elif x=="2":
break
else:
invalid()发布于 2018-11-19 00:32:07
如果我有一个chance__,我会在稍后完成复习!最重要的是为重复的代码创建函数。使用类而不是基于列表/元组/字典的结构也有帮助。我可以推荐你去Python的禅宗吗?
秩序中的小贴士们提出了这样的建议:
import os。check_deck更好地命名为sum_hand。input_integer的函数中将大大提高可读性。for i in range(len(player_list)):这样的代码应该被重构为for player in player_list: (如果需要索引的话也可以重构for i, player in enumerate(player_list): )。这既提高了效率,也提高了可读性。main函数和if __name__ == '__main__':检查。有关更多信息,请参见这个答案 (以及该问题的其他答案)。a = a + b在功能上(几乎)与a += b相同)。记住使用函数对您有利,并使用描述性名称!
发布于 2018-11-18 18:09:31
而不是手动输入命令中的每个项。
card={"1":1, "2":2, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "Jack":10, "Queen":10, "King":10, "Ace":1}你可以用
card = {str(i): i for i in range(1, 10)}
card.update(dict(zip("Jack Queen King Ace".split(), 3 * [10] + [1])))而且,类似地,代替
Deck={"Hearts":None, "Spades":None, "Clubs":None, "Diamonds":None}你可以用
Deck = dict(zip("Hearts Spades Clubs Diamonds".split(), 4 * [None]))或者更好(多亏了亚伦霍尔) -
Deck = dict.fromkeys("Hearts Spades Clubs Diamonds".split())而不是这样的命令
ace_number=ace_number+1你可以用
ace_number += 1如果遵循PEP 8- Python代码样式指南的建议,那就太好了。
https://codereview.stackexchange.com/questions/207932
复制相似问题