Esempio n. 1
0
class ValueTileTestCase(unittest.TestCase):
    def setUp(self):
        self.tile_collection = TileCollection()
        self.test_tile = self.tile_collection.get_tile('value', value=2)
        self.empty_tile = self.tile_collection.get_tile('empty')
        self.blocking_tile = self.tile_collection.get_tile('blocking')
        self.fitting_value_tile = self.test_tile
        self.unfitting_value_tile = self.tile_collection.get_tile('value', value=4)

    def test_to_str(self):
        self.assertEqual('2', str(self.test_tile))

    def test_movingToValueTile(self):
        self.assertFalse(self.test_tile.can_be_replaced_with(self.empty_tile))
        self.assertFalse(self.test_tile.can_be_replaced_with(self.blocking_tile))
        self.assertFalse(self.test_tile.can_be_replaced_with(self.fitting_value_tile))
        self.assertFalse(self.test_tile.can_be_replaced_with(self.unfitting_value_tile))

    def test_movingValueTile(self):
        self.assertTrue(self.test_tile.can_move_to(self.empty_tile))
        self.assertFalse(self.test_tile.can_move_to(self.blocking_tile))
        self.assertFalse(self.test_tile.can_move_to(self.fitting_value_tile))
        self.assertFalse(self.test_tile.can_move_to(self.unfitting_value_tile))

    def test_fusingWithValueTile(self):
        self.assertFalse(self.test_tile.can_fuse_with(self.empty_tile))
        self.assertFalse(self.test_tile.can_fuse_with(self.blocking_tile))
        self.assertTrue(self.test_tile.can_fuse_with(self.fitting_value_tile))
        self.assertFalse(self.test_tile.can_fuse_with(self.unfitting_value_tile))

    def test_fusingValueTile(self):
        self.assertFalse(self.test_tile.can_accept_fusion_with(self.empty_tile))
        self.assertFalse(self.test_tile.can_accept_fusion_with(self.blocking_tile))
        self.assertTrue(self.test_tile.can_accept_fusion_with(self.fitting_value_tile))
        self.assertFalse(self.test_tile.can_accept_fusion_with(self.unfitting_value_tile))
Esempio n. 2
0
class EmptyTileTestCase(unittest.TestCase):
    def setUp(self):
        self.tile_collection = TileCollection()
        self.test_tile = self.tile_collection.get_tile('empty')
        self.empty_tile = self.test_tile
        self.blocking_tile = self.tile_collection.get_tile('blocking')
        self.value_tile = self.tile_collection.get_tile('value', value=4)

    def test_to_str(self):
        self.assertEqual(' ', str(self.test_tile))

    def test_movingToEmptyTile(self):
        self.assertTrue(self.test_tile.can_be_replaced_with(self.empty_tile))
        self.assertTrue(self.test_tile.can_be_replaced_with(
            self.blocking_tile))
        self.assertTrue(self.test_tile.can_be_replaced_with(self.value_tile))

    def test_movingEmptyTile(self):
        self.assertFalse(self.test_tile.can_move_to(self.empty_tile))
        self.assertFalse(self.test_tile.can_move_to(self.blocking_tile))
        self.assertFalse(self.test_tile.can_move_to(self.value_tile))

    def test_fusingWithEmptyTile(self):
        self.assertFalse(self.test_tile.can_fuse_with(self.empty_tile))
        self.assertFalse(self.test_tile.can_fuse_with(self.blocking_tile))
        self.assertFalse(self.test_tile.can_fuse_with(self.value_tile))

    def test_fusingEmptyTile(self):
        self.assertFalse(self.test_tile.can_accept_fusion_with(
            self.empty_tile))
        self.assertFalse(
            self.test_tile.can_accept_fusion_with(self.blocking_tile))
        self.assertFalse(self.test_tile.can_accept_fusion_with(
            self.value_tile))
Esempio n. 3
0
 def setUp(self):
     self.tile_collection = TileCollection()
     self.test_tile = self.tile_collection.get_tile('value', value=2)
     self.empty_tile = self.tile_collection.get_tile('empty')
     self.blocking_tile = self.tile_collection.get_tile('blocking')
     self.fitting_value_tile = self.test_tile
     self.unfitting_value_tile = self.tile_collection.get_tile('value',
                                                               value=4)
Esempio n. 4
0
 def setUp(self):
     self.tile_collection = TileCollection()
     self.empty_tile_container = TileContainer(
         self.tile_collection.get_tile('empty'),
         self.tile_collection
     )
     self.blocking_tile_container = TileContainer(
         self.tile_collection.get_tile('blocking'),
         self.tile_collection
     )
Esempio n. 5
0
 def setUp(self):
     self.tile_collection = TileCollection()
     self.test_tile = self.tile_collection.get_tile('value', value=2)
     self.empty_tile = self.tile_collection.get_tile('empty')
     self.blocking_tile = self.tile_collection.get_tile('blocking')
     self.fitting_value_tile = self.test_tile
     self.unfitting_value_tile = self.tile_collection.get_tile('value', value=4)
Esempio n. 6
0
class TileContainerTestCase(unittest.TestCase):
    def setUp(self):
        self.tile_collection = TileCollection()
        self.empty_tile_container = TileContainer(
            self.tile_collection.get_tile('empty'),
            self.tile_collection
        )
        self.blocking_tile_container = TileContainer(
            self.tile_collection.get_tile('blocking'),
            self.tile_collection
        )
        # TODO: add other tiles to containers

    def test_tileProperty(self):
        """
        Asserts that the TileContainer returns its Tile unless it was fused this
        turn, then it returns a BlockingTile.
        """
        self.assertIsInstance(self.empty_tile_container.tile, EmptyTile)
        self.assertIsInstance(self.blocking_tile_container.tile, BlockingTile)
        self.empty_tile_container.fused = True
        self.blocking_tile_container.fused = True
        self.assertIsInstance(self.empty_tile_container.tile, BlockingTile)
        self.assertIsInstance(self.blocking_tile_container.tile, BlockingTile)
Esempio n. 7
0
 def __init__(
         self,
         input_stream: ConsoleInput,
         output_stream: ConsoleOutput
 ):
     self.input = input_stream
     self.output = output_stream
     self.tile_collection = TileCollection()
     self.game_field = GameField.basic_field(self.tile_collection)
     self.game_controller = GameController(
         self.game_field,
         self.tile_collection
     )
     self.game_controller.initialize()
     self.renderer = ConsoleRenderer(output_stream)  # type: Renderer
     self.prompt_counter = 0
Esempio n. 8
0
class GameControllerTestCase(unittest.TestCase):
    def setUp(self):
        self.tile_collection = TileCollection()
        self.game_field = GameField(self.tile_collection)
        self.game_controller = GameController(self.game_field, self.tile_collection)
        self.game_controller._random.seed(1337)

    def test_initialize(self):
        """
        Tests that initialization places two random Tiles on the GameField.
        """
        self.game_controller.initialize()
        # The spaces which the random tiles occupy are based on the random gene-
        # rator seed and thus are always equal in tests.
        self.assertEqual(
            self.game_field.field_data[2][1].tile,
            self.tile_collection.get_tile('value', value=2)
        )
        self.assertEqual(
            self.game_field.field_data[2][3].tile,
            self.tile_collection.get_tile('value', value=4)
        )

    def test_swipeNorth(self):
        """
        Test that issueing a swipeNorthAction uses the north-ward iterator.
        """
        self.game_field = create_autospec(GameField)
        self.game_field.field_data = {}
        self.game_controller = GameController(self.game_field, self.tile_collection)
        self.game_controller._random.seed(1337)
        self.game_controller._swipe = MagicMock()
        self.game_controller.swipe_north_action()
        self.game_field.get_north_iterator.assert_any_call()
        self.game_controller._swipe.assert_called_with(self.game_field.get_north_iterator())

    def test_swipeEast(self):
        """
        Test that issueing a swipeNorthAction uses the east-ward iterator.
        """
        self.game_field = create_autospec(GameField)
        self.game_field.field_data = {}
        self.game_controller = GameController(self.game_field, self.tile_collection)
        self.game_controller._random.seed(1337)
        self.game_controller._swipe = MagicMock()
        self.game_controller.swipe_east_action()
        self.game_field.get_east_iterator.assert_any_call()
        self.game_controller._swipe.assert_called_with(self.game_field.get_east_iterator())

    def test_swipeSouth(self):
        """
        Test that issueing a swipeNorthAction uses the south-ward iterator.
        """
        self.game_field = create_autospec(GameField)
        self.game_field.field_data = {}
        self.game_controller = GameController(self.game_field, self.tile_collection)
        self.game_controller._random.seed(1337)
        self.game_controller._swipe = MagicMock()
        self.game_controller.swipe_south_action()
        self.game_field.get_south_iterator.assert_any_call()
        self.game_controller._swipe.assert_called_with(self.game_field.get_south_iterator())

    def test_swipeWest(self):
        """
        Test that issueing a swipeNorthAction uses the west-ward iterator.
        """
        self.game_field = create_autospec(GameField)
        self.game_field.field_data = {}
        self.game_controller = GameController(self.game_field, self.tile_collection)
        self.game_controller._random.seed(1337)
        self.game_controller._swipe = MagicMock()
        self.game_controller.swipe_west_action()
        self.game_field.get_west_iterator.assert_any_call()
        self.game_controller._swipe.assert_called_with(self.game_field.get_west_iterator())

    def test_swipe(self):
        """
        Functional test that a swipe action correctly traverses the created
        iterator.
        The field is layed out like this:
         2  x  x  4
         2  4  8  4
         4  4  2  4
        32 16  2  4
        The result should be this: (northward swipe)
         4  8  8  8
         4 16  4  8
        32  x  x  x
         x  x  x  x
        """
        # set up field:
        self.game_field.field_data[0][0].tile = self.tile_collection.get_tile('value', value=2)
        self.game_field.field_data[3][0].tile = self.tile_collection.get_tile('value', value=4)
        self.game_field.field_data[0][1].tile = self.tile_collection.get_tile('value', value=2)
        self.game_field.field_data[1][1].tile = self.tile_collection.get_tile('value', value=4)
        self.game_field.field_data[2][1].tile = self.tile_collection.get_tile('value', value=8)
        self.game_field.field_data[3][1].tile = self.tile_collection.get_tile('value', value=4)
        self.game_field.field_data[0][2].tile = self.tile_collection.get_tile('value', value=4)
        self.game_field.field_data[1][2].tile = self.tile_collection.get_tile('value', value=4)
        self.game_field.field_data[2][2].tile = self.tile_collection.get_tile('value', value=2)
        self.game_field.field_data[3][2].tile = self.tile_collection.get_tile('value', value=4)
        self.game_field.field_data[0][3].tile = self.tile_collection.get_tile('value', value=32)
        self.game_field.field_data[1][3].tile = self.tile_collection.get_tile('value', value=16)
        self.game_field.field_data[2][3].tile = self.tile_collection.get_tile('value', value=2)
        self.game_field.field_data[3][3].tile = self.tile_collection.get_tile('value', value=4)

        self.game_controller.swipe_north_action()

        self.assertEqual(
            self.tile_collection.get_tile('value', value=4),
            self.game_field.field_data[0][0]._tile
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=8),
            self.game_field.field_data[1][0]._tile
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=8),
            self.game_field.field_data[2][0]._tile
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=8),
            self.game_field.field_data[3][0]._tile
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=4),
            self.game_field.field_data[0][1]._tile
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=16),
            self.game_field.field_data[1][1]._tile
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=4),
            self.game_field.field_data[2][1]._tile
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=8),
            self.game_field.field_data[3][1]._tile
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=32),
            self.game_field.field_data[0][2]._tile
        )
        # One Tile is randomly inserted after swiping
        self.assertEqual(
            self.tile_collection.get_tile('value', value=4),
            self.game_field.field_data[2][3].tile
        )

    def test_scorekeeping(self) -> None:
        """
        Tests, that swipes return the resulting score and score is accessible.
        The Score numbers are based on the random seed and thus are equal every
        time the Test is run.
        """
        with self.assertRaises(GameNotInitializedError):
            score = self.game_controller.score
        self.game_controller.initialize()

        self.assertEqual(
            0,
            self.game_controller.swipe_east_action()
        )
        self.assertEqual(
            4,
            self.game_controller.swipe_south_action()
        )
        self.assertEqual(
            12,
            self.game_controller.swipe_south_action()
        )
        self.assertEqual(
            16,
            self.game_controller.swipe_south_action()
        )
        self.assertEqual(
            16,
            self.game_controller.swipe_south_action()
        )
        self.assertEqual(
            20,
            self.game_controller.swipe_south_action()
        )
        self.assertEqual(20, self.game_controller.score)

    def test_initialization_enables_score(self):
        """
        Tests, that calling GameController.initialize() allows to acces GameCon-
        troller.score afterwards. Before, it raises a GameNotInitializedError.
        """
        self.game_field = GameField.basic_field(self.tile_collection)
        self.game_controller = GameController(
            self.game_field,
            self.tile_collection
        )

        with self.assertRaises(GameNotInitializedError):
            score = self.game_controller.score
        self.game_controller.initialize()
        self.assertEqual(0, self.game_controller.score)

    def test_invalid_action_error(self):
        """
        Tests that an InvalidActionError is raised when an action is issued that
        can't be executed.

        This test is very rough but should work.
        """
        self.game_controller.initialize()

        with self.assertRaises(InvalidActionError):
            for i in range(100):
                self.game_controller.swipe_east_action()

    def test_game_lost_error(self):
        """
        Tests that a GameLostError is raised when an action is issued but no
        action can be executed anymore.

        This test is very rough but should work.
        """
        self.game_controller.initialize()

        # fill the GameField in a checkers mannern with value twos/fours, so
        # that they can't be fused with each other.
        for x in range(4):
            for y in range(4):
                if x % 2 == y % 2:
                    self.game_field.field_data[x][y].tile = \
                        self.tile_collection.get_tile('value', value=2)
                else:
                    self.game_field.field_data[x][y].tile = \
                        self.tile_collection.get_tile('value', value=4)

        with self.assertRaises(GameLostError):
            self.game_controller.swipe_north_action()
Esempio n. 9
0
 def setUp(self):
     self.tile_collection = TileCollection()
     self.game_field = GameField(self.tile_collection)
     self.game_controller = GameController(self.game_field, self.tile_collection)
     self.game_controller._random.seed(1337)
Esempio n. 10
0
 def setUp(self):
     self.tile_collection = TileCollection()
Esempio n. 11
0
class TileCollectionTestCase(unittest.TestCase):
    def setUp(self):
        self.tile_collection = TileCollection()

    def test_lazyLoading(self):
        """
        The Tiles are only created in storage if the are asked for, not in ad-
        vance.
        """
        with self.assertRaises(Exception):
            _ = self.tile_collection.tile_storage[('blocking', str(()), str({}))]
        tile = self.tile_collection.get_tile('blocking')
        self.assertEqual(tile, self.tile_collection.tile_storage[('blocking', str(()), str({}))])

    def test_identicalInstances(self):
        """
        The Tiles are kept in storage and every time a Tile is asked for, the
        same instance is returned.
        """
        self.assertEqual(
            self.tile_collection.get_tile('empty'),
            self.tile_collection.get_tile('empty')
        )
        self.assertEqual(
            self.tile_collection.get_tile('blocking'),
            self.tile_collection.get_tile('blocking')
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=2),
            self.tile_collection.get_tile('value', value=2)
        )
        self.assertEqual(
            self.tile_collection.get_tile('value', value=4),
            self.tile_collection.get_tile('value', value=4)
        )
        self.assertNotEqual(
            self.tile_collection.get_tile('value', value=2),
            self.tile_collection.get_tile('value', value=4)
        )

    def test_tileTypes(self):
        """
        The different Requests return different Types of Tiles.
        """
        self.assertIsInstance(
            self.tile_collection.get_tile('empty'),
            EmptyTile
        )
        self.assertIsInstance(
            self.tile_collection.get_tile('blocking'),
            BlockingTile
        )
        self.assertIsInstance(
            self.tile_collection.get_tile('value', value=1),
            ValueTile
        )

    def test_invalidTypeRaisesException(self):
        with self.assertRaises(Exception):
            self.tile_collection.get_tile('invalid_tilename')

    def test_fuse(self):
        """
        Tests that fusing two value tiles returns a ValueTile with their sum as
        its value and the resulting score.
        """
        self.assertEqual(
            (self.tile_collection.get_tile('value',  value=32), 32),
            self.tile_collection.fuse(
                self.tile_collection.get_tile('value',  value=16),
                self.tile_collection.get_tile('value',  value=16)
            )
        )
        # this is technically illegal in the game, but works for engine purpo-
        # ses:
        self.assertEqual(
            (self.tile_collection.get_tile('value',  value=12), 12),
            self.tile_collection.fuse(
                self.tile_collection.get_tile('value',  value=4),
                self.tile_collection.get_tile('value',  value=8)
            )
        )

        with self.assertRaises(Exception):
            self.tile_collection.fuse(
                self.tile_collection.get_tile('empty'),
                self.tile_collection.get_tile('value',  value=8)
            )
Esempio n. 12
0
 def empty(tile_collection: TileCollection) -> 'TileContainer':
     return TileContainer(tile_collection.get_tile('empty'), tile_collection)
Esempio n. 13
0
 def empty(tile_collection: TileCollection) -> 'TileContainer':
     return TileContainer(tile_collection.get_tile('empty'),
                          tile_collection)
Esempio n. 14
0
 def setUp(self):
     # basic_field instantiates a GameField and fills it with TileContainers
     # and empty Tiles
     self.tile_collection = TileCollection()
     self.game_field = GameField.basic_field(self.tile_collection)
Esempio n. 15
0
 def setUp(self):
     self.tile_collection = TileCollection()
Esempio n. 16
0
from controller.game_controller import GameController
from exceptions import InvalidActionError, GameLostError
from gamefield.gamefield import GameField
from gamefield.tilecollection import TileCollection
import random
"""
This script simulates a player who randomly swipes the game until they lose.
Afterwards, the number of valid swipes and the reached score are displayed.
"""

tile_collection = TileCollection()
game_field = GameField(tile_collection)
game_controller = GameController(game_field, tile_collection)
game_controller.initialize()
random = random.Random()

moves = 0
actions = [
    game_controller.swipe_north_action, game_controller.swipe_east_action,
    game_controller.swipe_south_action, game_controller.swipe_west_action
]
while True:
    try:
        # do anything
        action = random.choice(actions)
        action()
        moves += 1
    except InvalidActionError as _:
        # if the action was invalid, ignore and keep going
        pass
    except GameLostError as e:
Esempio n. 17
0
class TileCollectionTestCase(unittest.TestCase):
    def setUp(self):
        self.tile_collection = TileCollection()

    def test_lazyLoading(self):
        """
        The Tiles are only created in storage if the are asked for, not in ad-
        vance.
        """
        with self.assertRaises(Exception):
            _ = self.tile_collection.tile_storage[('blocking', str(
                ()), str({}))]
        tile = self.tile_collection.get_tile('blocking')
        self.assertEqual(
            tile, self.tile_collection.tile_storage[('blocking', str(
                ()), str({}))])

    def test_identicalInstances(self):
        """
        The Tiles are kept in storage and every time a Tile is asked for, the
        same instance is returned.
        """
        self.assertEqual(self.tile_collection.get_tile('empty'),
                         self.tile_collection.get_tile('empty'))
        self.assertEqual(self.tile_collection.get_tile('blocking'),
                         self.tile_collection.get_tile('blocking'))
        self.assertEqual(self.tile_collection.get_tile('value', value=2),
                         self.tile_collection.get_tile('value', value=2))
        self.assertEqual(self.tile_collection.get_tile('value', value=4),
                         self.tile_collection.get_tile('value', value=4))
        self.assertNotEqual(self.tile_collection.get_tile('value', value=2),
                            self.tile_collection.get_tile('value', value=4))

    def test_tileTypes(self):
        """
        The different Requests return different Types of Tiles.
        """
        self.assertIsInstance(self.tile_collection.get_tile('empty'),
                              EmptyTile)
        self.assertIsInstance(self.tile_collection.get_tile('blocking'),
                              BlockingTile)
        self.assertIsInstance(self.tile_collection.get_tile('value', value=1),
                              ValueTile)

    def test_invalidTypeRaisesException(self):
        with self.assertRaises(Exception):
            self.tile_collection.get_tile('invalid_tilename')

    def test_fuse(self):
        """
        Tests that fusing two value tiles returns a ValueTile with their sum as
        its value and the resulting score.
        """
        self.assertEqual(
            (self.tile_collection.get_tile('value', value=32), 32),
            self.tile_collection.fuse(
                self.tile_collection.get_tile('value', value=16),
                self.tile_collection.get_tile('value', value=16)))
        # this is technically illegal in the game, but works for engine purpo-
        # ses:
        self.assertEqual(
            (self.tile_collection.get_tile('value', value=12), 12),
            self.tile_collection.fuse(
                self.tile_collection.get_tile('value', value=4),
                self.tile_collection.get_tile('value', value=8)))

        with self.assertRaises(Exception):
            self.tile_collection.fuse(
                self.tile_collection.get_tile('empty'),
                self.tile_collection.get_tile('value', value=8))