def set_start(self): start = Cell(self.width - 2, self.height - 2, self.size) self.start = start self.set(start) start.surf.fill((0, 255, 0)) start.draw(self.screen) pygame.display.update()
def test_cell_can_place_a_ship(self): cell = Cell("B4") cruiser = Ship("cruiser", 1) self.assertTrue(cell.empty) self.assertEqual(None, cell.ship) cell.place_ship(cruiser) self.assertFalse(cell.empty) self.assertEqual(cruiser, cell.ship)
def setup_cell(is_alive, num_of_surrounding_alive): living_cell = Cell() living_cell.is_alive = True surrounders = [living_cell] * num_of_surrounding_alive cell = Cell() cell.set_surrounders(surrounders) cell.is_alive = is_alive return cell
def test_cell_attributes(self): cell = Cell("B4") self.assertEqual("B4", cell.coordinate) self.assertEqual(None, cell.ship) self.assertTrue(cell.empty) self.assertFalse(cell.fired_upon)
def set_end(self): cell = self.get(1, 1) cells = [(2, 1), (1, 2), (2, 2)] i = 0 while type(cell) != Cell: x, y = cells[i] cell = self.get(x, y) i += 1 end = Cell(cell.x, cell.y, self.size) self.end = end self.set(end) end.surf.fill((255, 0, 0)) end.draw(self.screen) pygame.display.update()
def __init__(self, width, height): self.cells = {} letters = list(string.ascii_uppercase) numbers = list(range(1, 26)) for x in range(width): for y in range(height): self.cells[f"{letters[x]}{numbers[y]}"] = Cell( f"{letters[x]}{numbers[y]}")
def test_fire_upon(self): cell = Cell("B4") cruiser = Ship("Cruiser", 3) cell.place_ship(cruiser) self.assertEqual(cell.isFiredUpon, False) cell.fire_upon() self.assertEqual(cell.isFiredUpon, True) self.assertEqual(cell.ship.health, 2)
def test_cell_can_be_fired_upon(self): cell = Cell("B4") cruiser = Ship("cruiser", 1) cell.place_ship(cruiser) self.assertFalse(cell.fired_upon) self.assertEqual(1, cruiser.health) cell.fire_upon() self.assertTrue(cell.fired_upon) self.assertEqual(0, cruiser.health)
def generate(self): self.clean() # Iterative implementation unvisited = [ cell for row in self.grid for cell in row if cell.x % 2 and cell.y % 2 ] current = unvisited.pop() stack = deque() while unvisited: try: neighbor = self.get_random_neighbor(current, unvisited) stack.append(current) x = current.x - (current.x - neighbor.x) // 2 y = current.y - (current.y - neighbor.y) // 2 current_cell = Cell(current.x, current.y, self.size) neighbor_cell = Cell(x, y, self.size) self.set(current_cell) self.set(neighbor_cell) current = neighbor unvisited.remove(neighbor) if self.animate: current_cell.draw(self.screen) neighbor_cell.draw(self.screen) pygame.display.update() pygame.time.wait(self.sleep) except IndexError: if stack: current = stack.pop() self.set_start() self.set_end()
def test_it_exists(self): cell = Cell("B4") self.assertEqual(cell.coordinate, "B4") self.assertEqual(cell.ship, None) self.assertEqual(cell.isEmpty(), True)
def test_render(self): cell_1 = Cell("B4") cell_2 = Cell("C3") cruiser = Ship("Cruiser", 3) self.assertEqual(cell_1.render(), ".") cell_1.fire_upon() self.assertEqual(cell_1.render(), "M") cell_2.place_ship(cruiser) self.assertEqual(cell_2.render(), ".") self.assertEqual(cell_2.render(reveal=True), "S") cell_2.fire_upon() self.assertEqual(cruiser.health, 2) self.assertEqual(cell_2.render(), "H") self.assertEqual(cruiser.is_sunk(), False) cruiser.hit() self.assertEqual(cruiser.health, 1) cruiser.hit() self.assertEqual(cruiser.health, 0) self.assertEqual(cruiser.is_sunk(), True) self.assertEqual(cell_2.render(), "X")
def test_place_ship(self): cell = Cell("B4") cruiser = Ship("Cruiser", 3) cell.place_ship(cruiser) self.assertEqual(cell.ship, cruiser) self.assertEqual(cell.isEmpty(), False)
def test_cell_rendering(self): cell1 = Cell("B4") cell2 = Cell("C5") cell3 = Cell("D6") cruiser1 = Ship("cruiser", 1) cruiser2 = Ship("cruiser", 2) cell1.place_ship(cruiser1) cell2.place_ship(cruiser2) self.assertEqual(".", cell1.render()) self.assertEqual(".", cell2.render()) self.assertEqual(".", cell3.render()) cell1.fire_upon() cell2.fire_upon() cell3.fire_upon() self.assertEqual("X", cell1.render()) self.assertEqual("H", cell2.render()) self.assertEqual("M", cell3.render())
def test_starts_dead(): cell = Cell() assert cell.is_alive == False
def test_toggle_toggles_alive_or_dead(): cell = Cell() cell.toggle() assert cell.is_alive == True
""" Assumptions made about the problem 1. The display of the live and dead cells including the grid did not have to have visual elements 2. The seed of the universe did not require to be changeable during execution 3. The dimensions of the world could be set to anything """ #Variables x = int(input("Enter width 0 - 20: ")) y = int(input("Enter height 0 - 20: ")) max_generations = int( input( "Enter maximum amount of generations you would like to generate e.g 10: " )) myMap = Map(x, y) # - Generate Map and create object myCell = Cell() #Seed - This activates some cells in advance before the game starts. myMap.live([1, 4]) myMap.live([1, 5]) myMap.live([1, 6]) def start(): generation_count = 0 while (np.any(myMap.world)) and (generation_count <= max_generations ): #np.any checks if any cell is alive print("Current generation: " + str(generation_count)) #Generation counter print(myMap.world, "\n") #Print cell world
def __init__(self): self.cells = { "A1": Cell("A1"), "A2": Cell("A2"), "A3": Cell("A3"), "A4": Cell("A4"), "B1": Cell("B1"), "B2": Cell("B2"), "B3": Cell("B3"), "B4": Cell("B4"), "C1": Cell("C1"), "C2": Cell("C2"), "C3": Cell("C3"), "C4": Cell("C4"), "D1": Cell("D1"), "D2": Cell("D2"), "D3": Cell("D3"), "D4": Cell("D4") }