def test_is_same_suit(self): tiles = Tile.from_masks( [33, 33, 37, 37, 38, 38, 39, 39, 39, 39, 40, 40, 41, 41]) self.assertTrue(is_same_suit(tiles)) tiles = Tile.from_masks( [33, 33, 37, 37, 38, 38, 71, 71, 73, 73, 97, 97, 98, 98]) self.assertFalse(is_same_suit(tiles))
def test_is_7_pairs(self): tiles = Tile.from_masks( [33, 33, 37, 37, 38, 38, 71, 71, 73, 73, 97, 97, 98, 98]) self.assertTrue(is_7_pairs(tiles)) tiles = Tile.from_masks( [33, 33, 34, 34, 35, 35, 36, 36, 38, 39, 65, 65, 65, 65]) self.assertFalse(is_7_pairs(tiles))
def test_get_combinations(self): tiles = Tile.from_masks([33, 34, 35, 38, 38, 38, 71, 71]) self.assertListEqual( list(combinations(tiles)), list(self.mahjong_manager.get_combinations(tiles))) tiles = Tile.from_masks([33, 34, 35, 36, 38, 38, 38]) self.assertListEqual( list(combinations(tiles, 1)), list(self.mahjong_manager.get_combinations(tiles, 1)))
def test_normal(self): mm = self.mahjong_manager # 1w2w3w 4w4w 6t7t8t 9t9t9t 7T7T wall = TileWall.from_mask( [33, 34, 35, 36, 36, 70, 71, 72, 73, 73, 73, 103, 103]) tile = Tile.from_mask(36) self.assertIs(mm.get_pattern_by_tile(wall, tile), Normal) tile2 = Tile.from_mask(103) self.assertIs(mm.get_pattern_by_tile(wall, tile2), Normal)
def test_get_pattern_by_tile(self): manager = self.mahjong_manager manager.register_patterns([Pairs, Normal]) wall = TileWall.from_mask([33, 34, 35, 36, 38, 38, 38]) self.assertEqual(manager.get_pattern_by_tile(wall, Tile.from_mask(36)), Normal) wall = TileWall.from_mask( [33, 33, 34, 34, 35, 35, 38, 38, 39, 39, 40, 40, 41]) self.assertEqual(manager.get_pattern_by_tile(wall, Tile.from_mask(41)), Pairs)
def test_get_candidates_with_joker(self): mm = self.mahjong_manager mm._use_joker_api() mm.jokers = {Tile(1, 5)} tiles = Tile.from_masks([33, 37, 37, 38, 38, 71, 71]) self.assertListEqual(list(mm.get_candidates(tiles)), Tile.from_masks([33, 34, 35, 38, 71])) tiles = Tile.from_masks( [33, 33, 37, 37, 37, 37, 38, 38, 71, 71, 72, 72, 73]) self.assertListEqual(sorted(mm.get_candidates(tiles)), sorted(mm.deck.tile_set))
def test_has_candidates(self): manager = self.mahjong_manager tiles = Tile.from_masks([33, 34, 35, 36, 38, 38, 38]) self.assertTrue(manager.has_candidates(tiles)) tiles = Tile.from_masks([33, 34, 35, 38, 38, 38]) self.assertFalse(manager.has_candidates(tiles)) tiles = Tile.from_masks( [33, 33, 34, 34, 36, 36, 65, 65, 67, 67, 99, 103, 103]) manager.has_pairs_patterns = True self.assertTrue(manager.has_candidates(tiles))
def test_check_win(self): mm = self.mahjong_manager tiles = Tile.from_masks( [33, 33, 37, 37, 38, 38, 39, 39, 70, 71, 71, 72, 72, 73]) self.assertTrue(mm.check_win(tiles)) tiles = Tile.from_masks( [33, 33, 37, 37, 38, 38, 39, 39, 70, 71, 71, 72, 72, 73, 73]) self.assertFalse(mm.check_win(tiles)) tiles = Tile.from_masks( [33, 33, 37, 37, 38, 38, 39, 39, 70, 71, 71, 72, 72]) self.assertFalse(mm.check_win(tiles))
def test_pong_tile(self): mm = self.mahjong_manager wall = self.wall tile = Tile.from_mask(98) self.assertTrue(mm.check_pong_tile(wall, tile)) self.assertEqual(wall.available_tiles.count(tile), 2) self.assertNotIn(tile, wall.used_tiles['pong']) mm.pong(wall, tile) self.assertEqual(wall.available_tiles.count(tile), 0) self.assertIn([tile, tile, tile], wall.used_tiles['pong']) self.assertEqual(wall.available_count, 11) self.assertFalse(mm.check_pong_tile(wall, Tile.from_mask(37)))
def test_wall_actions(self): # 1w2w2w4w5w8w 5t6t6t 1T6T7T7T wall = TileWall.from_mask( [33, 34, 34, 36, 37, 40, 69, 70, 70, 97, 102, 103, 103]) self.assertEqual(wall.used_count, 0) self.assertEqual(wall.available_count, 13) # drawn 2w wall.add_tile(Tile.from_mask(34)) # discard 1T wall.remove_tile(Tile.from_mask(97)) mm = self.mahjong_manager # pong 6t t6 = Tile.from_mask(70) mm.pong(wall, t6) # discard 5t wall.remove_tile(Tile.from_mask(69)) self.assertDictEqual( wall.used_tiles, { 'kong': { 'exposed': [], 'concealed': [], 'additional': [] }, 'chow': [], 'pong': [[t6, t6, t6]], 'flower': [] }) # drawn 8w wall.add_tile(Tile.from_mask(40)) # discard 1w wall.remove_tile(Tile.from_mask(33)) # pong 7T T7 = Tile.from_mask(103) mm.pong(wall, T7) # discard 6T wall.remove_tile(Tile.from_mask(102)) self.assertDictEqual( wall.used_tiles, { 'kong': { 'exposed': [], 'concealed': [], 'additional': [] }, 'chow': [], 'pong': [[t6, t6, t6], [T7, T7, T7]], 'flower': [] }) w3 = Tile.from_mask(35) self.assertTrue(mm.check_win_by_tile(wall.available_tiles, w3))
def test_has_candidates_with_joker(self): mm = self.mahjong_manager mm._use_joker_api() mm.jokers = {Tile(1, 5)} tiles = Tile.from_masks([33, 37, 37, 38, 38, 71, 71]) self.assertTrue(mm.has_candidates(tiles)) tiles = Tile.from_masks( [33, 33, 37, 37, 37, 37, 38, 38, 71, 71, 72, 72, 73]) self.assertTrue(mm.has_candidates(tiles)) tiles = Tile.from_masks( [33, 33, 34, 34, 36, 36, 37, 65, 65, 67, 99, 103, 103]) mm.has_pairs_patterns = True self.assertTrue(mm.has_candidates(tiles))
def test_get_pattern_by_tile_with_joker(self): manager = self.mahjong_manager manager.register_patterns([Pairs, Normal]) def func(deck): return Tile.from_mask(33), {Tile.from_mask(33)} manager.set_joker_factory(func) wall = TileWall.from_mask([33, 34, 35, 36, 38, 38, 38]) self.assertEqual(manager.get_pattern_by_tile(wall, Tile.from_mask(36)), Normal) wall = TileWall.from_mask( [33, 33, 34, 34, 35, 35, 38, 38, 39, 39, 40, 40, 41]) self.assertEqual(manager.get_pattern_by_tile(wall, Tile.from_mask(41)), Pairs)
def test_7_pairs(self): mm = self.mahjong_manager # 1w1w 2w2w 3w3w 6w6w 8w8w 9w9w 9t wall = TileWall.from_mask( [33, 33, 34, 34, 35, 35, 38, 38, 40, 40, 41, 41, 73]) tile = Tile.from_mask(73) self.assertTrue(mm.check_win_by_tile(wall.available_tiles, tile)) self.assertIs(mm.get_pattern_by_tile(wall, tile), Pairs)
def test_reset_with_joker_factory(self): manager = self.mahjong_manager def func(deck): return Tile.from_mask(33), {Tile.from_mask(33)} manager.set_joker_factory(func) manager.reset(self.deck, random) self.assertEqual(manager.jokers, {Tile.from_mask(33)})
def test_wall_add_tile(self): wall = self.wall tile = Tile.from_mask(35) self.assertNotIn(tile, wall.available_tiles) wall.add_tile(tile) self.assertEqual(wall.total_count, 14) self.assertEqual(wall.available_count, 14) self.assertIn(tile, wall.available_tiles) self.assertEqual(wall.suits[1], 5)
def test_wall_remove_tile(self): wall = self.wall tile = Tile.from_mask(37) self.assertIn(tile, wall.available_tiles) wall.remove_tile(tile) self.assertNotIn(tile, wall.available_tiles) self.assertEqual(wall.total_count, 12) self.assertEqual(wall.available_count, 12) self.assertEqual(wall.suits[1], 3) with self.assertRaises(ValueError): wall.remove_tile(tile) tile = Tile.from_mask(33) self.assertIn(tile, wall.available_tiles) wall.remove_tile(tile) self.assertIn(tile, wall.available_tiles) self.assertEqual(wall.total_count, 11) self.assertEqual(wall.available_count, 11) self.assertEqual(wall.suits[1], 2)
def test_exposed_kong(self): mm = self.mahjong_manager wall = self.wall tile = Tile.from_mask(33) self.assertTrue(mm.check_exposed_kong(wall, tile)) self.assertEqual(wall.available_tiles.count(tile), 3) self.assertNotIn(tile, wall.used_tiles['kong']) mm.exposed_kong(wall, tile) self.assertEqual(wall.available_tiles.count(tile), 0) self.assertIn([tile, tile, tile, tile], wall.used_tiles['kong']['exposed']) self.assertEqual(wall.available_count, 10)
def test_concealed_kong(self): mm = self.mahjong_manager wall = self.wall tile = Tile.from_mask(33) self.assertListEqual(mm.get_concealed_kong_tiles(wall), []) wall.add_tile(tile) self.assertTrue(mm.check_concealed_kong(wall, tile)) self.assertListEqual(mm.get_concealed_kong_tiles(wall), [tile]) mm.concealed_kong(wall, tile) self.assertEqual(wall.available_tiles.count(tile), 0) self.assertIn([tile, tile, tile, tile], wall.used_tiles['kong']['concealed']) self.assertEqual(wall.available_count, 10)
def test_is_7_pairs_with_joker(self): tiles = Tile.from_masks( [33, 33, 34, 34, 35, 35, 36, 36, 38, 39, 65, 65, 65, 65]) for joker, count in ((Tile.from_mask(65), 4), (Tile.from_mask(38), 1), (Tile.from_mask(39), 1), (Tile.from_mask(34), 2)): self.assertTrue( is_7_pairs_with_joker( [tile for tile in tiles if tile is not joker], count)) tiles = Tile.from_masks( [33, 33, 34, 34, 35, 35, 36, 36, 37, 38, 39, 65, 65, 65]) self.assertTrue( is_7_pairs_with_joker( [tile for tile in tiles if tile is not Tile.from_mask(65)], 3))
def test_additional_kong(self): mm = self.mahjong_manager wall = self.wall tile = Tile.from_mask(98) mm.pong(wall, tile) self.assertIn([tile, tile, tile], wall.used_tiles['pong']) self.assertListEqual(mm.get_additional_kong_tiles(wall), []) wall.add_tile(tile) self.assertTrue(mm.check_additional_kong(wall, tile)) self.assertListEqual(mm.get_additional_kong_tiles(wall), [tile]) mm.additional_kong(wall, tile) self.assertNotIn([tile, tile, tile], wall.used_tiles['pong']) self.assertIn([tile, tile, tile, tile], wall.used_tiles['kong']['additional']) self.assertEqual(wall.available_count, 11)
def setUp(self): self.deck = Deck(Tile.create_tiles()) self.mahjong_manager = MahjongManager() self.mahjong_manager.reset(self.deck, random) self.wall = TileWall.from_mask([ 33, 33, 33, 37, 38, 39, 66, 68, 98, 98, 99, 100, 101, ])
def test_get_lack_of_joker(self): tiles = Tile.from_masks([33, 34, 35, 36, 38, 38, 38]) self.assertEqual(get_lack_of_joker(tiles), 2) tiles = Tile.from_masks([33, 36, 38, 38, 38]) self.assertEqual(get_lack_of_joker(tiles), 4) tiles = Tile.from_masks([33, 33, 33, 36, 38, 39]) self.assertEqual(get_lack_of_joker(tiles), 3) tiles = Tile.from_masks([33, 33, 33, 138, 138]) self.assertEqual(get_lack_of_joker(tiles), 1) tiles = Tile.from_masks([33, 33, 33]) self.assertEqual(get_lack_of_joker(tiles), 0) tiles = Tile.from_masks([]) self.assertEqual(get_lack_of_joker(tiles), 0)
def test_deck(self): deck = self.deck tile = Tile.from_mask(33) self.assertEqual(len(deck), 108) self.assertEqual(deck[0], tile) self.assertListEqual(deck[:4], [tile] * 4) self.assertIn(tile, deck) self.assertEqual( str(deck), '[CHARACTER,ONE]|[CHARACTER,ONE]|[CHARACTER,ONE]|[CHARACTER,ONE]|' '[CHARACTER,TWO]|[CHARACTER,TWO]|[CHARACTER,TWO]|[CHARACTER,TWO]|' '[CHARACTER,THREE]|[CHARACTER,THREE]|[CHARACTER,THREE]|' '[CHARACTER,THREE]|[CHARACTER,FOUR]|[CHARACTER,FOUR]|' '[CHARACTER,FOUR]|[CHARACTER,FOUR]|[CHARACTER,FIVE]|' '[CHARACTER,FIVE]|[CHARACTER,FIVE]|[CHARACTER,FIVE]' '|[CHARACTER,SIX]|[CHARACTER,SIX]|[CHARACTER,SIX]|' '[CHARACTER,SIX]|[CHARACTER,SEVEN]|[CHARACTER,SEVEN]|' '[CHARACTER,SEVEN]|[CHARACTER,SEVEN]|[CHARACTER,EIGHT]|' '[CHARACTER,EIGHT]|[CHARACTER,EIGHT]|[CHARACTER,EIGHT]|' '[CHARACTER,NINE]|[CHARACTER,NINE]|[CHARACTER,NINE]|' '[CHARACTER,NINE]|[DOT,ONE]|[DOT,ONE]|[DOT,ONE]|[DOT,ONE]|' '[DOT,TWO]|[DOT,TWO]|[DOT,TWO]|[DOT,TWO]|[DOT,THREE]|[DOT,THREE]|' '[DOT,THREE]|[DOT,THREE]|[DOT,FOUR]|[DOT,FOUR]|[DOT,FOUR]|' '[DOT,FOUR]|[DOT,FIVE]|[DOT,FIVE]|[DOT,FIVE]|[DOT,FIVE]|' '[DOT,SIX]|[DOT,SIX]|[DOT,SIX]|[DOT,SIX]|[DOT,SEVEN]|[DOT,SEVEN]|' '[DOT,SEVEN]|[DOT,SEVEN]|[DOT,EIGHT]|[DOT,EIGHT]|[DOT,EIGHT]|' '[DOT,EIGHT]|[DOT,NINE]|[DOT,NINE]|[DOT,NINE]|[DOT,NINE]|' '[BAMBOO,ONE]|[BAMBOO,ONE]|[BAMBOO,ONE]|[BAMBOO,ONE]|[BAMBOO,TWO]|' '[BAMBOO,TWO]|[BAMBOO,TWO]|[BAMBOO,TWO]|[BAMBOO,THREE]|' '[BAMBOO,THREE]|[BAMBOO,THREE]|[BAMBOO,THREE]|[BAMBOO,FOUR]|' '[BAMBOO,FOUR]|[BAMBOO,FOUR]|[BAMBOO,FOUR]|[BAMBOO,FIVE]|' '[BAMBOO,FIVE]|[BAMBOO,FIVE]|[BAMBOO,FIVE]|[BAMBOO,SIX]|' '[BAMBOO,SIX]|[BAMBOO,SIX]|[BAMBOO,SIX]|[BAMBOO,SEVEN]|' '[BAMBOO,SEVEN]|[BAMBOO,SEVEN]|[BAMBOO,SEVEN]|[BAMBOO,EIGHT]|' '[BAMBOO,EIGHT]|[BAMBOO,EIGHT]|[BAMBOO,EIGHT]|[BAMBOO,NINE]|' '[BAMBOO,NINE]|[BAMBOO,NINE]|[BAMBOO,NINE]')
def test_has_free_joker(self): mm = self.mahjong_manager mm._use_joker_api() joker = Tile.from_mask(37) mm.jokers = set([joker]) tiles = Tile.from_masks([33, 34, 35, 38, 38, 71, 71]) self.assertFalse(mm.has_free_joker(tiles)) tiles = Tile.from_masks([33, 34, 35, 37, 71, 71, 71]) self.assertTrue(mm.has_free_joker(tiles)) tiles = Tile.from_masks([33, 34, 37, 37, 71, 71, 71]) self.assertTrue(mm.has_free_joker(tiles)) tiles = Tile.from_masks([33, 34, 37, 38, 71, 71, 71]) self.assertFalse(mm.has_free_joker(tiles)) tiles = Tile.from_masks([37, 37, 71, 71, 71, 97, 98]) self.assertTrue(mm.has_free_joker(tiles)) tiles = Tile.from_masks([37, 37, 37, 71, 71, 97, 103]) self.assertFalse(mm.has_free_joker(tiles))
def test_deck_exclude_rank(self): deck = self.deck deck.exclude_rank([Tile.RANK_ONE]) self.assertEqual(len(deck), 96) self.assertNotIn(Tile.from_mask(33), deck)
def setUp(self): self.deck = Deck(Tile.create_tiles(with_wind=False, with_dragon=False))
def test_deck_exclude_suit(self): deck = self.deck deck.exclude_suit([Tile.SUIT_BAMBOO]) self.assertEqual(len(deck), 72) self.assertNotIn(Tile.from_mask(98), deck)
def test_deck_full(self): deck = Deck(Tile.create_tiles()) self.assertEqual(len(deck), 136) self.assertEqual(deck[-1], Tile.from_mask(176)) # WHITE
def test_check_win_with_joker(self): mm = self.mahjong_manager mm._use_joker_api() joker = Tile.from_mask(37) tiles = Tile.from_masks([33, 34, 37, 37, 38, 38, 71, 71]) mm.jokers = set([joker]) self.assertTrue(mm.check_win(tiles)) joker = Tile.from_mask(141) tiles = Tile.from_masks([36, 38, 39, 40, 41, 68, 141, 141]) mm.jokers = set([joker]) self.assertTrue(mm.check_win(tiles)) joker = Tile.from_mask(99) mm.jokers = set([joker]) tiles = Tile.from_masks([36, 38, 40, 41, 68, 99, 99, 99]) self.assertTrue(mm.check_win(tiles)) tiles = Tile.from_masks([39, 40, 41, 138, 139, 99, 99, 99]) self.assertTrue(mm.check_win(tiles)) tiles = Tile.from_masks([34, 35, 35, 36, 36, 36, 37, 37, 38, 138, 99]) self.assertTrue(mm.check_win(tiles)) tiles = Tile.from_masks([34, 34, 36, 37, 38, 138, 99, 99]) self.assertTrue(mm.check_win(tiles)) joker = Tile.from_mask(67) mm.jokers = set([joker]) tiles = Tile.from_masks( [35, 36, 37, 67, 68, 68, 69, 70, 71, 102, 102, 103, 104, 105]) self.assertTrue(mm.check_win(tiles)) tiles = Tile.from_masks( [33, 33, 37, 37, 38, 38, 39, 39, 70, 71, 71, 72, 72, 73, 73]) self.assertFalse(mm.check_win(tiles)) tiles = Tile.from_masks( [33, 33, 37, 37, 38, 38, 39, 39, 70, 71, 71, 72, 72]) self.assertFalse(mm.check_win(tiles))
def func(deck): return Tile.from_mask(33), {Tile.from_mask(33)}