def generate_empty(self): h_cells = 2 * self.width + 1 v_cells = 2 * self.height + 1 cells = [] for y in range(v_cells): cells.append([]) for x in range(h_cells): if x == 0 or x == (h_cells - 1) or y == 0 or y == (v_cells - 1): cells[y].append(Cell(CellType.Monolith, x, y)) elif x % 2 == 1 and y % 2 == 1: cells[y].append(Cell(CellType.Empty, x, y)) else: cells[y].append(Cell(CellType.Wall, x, y)) for y in range(v_cells): for x in range(h_cells): neighbours = { 'left': cells[y][x - 1] if x > 0 else None, 'right': cells[y][x + 1] if x + 1 < h_cells else None, 'up': cells[y - 1][x] if y > 0 else None, 'down': cells[y + 1][x] if y + 1 < v_cells else None } cells[y][x].add_neighbours(neighbours) return cells
def test_board(self): repo = Repository() board = BoardService(repo) self.assertEqual(len(board.get_board()), 6) self.assertEqual(len(board.get_board()[randint(0, 5)]), 6) board.make_move(0, 0, 1) self.assertEqual(board.get_cell(0, 0), Cell(board.cell_id(0, 0), 2)) for i in [0, 1]: for j in [0, 1]: if i + j: self.assertEqual(board.get_cell(i, j), Cell(board.cell_id(i, j), 1)) board.make_move(2, 2, 2) self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3)) for i in [1, 3]: for j in [1, 3]: if i != 2 or j != 2: self.assertEqual(board.get_cell(i, j), Cell(board.cell_id(i, j), 1)) self.assertRaises(AttributeError, lambda: board.make_move(1, 1, 1)) self.assertRaises(AttributeError, lambda: board.make_move(6, 1, 1)) self.assertRaises(AttributeError, lambda: board.make_move(4, 4, 3)) self.assertFalse(board.ctrl_win()) board.make_move(3, 0, 2) board.make_move(5, 1, 2) board.make_move(4, 4, 2) board.make_move(1, 4, 2) self.assertFalse(board.ctrl_win()) board.make_move(0, 2, 2) self.assertTrue(board.ctrl_win())
def test_cell(self): cell = Cell(1, 1) self.assertEqual(cell.id, 1) self.assertEqual(cell.state, 1) self.assertNotEqual(Cell(1, 1), Cell(1, 2)) self.assertRaises(AttributeError, lambda: Cell(37, 0)) self.assertRaises(AttributeError, lambda: Cell(6, 4))
def _cell_from_symbol(self, pos: Point, char: str) -> Cell: if char == ' ': return Cell(position=pos, cell_type=CellType.DELETED, value=-1) elif char.isdigit(): return Cell(position=pos, cell_type=CellType.NUMBER, value=int(char)) else: raise AttributeError('unknown symbol')
def __init_game_field(self, size: Point) -> Matrix[Cell]: game_field = Matrix.from_point(size) for x in range(game_field.width): for y in range(game_field.height): point = Point(x, y) game_field[point] = Cell.create_empty_cell(point) return game_field
def __init__(self, board_repository, history_service=None): self.__board_repository = board_repository self.__history_service = history_service for i in range(6): for j in range(6): self.__board_repository.create_item(Cell( self.cell_id(i, j), 0))
def __init__(self, size: int): self.size = size self.grid = [[0] * size] * size self.alive_cell_counter = 0 for i in range(size): for j in range(size): self.grid[i][j] = Cell(i, j, self)
def parse(battle_state_json_string): battle_state = BattleState() battle_state_json = json.loads(battle_state_json_string) battle_state.scenario_id = battle_state_json["scenario"] battle_state.cells = [ Cell.parse(cell_json) for cell_json in battle_state_json["cells"] ] return battle_state
def parse(war_map_json_string): war_map = WarMap() war_map_obj = war_map_json_string['cells'] war_map.cells = [ Cell.parse(war_map_obj) for war_map_obj in war_map_obj ] war_map.rows = war_map_json_string['rows'] war_map.cols = war_map_json_string['cols'] return war_map
def test_board_undo(self): repo = Repository() history_repo = Repository() history_service = HistoryService(history_repo) board = BoardService(repo, history_service) self.assertRaises(AttributeError, lambda: history_service.undo()) self.assertRaises(AttributeError, lambda: history_service.redo()) board.make_move(2, 2, 2, assignUndo=True) self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3)) for i in [1, 3]: for j in [1, 3]: if i != 2 or j != 2: self.assertEqual(board.get_cell(i, j), Cell(board.cell_id(i, j), 1)) history_service.undo() self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 0)) for i in [1, 3]: for j in [1, 3]: self.assertEqual(board.get_cell(i, j), Cell(board.cell_id(i, j), 0)) history_service.redo() self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3)) for i in [1, 3]: for j in [1, 3]: if i != 2 or j != 2: self.assertEqual(board.get_cell(i, j), Cell(board.cell_id(i, j), 1)) board.make_move(0, 0, 2, assignUndo=True) history_service.undo() for i in [0, 1, 2]: for j in [0, 1, 2]: if not i or not j: self.assertEqual(board.get_cell(i, j), Cell(board.cell_id(i, j), 0)) elif i == 2 and j == 2: self.assertEqual(board.get_cell(i, j), Cell(board.cell_id(i, j), 3)) else: self.assertEqual(board.get_cell(i, j), Cell(board.cell_id(i, j), 1))
def __draw_rects(self, screen): for x in range(0, constants.WINSIZE[0], constants.RECT_W): vector = [] for y in range(0, constants.WINSIZE[1], constants.RECT_W): vector.append( Cell( x, y, draw_rect(screen, constants.WHITE, constants.GRAY, pygame.Rect(x, y, constants.RECT_W, constants.RECT_W), border=1))) self.cells.append(vector)
def test_repositories(self): repo = Repository() repo.create_item(Cell(1, 1)) repo.create_item(Cell(2, 3)) self.assertEqual(repo.all_items()[0], Cell(1, 1)) self.assertEqual(repo.all_items()[1], Cell(2, 3)) self.assertRaises(AttributeError, lambda: repo.create_item(Cell(1, 3))) self.assertRaises(AttributeError, lambda: repo.item_by_id(23)) repo.update_item(Cell(1, 3)) self.assertEqual(repo.item_by_id(1), Cell(1, 3)) self.assertRaises(AttributeError, lambda: repo.update_item(Cell(32, 0))) repo.delete_item_by_id(1) repo.delete_item_by_id(2) self.assertFalse(len(repo))
def make_move(self, i, j, num, **kwargs): """ states: 0 - nothing, 1 - shaded, 2 - an O (first player), 3 - an X (second player) :param i: the row - 0 to 5 :param j: the column - 0 to 5 :param num: the number of the player, 1 or 2 :return: - """ if i < 0 or i > 5 or j < 0 or j > 5: raise AttributeError("Invalid coordinates.") if num not in [1, 2]: raise AttributeError("Not valid num of player!") if not self.check_free(i, j): raise AttributeError("Can't make a move there!") op_list = [ Function(self.__board_repository.update_item, Cell(self.cell_id(i, j), num + 1)) ] rev_op_list = [ Function(self.__board_repository.update_item, Cell(self.cell_id(i, j), self.get_cell(i, j).state)) ] self.__board_repository.update_item(Cell(self.cell_id(i, j), num + 1)) for ii in range(max(0, i - 1), min(6, i + 2)): for jj in range(max(0, j - 1), min(6, j + 2)): if i != ii or j != jj: if "assignUndo" in kwargs and kwargs["assignUndo"]: op_list.append( Function(self.__board_repository.update_item, Cell(self.cell_id(ii, jj), 1))) rev_op_list.append( Function( self.__board_repository.update_item, Cell(self.cell_id(ii, jj), self.get_cell(ii, jj).state))) self.__board_repository.update_item( Cell(self.cell_id(ii, jj), 1)) if "assignUndo" in kwargs and kwargs["assignUndo"]: ops = Operation(*op_list) reverse_ops = Operation(*rev_op_list) self.__history_service.add_op(ops, reverse_ops)
def _activate_cell(self, player: Player, cell: Cell): cell.real_owner = player cell.graphic_owner = player cell.cell_type = CellType.active_point
scenario = Scenario() logging.debug('Number of devices: %s' % (config['scenario']['devices']['number'])) scenario.setattr('num_devices', config['scenario']['devices']['number']) scenario.setattr('application_layer', config['scenario']['application_layer']['protocol']) scenario.setattr('transport_layer', config['scenario']['transport_layer']['protocol']) scenario.setattr('network_layer', config['scenario']['network_layer']['protocol']) scenario.setattr('link_layer', config['scenario']['link_layer']['protocol']) scenario.setattr('config', config) logging.debug(scenario) #Setting up the cell cell = Cell(config[scenario.getattr('link_layer')]['cell_size']) scenario.setattr('cell', cell) for i in range(scenario.getattr('num_devices')): try: device = Device( getattr(cell, 'size'), task, config[scenario.getattr('application_layer')]['authentication']) except KeyError: device = Device(getattr(cell, 'size'), task) cell.add_device(device) logging.debug(cell) devices = getattr(cell, 'devices') for device in devices: device.run()