def test_get_links_contains_cell_when_linked(): cell_A = Cell(0, 0) cell_B = Cell(1, 1) cell_A.link(cell_B) links = cell_A.links assert cell_B in links
def test_neighbours_contains_cell_if_neighbour(): cell_A = Cell(0, 0) cell_B = Cell(1, 1) cell_A.north = cell_B neigbours = cell_A.neighbors assert cell_B in neigbours
def test_unlink_cells_bidirectional_unlinks_both_cells(): cell_A = Cell(0, 0) cell_B = Cell(1, 1) cell_A.link_biderectional(cell_B) cell_A.unlink_bidirectional(cell_B) assert not cell_A.is_linked(cell_B) assert not cell_B.is_linked(cell_A)
def get_neighbor_encoding(self, cell: Cell): arr = [] x, y = cell.get_x(), cell.get_y() for dx in range(-1, 2): for dy in range(-1, 2): if not dx and not dy: continue if self.in_bound(x + dx, y + dy): arr.append(self.map[x + dx][y + dy]) else: arr.append(None) return [CELL_ENCODE[decode(c)] for c in arr]
def test_link_cell_links_cell(): cell_A = Cell(0, 0) cell_B = Cell(1, 1) cell_A.link(cell_B) assert cell_A.is_linked(cell_B) assert not cell_B.is_linked(cell_A)
def __init__(self, index: int, randomize=False): super().__init__(index, print_turn=False) self.training_agent: TrainingAgent = None self.moving_agent: MovingAgent = None self.map = [[Cell(x, y) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)] self.my_units = {} self.enemy_unit_num = 0 self.actions = [] self.population = None self.id = AIBot.ID self.uid = "" self.randomize_onplay = randomize self.sess = None AIBot.ID += 1 # print(f'AIBot#{self.id} constructor called') # Connect the nodes for x in range(MAP_WIDTH): for y in range(MAP_HEIGHT): self.in_bound(x, y - 1) and self.map[x][y].set_neighbor( UP, self.map[x][y - 1]) self.in_bound(x, y + 1) and self.map[x][y].set_neighbor( DOWN, self.map[x][y + 1]) self.in_bound(x + 1, y) and self.map[x][y].set_neighbor( RIGHT, self.map[x + 1][y]) self.in_bound(x - 1, y) and self.map[x][y].set_neighbor( LEFT, self.map[x - 1][y])
def __init__(self, min_val=1, max_val=13, suit=4): self._deck = Deck(min_val, max_val, suit) self._cells = [] # creates list of cells self._tableaus = [] # creates list of tableaus self._foundations = [] # creates list of foundations self._matrix = [] # initial matrix list self._max_length = 0 # this will adjust based on the maximum length object of tableau # create 4 cells, n foundations and 8 tableaus objects # the number of foundation will have a default of 4 if the number of suit less than or equal to 4 for i in range(0, 8): if i < 4: cell = Cell(i) self._cells.append(cell) foundation = Foundation(i) self._foundations.append(foundation) tableau = Tableau(i) self._tableaus.append(tableau) # if the number of suit more than 4, the number of foundation will follow i = 4 while i < suit: foundation = Foundation(i) self._foundations.append(foundation) i += 1 self._deck.shuffle_card() self.distribute_card()
def get_neighbor_move_encoding(self, unit: Unit, cell: Cell): arr = [] x, y = cell.get_x(), cell.get_y() for dx in range(-1, 2): for dy in range(-1, 2): if not dx and not dy: continue if self.in_bound(x + dx, y + dy): # Might need to remove this if not self.map[x + dx][y + dy].is_capturable( ME, unit.get_level()): arr.append(None) else: arr.append(self.map[x + dx][y + dy]) else: arr.append(None) return [CELL_ENCODE[decode(c)] for c in arr]
def __init__(self, rows: int, cols: int) -> None: self.cells = { row: {col: Cell(row, col) for col in range(0, cols)} for row in range(0, rows) } # self.cells = [self.Row(row, cols) for row in range(0, rows)] self.nr_rows = rows self.nr_cols = cols self._row = 0 self._col = -1 self._configure_cells()
def __init__(self, rows, cols): """ Make a grid graph of "rows" rows and "cols" columns """ self._rows = rows self._cols = cols self._grid = [] for row in xrange(0, rows): line = [] for col in xrange(0, cols): line.append(Cell(self, row, col)) self._grid.append(line)
def decode(cell: Cell): if not cell: return "#" if cell.get_owner() == ME: return "O" if cell.is_active() else "o" elif cell.get_owner() == ENEMY: return "X" if cell.is_active() else "x" elif cell.get_owner() == VOID: return "#" elif cell.get_owner() == NEUTRAL: return "." else: raise KeyError()
def test_new_cell_has_no_links(): cell = Cell(0, 0) assert len(cell.links) == 0
def build( mesh_file: str, field_dimension: int, face_polynomial_order: int, cell_polynomial_order: int, operator_type: str, ): """ ==================================================================================================================== Description : ==================================================================================================================== ==================================================================================================================== Parameters : ==================================================================================================================== ==================================================================================================================== Exemple : ==================================================================================================================== """ # ------------------------------------------------------------------------------------------------------------------ # Checking polynomial order consistency # ------------------------------------------------------------------------------------------------------------------ # is_polynomial_order_consistent(face_polynomial_order, cell_polynomial_order) # ------------------------------------------------------------------------------------------------------------------ # Reading the mesh file, and extracting conectivity matrices # ------------------------------------------------------------------------------------------------------------------ ( problem_dimension, vertices, cells_vertices_connectivity_matrix, faces_vertices_connectivity_matrix, cells_faces_connectivity_matrix, cells_connectivity_matrix, nsets, nsets_faces, ) = parse_mesh(mesh_file) # ------------------------------------------------------------------------------------------------------------------ # Writing the vertices matrix as a numpy object # ------------------------------------------------------------------------------------------------------------------ vertices = np.array(vertices) # print("vertices : \n{}".format(vertices[0:4])) # ------------------------------------------------------------------------------------------------------------------ # Creating unknown object # ------------------------------------------------------------------------------------------------------------------ unknown = Unknown(problem_dimension, field_dimension, cell_polynomial_order, face_polynomial_order) # ------------------------------------------------------------------------------------------------------------------ # Initilaizing polynomial bases # ------------------------------------------------------------------------------------------------------------------ face_basis_k = ScaledMonomial(face_polynomial_order, problem_dimension - 1) cell_basis_k = ScaledMonomial(face_polynomial_order, problem_dimension) cell_basis_l = ScaledMonomial(cell_polynomial_order, problem_dimension) cell_basis_k1 = ScaledMonomial(face_polynomial_order + 1, problem_dimension) integration_order = unknown.integration_order integration_order = 2 * (face_polynomial_order + 1) # print(integration_order) # ------------------------------------------------------------------------------------------------------------------ # Initilaizing Face objects # ------------------------------------------------------------------------------------------------------------------ faces = [] for i, face_vertices_connectivity_matrix in enumerate( faces_vertices_connectivity_matrix): face_vertices = vertices[face_vertices_connectivity_matrix] face = Face(face_vertices, integration_order) faces.append(face) # ------------------------------------------------------------------------------------------------------------------ # Initilaizing Cell objects # ------------------------------------------------------------------------------------------------------------------ cells = [] for cell_vertices_connectivity_matrix, cell_connectivity_matrix in zip( cells_vertices_connectivity_matrix, cells_connectivity_matrix): cell_vertices = vertices[cell_vertices_connectivity_matrix] cell = Cell(cell_vertices, cell_connectivity_matrix, integration_order) cells.append(cell) print("DONE") # ------------------------------------------------------------------------------------------------------------------ # Initilaizing Elements objects # ------------------------------------------------------------------------------------------------------------------ elements = [] operators = [] for i, cell in enumerate(cells): local_faces = [faces[j] for j in cells_faces_connectivity_matrix[i]] op = get_operator(operator_type, cell, local_faces, cell_basis_l, cell_basis_k, cell_basis_k1, face_basis_k, unknown) operators.append(op) return ( vertices, faces, cells, operators, cells_faces_connectivity_matrix, cells_vertices_connectivity_matrix, faces_vertices_connectivity_matrix, nsets, nsets_faces, cell_basis_l, cell_basis_k, face_basis_k, unknown, )
def test_new_cell_has_no_neighbours(): cell = Cell(0, 0) assert cell.north is None assert cell.south is None assert cell.east is None assert cell.west is None
def test_neighbours_empty_when_no_neighbours(): cell_A = Cell(0, 0) neigbours = cell_A.neighbors assert len(neigbours) == 0
def update_cell(cell: Cell, char: str): if char == "X": cell.set_active() cell.set_owner(ENEMY) elif char == "x": cell.set_inactive() cell.set_owner(ENEMY) elif char == "O": cell.set_active() cell.set_owner(ME) elif char == "o": cell.set_inactive() cell.set_owner(ME) elif char == "#": cell.set_owner(VOID) elif char == ".": cell.set_owner(NEUTRAL) else: raise KeyError()
def test_get_links_empty_when_no_links(): cell_A = Cell(0, 0) links = cell_A.links assert len(links) == 0