簡體   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