示例#1
0
    def test_get_free_position(self):
        """Given a playfield with some tiles and goldcar
        When get_free_position is called
        Then that position doesn't collide with goldcar
        """
        # Given
        playfield = Playfield()

        playfield.level = Level()

        playfield.level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(2, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(3, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(4, 0, 0), Tile.Type.FLAT))

        playfield.goldcars = [
            GoldCar(TrailPosition(playfield.level.get_tile(1, 0), 500), 1)
        ]

        for i in range(0, 256):
            # When
            pos = playfield.get_free_position()

            # Then
            if pos is not None:
                assert playfield.is_free_position(pos)
示例#2
0
    def test_isnt_free_position(self):
        """Given a level with some flat tiles
        When a goldcar occupies a tile
        And we have a position on another tile far from goldcar
        Then is_free_position returns true
        """
        # Given
        playfield = Playfield()

        playfield.level = Level()

        playfield.level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(2, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(3, 0, 0), Tile.Type.FLAT))
        playfield.level.set_tile(Tile(Vec3D(4, 0, 0), Tile.Type.FLAT))

        # When
        playfield.goldcars = [
            GoldCar(TrailPosition(playfield.level.get_tile(2, 0), 900), 1)
        ]

        pos = TrailPosition(playfield.level.get_tile(2, 0), 100)

        # Then
        assert playfield.is_free_position(pos)
示例#3
0
    def test_add_level(self):
        quest = Quest()
        scen0 = ScenarioCoinCollect(False, 20, 1, [])
        scen1 = ScenarioDiamondCollect(False, 20, 1, [])
        level0 = Level()
        level1 = Level()
        quest.add_level(scen0, 0)
        quest.add_level(scen1, 4)

        assert quest.create_scenario() is not scen0
        assert isinstance(quest.create_scenario(), ScenarioCoinCollect)
        assert quest.get_current_level_nr() == 0

        quest.to_next_level()

        assert quest.create_scenario() is not scen1
        assert quest.get_current_level_nr() == 4
示例#4
0
    def test_slope_neighbors(self):
        level = Level()

        level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.SOUTH_SLOPE_TOP))
        level.set_tile(Tile(Vec3D(-1, 2, 0), Tile.Type.SOUTH_SLOPE_BOT))

        neighbor = level.get_tile(0, 0).get_neighbor(Direction.SOUTH)
        assert neighbor is not None and neighbor.type == Tile.Type.SOUTH_SLOPE_BOT

        neighbor = level.get_tile(-1, 2).get_neighbor(Direction.NORTH)
        assert neighbor is not None and neighbor.type == Tile.Type.SOUTH_SLOPE_TOP
示例#5
0
    def test_edge_portal_handling(self):
        """Given a goldcar on a small track with 2 portals
           When goldcar rides through one portal
           Then it comes out of the other"""
        # Given
        level = Level()
        portalA = Enterance(Vec3D(1, 0, 0))
        tileA = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        tileB = Tile(Vec3D(0, 1, 0), Tile.Type.FLAT)
        tileC = Tile(Vec3D(0, 2, 0), Tile.Type.FLAT)
        portalC = Enterance(Vec3D(1, 2, 0))

        level.set_tile(portalA)
        level.set_tile(tileA)
        level.set_tile(tileB)
        level.set_tile(tileC)
        level.set_tile(portalC)

        goldcar = GoldCar(TrailPosition(tileB, 0), 0)
        #goldcar.pos.reverse_progress()

        playfield = Playfield()
        playfield.level = level
        playfield.goldcars = [goldcar]

        order = [tileB, tileC, portalC, portalA, tileA, tileB]
        order_at = 0

        count = 0
        while order_at < len(order) - 1 and count < 1000:
            playfield.game_tick()

            print goldcar.pos.tile, goldcar.pos.progress

            if goldcar.pos.tile == order[order_at]:
                pass
            elif goldcar.pos.tile == order[order_at + 1]:
                order_at += 1
            else:
                assert False, "unknown tile after " + str(order_at)

            count += 1

        assert count < 1000
示例#6
0
    def test_slope_neighbors( self ):
        level = Level()

        level.set_tile( Tile( Vec3D(0,0,0), Tile.Type.SOUTH_SLOPE_TOP ) )
        level.set_tile( Tile( Vec3D(-1,2,0), Tile.Type.SOUTH_SLOPE_BOT ) )

        neighbor = level.get_tile( 0,0 ).get_neighbor( Direction.SOUTH )
        assert neighbor is not None and neighbor.type == Tile.Type.SOUTH_SLOPE_BOT

        neighbor = level.get_tile( -1,2 ).get_neighbor( Direction.NORTH )
        assert neighbor is not None and neighbor.type == Tile.Type.SOUTH_SLOPE_TOP
示例#7
0
    def test_edge_portal_handling( self ):
        """Given a goldcar on a small track with 2 portals
           When goldcar rides through one portal
           Then it comes out of the other"""
        # Given
        level = Level()
        portalA = Enterance( Vec3D(1,0,0) )        
        tileA = Tile(        Vec3D(0,0,0), Tile.Type.FLAT )
        tileB = Tile(        Vec3D(0,1,0), Tile.Type.FLAT )        
        tileC = Tile(        Vec3D(0,2,0), Tile.Type.FLAT )        
        portalC = Enterance( Vec3D(1,2,0) )
        
        level.set_tile( portalA )
        level.set_tile( tileA )
        level.set_tile( tileB )
        level.set_tile( tileC )
        level.set_tile( portalC)

        goldcar = GoldCar( TrailPosition( tileB, 0), 0 )
        #goldcar.pos.reverse_progress()

        playfield = Playfield()
        playfield.level = level
        playfield.goldcars = [goldcar]        

        order = [tileB, tileC, portalC, portalA, tileA, tileB]
        order_at = 0

        count = 0
        while order_at < len(order)-1 and count < 1000:
            playfield.game_tick()

            print goldcar.pos.tile, goldcar.pos.progress

            if goldcar.pos.tile == order[ order_at ]:
                pass
            elif goldcar.pos.tile == order[ order_at+1 ]:
                order_at += 1
            else: 
                assert False, "unknown tile after " + str(order_at)
            
            count += 1

        assert  count < 1000
示例#8
0
    def test_goldcar_rides_to_next_tile(self):
        """Given a goldcar on one of 2 sequent tiles
           When the goldcar rides past the first one
           Then the goldcar is located on the second one"""
        # Given
        level = Level()
        tileA = Tile(Vec3D(0, 0, 0), Tile.Type.FLAT)
        tileB = Tile(Vec3D(0, 1, 0), Tile.Type.FLAT)
        level.set_tile(Tile(Vec3D(0, -1, 0), Tile.Type.FLAT))
        level.set_tile(tileA)
        level.set_tile(tileB)
        level.set_tile(Tile(Vec3D(0, 2, 0), Tile.Type.FLAT))

        goldcar = GoldCar(TrailPosition(tileA, 0), 0)

        count = 0
        while goldcar.pos.tile != tileB and count < 100:
            goldcar.game_tick()
            print goldcar.pos.tile, goldcar.pos.progress
            count += 1

        assert count < 100
示例#9
0
    def test_goldcar_rides_to_next_tile( self ):
        """Given a goldcar on one of 2 sequent tiles
           When the goldcar rides past the first one
           Then the goldcar is located on the second one"""
        # Given
        level = Level()
        tileA = Tile( Vec3D(0,0,0), Tile.Type.FLAT )
        tileB = Tile( Vec3D(0,1,0), Tile.Type.FLAT )
        level.set_tile( Tile( Vec3D(0,-1,0), Tile.Type.FLAT ) )
        level.set_tile( tileA )
        level.set_tile( tileB )
        level.set_tile( Tile( Vec3D(0,2,0), Tile.Type.FLAT ) )


        goldcar = GoldCar( TrailPosition( tileA, 0 ), 0 )


        count = 0
        while goldcar.pos.tile != tileB and count < 100:
            goldcar.game_tick()
            print goldcar.pos.tile, goldcar.pos.progress
            count += 1

        assert count < 100
示例#10
0
    def test_neighbors( self ):
        level = Level();

        level.set_tile( Tile( Vec3D(0,0,0), Tile.Type.FLAT ) )
        level.set_tile( Tile( Vec3D(0,1,0), Tile.Type.FLAT ) )
        level.set_tile( Tile( Vec3D(1,0,0), Tile.Type.WEST_SLOPE_TOP ) )
        level.set_tile( Tile( Vec3D(1,1,0), Tile.Type.WEST_SLOPE_BOT ) )

        neighbor = level.get_tile( 0,0 ).get_neighbor( Direction.SOUTH )
        assert neighbor is not None
        assert neighbor.type == Tile.Type.FLAT
        assert neighbor == level.get_tile(0,1)

        neighbor = level.get_tile( 0,1 ).get_neighbor( Direction.NORTH )
        assert neighbor == level.get_tile(0,0)

        neighbor = level.get_tile( 0,0 ).get_neighbor( Direction.EAST )
        assert neighbor is None
        
        neighbor = level.get_tile( 0,1 ).get_neighbor( Direction.EAST )
        assert neighbor == level.get_tile(1,1)
        
        level.remove_tile( 0, 1 )
        neighbor = level.get_tile( 0,0 ).get_neighbor( Direction.SOUTH )
        assert neighbor is None
示例#11
0
    def test_save_load_level( self ):
        level = Level()
        level_loaded = Level()

        level.set_tile( Tile( Vec3D(0,0,0), Tile.Type.FLAT ) )
        level.set_tile( Tile( Vec3D(0,1,0), Tile.Type.SOUTH_SLOPE_TOP ) )
        assert level.get_tile(0,1).trail.type == Trail.Type.HILL
        level.set_tile( Tile( Vec3D(1,3,0), Tile.Type.SOUTH_SLOPE_BOT ) )
        assert level.get_tile(0,1).trail.type == Trail.Type.HILL
        level.set_tile( Tile( Vec3D(1,0,0), Tile.Type.WEST_SLOPE_TOP ) )

        assert level.get_tile(0,0).get_neighbor( Direction.SOUTH ) is not None
        assert level.get_tile(0,0).get_neighbor( Direction.SOUTH ).type == Tile.Type.SOUTH_SLOPE_TOP

        level.save("saveloadtest.lvl");
        level_loaded.set_tile( Tile( Vec3D(2,2,0), Tile.Type.NORTH_SLOPE_TOP ) )
        level_loaded.load("saveloadtest.lvl");

        assert level_loaded.get_tile( 0, 0 ) is not None
        assert level_loaded.get_tile( 0, 0 ).type == Tile.Type.FLAT
        assert level_loaded.get_tile( 0, 1 ) is not None
        assert level_loaded.get_tile( 0, 1 ).type == Tile.Type.SOUTH_SLOPE_TOP
        assert level_loaded.get_tile( 1, 3 ) is not None
        assert level_loaded.get_tile( 1, 3 ).type == Tile.Type.SOUTH_SLOPE_BOT
        assert level_loaded.get_tile( 1, 0 ) is not None
        assert level_loaded.get_tile( 1, 0 ).type == Tile.Type.WEST_SLOPE_TOP
        assert level_loaded.get_tile( 1, 1 ) is None

        # check neighbors
        assert level_loaded.get_tile(0,0).get_neighbor( Direction.SOUTH ) is not None
        assert level_loaded.get_tile(0,0).get_neighbor( Direction.SOUTH ).type == Tile.Type.SOUTH_SLOPE_TOP

        os.remove("saveloadtest.lvl")
示例#12
0
    def test_get_add_tile( self ):
        level = Level()

        assert level.get_tile( 0, 0 ) is None
        assert level.get_tile( -1, 9 ) is None

        tile1 = Tile( Vec3D(), Tile.Type.FLAT )
        tile2 = Tile( Vec3D(), Tile.Type.EAST_SLOPE_TOP )
        level.set_tile( tile1 )
        assert level.get_tile( 0, 0 ) is not None
        assert level.get_tile( 0, 0 ).type == Tile.Type.FLAT

        level.set_tile( tile2 )
        assert level.get_tile( 0, 0 ) is not None 
        assert level.get_tile( 0, 0 ).type == Tile.Type.EAST_SLOPE_TOP

        level.remove_tile( 0, 0 )
        assert level.get_tile( 0, 0 ) is None
示例#13
0
    def test_get_flat_tile( self ):
        level = Level()

        level.set_tile( Tile( Vec3D(1,0,0), Tile.Type.WEST_SLOPE_TOP ) )
        level.set_tile( Tile( Vec3D(5,5,0), Tile.Type.FLAT ) )
        level.set_tile( Tile( Vec3D(1,1,0), Tile.Type.WEST_SLOPE_BOT ) )
        level.set_tile( Tile( Vec3D(0,0,0), Tile.Type.FLAT ) )

        assert level.get_first_flat_tile() == level.get_tile(0,0) \
               or level.get_first_flat_tile() == level.get_tile(5,5)

        first_tile = False;
        second_tile = False;

        for i in range (0, 20): # chance is realy small that all 20 will be same
            t = level.get_random_flat_tile()
            if t == level.get_tile(5,5):
                first_tile = True
            elif t == level.get_tile(0,0):
                second_tile = True
            else:
                assert False

        assert first_tile and second_tile
示例#14
0
    def test_neighbors(self):
        level = Level()

        level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT))
        level.set_tile(Tile(Vec3D(0, 1, 0), Tile.Type.FLAT))
        level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.WEST_SLOPE_TOP))
        level.set_tile(Tile(Vec3D(1, 1, 0), Tile.Type.WEST_SLOPE_BOT))

        neighbor = level.get_tile(0, 0).get_neighbor(Direction.SOUTH)
        assert neighbor is not None
        assert neighbor.type == Tile.Type.FLAT
        assert neighbor == level.get_tile(0, 1)

        neighbor = level.get_tile(0, 1).get_neighbor(Direction.NORTH)
        assert neighbor == level.get_tile(0, 0)

        neighbor = level.get_tile(0, 0).get_neighbor(Direction.EAST)
        assert neighbor is None

        neighbor = level.get_tile(0, 1).get_neighbor(Direction.EAST)
        assert neighbor == level.get_tile(1, 1)

        level.remove_tile(0, 1)
        neighbor = level.get_tile(0, 0).get_neighbor(Direction.SOUTH)
        assert neighbor is None
示例#15
0
    def test_get_add_tile(self):
        level = Level()

        assert level.get_tile(0, 0) is None
        assert level.get_tile(-1, 9) is None

        tile1 = Tile(Vec3D(), Tile.Type.FLAT)
        tile2 = Tile(Vec3D(), Tile.Type.EAST_SLOPE_TOP)
        level.set_tile(tile1)
        assert level.get_tile(0, 0) is not None
        assert level.get_tile(0, 0).type == Tile.Type.FLAT

        level.set_tile(tile2)
        assert level.get_tile(0, 0) is not None
        assert level.get_tile(0, 0).type == Tile.Type.EAST_SLOPE_TOP

        level.remove_tile(0, 0)
        assert level.get_tile(0, 0) is None
示例#16
0
    def test_save_load_level(self):
        level = Level()
        level_loaded = Level()

        level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT))
        level.set_tile(Tile(Vec3D(0, 1, 0), Tile.Type.SOUTH_SLOPE_TOP))
        assert level.get_tile(0, 1).trail.type == Trail.Type.HILL
        level.set_tile(Tile(Vec3D(1, 3, 0), Tile.Type.SOUTH_SLOPE_BOT))
        assert level.get_tile(0, 1).trail.type == Trail.Type.HILL
        level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.WEST_SLOPE_TOP))

        assert level.get_tile(0, 0).get_neighbor(Direction.SOUTH) is not None
        assert level.get_tile(0, 0).get_neighbor(
            Direction.SOUTH).type == Tile.Type.SOUTH_SLOPE_TOP

        level.save("saveloadtest.lvl")
        level_loaded.set_tile(Tile(Vec3D(2, 2, 0), Tile.Type.NORTH_SLOPE_TOP))
        level_loaded.load("saveloadtest.lvl")

        assert level_loaded.get_tile(0, 0) is not None
        assert level_loaded.get_tile(0, 0).type == Tile.Type.FLAT
        assert level_loaded.get_tile(0, 1) is not None
        assert level_loaded.get_tile(0, 1).type == Tile.Type.SOUTH_SLOPE_TOP
        assert level_loaded.get_tile(1, 3) is not None
        assert level_loaded.get_tile(1, 3).type == Tile.Type.SOUTH_SLOPE_BOT
        assert level_loaded.get_tile(1, 0) is not None
        assert level_loaded.get_tile(1, 0).type == Tile.Type.WEST_SLOPE_TOP
        assert level_loaded.get_tile(1, 1) is None

        # check neighbors
        assert level_loaded.get_tile(0, 0).get_neighbor(
            Direction.SOUTH) is not None
        assert level_loaded.get_tile(0, 0).get_neighbor(
            Direction.SOUTH).type == Tile.Type.SOUTH_SLOPE_TOP

        os.remove("saveloadtest.lvl")
示例#17
0
    def test_get_flat_tile(self):
        level = Level()

        level.set_tile(Tile(Vec3D(1, 0, 0), Tile.Type.WEST_SLOPE_TOP))
        level.set_tile(Tile(Vec3D(5, 5, 0), Tile.Type.FLAT))
        level.set_tile(Tile(Vec3D(1, 1, 0), Tile.Type.WEST_SLOPE_BOT))
        level.set_tile(Tile(Vec3D(0, 0, 0), Tile.Type.FLAT))

        assert level.get_first_flat_tile() == level.get_tile(0,0) \
               or level.get_first_flat_tile() == level.get_tile(5,5)

        first_tile = False
        second_tile = False

        for i in range(0,
                       20):  # chance is realy small that all 20 will be same
            t = level.get_random_flat_tile()
            if t == level.get_tile(5, 5):
                first_tile = True
            elif t == level.get_tile(0, 0):
                second_tile = True
            else:
                assert False

        assert first_tile and second_tile