【发布时间】: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) 因为它会改变每一步。我的目标是让玩家的每一步都获得一个新的布雷特,然后我希望董事会改变