def put_walls(world): for i in range(0, WORLD_WIDTH): world.put_cell(Location(i, 0), CellType.WALL) world.put_cell(Location(i, WORLD_HEIGHT - 1), CellType.WALL) for i in range(0, WORLD_HEIGHT): world.put_cell(Location(0, i), CellType.WALL) world.put_cell(Location(WORLD_WIDTH - 1, i), CellType.WALL)
def test_move_to_wall(self): world = World() old_location = Location(1, 1) bot = Bot(old_location, world) world.update_cell(old_location, bot) bot.direction = Direction.NW bot.move(0) self.assertEqual(CellType.WALL, world.get_cell(Location(0, 0))) self.assertTrue(isinstance(world.get_cell(old_location), Bot)) self.assertEqual(Direction.NW, bot.direction) self.assertEqual(ROTATE_45_DEGREE_GENE, bot.genes.get_next())
def test_lookup_empty(self): world = World() location = Location(2, 2) bot = Bot(location, world) world.update_cell(location, bot) world.update_cell(Location(3, 1), CellType.EMPTY) bot.energy = 77 bot.direction = Direction.NE bot.lookup(0) self.assertEqual(77, bot.energy) self.assertEqual(Direction.NE, bot.direction) self.assertEqual(ROTATE_45_DEGREE_GENE, bot.genes.get_next()) self.assertTrue(isinstance(world.get_cell(location), Bot))
def test_peek_wall(self): world = World() bot_location = Location(1, 1) bot = Bot(bot_location, world) world.update_cell(bot_location, bot) bot.energy = 77 bot.direction = Direction.E bot.lookup(7) self.assertEqual(77, bot.energy) self.assertEqual(Direction.E, bot.direction) self.assertEqual(CellType.WALL, world.get_cell(Location(0, 0))) self.assertTrue(isinstance(world.get_cell(bot_location), Bot)) self.assertEqual(ROTATE_45_DEGREE_GENE, bot.genes.get_next())
def test_move_to_empty(self): world = World() old_location = Location(2, 2) bot = Bot(old_location, world) world.update_cell(old_location, bot) expected_future_location = Location(2, 1) world.update_cell(expected_future_location, CellType.EMPTY) bot.direction = Direction.N bot.move(0) self.assertTrue( isinstance(world.get_cell(expected_future_location), Bot)) self.assertEqual(CellType.EMPTY, world.get_cell(old_location)) self.assertEqual(Direction.N, bot.direction)
def test_move_to_poison(self): world = World() old_location = Location(3, 1) expected_future_location = Location(4, 2) world.update_cell(expected_future_location, CellType.POISON) bot = Bot(old_location, world) world.update_cell(old_location, bot) bot.direction = Direction.E bot.move(1) self.assertEqual(CellType.POISON, world.get_cell(expected_future_location)) self.assertEqual(CellType.EMPTY, world.get_cell(old_location)) self.assertEqual(Direction.E, bot.direction)
def test_peek_food(self): world = World() bot_location = Location(2, 2) bot = Bot(bot_location, world) world.update_cell(bot_location, bot) bot.energy = 77 bot.direction = Direction.E food_location = Location(3, 2) world.update_cell(food_location, CellType.FOOD) bot.lookup(0) self.assertEqual(77, bot.energy) self.assertEqual(Direction.E, bot.direction) self.assertTrue(isinstance(world.get_cell(bot_location), Bot)) self.assertEqual(8, bot.genes.get_next())
def test_peek_poison(self): world = World() bot_location = Location(2, 2) bot = Bot(bot_location, world) world.update_cell(bot_location, bot) bot.energy = 77 bot.direction = Direction.E poison_location = Location(3, 3) world.update_cell(poison_location, CellType.POISON) bot.lookup(1) self.assertEqual(77, bot.energy) self.assertEqual(Direction.E, bot.direction) self.assertEqual(CellType.POISON, world.get_cell(poison_location)) self.assertTrue(isinstance(world.get_cell(bot_location), Bot)) self.assertEqual(24, bot.genes.get_next())
def test_move_to_food(self): world = World() old_location = Location(2, 2) expected_future_location = Location(3, 1) world.update_cell(expected_future_location, CellType.FOOD) bot = Bot(old_location, world) world.update_cell(old_location, bot) bot.direction = Direction.NE bot.energy = 11 bot.move(0) self.assertTrue( isinstance(world.get_cell(expected_future_location), Bot)) self.assertEqual(CellType.EMPTY, world.get_cell(old_location)) self.assertEqual(Direction.NE, bot.direction) self.assertEqual(22, bot.energy)
def test_move_to_bot(self): world = World() other_bot_location = Location(2, 4) current_bot_location = Location(1, 3) other_bot = Bot(other_bot_location, world) current_bot = Bot(current_bot_location, world) world.update_cell(other_bot_location, other_bot) world.update_cell(current_bot_location, current_bot) current_bot.direction = Direction.S current_bot.energy = 10 current_bot.move(7) self.assertEqual(current_bot, world.get_cell(current_bot_location)) self.assertEqual(other_bot, world.get_cell(other_bot_location)) self.assertEqual(Direction.S, current_bot.direction) self.assertEqual(12, current_bot.energy) self.assertEqual(ROTATE_45_DEGREE_GENE, current_bot.genes.get_next())
def test_peek_bot(self): world = World() current_bot_location = Location(3, 4) current_bot = Bot(current_bot_location, world) world.update_cell(current_bot_location, current_bot) current_bot.energy = 77 current_bot.direction = Direction.NW other_bot_location = Location(2, 3) other_bot = Bot(other_bot_location, world) world.update_cell(other_bot_location, other_bot) current_bot.lookup(7) self.assertEqual(77, current_bot.energy) self.assertEqual(Direction.NW, current_bot.direction) self.assertEqual(other_bot, world.get_cell(other_bot_location)) self.assertEqual(current_bot, world.get_cell(current_bot_location)) self.assertEqual(ROTATE_45_DEGREE_GENE, current_bot.genes.get_next())
def test_update(self): world = World() location = Location(2, 3) world.update_cell(location, CellType.EMPTY) self.assertEqual(CellType.EMPTY, world.get_cell(location)) world.update_cell(location, CellType.FOOD) self.assertEqual(CellType.FOOD, world.get_cell(location)) world.update_cell(location, CellType.EMPTY) self.assertEqual(CellType.EMPTY, world.get_cell(location))
def test_put(self): world = World() location = Location(2, 3) world.put_cell(location, CellType.EMPTY) self.assertEqual(CellType.EMPTY, world.get_cell(location)) world.put_cell(location, CellType.FOOD) self.assertEqual(CellType.FOOD, world.get_cell(location)) result = world.put_cell(location, CellType.EMPTY) self.assertFalse(result)
def test_get_when_not_exist(self): world = World() self.assertEqual(world.get_cell(Location(1, 1)), CellType.EMPTY)
def test_get_when_out_of_range(self): world = World() with self.assertRaises(Exception): world.get_cell(Location(1000000, 1000000))
def test_get_wall(self): world = World() location = Location(0, 0) result = world.get_cell(location) self.assertEqual(CellType.WALL, result)
def test_get(self): world = World() world.put_cell(Location(1, 3), CellType.POISON) result = world.get_cell(Location(1, 3)) self.assertEqual(result, CellType.POISON)
def test_put_on_wall(self): world = World() location = Location(0, 0) result = world.put_cell(location, CellType.POISON) self.assertFalse(result)
def test_put_empty_when_not_exist(self): world = World() location = Location(1, 1) world.put_cell(location, CellType.EMPTY)
def test_put_when_not_empty(self): world = World() world.put_cell(Location(1, 1), CellType.FOOD) result = world.put_cell(Location(1, 1), CellType.FOOD) self.assertFalse(result)
def test_update_on_wall(self): world = World() location = Location(0, 0) result = world.update_cell(location, CellType.POISON) self.assertEqual(CellType.WALL, result)
def test_put_when_out_of_range(self): world = World() with self.assertRaises(Exception): world.put_cell(Location(999999, 988888), CellType.POISON)
def test_update_when_is_not_free(self): world = World() world.put_cell(Location(1, 1), CellType.FOOD) old_value = world.update_cell(Location(1, 1), CellType.POISON) self.assertEqual(old_value, CellType.FOOD)
def test_update_when_is_free(self): world = World() old_value = world.update_cell(Location(1, 1), CellType.POISON) self.assertIsNone(old_value)
def test_update_empty_when_empty(self): world = World() location = Location(1, 1) world.update_cell(location, CellType.EMPTY) world.update_cell(location, CellType.EMPTY)
def draw_world(self, world): for i in range(0, WORLD_WIDTH): for j in range(0, WORLD_HEIGHT): self.draw_cell(i, j, world.get_cell(Location(i, j)))