Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
 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)
Пример #6
0
 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)
Пример #7
0
 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)
Пример #8
0
 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)
Пример #9
0
 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)
Пример #10
0
 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)
Пример #11
0
    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)
Пример #12
0
 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')
Пример #13
0
    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)
Пример #14
0
 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')
Пример #15
0
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()
Пример #16
0
 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')
Пример #17
0
 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))
Пример #18
0
 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')
Пример #19
0
 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)
Пример #20
0
 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))
Пример #21
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)))
Пример #22
0
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()
Пример #23
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)))
Пример #24
0
 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)
Пример #25
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)
Пример #26
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)
Пример #27
0
 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)])
Пример #28
0
def show_map(filename, squaresize=15):
    board = tronutils.read_board(filename)
    show_board(board, squaresize, filename)
Пример #29
0
 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)
Пример #30
0
 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)
Пример #31
0
 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))
Пример #32
0
 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)
Пример #33
0
 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)))
Пример #34
0
 def setUp(self):
     board = tronutils.read_board('maps/test-board.txt')
     self.game = aimatron.TronGame()
     self.state = aimatron.TronState(board, tron.ME)
Пример #35
0
 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)
Пример #36
0
 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))
Пример #37
0
 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)])
Пример #38
0
def show_map(filename, squaresize=15):
    board = tronutils.read_board(filename)
    show_board(board, squaresize, filename)
Пример #39
0
def load_map_dir(path):
    return dict(
        (b, tronutils.read_board(b)) for b in tronutils.list_files(path))
Пример #40
0
 def setUp(self):
     board = tronutils.read_board('maps/test-board.txt')
     self.game = aimatron.TronGame()
     self.state = aimatron.TronState(board, tron.ME)
Пример #41
0
def load_map_dir(path):
    return dict((b,tronutils.read_board(b)) for b in tronutils.list_files(path))
Пример #42
0
 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)))