Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

Making the game Checkers in python with pygame but are running into minor logical errors

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I recommend simplifying your code. The player's movement can be calculated by multiplying x and y by two.
Do not remove the opponent's piece when evaluating possible moves. It's too early and it removes all opponents along possible moves:

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:

        self.white_valid_moves.clear()
        for d in self.directions_1:
            x, y = d
            
            if self.board[start_row + x][start_column + y] == 0:
                self.white_valid_moves.append((x, y))
            
            elif self.board[start_row + x][start_column + y] == player1:
                self.white_valid_moves.append((x*2, y*2))

You must remove the opponent when the player is moved:

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:
            valid_moves = self.red_valid_moves
        else
            valid_moves = self.white_valid_moves
        
        dx = end_column - start_column
        dy = end_row - start_row
        
        if (dx, dy) in valid_moves: 
            self.board[start_row][start_column] = 0
            self.board[start_row + dx//2][start_column + dy//2] = 0
            self.board[end_row][end_column] = self.turn

    self.get_turn()

    # [...]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...