我是 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() 但我不知道该放什么。 感谢您的帮助
答案 0 :(得分: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)
答案 1 :(得分:0)
在您的 main()
函数中,当您初始化 GameState()
时,您需要传入将用作 brett
的 something,然后将其分配给self.Board
内的 GameState.__init__(self,brett)
。
换句话说,在您的 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):
...