我目前正在学习并努力提高 Python 水平。
我正在尝试与你一起玩扑克,但它不会将 you_fold 或 comp_fold 返回为 True 并结束游戏,因此它会永远持续下去。
另一件事是底池不会持续上涨。基本上,如果您买入 100,您可以添加到底池中,它会显示您已添加到底池中,但电脑移动后的底池始终是您的买入金额。
我知道其他一些东西不存在/不起作用,但添加/修复它们会非常好。
谢谢!
这是代码:
import random
suit_name = ["","S","H","C","D"]
number_name = ["","","2","3","4","5","6","7","8","9","10","J","Q","K","A"]
deck_ = [[ i // 13 + 1 , i%13 + 2 ] for i in range(52)]
#[suit, number(2-13(K),14(ACE))] for each card
def print_card(card_):
suit_curr, number_curr = card_
name_card = suit_name[suit_curr] + number_name[number_curr]
return name_card
def deck_maker():
random.shuffle(deck_)
def deal_cards(player, no_cards):
for i in range(no_cards):
player.append(deck_.pop(0))
def print_hand(hand):
hand_list = []
for card_ in hand:
hand_list.append(print_card(card_))
print(hand_list)
def played_checker(hand):
hand_list = []
for card_ in hand:
hand_list.append(print_card(card_))
def check_strength(hand):
num_count = [0 for i in range(15)]
for card_ in hand:
num_count[card_[1]] += 1
#print(num_count[2:])
suit_count = [0 for i in range(5)]
for card_ in hand:
suit_count[card_[0]] += 1
#print(suit_count[1:5])
def hands(hand):
#Is there a flush
flush = False
for i in range(1,5):
if suit_count[i] == 5:
flush = True
#Pairs
pairs = 0
for i in range(2,15):
if num_count[i] == 2:
pairs = pairs + 1
#Three of a kind
threes = 0
for i in range(2,15):
if num_count[i] == 3:
threes = threes + 1
#Full house
Full_house = False
FH_count = 0
if (pairs > 0) and (threes > 0):
pairs -= 1
threes -= 1
FH_count += 1
Full_house = True
#Four of a kind
fours = 0
for i in range(2,15):
if num_count[i] == 4:
fours = fours + 1
#Straight
straight_count = 0
for i in range(2,10):
if num_count[i]==1:
flag_ = True
for j in range(i+1, i+5):
if num_count[j]==0:
flag_ = False
if flag_ :
straight_count += 1
if pairs >= 1:
print(pairs, "Pair!")
if threes >= 1:
print(threes, "Three-of-a-kind1")
if fours >= 1:
print(fours, "Four-of-a-kind")
if straight_count >= 1:
print(straight_count, "Straight!")
if straight_count != 0 and flush:
print(straight_count, "Straight flush!")
if Full_house:
print(FH_count, "Full house!")
def your_move(pot, you_fold, bet_amount, comp_raise):
if you_fold == False:
option = input("Bet, Call, Check, Fold")
if option.lower() == "bet":
bet_amount = int(input("How much do you want to bet?"))
pot += bet_amount
print("You bet", bet_amount)
print("Pot is", pot)
elif option.lower() == "check":
pass
elif option.lower() == "call":
pot += comp_raise
elif option.lower() == "fold":
print("Fold. Computer wins!")
you_fold = True
else:
print("That's not a valid move. Try again")
your_move(pot, you_fold, bet_amount)
else:
return you_fold
def computer_move(pot, comp_fold, bet_amount):
comp_action = random.randint(0,9)
comp_raise = random.randint(10,100)
starter = 1
while starter == 1:
if comp_action <= 8 and 6 <= comp_action:
comp_action = random.randint(0,9)
else:
starter = 0
if comp_action < 2:
print("Computer has raised it by", comp_raise)
pot += comp_raise
elif comp_action <= 5 and 2 <= comp_action:
print("Computer calls")
pot += bet_amount
elif comp_action <= 8 and 6 <= comp_action:
print("Computer checks")
elif comp_action == 9:
print("Computer folds. You win!")
comp_fold = True
return comp_fold
deck_maker()
P1_hand = []
dealers = []
played_check1 = []
deal_cards(played_check1, 5)
played_checker(played_check1)
check_strength(played_check1)
deal_cards(P1_hand, 2)
print_hand(P1_hand)
check_strength(P1_hand)
deal_cards(dealers, 2)
#print_hand(dealers)
check_strength(dealers)
played_check2 = played_check1.copy()
played_checker(played_check2)
check_strength(played_check2)
showed_cards = played_check1.copy()
#print_hand(showed_cards)
you_fold = False
comp_fold = False
bet_amount = 0
showed_no = 0
comp_raise = 0
buy_in = int(input("Buy in: "))
pot = buy_in
while you_fold == False:
print("Pot is", pot)
your_move(pot, you_fold, bet_amount, comp_raise)
computer_move(pot, comp_fold, bet_amount)
if 5 <= showed_no:
pass
if comp_fold == True:
print("You win:", pot)
以下是程序如何工作的示例:
you_fold
和comp_fold
的值不变的原因是Python中的函数内部默认使用的所有变量都是本地的。如果要使用全局变量,则需要使用global a, b, c
表达式。在您的情况下,您可能不会将任何参数传递给 your_move
和 computer_move
函数,而是执行以下操作:
def your_move():
global pot, you_fold, bet_amount, comp_raise
# further code remains unchanged
它解决了你的无限游戏问题。关于你的底池问题,我建议声明变量
to_call
或类似的东西,以便跟踪拨打电话所需的当前金额。然后,当您初始化 buy_in
变量时(我不明白为什么需要它),您应该将相同的值设置为 to_call
:
buy_in = int(input("Buy in: "))
pot = buy_in
to_call = buy_in
您可以在
to_call
计算中使用 pot
变量。我认为不需要全局 comp_raise
变量,也不需要 bet_amount
。它们可以用作函数中的局部变量。当您或 comp“调用”时,您可以执行此操作来计算新的 pot
和 to_call
值:
pot += to_call
to_call = 0
当有人“下注”时,其他人需要投入
to_call + bet_amount
金额才能使赌注相等。而且 to_call
值现在等于 bet_amount
:
pot += to_call + bet_amount
to_call = bet_amount
这些改变将解决
pot
计算的问题。
我还想给你一些关于代码风格的建议。
首先,你可以用Python编写更简单的变量或语句的真值检查:
if comp_fold == True: # Bad style
if comp_fold: # Good style
while you_fold == False: # Bad style
while not you_fold: # Good style
其次,检查值是否在范围内也可以写得更简单:
elif comp_action <= 5 and 2 <= comp_action: # Bad style
elif 2 <= comp_action <= 5: # Better
此外,在
your_move
函数中还有更好的方法来处理不同的命令。您可以使用 模式匹配,而不是使用多个
if/else
语句。所以,你的代码将如下所示:
# added \n character to take the input from the new line
option = input("Bet, Call, Check, Fold\n")
# Split option to words and match different cases
match option.lower().strip().split():
# square brackets needed because of .split() which gives us the list of strings
case ["bet", amount]:
# bet_amount = int(input("How much do you want to bet? "))
pot += to_call + int(amount)
to_call = int(amount)
print("You bet", to_call)
print("Pot is", pot)
case ["check"]:
pass
case ["call"]:
pot += to_call
print("You call", to_call)
to_call = 0
print("Pot is", pot)
case ["fold"]:
print("Fold. Computer wins!")
you_fold = True
case _:
print("That's not a valid move. Try again")
your_move()
这看起来好多了,也让您有机会相当轻松地处理一些复杂的命令。例如,上面的代码允许您将“bet”命令写在一行中,即
bet 100
。
最后,我建议您重构代码,使所有全局变量和使用它们的函数成为一个类的成员。它将使您的代码更易于调试并且更可重用。它看起来像:
class Game:
def __init__(self):
self.you_fold = False
self.comp_fold = False
self.pot = 0
self.to_call = 0
self.bet_amount = 0
def your_move(self):
...
def computer_move(self):
...
def play(self):
while not self.you_fold:
self.your_move()
self.computer_move()
...
game = Game()
game.play()