繁体   English   中英

Python 问题:TypeError:__init__() 缺少 1 个必需的位置参数:'brett'

[英]Python Problem: TypeError: __init__() missing 1 required positional argument: 'brett'

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

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

类型错误: 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() 但我不知道该放什么。 谢谢您的帮助

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

game_state = ChessEngine.GameState()

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

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

因此,在main()中做这样的事情:

game_state = ChessEngine.GameState(value)

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

换句话说,在您的main() function 中,该行:

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):
        ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM