def basic_life(cell: Cell): cell.process_energy() cell.process_matter() cell.energy = (cell.energy * 0.98) cell.matter = (cell.matter * 0.99) if cell.is_too_old() or cell.has_not_enough_energy() or cell.has_not_enough_matter(): cell.life = False
def castling(self): piece = self._active_piece if piece.type is not PieceType.KING or self._current_map.king_is_moved( piece): return False y = 0 if piece.color == PieceColor.WHITE else 7 start_x = 4 end_x = 6 start_cell = Cell(start_x, y) end_cell = Cell(end_x, y) vector = Vector(start_cell, end_cell) if vector != self._move_vector: return False rook = self._current_map.get(Cell(end_x + 1, y)) if rook is None or rook.type is not PieceType.ROOK: return False if self._current_map.rook_is_moved(rook): return False y = 0 if piece.color == PieceColor.WHITE else 7 for x in range(4, 8): cell_bit_determinant = CellBitDeterminant(self._current_map, Cell(x, y)) if cell_bit_determinant.is_bit(piece.color): return False return True
def _clear_possible_action(board: Board, mod_cell: Cell, value: int): if mod_cell.is_initial(): return if mod_cell.value() is not None: return if mod_cell.has_possible_val(value): new_cell = mod_cell.clear_possible_value(value) board.set_cell(new_cell)
def test_possible_val_mod(self): # Change the set returned by the possible val method, make sure it stays the same. c = Cell(9, 0, 0, poss_vals=frozenset({2, 3, 4})) ret_set = c.possible_vals() ret_set.add(5) self.assertEqual(4, len(ret_set)) self.assertEqual(3, len(c.possible_vals())) self.assertEqual(3, c.possible_count())
def test_max_sqrt(self): expected_vals = [1, 2, 3, 4, 5] for i in range(len(self.__test_maxes)): test_val = self.__test_maxes[i] expected = expected_vals[i] c = Cell(test_val, 0, 0) self.assertEqual(test_val, c.max_val()) self.assertEqual(expected, c.max_sqrt())
def test_value_poss_exclusive(self): c = Cell(9, 0, 0, 4) self.assertEqual(4, c.value()) self.assertEqual(-1, c.possible_count()) self.assertIsNone(c.possible_vals()) c = Cell(9, 0, 0, poss_vals=frozenset({2, 3, 4})) self.assertIsNone(c.value()) self.assertEqual({2, 3, 4}, c.possible_vals()) self.assertEqual(3, c.possible_count())
def test_value_range(self): with self.assertRaises(ValueError): Cell(9, 0, 0, 0) with self.assertRaises(ValueError): Cell(9, 0, 0, -1) with self.assertRaises(ValueError): Cell(9, 0, 0, 10) with self.assertRaises(ValueError): Cell(4, 0, 0, 5) with self.assertRaises(ValueError): Cell(16, 0, 0, 17)
def test_modify_initial_cell(self): c = Cell(9, 0, 0, 4, is_initial=True) with self.assertRaises(ValueError): c.set_possible_value(3) with self.assertRaises(ValueError): c.set_value(5) with self.assertRaises(ValueError): c.clear_value(6) with self.assertRaises(ValueError): c.clear_possible_value(2)
def __copy__(self): new_map = Map(is_auto_init=False) for x in range(Map.SIZE): for y in range(Map.SIZE): new_map.__map[x][y] = self.get(Cell(x, y)) return new_map
def test_neither_set(self): c = Cell(9, 0, 0) self.assertIsNone(c.value()) self.assertIsNotNone(c.possible_vals()) self.assertEqual(9, c.possible_count()) for x in self.__test_maxes: s = set(range(1, x + 1)) c = Cell(x, 0, 0) self.assertEqual(s, c.possible_vals())
def test_initial_value(self): with self.assertRaises(ValueError): Cell(9, 0, 0, is_initial=True) with self.assertRaises(ValueError): Cell(9, 0, 0, poss_vals=frozenset(), is_initial=True) c = Cell(9, 0, 0, 1, is_initial=True) self.assertTrue(c.is_initial()) c = Cell(9, 0, 0, 1) self.assertFalse(c.is_initial())
def _set_cell_colour(self, table_cell, spot: Spot, data_cell: Cell, cell_number: int) -> None: if data_cell.is_valid(): if spot.has_rejected_outputs() and cell_number == 20: table_cell.setBackground(config.Q_REJECTED_CALCULATION_COLOUR) return if spot.has_invalid_inputs(): colour = config.Q_INVALID_IMPORT_COLOUR else: colour = config.Q_INVALID_CALCULATION_COLOUR table_cell.setBackground(colour)
def __init__(self, *args): self.__start_cell = None self.__end_cell = None if len(args) == 2: self.init(args[0], args[1]) elif len(args) == 3: start_cell = args[0] x = args[1] y = args[2] self.init(start_cell, Cell(start_cell.x + x, start_cell.y + y))
def get_last_move_vector(previous_map, current_map): if not (isinstance(previous_map, Map) and isinstance( current_map, Map)): raise TypeError("arguments should be Map") begin_cell = None end_cell = None for x in range(Map.SIZE): for y in range(Map.SIZE): if previous_map.get(Cell(x, y)) == current_map.get(Cell(x, y)): continue if current_map.get(Cell(x, y)) is None: begin_cell = Cell(x, y) else: end_cell = Cell(x, y) if begin_cell is None or end_cell is None: return None return Vector(begin_cell, end_cell)
def find(map, sought): for x in range(Map.SIZE): for y in range(Map.SIZE): cell = Cell(x, y) piece = map.get(cell) if piece is None: continue if piece == sought: return cell raise NotImplementedError
def draw_pieces_on_board(self): game_map = self.game.map for x in range(Map.SIZE): for y in range(Map.SIZE): piece = game_map.get(Cell(x, y)) offset = (GameBoard.CELL_SIZE - GameBoard.PIECE_SIZE) / 2 if piece is not None: Rectangle(pos=(x * GameBoard.CELL_SIZE + offset, y * GameBoard.CELL_SIZE + offset), size=(GameBoard.PIECE_SIZE, ) * 2, source=piece.asset_path)
def set_cell(self, cell: Cell) -> None: if self.__cell is not None and self.__cell.max_val() != cell.max_val(): raise ValueError( "GUICell cannot adapt to different sizes of puzzles!") self.__actions.clear() if self.__cell == cell: return self.__cell = cell # Trigger a re-draw of the cell self._trigger_layout()
def on_touch_down(self, touch): if touch.device != 'mouse': return clicked_cell = Cell(int(touch.x // GameBoard.CELL_SIZE), int(touch.y // GameBoard.CELL_SIZE)) if self.__allotted_cell is None: self.__allotted_cell = clicked_cell return move_made = self.game.try_make_move( Vector(self.__allotted_cell, clicked_cell)) self.__allotted_cell = None if move_made else clicked_cell
def get_bit_cell_from(self, cells_list): for x in range(Map.SIZE): for y in range(Map.SIZE): cell = Cell(x, y) piece = self.__current_map.get(cell) if piece is None: continue for sought_cell in cells_list: if self.__valid_trajectory(Vector(cell, sought_cell)): return sought_cell, cell return None
def test_square_val(self): with self.assertRaises(ValueError): Cell(None, 0, 0, 1) with self.assertRaises(ValueError): Cell(0, 0, 0, 1) Cell(2, 0, 0, 1) Cell(4, 0, 0, 1) Cell(9, 0, 0, 1) Cell(16, 0, 0, 1) with self.assertRaises(ValueError): Cell(3, 0, 0, 1) with self.assertRaises(ValueError): Cell(5, 0, 0, 1)
def test_possible_values(self): for x in self.__test_maxes: vals = frozenset(range(1, x + 1)) Cell(x, 0, 0, poss_vals=vals) with self.assertRaises(ValueError): vals = frozenset(range(0, x + 1)) Cell(x, 0, 0, poss_vals=vals) with self.assertRaises(ValueError): vals = frozenset(range(1, x + 2)) Cell(x, 0, 0, poss_vals=vals) s = frozenset({2, 3, 4}) c = Cell(9, 0, 0, poss_vals=s) self.assertEqual(s, c.possible_vals()) s = frozenset({4, 2, 9}) c = Cell(9, 0, 0, poss_vals=s) self.assertEqual(s, c.possible_vals())
def __draw_possible_val(self, val: int, draw_color): sqrt_max = self.__cell.max_sqrt() cell_size = math.floor(self.width / sqrt_max) # We don't want the lettering to be too small, so don't draw possible values if the space is too small. if cell_size < 8: return row = sqrt_max - 1 - math.floor((val - 1) / sqrt_max) col = (val - 1) % sqrt_max draw_x = self.x + cell_size * col draw_y = self.y + cell_size * row self.__display_text(draw_x, draw_y, cell_size, cell_size, Cell.val_to_chr(val), draw_color)
def test_has_possible_val(self): test_set = frozenset({2, 3, 4}) c = Cell(9, 0, 0, poss_vals=test_set) for i in range(1, 10): self.assertEqual(i in test_set, c.has_possible_val(i)) test_set = {1, 2, 3, 4, 5, 6, 7, 8, 9} c = Cell(9, 0, 0) for i in range(1, 10): self.assertEqual(i in test_set, c.has_possible_val(i)) # Test outside of range. self.assertFalse(c.has_possible_val(0)) self.assertFalse(c.has_possible_val(-1)) self.assertFalse(c.has_possible_val(10))
def test_clear_possible_value(self): c = Cell(9, 0, 0, poss_vals=frozenset({2, 3, 4})) c2 = c for i in range(1, 10): c2 = c2.clear_possible_value(i) self.assertEqual({2, 3, 4}, c.possible_vals()) self.assertEqual(set(), c2.possible_vals()) c2 = c.clear_possible_value(5) self.assertEqual(c, c2) c2 = c.clear_possible_value(4) self.assertEqual({2, 3}, c2.possible_vals()) self.assertEqual({2, 3, 4}, c.possible_vals()) c = Cell(9, 0, 0, 4)
def test_xy_range(self): with self.assertRaises(ValueError): Cell(9, -1, 0, 1) with self.assertRaises(ValueError): Cell(9, 0, -1, 1) with self.assertRaises(ValueError): Cell(9, 9, 0, 1) with self.assertRaises(ValueError): Cell(9, 0, 9, 1) with self.assertRaises(ValueError): Cell(16, 16, 0, 1) with self.assertRaises(ValueError): Cell(16, 0, 16, 1)
def test_both_set(self): with self.assertRaises(ValueError): Cell(9, 0, 0, cur_val=1, poss_vals=frozenset()) with self.assertRaises(ValueError): Cell(9, 0, 0, cur_val=1, poss_vals=frozenset(), is_initial=True)
def __test_xy_value_helper(self, val): for i in range(val): c = Cell(val, i, 0, 1) self.assertEqual(i, c.x()) self.assertEqual(0, c.y()) self.assertEqual(1, c.value()) c = Cell(val, 0, i, 1) self.assertEqual(0, c.x()) self.assertEqual(i, c.y()) self.assertEqual(1, c.value()) c = Cell(val, i, i, 1) self.assertEqual(i, c.x()) self.assertEqual(i, c.y()) self.assertEqual(1, c.value()) c = Cell(val, 0, 0, i + 1) self.assertEqual(i + 1, c.value())
def __init_cells(self, cells) -> None: """ initialized the cell list :param cells: the number of cells to be palaced """ self.__cells = [Cell(i) for i in range(cells)]
def test_set_value(self): c = Cell(9, 0, 0) c2 = c.set_value(0)
def test_set_possible_value(self): c = Cell(9, 0, 0, poss_vals=frozenset({1})) c2 = c.set_possible_value(2) self.assertEqual({1}, c.possible_vals()) self.assertEqual({1, 2}, c2.possible_vals()) # Out of range checks with self.assertRaises(ValueError): c.set_possible_value(0) with self.assertRaises(ValueError): c.set_possible_value(-1) with self.assertRaises(ValueError): c.set_possible_value(10) # Make sure setting a possible value on a set value doesn't change the value. c = Cell(9, 0, 0, 4) c2 = c.set_possible_value(5) self.assertFalse(c2.has_possible_val(5)) self.assertEqual(-1, c2.possible_count()) self.assertEqual(4, c2.value())
def base_test(self, game, x1, y1, x2, y2, result): start_cell = Cell(x1, y1) end_cell = Cell(x2, y2) self.assertEqual(game.try_make_move(Vector(start_cell, end_cell)), result)