【问题标题】:Python Problem: TypeError: __init__() missing 1 required positional argument: 'brett'Python 问题:TypeError:__init__() 缺少 1 个必需的位置参数:'brett'
【发布时间】:2021-05-26 18:43:38
【问题描述】:

我是 Python 新手,我正在尝试编写一个棋盘。我按照教授的步骤进行操作,现在我想实现从串行输入(Arduino)获取板(零件所在的位置)的可能性。不知何故,我得到了越来越多的错误。

在我查了所有之后,我无法解决这个问题:

TypeError: init() 缺少 1 个必需的位置参数:'brett'。

这是我的董事会准则:

import serial

class GameState:

    def __init__(self,brett):

        self.Board = [brett]
        self.moveFunctions = {"p": self.getPawnMoves, "R": self.getRookMoves, "N": self.getKnightMoves,
                              "B": self.getBishopMoves, "Q": self.getQueenMoves, "K": self.getKingMoves}
        self.white_to_move = True
        self.move_log = []
        self.white_king_location = (7, 4)
        self.black_king_location = (0, 4)
        self.checkmate = False
        self.stalemate = False
        self.in_check = False
        self.pins = []
        self.checks = []
        self.enpassant_possible = ()  # coordinates for the square where en-passant capture is possible
        self.enpassant_possible_log = [self.enpassant_possible]
        self.current_castling_rights = CastleRights(True, True, True, True)
        self.castle_rights_log = [CastleRights(self.current_castling_rights.wks, self.current_castling_rights.bks,
                                               self.current_castling_rights.wqs, self.current_castling_rights.bqs)]

    def brett(self):
        if __name__ == '__main__':
            ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
            ser.flush()
            while True:
                if ser.in_waiting > 0:
                    brett = ser.readline().decode('utf-8').rstrip()
                    print(brett)
        else:
            brett = ['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],['bp'] * 8,['--'] * 8, ['--'] * 8,['--'] * 8,['--'] * 8,['wp'] * 8,['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR']
            print(brett)

和我的实际程序的主要内容:

def main():
    """
    The main driver for our code.
    This will handle user input and updating the graphics.
    """
    p.init()
    screen = p.display.set_mode((BOARD_WIDTH + MOVE_LOG_PANEL_WIDTH, BOARD_HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    game_state = ChessEngine.GameState()
    valid_moves = game_state.getValidMoves()
    move_made = False  # flag variable for when a move is made
    animate = False  # flag variable for when we should animate a move
    loadImages()  # do this only once before while loop

    running = True
    square_selected = ()  # no square is selected initially, this will keep track of the last click of the user (tuple(row,col))
    player_clicks = []  # this will keep track of player clicks (two tuples)
    game_over = False

    white_did_check = ""
    black_did_check = ""
    last_move_printed = False
    moves_list = []
    move_log_font = p.font.SysFont("Arial", 14, False, False)

    turn = 1

    player_one = True  # if a human is playing white, then this will be True, else False
    player_two = False  # if a hyman is playing white, then this will be True, else False

它告诉我问题出在 game_state = ChessEngine.GameState() 但我不知道该放什么。 谢谢你的帮助

【问题讨论】:

  • ChessEngine.GameState(someBrett) 你将 init 设置为接收“brett”参数,所以在调用它时,传递一个
  • def brett(self): 中的代码是什么,为什么会有if __name__ == '__main__': ??
  • 我不明白 p 在来自 p.init() 的代码中是什么。它是在哪里创建的?您能否将问题改写为您遇到的错误,以便其他人可以复制它并清楚地发送您要解决的问题。
  • def brett 代码用于接收新的棋盘布局(移动后棋子站立的 8x8 阵列)。我不知道它为什么在那里,它只是在我找到的示例序列代码中,它就像这样工作,所以我正在使用它。我不能改变 ChessEngine.GameState(someBrett) 因为它会改变每一步。我的目标是让玩家的每一步都获得一个新的布雷特,然后我希望董事会改变

标签: python arduino chess


【解决方案1】:

问题在于,在main() 中,game_state 是在没有brett 参数的情况下初始化的:

game_state = ChessEngine.GameState()

尽管您的 ChessEngine.GameState 类的对象需要 brett 参数进行初始化:

class GameState:
    def __init__(self,brett):
        self.Board = [brett]

因此,在main() 中执行类似的操作:

game_state = ChessEngine.GameState(value)

【讨论】:

  • 感谢您的快速帮助!可悲的是我不能做 game_state = ChessEngine.GameState(value),因为我还不知道值。 arduino 的每一步我都会得到一个新的板
  • 在这种情况下,我建议遵循 p3j4p5 的建议并在没有 brett 属性的情况下初始化 GameState,并将 brett 移出 init 方法。
【解决方案2】:

在您的main() 函数中,当您初始化GameState() 时,您需要传入将用作brett一些东西,然后将其分配给@987654325 内的self.Board @。

换句话说,在您的main() 函数中,行:

def main():
    ...
    game_state = ChessEngine.GameState()
    ...

需要替换为:

    ...
    game_state = ChessEngine.GameState(something_that_will_be_brett)
    ...

另一方面,GameState.Board 似乎没有用于任何用途,所以也许您可以删除 Board 并进行更改:

class GameState:
    def __init__(self,brett):
        ...

class GameState:
    def __init__(self):
        ...

【讨论】:

  • 感谢您的快速帮助!在我的程序中,当我编写 self.Board[....(8x8 array)...] 时,一切正常。现在我想从 arduino 中获取这个 8x8 数组,我调用了传入数组 brett。我现在像你说的那样改变了它,但它只是不想工作。现在它告诉我:```` 第 300 行,在 checkForPinsAndChecks end_piece = self.board[end_row][end_col] IndexError: list index out of range。即使 brett 的信息与以前工作的数组完全相同。我真的无法理解,因为对于计算机而言,信息应该是相同的。
  • 我建议检查end_rowend_col 的值(如果可以的话),并确保它们都小于8(即最大为7)。
猜你喜欢
  • 2019-06-25
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
相关资源
最近更新 更多