Python: list does not execute diagonally

I am implementing the Ptheon Othello / reversi version of the game, but my algorithm is having trouble searching in SouthWest Direction.

Here are some important features to understand how my current code works:

def _new_game_board(self)->[[str]]:
    board = []
    for row in range(self.rows):
        board.append([])
        for col in range(self.columns):
            board[-1].append(0)
    return board
def _is_valid_position(self, turn:list)->bool:
        '''return true if the turn is a valid row and column'''
        row = int(turn[0]) - 1
        column = int(turn[1]) - 1
        if row >= 0:
            if row < self.rows:
                if column >= 0:
                    if column < self.columns:
                        return True
        else:
            return False


def _is_on_board(self, row:int, col:int)->bool:
    '''returns true is coordinate is on the board'''
    if row >=0:
        if row < self.rows:
            if col >=0:
                if col < self.columns:
                    return True




def _searchNorthEast(self)->None:
    '''Search the board NorthEast'''
    print("NorthEast")
    row = self.move_row
    column = self.move_column
    should_be_flipped = list()
    row += 1
    column -= 1
    if self._is_on_board(row, column):
         print("column searching NorthEast on board")
         if self.board[row][column] == self._opponent:
             should_be_flipped.append([row, column])
             while True:
                row += 1
                column -= 1
                if self._is_on_board(row, column):
                    if self.board[row][column] == self._opponent:
                        should_be_flipped.append([row, column])
                        continue
                    elif self.board[row][column] == self.turn:
                        self._to_be_flipped.extend(should_be_flipped)
                        break
                    else:
                        break
                else:
                    self._to_be_flipped.extend(should_be_flipped)
    else:
        pass

    def _searchSouthWest(self)->None:
    '''Search the board SouthWest'''
    print("in SouthWest")
    row = self.move_row
    column = self.move_column
    should_be_flipped = list()
    row -= 1
    column += 1
    if self._is_on_board(row, column):
         print("column searching SouthWest on board")
         if self.board[row][column] == self._opponent:
             should_be_flipped.append([row, column])
             while True:
                row -= 1
                column += 1
                if self._is_on_board(row, column):
                    if self.board[row][column] == self._opponent:
                        should_be_flipped.append([row, column])
                        continue
                    elif self.board[row][column] == self.turn:
                        self._to_be_flipped.extend(should_be_flipped)
                        break
                    else:
                        break
                else:
                    self._to_be_flipped.extend(should_be_flipped)
    else:
        pass

def _move_is_valid(self, turn:list)->bool:
    '''Verify move is valid'''
    self._to_be_flipped = list()
    self._opponent = self._get_opposite(self.turn)
    if self._is_valid_position(turn):
        self.move_row = int(turn[0]) - 1
        self.move_column = int(turn[1]) - 1
        self._searchRight()
        self._searchLeft()
        self._searchUp()
        self._searchDown()
        self._searchNorthWest()
        self._searchNorthEast
        self._searchSouthEast()
        self._searchSouthWest()
        if len(self._to_be_flipped) > 0:
            return True
    else:
         return False

      

Now let's say that the current board looks like this:

. . . .
W W W .
. B B .
. B . .

Turn: B

      

and the player makes a jump to column 1 of row 1, it says invalid as it does not detect that the white part in column 2 of row 2 is flipped. All my other functions are written the same way I can get it to work in all other directions except in this case.

Any ideas why it doesn't detect this detail in this diagonal direction?

+3


source to share


1 answer


Don't repeat yourself. The methods are _search*

extremely redundant, which makes it difficult to understand that the signs in

row -= 1
column += 1

      



are correct. Since you only listed two directions (NE, SW) and the board orientation documentation, I cannot tell if the signs agree with the board layout or even agree with themselves.

The methods are _search*

also too large and should be split into multiple functions, but this is a minor issue.

0


source







All Articles