我正在开发一个简单的扑克游戏只是为了提高我的OOP体验,但我遇到了一个错误,这可能是我所无法理解的。
程序是这样的,我有一个叫做Deck()的类,它当然是套牌,一个叫player的类和一个叫table的类。在播放器类中,我有几种方法,但是我只想说出什么是使它尽可能短的必要条件。我有以下方法(在播放器类中)bet(),call()fold(),all_in()和player_choice()。我命名的前四个方法全部由player_choice method()调用。但是问题出在bet(),all_in()和call方法中。以下方法看起来像这样
def all_in(self):
table.table_money += self.money
self.money = 0
print('{} went all in!').format(self.name)
def cal(self, last_bet):
if last_bet > self.money:
while True:
res = input('You dont have enough money to call.\tyou have=> {}\nDo you want to go all in?\ty/n\n>>> ').format(str(self.money))
if res.lower() == 'y':
self.all_in()
break
elif res.lower() == 'n':
self.fold()
break
else:
print('Invalid input. Try again...')
else:
table.table_money += self.money - last_bet
self.money -= last_bet
print('{} called.').format(self.name)
def bet(self, last_bet):
res = int(input('Enter bet below.\t must be over {}\n>>> '.format(str(last_bet))))
table.latest_bet += res
table.table_money += res
self.money -= res
print('{} has bet {}$').format(self.name, str(res))
问题是当我调用这些方法之一并且该方法正在减去玩家的钱并将其添加到table.table_money()(如上面的代码所示)的过程中,即当我得到NameError时说“表”未定义。
这是Table()类:
class Table():
def __init__(self):
self.table_money = 0
self.latest_bet = 0
if players == '':
print('Table is empty!')
else:
print(str(len(players)) + ' players on the table.')
def list_table(self):
for i in players:
print(i.__str__())
我知道您可能在想我键入table而不是Table。但是他是这样,当我在main()函数中运行代码时,我将Table对象初始化为一个名为table的变量,该变量应该与Player()类方法所调用的变量相同。>
这是main()函数的简短视图:
def main():
while welcome():
rond = 1
while rond <= 5:
table = Table()
deck = Deck()
deck.deal()
user = players[0]
user.list_hand()
user.player_choice(table.latest_bet)
break
break
问题的结构如下:
这是错误的输出:
Traceback (most recent call last):
File "C:/Users/Alex/PycharmProjects/GUI_prject/venv/Lib/site-
packages/resors2.py", line 161, in <module>
main()
File "C:/Users/Alex/PycharmProjects/GUI_prject/venv/Lib/site-
packages/resors2.py", line 157, in main
user.player_choice(table.latest_bet)
File "C:/Users/Alex/PycharmProjects/GUI_prject/venv/Lib/site-
packages/resors2.py", line 38, in player_choice
self.cal(last_bet)
File "C:/Users/Alex/PycharmProjects/GUI_prject/venv/Lib/site-
packages/resors2.py", line 71, in cal
table.table_money += self.money - last_bet
NameError: name 'table' is not defined
如果我没有提供有关程序的足够详细信息,请告诉我,如果需要,我将分享更多信息。如果我没有以最好的方式解释所有这些内容,并且帖子过长,我深表歉意。接下来,我将不胜感激。 感谢您的阅读。
完整代码:
import random as r
suits = ['H','C','S','D']
values = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
players = []
class Card():
def __init__(self, suit, value):
self.suit = suit
self.value = value
def valu(self):
return self.value + self.suit
class Player():
def __init__(self, name, money):
global players
self.name = name
self.money = money
print(self.name+ ' joined the table!')
players.append(self)
self.hand = []
def __str__(self):
return '{}: {}'.format(self.name, str(self.money))
def add_to_hand(self, cards):
self.hand.append(cards)
def list_hand(self):
for i in self.hand:
print(i)
def player_choice(self, last_bet):
while True:
resu = input('c - Call | b - bet | a - all in | f - fold\n>>>
').lower()
if resu == 'c':
self.cal(last_bet)
break
elif resu == 'b':
self.bet(last_bet)
break
elif resu == 'a':
self.all_in()
break
elif resu == 'f':
self.fold()
break
else:
print('Invalid input. Try again...')
def all_in(self):
table.table_money += self.money
self.money = 0
print('{} went all in!').format(self.name)
def cal(self, last_bet):
if last_bet > self.money:
while True:
res = input('You dont have enough money to call.\tyou have=> {}\nDo you want to go all in?\ty/n\n>>> ').format(str(self.money))
if res.lower() == 'y':
self.all_in()
break
elif res.lower() == 'n':
self.fold()
break
else:
print('Invalid input. Try again...')
else:
table.table_money += self.money - last_bet
self.money -= last_bet
print('{} called.').format(self.name)
def bet(self, last_bet):
res = int(input('Enter bet below.\t must be over {}\n>>> '.format(str(last_bet))))
table.latest_bet += res
table.table_money += res
self.money -= res
print('{} has bet {}$').format(self.name, str(res))
def fold(self):
players.remove(self)
print('{} has folded.').format(self.name)
class Table():
def __init__(self):
self.table_money = 0
self.latest_bet = 0
if players == '':
print('Table is empty!')
else:
print(str(len(players)) + ' players on the table.')
def list_table(self):
for i in players:
print(i.__str__())
class Deck():
def __init__(self):
self.cards = []
for suit in suits:
for value in values:
self.cards.append(Card(suit, value).valu())
r.shuffle(self.cards)
def show_deck(self):
return self.cards
def shuffle_d(self):
r.shuffle(self.cards)
def deal(self):
for p in players:
for c in range(0, 1):
p.add_to_hand((self.cards.pop(), self.cards.pop()))
#The welcome function is not a method for any of the classes above
def welcome():
global players
print('''
Welcome to the poker table.
q - Quit
s - Take a seat
''')
res = ''
while res != 'q' or 's':
res = input('>>> ')
if res == 'q':
return False
elif res == 's':
name = input('Enter your name: ')
money = int(input('Enter how much money you wish to have(max 500$): '))
Player(name, money)
return True
else:
print('Invalid input. q - Quit\ts - Take a seat')
def main():
while welcome():
rond = 1
while rond <= 5:
table = Table()
deck = Deck()
deck.deal()
user = players[0]
user.list_hand()
user.player_choice(table.latest_bet)
break
break
main()
尝试忽略未命中
答案 0 :(得分:1)
tables
是main()
函数中的局部变量。您应该将其添加为player
类的属性。
def go_to_table(self, table):
self.table = table
然后,所有方法都应使用self.table
而不是table
。一般来说,您可以这样做:
def main():
while welcome():
rond = 1
while rond <= 5:
table = Table()
deck = Deck()
deck.deal()
user = players[0]
user.go_to_table(table)
user.list_hand()
user.player_choice(table.latest_bet)
break
break
答案 1 :(得分:0)
您将类型(如Table
)与对象的单个实例(如table
之后的table = Table()
混淆了。
行table = Table()
创建一个类型为Table
的新对象,并将其分配给变量table
,因此您以后可以引用它。但是,此变量在函数main
内定义,仅在此处可用。在方法cal
(我假设是call
的拼写错误)内部,没有table
,因为您可能正在进行很多游戏,而且它不知道它在哪个表上属于。
由于玩家坐在特定的桌子上,因此在Table
内保留对Player
的引用,并在其构造函数中为其分配诸如self.table = table
之类的内容是很有意义的。 。然后,您可以使用call
从self.table
方法中引用它。
class Player:
def __init__(self, table):
self.table = table
def call(self):
self.table.money += 1
class Table:
def __init__(self):
self.money = 0
table = Table()
player = Player(table)
player.call()
print(table.money)