The problem is when this valid moves function runs and goes through the 2 tuples it only ever appends the second tuple when both of the tuples should be appended as valid moves.
For example the first move on the board, both (1, 1) and (-1, 1) should be allowed but only (-1, 1) is allowed always vice versa for the other player the coordinates are just different( (-1, -1) and (1, -1) ).
In checkers if there is a opponent piece on your diagonal you can jump over it diagonally and take it. This works fine but again only for the second tuple. Ive stepped through it in the debugger and it just passes by the the first tuple. I don't know why this may be and I've been stuck for days. The board is a big list with 8 more lists for rows. Just to clarify the problem is in this method since the piece can move (1,1) but never
(-1, 1)
The get mouse pos simply iterates through the list and checks if a move is allowed. The get turn function just changes the turn accordingly.
The last bit is from the main file, its the only thing I link to the problem but in the debugger it seems theres something wrong with the valid_moves functions.
self.directions_1 = [(-1, -1), (1, - 1)]
def valid_moves(self, start_x, start_y):
"""
Find all of the valid moves for a selected piece and append them to a list
"""
start_column = int(start_x // SQUARESIZE)
start_row = int(start_y // SQUARESIZE)
if self.turn == player2:
for d in self.directions_1:
x, y = d
self.white_valid_moves.clear()
if self.board[start_row + x][start_column + y] == 0:
self.white_valid_moves.append((x, y))
if self.board[start_row + x][start_column + y] == player1:
if x == -1 and y == -1:
self.white_valid_moves.append((x - 1, x - 1))
self.board[start_row + x][start_column + y] = 0
if x == 1 and y == -1:
self.white_valid_moves.append((x + 1, y - 1))
self.board[start_row + x][start_column + y] = 0
def get_mouse_pos_and_place(self, start_x, start_y, end_x, end_y):
"""
Takes care of placing a piece and iterating through the valid moves lists
"""
start_column = int(start_x // SQUARESIZE)
start_row = int(start_y // SQUARESIZE)
end_column = int(end_x // SQUARESIZE)
end_row = int(end_y // SQUARESIZE)
if (self.board[start_row][start_column]) in (player1, player2):
if self.turn == player1:
for x, y in self.red_valid_moves:
print(self.red_valid_moves)
if (x == start_column - end_column) and (y == start_row - end_row):
self.board[start_row][start_column] = 0
self.board[end_row][end_column] = player1
elif self.turn == player2:
for x, y in self.white_valid_moves:
print(self.white_valid_moves)
if (x == start_column - end_column) and (y == start_row - end_row):
self.board[start_row][start_column] = 0
self.board[end_row][end_column] = player2
self.get_turn()
if event.type == pygame.MOUSEBUTTONDOWN:
start_x = event.pos[0]
start_y = event.pos[1]
if event.type == pygame.MOUSEBUTTONUP:
end_x = event.pos[0]
end_y = event.pos[1]
board.valid_moves(start_x, start_y)
board.get_mouse_pos_and_place(start_x, start_y, end_x, end_y)
board.print_board()
board.draw_board(screen)
board.draw_checkers(screen)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…