def test_tile_combinations_work(self): # Given different combos of tiles, they combine properly. self.tile1 = Tile(0) self.tile2 = Tile(0) # Two empty tiles can combine. self.assertTrue(can_combine(self.tile1, self.tile2)) self.tile1 = Tile(1) self.tile2 = Tile(2) # 1s and 2s can combine. self.assertTrue(can_combine(self.tile1, self.tile2)) self.tile1 = Tile(2) self.tile2 = Tile(1) # 1s and 2s can combine, regardless of order. self.assertTrue(can_combine(self.tile1, self.tile2)) self.tile1 = Tile(3) self.tile2 = Tile(3) # Twins can combine. self.assertTrue(can_combine(self.tile1, self.tile2)) self.tile1 = Tile(1) self.tile2 = Tile(3) # 1 and 3 cannot combine. self.assertFalse(can_combine(self.tile1, self.tile2))
def test_create_tile_with_default_value(self): # I create a tile object with default value of 0 self.test_tile = Tile() self.assertEqual(self.test_tile.value, 0)
def test_create_tile_with_nondefault_value(self): # I create a tile object with value 3 instead of the default 0 self.test_tile = Tile(3) self.assertEqual(self.test_tile.value, 3)
def test_swipe_down_adds_new_tile_in_correct_spot(self): # With the board initialized, I swipe on the board, and a new tile # is initilalized properly at the opposite edge in the only open slot # Start with all zeroes. self.board.tiles = [[Tile(0), Tile(1), Tile(1), Tile(1)], [Tile(2), Tile(1), Tile(1), Tile(1)], [Tile(1), Tile(1), Tile(1), Tile(1)], [Tile(1), Tile(1), Tile(1), Tile(1)]] self.board.swipe("down") non_zero = [] for tile in self.board.tiles[0]: if tile.value != 0: non_zero.append(tile.value) # Check if a new tile has been generated. self.assertTrue(len(non_zero) == 4)
def test_game_score_is_accurate(self): self.board.tiles = [[Tile(2), Tile(6), Tile(12), Tile(2)], [Tile(3), Tile(48), Tile(24), Tile(6)], [Tile(48), Tile(24), Tile(384), Tile(768)], [Tile(1), Tile(3), Tile(1), Tile(24)]] self.assertEqual(self.board.read_board()["points"], 27024) self.board.tiles = [[Tile(3), Tile(2), Tile(3), Tile(12)], [Tile(1536), Tile(12), Tile(48), Tile(24)], [Tile(12), Tile(6), Tile(96), Tile(48)], [Tile(3), Tile(24), Tile(1), Tile(3)]] self.assertEqual(self.board.read_board()["points"], 60528)
def test_swipe_down_with_illegal_move_doesnt_add_new_tile(self): # Swiping down in an illegal move, a new tile # is NOT added at the opposite edge # Start with a full column self.board.tiles = [[Tile(0), Tile(0), Tile(0), Tile(99)], [Tile(0), Tile(0), Tile(0), Tile(98)], [Tile(0), Tile(0), Tile(0), Tile(97)], [Tile(0), Tile(0), Tile(0), Tile(96)]] self.board.swipe("down") non_zero = [] for tile in self.board.tiles[0]: if tile.value != 0: non_zero.append(tile.value) # Check if a new tile has been generated. self.assertTrue(len(non_zero) == 1 and non_zero[0] == 99)
def test_board_next_tiles_varies_by_max_tile(self): self.board.board_init() self.board.tiles = [[Tile(3), Tile(2), Tile(3), Tile(12)], [Tile(768), Tile(768), Tile(12), Tile(48)], [Tile(12), Tile(6), Tile(96), Tile(48)], [Tile(3), Tile(24), Tile(1), Tile(3)]] self.board.set_max_tile_value() self.board.possible_new_tiles = get_possible_tiles( self.board.max_tile_value) self.assertEqual(self.board.possible_new_tiles, [1, 2, 3, [96, 48, 24]]) self.board.swipe("left") self.assertEqual(self.board.possible_new_tiles, [1, 2, 3, [192, 96, 48]])
def test_swipe_right_initialized_board(self): self.board.board_init(test=True) self.expectedboard = Board() self.expectedboard.tiles = [[Tile(0), Tile(0), Tile(0), Tile(3)], [Tile(0), Tile(1), Tile(2), Tile(3)], [Tile(0), Tile(3), Tile(2), Tile(1)], [Tile(0), Tile(2), Tile(1), Tile(0)]] self.board.swipe("right", add_new_tile=False) self.assertTrue(boards_match(self.board, self.expectedboard))
def test_swipe_right_one_tile_on_edge(self): # With the board initialized, I swipe up on the board, and the tiles # all move correctly. # Start with just one 1 tile on the top wall, see if it moves properly. self.board.tiles = [[Tile(0), Tile(0), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(1)], [Tile(0), Tile(0), Tile(0), Tile(0)]] self.expectedboard = Board() self.expectedboard.tiles = [[Tile(0), Tile(0), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(1)], [Tile(0), Tile(0), Tile(0), Tile(0)]] self.board.swipe("right", add_new_tile=False) # Check if the 1 tile moved up properly. self.assertTrue(boards_match(self.board, self.expectedboard))
def test_swipe_up_one_tile_on_edge(self): # With the board initialized, I swipe up on the board, and the tiles # all move correctly. # Start with just one 1 tile on the top wall, see if it moves properly. self.board.tiles = [[Tile(0), Tile(1), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(0)]] self.expectedboard = Board() self.expectedboard.tiles = [[Tile(0), Tile(1), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(0)], [Tile(0), Tile(0), Tile(0), Tile(0)]] self.board.swipe("up", add_new_tile=False) # Check if the 1 tile moved up properly. # for index, board_row in enumerate(self.board.tiles): # self.assertEqual([tile.value for tile in board_row], [ # tile.value for tile in self.expectedboard.tiles[index]]) self.assertTrue(boards_match(self.board, self.expectedboard))