def test_count_around(self): board = tronutils.read_board('maps/u.txt') self.assertEquals(MyTronBot.count_around(board, board.me()), 97) board = tronutils.read_board('maps/ring.txt') self.assertEquals(MyTronBot.count_around(board, board.me()), 131) board = tronutils.read_board('maps/test-board.txt') self.assertEquals(MyTronBot.count_around(board, board.me()), 4)
def test_distance_map(self): board = tronutils.read_board('maps/test-board.txt') dmap = MyTronBot.distance_map(board, board.me()) self.assertEquals(dmap[(2,1)], 1) self.assertEquals(dmap[(2,2)], 2) self.assertEquals(dmap[(2,3)], 3) board = tronutils.read_board('maps/quadrant.txt') dmap = MyTronBot.distance_map(board, board.me()) self.assertEquals(dmap[(3,4)], 1) self.assertEquals(dmap[(4,3)], 1) self.assertEquals(dmap[(9,5)], 8) self.assertEquals(dmap[(13,13)], 20)
def test_distance_map(self): board = tronutils.read_board('maps/test-board.txt') dmap = MyTronBot.distance_map(board, board.me()) self.assertEquals(dmap[(2, 1)], 1) self.assertEquals(dmap[(2, 2)], 2) self.assertEquals(dmap[(2, 3)], 3) board = tronutils.read_board('maps/quadrant.txt') dmap = MyTronBot.distance_map(board, board.me()) self.assertEquals(dmap[(3, 4)], 1) self.assertEquals(dmap[(4, 3)], 1) self.assertEquals(dmap[(9, 5)], 8) self.assertEquals(dmap[(13, 13)], 20)
def test_adjacent(self): board = tronutils.read_board('maps/test-board.txt') coords = board.me() wall = MyTronBot.adjacent(board, coords, MyTronBot.is_wall) floor = MyTronBot.adjacent(board, coords, MyTronBot.is_floor) self.assertEquals(len(wall), 3) self.assertEquals(len(floor), 1)
def test_move_made(self): board = tronutils.read_board('maps/test-board.txt') fn = lambda a,b: MyTronBot.move_made(a, b) self.assertEquals(fn((1,1),(2,1)), tron.SOUTH) self.assertEquals(fn((2,1),(1,1)), tron.NORTH) self.assertEquals(fn((1,1),(1,2)), tron.EAST) self.assertEquals(fn((1,2),(1,1)), tron.WEST)
def test_move_made(self): board = tronutils.read_board('maps/test-board.txt') fn = lambda a, b: MyTronBot.move_made(a, b) self.assertEquals(fn((1, 1), (2, 1)), tron.SOUTH) self.assertEquals(fn((2, 1), (1, 1)), tron.NORTH) self.assertEquals(fn((1, 1), (1, 2)), tron.EAST) self.assertEquals(fn((1, 2), (1, 1)), tron.WEST)
def test_win_lose_or_draw(self): board = tronutils.read_board('maps/test-board.txt') self.assertEqual(MyTronBot.win_lose_or_draw(board, tron.ME), -0.5) self.assertEqual(MyTronBot.win_lose_or_draw(board, tron.THEM), -0.5) board.board[2] = '######' self.assertEqual(MyTronBot.win_lose_or_draw(board, tron.ME), -1) self.assertEqual(MyTronBot.win_lose_or_draw(board, tron.THEM), 1)
def test_same_distance(self): board = tronutils.read_board('maps/test-board.txt') points = MyTronBot.same_distance(board, board.me(), board.them()) self.assertEquals(points, []) board = tronutils.read_board('maps/u.txt') points = set(MyTronBot.same_distance(board, board.me(), board.them())) self.assertEquals(points, set([(7,1),(7,2),(7,3)])) board = tronutils.read_board('maps/twin-rooms.txt') points = set(MyTronBot.same_distance(board, board.me(), board.them())) self.assertEquals(points, set([(12,11),(12,12),(12,13)])) board = tronutils.read_board('maps/huge-room.txt') points = MyTronBot.same_distance(board, board.me(), board.them()) self.assertTrue((48,1) in points) self.assertTrue((1,48) in points) self.assertTrue((24,25) in points) self.assertTrue((25,24) in points)
def test_read_board(self): board = tronutils.read_board('maps/test-board.txt') self.assertEqual(board.width, 6, 'incorrect width') self.assertEqual(board.height, 4, 'incorrect height') self.assertEqual(board[1,1], tron.ME, 'expected ME') self.assertEqual(board[1,4], tron.THEM, 'expected THEM') self.assertEqual(board.me(), (1,1)) self.assertEqual(board.them(), (1,4)) self.assertEqual(board[1,2], tron.WALL, 'expected WALL') self.assertEqual(board[1,3], tron.FLOOR, 'expected FLOOR')
def test_same_distance(self): board = tronutils.read_board('maps/test-board.txt') points = MyTronBot.same_distance(board, board.me(), board.them()) self.assertEquals(points, []) board = tronutils.read_board('maps/u.txt') points = set(MyTronBot.same_distance(board, board.me(), board.them())) self.assertEquals(points, set([(7, 1), (7, 2), (7, 3)])) board = tronutils.read_board('maps/twin-rooms.txt') points = set(MyTronBot.same_distance(board, board.me(), board.them())) self.assertEquals(points, set([(12, 11), (12, 12), (12, 13)])) board = tronutils.read_board('maps/huge-room.txt') points = MyTronBot.same_distance(board, board.me(), board.them()) self.assertTrue((48, 1) in points) self.assertTrue((1, 48) in points) self.assertTrue((24, 25) in points) self.assertTrue((25, 24) in points)
def test_read_board(self): board = tronutils.read_board('maps/test-board.txt') self.assertEqual(board.width, 6, 'incorrect width') self.assertEqual(board.height, 4, 'incorrect height') self.assertEqual(board[1, 1], tron.ME, 'expected ME') self.assertEqual(board[1, 4], tron.THEM, 'expected THEM') self.assertEqual(board.me(), (1, 1)) self.assertEqual(board.them(), (1, 4)) self.assertEqual(board[1, 2], tron.WALL, 'expected WALL') self.assertEqual(board[1, 3], tron.FLOOR, 'expected FLOOR')
def derive_case(infile, outfile, squaresize=15): root = Tk() root.bind("<Escape>", lambda e: done()) board = tronutils.read_board(infile) def done(): tronutils.write_board(board, outfile) root.destroy() grid = TronGridEditor(root, board, squaresize, colors) grid.draw(board) grid.pack() mainloop()
def test_apply_move(self): board = tronutils.read_board('maps/test-board.txt') self.assertEquals(board.me(), (1, 1)) self.assertEquals(board.them(), (1, 4)) self.assertEquals(board[2, 1], tron.FLOOR, 'should be FLOOR') next = MyTronBot.apply_move(board, tron.ME, tron.SOUTH) self.assertEquals(next.me(), (2, 1), 'should have changed') self.assertEquals(next.them(), (1, 4), 'should not have changed') self.assertEquals(next[1, 1], tron.WALL, 'should now be WALL') self.assertEquals(board.me(), (1, 1), 'should not have changed') self.assertEquals(board.them(), (1, 4), 'should not have changed') self.assertEquals(board[2, 1], tron.FLOOR, 'should still be FLOOR')
def test_tile_is_a(self): board = tronutils.read_board('maps/test-board.txt') is_wall = MyTronBot.tile_is_a(tron.WALL) is_floor = MyTronBot.tile_is_a(tron.FLOOR) known_wall = [(0,0),(0,5),(3,0),(3,5)] known_floor = [(2,1),(2,2),(2,3),(1,3)] for coords in known_wall: self.assertTrue(is_wall(board, coords)) self.assertFalse(is_floor(board, coords)) for coords in known_floor: self.assertFalse(is_wall(board, coords)) self.assertTrue(is_floor(board, coords))
def test_apply_move(self): board = tronutils.read_board('maps/test-board.txt') self.assertEquals(board.me(), (1,1)) self.assertEquals(board.them(), (1,4)) self.assertEquals(board[2,1], tron.FLOOR, 'should be FLOOR') next = MyTronBot.apply_move(board, tron.ME, tron.SOUTH) self.assertEquals(next.me(), (2,1), 'should have changed') self.assertEquals(next.them(), (1,4), 'should not have changed') self.assertEquals(next[1,1], tron.WALL, 'should now be WALL') self.assertEquals(board.me(), (1,1), 'should not have changed') self.assertEquals(board.them(), (1,4), 'should not have changed') self.assertEquals(board[2,1], tron.FLOOR, 'should still be FLOOR')
def test_shortest_path(self): maps = { 'maps/u.txt': 27, 'maps/ring.txt': 15, 'maps/huge-room.txt': 93, 'maps/empty-room.txt': 23, 'maps/test-board.txt': 4 } for m in maps: board = tronutils.read_board(m) path = MyTronBot.shortest_path(board, board.me(), board.them()) expected = maps[m] actual = MyTronBot.moves_between(path) self.assertEquals(actual, expected)
def test_tile_is_a(self): board = tronutils.read_board('maps/test-board.txt') is_wall = MyTronBot.tile_is_a(tron.WALL) is_floor = MyTronBot.tile_is_a(tron.FLOOR) known_wall = [(0, 0), (0, 5), (3, 0), (3, 5)] known_floor = [(2, 1), (2, 2), (2, 3), (1, 3)] for coords in known_wall: self.assertTrue(is_wall(board, coords)) self.assertFalse(is_floor(board, coords)) for coords in known_floor: self.assertFalse(is_wall(board, coords)) self.assertTrue(is_floor(board, coords))
def test_valid_coords(self): board = tronutils.read_board('maps/test-board.txt') self.assertTrue(MyTronBot.valid_coords(board, (0, 0))) self.assertTrue(MyTronBot.valid_coords(board, (0, 5))) self.assertTrue(MyTronBot.valid_coords(board, (2, 2))) self.assertTrue(MyTronBot.valid_coords(board, (3, 0))) self.assertTrue(MyTronBot.valid_coords(board, (3, 5))) self.assertFalse(MyTronBot.valid_coords(board, (-1, -1))) self.assertFalse(MyTronBot.valid_coords(board, (-1, 0))) self.assertFalse(MyTronBot.valid_coords(board, (0, -1))) self.assertFalse(MyTronBot.valid_coords(board, (0, 6))) self.assertFalse(MyTronBot.valid_coords(board, (6, 6))) self.assertFalse(MyTronBot.valid_coords(board, (4, 0)))
def test_valid_coords(self): board = tronutils.read_board('maps/test-board.txt') self.assertTrue(MyTronBot.valid_coords(board, (0,0))) self.assertTrue(MyTronBot.valid_coords(board, (0,5))) self.assertTrue(MyTronBot.valid_coords(board, (2,2))) self.assertTrue(MyTronBot.valid_coords(board, (3,0))) self.assertTrue(MyTronBot.valid_coords(board, (3,5))) self.assertFalse(MyTronBot.valid_coords(board, (-1,-1))) self.assertFalse(MyTronBot.valid_coords(board, (-1,0))) self.assertFalse(MyTronBot.valid_coords(board, (0,-1))) self.assertFalse(MyTronBot.valid_coords(board, (0,6))) self.assertFalse(MyTronBot.valid_coords(board, (6,6))) self.assertFalse(MyTronBot.valid_coords(board, (4,0)))
def test_score(self): board = tronutils.read_board('maps/test-board.txt') state = aimatron.TronState(board, tron.ME) # very simple open board; should tie self.assertEquals(aimatron.eval_fn(state), 0.0) # very simple closed board; should also tie board.board[2] = '# ####' state = aimatron.TronState(board, tron.ME) self.assertEquals(aimatron.eval_fn(state), 0.0) # advantage me board.board[2] = '# ###' state = aimatron.TronState(board, tron.ME) a = 1.0 / 3.0 self.assertAlmostEqual(aimatron.eval_fn(state), a) # advantage them board.board[2] = '# # ##' state = aimatron.TronState(board, tron.ME) a = 1.0 / 3.0 self.assertAlmostEqual(aimatron.eval_fn(state), -a) # I'm stuck board.board[2] = '######' state = aimatron.TronState(board, tron.ME) self.assertEquals(aimatron.eval_fn(state), -1.0) # they're stuck board.board[1] = '#1##2#' board.board[2] = '# ##' state = aimatron.TronState(board, tron.ME) self.assertEquals(aimatron.eval_fn(state), 1.0) # both stuck - make sure doesn't divide by zero! board.board[1] = '#1##2#' board.board[2] = '######' state = aimatron.TronState(board, tron.ME) self.assertEquals(aimatron.eval_fn(state), -0.5)
def test_adjacent_floor(self): board = tronutils.read_board('maps/test-board.txt') fn = lambda pos: tronutils.adjacent(board, pos, tronutils.is_floor) self.assertEqual(fn(board.me()), [(2,1)]) self.assertEqual(fn(board.them()), [(1,3)])
def show_map(filename, squaresize=15): board = tronutils.read_board(filename) show_board(board, squaresize, filename)
def test_tiles_matching(self): board = tronutils.read_board('maps/test-board.txt') wall = MyTronBot.tiles_matching(board, MyTronBot.is_wall) floor = MyTronBot.tiles_matching(board, MyTronBot.is_floor) self.assertEquals(len(wall), 18) self.assertEquals(len(floor), 4)
def test_find_walls(self): board = tronutils.read_board('maps/test-board.txt') walls = MyTronBot.find_walls(board) self.assertEquals(len(walls), 1) self.assertEquals(len(walls[0]), 18)
def test_is_game_over(self): board = tronutils.read_board('maps/test-board.txt') self.assertFalse(MyTronBot.is_game_over(board)) board.board[2] = '######' self.assertTrue(MyTronBot.is_game_over(board))
def test_tiles_matching(self): board = tronutils.read_board('maps/test-board.txt') wall = MyTronBot.tiles_matching(board, MyTronBot.is_wall) floor = MyTronBot.tiles_matching(board, MyTronBot.is_floor) self.assertEquals(len(wall ), 18) self.assertEquals(len(floor), 4)
def test_is_nonwall(self): board = tronutils.read_board('maps/test-board.txt') self.assertFalse(MyTronBot.is_nonwall(board, (0,0))) self.assertTrue(MyTronBot.is_nonwall(board, (1,1))) self.assertTrue(MyTronBot.is_nonwall(board, (2,1)))
def setUp(self): board = tronutils.read_board('maps/test-board.txt') self.game = aimatron.TronGame() self.state = aimatron.TronState(board, tron.ME)
def test_adjacent_floor(self): board = tronutils.read_board('maps/test-board.txt') fn = lambda pos: tronutils.adjacent(board, pos, tronutils.is_floor) self.assertEqual(fn(board.me()), [(2, 1)]) self.assertEqual(fn(board.them()), [(1, 3)])
def load_map_dir(path): return dict( (b, tronutils.read_board(b)) for b in tronutils.list_files(path))
def load_map_dir(path): return dict((b,tronutils.read_board(b)) for b in tronutils.list_files(path))
def test_is_nonwall(self): board = tronutils.read_board('maps/test-board.txt') self.assertFalse(MyTronBot.is_nonwall(board, (0, 0))) self.assertTrue(MyTronBot.is_nonwall(board, (1, 1))) self.assertTrue(MyTronBot.is_nonwall(board, (2, 1)))