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_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 create_bots(world): bots = set() for i in range(BOTS_COUNT): location = world.rand_free_location() bot = Bot(location, world) world.put_cell(location, bot) bots.add(bot) return bots
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_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_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_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_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_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_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_rotate_from_n_with_0(self): bot = Bot(None, None) bot.direction = Direction.N bot.rotate(0) self.assertEqual(Direction.NE, bot.direction)
def test_rotate_from_nw_with_6(self): bot = Bot(None, None) bot.direction = Direction.NW bot.rotate(6) self.assertEqual(Direction.W, bot.direction)
def test_rotate_from_ne_with_1(self): bot = Bot(None, None) bot.direction = Direction.NE bot.rotate(1) self.assertEqual(Direction.SE, bot.direction)
def main(): bot = Bot() bot.start()
import logging import requests from requests import ConnectionError from src.bot import settings from src.bot.bot import Bot log = logging.getLogger(__file__) if __name__ == '__main__': bot = Bot() try: requests.get(settings.SERVER_URL) except ConnectionError: log.error('Did you start Django server?') log.error('Maybe your Server URL is different than in bot settings?') quit(0) bot.create_users() bot.get_tokens() bot.perform_posting() bot.perform_liking()