【问题标题】:Python Code working outside the function and class but not insidePython 代码在函数和类之外工作,但不在内部
【发布时间】:2020-08-24 10:32:30
【问题描述】:

我正在尝试制作 Connect 4 游戏。这是我的代码:

board = {}

for i in range(1,8):
    board[f'{i}_column'] = {}

    for a in range(1,7):
        board[f'{i}_column'][f'{i}x{a}_position'] = 'Empty'

class Checker:
    def __init__(self,color):
        self.color = color

    def find_column_amount(self,column):
        self.column_num = 0
        self.column_full = False
        for i in range(0,6):
            if board[f'{column}_column'][f'{column}x{6-i}_position'] != 'Empty':
                self.column_num += 1
            else:
                pass
        if self.column_num >= 6:
            self.column_full = True

    def place(self,column):
        self.find_column_amount(column)
        if self.column_full == False:
            if column <= 7 and column > 0:
                board[f'{column}_column'][f'{column}x{6-self.column_num}_position'] = self.color.title()
                else:
                print('You\'re out of the range!')
        else:
            print(f'Column {column} is full!\nTry another one!')

    def check_win(self):
        for d in range(1,7):
            for c in range(1,5):
                for b in range(c,c+4):
                    vars()[f'value_{b-c+1}'] = board[f'{b}_column'][f'{b}x{d}_position']

                if value_1 == value_2 == value_3 == value_4 and (value_1 and value_2 and value_3 and value_4) != 'Empty':
                    self.win()

    def win(self):
        print('You won!')

为了看看它是否有效,我运行了这个:

p1 = Checker('red')
p1.place(1)
p1.place(2)
p1.place(3)
p1.place(4)
p1.check_win()

我尝试了这段代码,但没有成功。错误的部分是 check_win 函数。我在函数外测试了代码,并将 self.win() 更改为 print('You won!') 并且成功了。

for d in range(1,7):
    for c in range(1,5):
        for b in range(c,c+4):
            vars()[f'value_{b-c+1}'] = board[f'{b}_column'][f'{b}x{d}_position']

        if value_1 == value_2 == value_3 == value_4 and (value_1 and value_2 and value_3 and value_4) != 'Empty':
            print('You won!')

结果是这样的:

You won!

当我再次将其插入该功能时,它不起作用。我不知道我做错了什么。谁能告诉我如何解决这个问题?

【问题讨论】:

  • 这:(value_1 and value_2 and value_3 and value_4) != 'Empty 不是您将多个变量与一个值进行比较的方式。
  • 动态生成的字典键和vars() 是一种非常复杂的二维数组存储方式。我将四处走动,并猜测那里的某些东西引起了问题。

标签: python function


【解决方案1】:

我只是快速浏览了这段代码,并用更简单的代码替换了所有让我头疼的东西,这些代码看起来就像原始代码试图做的那样。 (这肯定可以进一步简化,但我只是为了让它运行而努力实现低垂的果实。)现在似乎工作了,重写这些代码比尝试调试它们更快。

board = [[None for a in range(7)] for i in range(8)]


class Checker:
    def __init__(self, color):
        self.color = color

    def find_column_amount(self, column):
        self.column_num = 0
        self.column_full = False
        for i in range(0, 6):
            if board[column][6-i] is not None:
                self.column_num += 1
            else:
                pass
        if self.column_num >= 6:
            self.column_full = True

    def place(self, column):
        self.find_column_amount(column)
        if not self.column_full:
            if column <= 7 and column > 0:
                board[column][6-self.column_num] = self.color.title()
            else:
                print('You\'re out of the range!')
        else:
            print(f'Column {column} is full!\nTry another one!')

    def check_win(self):
        for d in range(1, 7):
            for c in range(1, 5):
                values = {board[b][d] for b in range(c, c+4)}
                if len(values) == 1 and values.pop() is not None:
                    self.win()

    def win(self):
        print('You won!')


p1 = Checker('red')
p1.place(1)
p1.place(2)
p1.place(3)
p1.place(4)
p1.check_win()  # prints "You won!" now

【讨论】:

  • 在高悬的情况下,每次放置令牌时都应执行check_win,它可以在 8 个适当的方向上检查邻居,而不是检查所有的棋盘。编辑:这是对 OP 的评论,而不是您的实际答案。
  • 另外,将棋盘作为一个独立的对象来处理双方的检查和移动会更有意义,而不是只为一个玩家操作的 Checker 类和以全局身份访问董事会。
  • 您还可以使连续检查 N 个相同部分的概念更加通用:stackoverflow.com/a/61395017/3799759
  • 此外,如果列是列表而不是字典,则整个 find_column_amount 函数可能是对 index 的一次调用。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多