def valid_moves(self, location, state): moves = [] dirs = [(2, -1), (2, 1), (1, -2), (1, 2), (-1, -2), (-1, 2), (-2, -1), (-2, 1)] for _dir in dirs: new_row = location[0] + _dir[0] new_col = location[1] + _dir[1] if not valid_position((new_row, new_col)): continue attacking = state.board[new_row][new_col] if isinstance(attacking, Piece) and attacking.color == self.color: continue moves.append((new_row, new_col)) return moves
def valid_moves(self, location, state): moves = [] for i in [-1, 0, 1]: for j in [-1, 0, 1]: new_row = location[0] + i new_col = location[1] + j if (i == 0 and j == 0) or not valid_position((new_row, new_col)): continue attacking = state.board[new_row][new_col] if isinstance(attacking, Piece) and attacking.color == self.color: continue moves.append((new_row, new_col)) return moves pass
def moves_helper(self, location, state, row_dir, col_dir): ''' Helper function to compute the valid moves in a given direction ''' moves = [] for i in range(1, 8): new_row = location[0] + (row_dir * i) new_col = location[1] + (col_dir * i) if not valid_position((new_row, new_col)): break attacking = state.board[new_row][new_col] if isinstance(attacking, Piece): if attacking.color == self.color: break else: # We still add this state as a valid move, but we break out of # the loop afterwards moves.append((new_row, new_col)) break moves.append((new_row, new_col)) return moves
def valid_moves(self, location, state): moves = [] # if we are on the first row, we can move forward if we are not blocked by # another piece if location[0] == 1 and not isinstance(state.board[2][location[1]], Piece) and \ not isinstance(state.board[3][location[1]], Piece): moves.append((3, location[1])) for i in [-1, 0, 1]: new_row = location[0] + 1 new_col = location[1] + i if not valid_position((new_row, new_col)): continue attacking = state.board[new_row][new_col] attacking_piece = isinstance(attacking, Piece) # we need to be attacking an opponents piece to move diagonally if i != 0 and not attacking_piece: continue if attacking_piece and attacking.color == self.color: continue if i == 0 and attacking_piece: continue moves.append((new_row, new_col)) return moves