def test_init_round(self): table = Table() round_number = 4 count_of_honba_sticks = 2 count_of_riichi_sticks = 3 dora_indicator = 126 dealer = 3 scores = [250, 250, 250, 250] table.init_round(round_number, count_of_honba_sticks, count_of_riichi_sticks, dora_indicator, dealer, scores) self.assertEqual(table.round_number, round_number) self.assertEqual(table.count_of_honba_sticks, count_of_honba_sticks) self.assertEqual(table.count_of_riichi_sticks, count_of_riichi_sticks) self.assertEqual(table.dora_indicators[0], dora_indicator) self.assertEqual(table.get_player(dealer).is_dealer, True) self.assertEqual(table.get_player(dealer).scores, 25000) dealer = 2 table.get_main_player().in_tempai = True table.get_main_player().in_riichi = True table.init_round(round_number, count_of_honba_sticks, count_of_riichi_sticks, dora_indicator, dealer, scores) # test that we reinit round properly self.assertEqual(table.get_player(3).is_dealer, False) self.assertEqual(table.get_main_player().in_tempai, False) self.assertEqual(table.get_main_player().in_riichi, False) self.assertEqual(table.get_player(dealer).is_dealer, True)
def test_is_dora(self): table = Table() table.init_round(0, 0, 0, 0, 0, []) table.dora_indicators = [self._string_to_136_tile(sou='1')] self.assertTrue(table.is_dora(self._string_to_136_tile(sou='2'))) table.dora_indicators = [self._string_to_136_tile(sou='9')] self.assertTrue(table.is_dora(self._string_to_136_tile(sou='1'))) table.dora_indicators = [self._string_to_136_tile(pin='9')] self.assertTrue(table.is_dora(self._string_to_136_tile(pin='1'))) table.dora_indicators = [self._string_to_136_tile(man='9')] self.assertTrue(table.is_dora(self._string_to_136_tile(man='1'))) table.dora_indicators = [self._string_to_136_tile(man='5')] self.assertTrue(table.is_dora(self._string_to_136_tile(man='6'))) table.dora_indicators = [self._string_to_136_tile(honors='1')] self.assertTrue(table.is_dora(self._string_to_136_tile(honors='2'))) table.dora_indicators = [self._string_to_136_tile(honors='2')] self.assertTrue(table.is_dora(self._string_to_136_tile(honors='3'))) table.dora_indicators = [self._string_to_136_tile(honors='3')] self.assertTrue(table.is_dora(self._string_to_136_tile(honors='4'))) table.dora_indicators = [self._string_to_136_tile(honors='4')] self.assertTrue(table.is_dora(self._string_to_136_tile(honors='1'))) table.dora_indicators = [self._string_to_136_tile(honors='5')] self.assertTrue(table.is_dora(self._string_to_136_tile(honors='6'))) table.dora_indicators = [self._string_to_136_tile(honors='6')] self.assertTrue(table.is_dora(self._string_to_136_tile(honors='7'))) table.dora_indicators = [self._string_to_136_tile(honors='7')] self.assertTrue(table.is_dora(self._string_to_136_tile(honors='5'))) table.dora_indicators = [self._string_to_136_tile(pin='1')] self.assertFalse(table.is_dora(self._string_to_136_tile(sou='2'))) settings.FIVE_REDS = True # red five man self.assertTrue(table.is_dora(FIVE_RED_MAN)) # red five pin self.assertTrue(table.is_dora(FIVE_RED_PIN)) # red five sou self.assertTrue(table.is_dora(FIVE_RED_SOU)) settings.FIVE_REDS = False
def test_set_scores(self): table = Table() table.init_round(0, 0, 0, 0, 0, []) scores = [230, 110, 55, 405] table.set_players_scores(scores) self.assertEqual(table.get_player(0).scores, 23000) self.assertEqual(table.get_player(1).scores, 11000) self.assertEqual(table.get_player(2).scores, 5500) self.assertEqual(table.get_player(3).scores, 40500)
def test_set_scores_and_uma(self): table = Table() table.init_round(0, 0, 0, 0, 0, []) scores = [230, 110, 55, 405] uma = [-17, 3, 48, -34] table.set_players_scores(scores, uma) self.assertEqual(table.get_player(0).scores, 23000) self.assertEqual(table.get_player(0).uma, -17) self.assertEqual(table.get_player(1).scores, 11000) self.assertEqual(table.get_player(1).uma, 3) self.assertEqual(table.get_player(2).scores, 5500) self.assertEqual(table.get_player(2).uma, 48) self.assertEqual(table.get_player(3).scores, 40500) self.assertEqual(table.get_player(3).uma, -34)
def test_set_names_and_ranks(self): table = Table() table.init_round(0, 0, 0, 0, 0, []) values = [ {'name': 'NoName', 'rank': u'新人'}, {'name': 'o2o2', 'rank': u'3級'}, {'name': 'shimmmmm', 'rank': u'三段'}, {'name': u'川海老', 'rank': u'9級'} ] table.set_players_names_and_ranks(values) self.assertEqual(table.get_player(0).name, 'NoName') self.assertEqual(table.get_player(0).rank, u'新人') self.assertEqual(table.get_player(3).name, u'川海老') self.assertEqual(table.get_player(3).rank, u'9級')
def test_round_wind(self): table = Table() table.init_round(0, 0, 0, 0, 0, []) self.assertEqual(table.round_wind, EAST) table.init_round(3, 0, 0, 0, 0, []) self.assertEqual(table.round_wind, EAST) table.init_round(7, 0, 0, 0, 0, []) self.assertEqual(table.round_wind, SOUTH) table.init_round(11, 0, 0, 0, 0, []) self.assertEqual(table.round_wind, WEST) table.init_round(12, 0, 0, 0, 0, []) self.assertEqual(table.round_wind, NORTH)
def test_set_scores_and_recalculate_player_position(self): table = Table() table.init_round(0, 0, 0, 0, 0, []) scores = [230, 110, 55, 405] table.set_players_scores(scores) self.assertEqual(table.get_player(0).position, 2) self.assertEqual(table.get_player(1).position, 3) self.assertEqual(table.get_player(2).position, 4) self.assertEqual(table.get_player(3).position, 1) scores = [110, 110, 405, 405] table.set_players_scores(scores) self.assertEqual(table.get_player(0).position, 3) self.assertEqual(table.get_player(1).position, 4) self.assertEqual(table.get_player(2).position, 1) self.assertEqual(table.get_player(3).position, 2)
def test_find_dealer_tile_to_discard(self): dealer = 2 dora = self._string_to_136_tile(honors='3') table = Table() table.init_round(0, 0, 0, dora, dealer, []) tiles = self._string_to_136_array(sou='2234678', pin='34', man='45789') table.player.init_hand(tiles) table.add_discarded_tile(1, self._string_to_136_tile(man='4'), False) table.add_discarded_tile(1, self._string_to_136_tile(man='5'), False) table.add_discarded_tile(2, self._string_to_136_tile(man='8'), False) table.add_discarded_tile(2, self._string_to_136_tile(man='9'), False) table.add_called_riichi(1) table.add_called_riichi(2) # for this test we don't need temporary_safe_tiles table.get_player(1).temporary_safe_tiles = [] table.get_player(2).temporary_safe_tiles = [] result = table.player.discard_tile() # second player is a dealer, let's fold against him self.assertEqual(self._to_string([result]), '9m') tiles = self._string_to_136_array(sou='234567', pin='348', man='234', honors='23') table.player.init_hand(tiles) result = table.player.discard_tile() # there is no safe tiles against dealer, so let's fold against other players self.assertEqual(table.player.ai.in_defence, True) self.assertEqual(self._to_string([result]), '4m')
def reproduce(self, dry_run=False): draw_tags = ['T', 'U', 'V', 'W'] discard_tags = ['D', 'E', 'F', 'G'] player_draw = draw_tags[self.player_position] player_draw_regex = re.compile('^<[{}]+\d*'.format( ''.join(player_draw))) discard_regex = re.compile('^<[{}]+\d*'.format(''.join(discard_tags))) table = Table() for tag in self.round_content: if dry_run: print(tag) if not dry_run and tag == self.stop_tag: break if 'INIT' in tag: values = self.decoder.parse_initial_values(tag) shifted_scores = [] for x in range(0, 4): shifted_scores.append( values['scores'][self._normalize_position( x, self.player_position)]) table.init_round( values['round_number'], values['count_of_honba_sticks'], values['count_of_riichi_sticks'], values['dora_indicator'], self._normalize_position(self.player_position, values['dealer']), shifted_scores, ) hands = [ [ int(x) for x in self.decoder.get_attribute_content( tag, 'hai0').split(',') ], [ int(x) for x in self.decoder.get_attribute_content( tag, 'hai1').split(',') ], [ int(x) for x in self.decoder.get_attribute_content( tag, 'hai2').split(',') ], [ int(x) for x in self.decoder.get_attribute_content( tag, 'hai3').split(',') ], ] table.player.init_hand(hands[self.player_position]) if player_draw_regex.match(tag) and 'UN' not in tag: tile = self.decoder.parse_tile(tag) table.player.draw_tile(tile) if discard_regex.match(tag) and 'DORA' not in tag: tile = self.decoder.parse_tile(tag) player_sign = tag.upper()[1] player_seat = self._normalize_position( self.player_position, discard_tags.index(player_sign)) if player_seat == 0: table.player.discard_tile( DiscardOption(table.player, tile // 4, 0, [], 0)) else: table.add_discarded_tile(player_seat, tile, False) if '<N who=' in tag: meld = self.decoder.parse_meld(tag) player_seat = self._normalize_position(self.player_position, meld.who) table.add_called_meld(player_seat, meld) if player_seat == 0: # we had to delete called tile from hand # to have correct tiles count in the hand if meld.type != Meld.KAN and meld.type != Meld.CHANKAN: table.player.draw_tile(meld.called_tile) if '<REACH' in tag and 'step="1"' in tag: who_called_riichi = self._normalize_position( self.player_position, self.decoder.parse_who_called_riichi(tag)) table.add_called_riichi(who_called_riichi) if not dry_run: tile = self.decoder.parse_tile(self.stop_tag) print('Hand: {}'.format(table.player.format_hand_for_print(tile))) # to rebuild all caches table.player.draw_tile(tile) tile = table.player.discard_tile() # real run, you can stop debugger here table.player.draw_tile(tile) tile = table.player.discard_tile() print('Discard: {}'.format( TilesConverter.to_one_line_string([tile])))
def reproduce(self, dry_run=False): draw_tags = ['T', 'U', 'V', 'W'] discard_tags = ['D', 'E', 'F', 'G'] player_draw = draw_tags[self.player_position] player_draw_regex = re.compile('^<[{}]+\d*'.format( ''.join(player_draw))) draw_regex = re.compile('^<[{}]+\d*'.format(''.join(draw_tags))) discard_regex = re.compile('^<[{}]+\d*'.format(''.join(discard_tags))) table = Table() previous_tag = "" score = 1 is_valid_sample = False for n, tag in enumerate(self.round_content): if dry_run: print(tag) if not dry_run and tag == self.stop_tag: break if 'INIT' in tag: values = self.decoder.parse_initial_values(tag) shifted_scores = [] for x in range(0, 4): shifted_scores.append( values['scores'][self._normalize_position( x, self.player_position)]) table.init_round( values['round_number'], values['count_of_honba_sticks'], values['count_of_riichi_sticks'], values['dora_indicator'], self._normalize_position(self.player_position, values['dealer']), shifted_scores, ) hands = [ [ int(x) for x in self.decoder.get_attribute_content( tag, 'hai0').split(',') ], [ int(x) for x in self.decoder.get_attribute_content( tag, 'hai1').split(',') ], [ int(x) for x in self.decoder.get_attribute_content( tag, 'hai2').split(',') ], [ int(x) for x in self.decoder.get_attribute_content( tag, 'hai3').split(',') ], ] # DEL: we can't only initialize the main player, we must initialize # other players as well. #table.player.init_hand(hands[self.player_position]) # ADD: initialize all players on the table table.players[0].init_hand(hands[self.player_position]) table.players[1].init_hand(hands[(self.player_position + 1) % 4]) table.players[2].init_hand(hands[(self.player_position + 2) % 4]) table.players[3].init_hand(hands[(self.player_position + 3) % 4]) # ADD: when restart a new game, we need to reinitialize the config self.extract_features.__init__() # We must deal with ALL players. #if player_draw_regex.match(tag) and 'UN' not in tag: if draw_regex.match(tag) and 'UN' not in tag: tile = self.decoder.parse_tile(tag) # CHG: we must deal with ALL players #table.player.draw_tile(tile) if "T" in tag: table.players[0].draw_tile(tile) elif "U" in tag: table.players[1].draw_tile(tile) elif "V" in tag: table.players[2].draw_tile(tile) elif "W" in tag: table.players[3].draw_tile(tile) #print("After draw `W`:", table.players[3].tiles) if discard_regex.match(tag) and 'DORA' not in tag: tile = self.decoder.parse_tile(tag) player_sign = tag.upper()[1] # TODO: I don't know why the author wrote the code as below, the # player_seat won't work if we use self._normalize_position. This # might be a tricky part, and we need to review it later. #player_seat = self._normalize_position(self.player_position, discard_tags.index(player_sign)) # Temporally solution to modify the player_seat player_seat = (discard_tags.index(player_sign) + self.player_position) % 4 #print("updated player seat:",player_seat) if player_seat == 0: table.players[player_seat].discard_tile( DiscardOption(table.players[player_seat], tile // 4, 0, [], 0)) else: # ADD: we must take care of ALL players tile_to_discard = tile is_tsumogiri = tile_to_discard == table.players[ player_seat].last_draw # it is important to use table method, # to recalculate revealed tiles and etc. table.add_discarded_tile(player_seat, tile_to_discard, is_tsumogiri) #print("seat:",player_seat) #print("tiles:", TilesConverter.to_one_line_string(table.players[player_seat].tiles), " discard?:", TilesConverter.to_one_line_string([tile_to_discard])) table.players[player_seat].tiles.remove(tile_to_discard) # DEL #table.add_discarded_tile(player_seat, tile, False) if '<N who=' in tag: meld = self.decoder.parse_meld(tag) #player_seat = self._normalize_position(self.player_position, meld.who) # Again, we change the player_seat here player_seat = (meld.who + self.player_position) % 4 table.add_called_meld(player_seat, meld) #if player_seat == 0: # CHG: we need to handle ALL players here if True: # we had to delete called tile from hand # to have correct tiles count in the hand if meld.type != Meld.KAN and meld.type != Meld.CHANKAN: table.players[player_seat].draw_tile(meld.called_tile) if '<REACH' in tag and 'step="1"' in tag: who_called_riichi = self._normalize_position( self.player_position, self.decoder.parse_who_called_riichi(tag)) table.add_called_riichi(who_called_riichi) # This part is to extract the features that will be used to train # our model. try: next_tag = self.round_content[n + 1] except IndexError: next_tag = "" if '<AGARI' in next_tag: who_regex = re.compile("who=\"\d+\"") fromWho_regex = re.compile("fromWho=\"\d+\"") sc_regex = "sc=\"[+-]?\d+,[+-]?\d+,[+-]?\d+,[+-]?\d+,[+-]?\d+,[+-]?\d+,[+-]?\d+,[+-]?\d+\"" score_regex = re.compile(sc_regex) who = int( who_regex.search(next_tag).group(0).replace( '"', '').split("=")[1]) fromWho = int( fromWho_regex.search(next_tag).group(0).replace( '"', '').split("=")[1]) scores = [ float(s) for s in score_regex.search(next_tag).group(0).replace( '"', '').split("=")[1].split(",") ] score = scores[fromWho * 2 + 1] player_seat, features = self.execute_extraction(tag, table) # # tsumo is not a valid sample for our training. # if (who!=fromWho): # not tsumo (lose the score to winner, score<0) # if (features is not None) and (player_seat is not None) and (score<0): # # The first element before ";" is table_info, therefore player_info starts # # from index 1, and we put who+1 here. # self.feature_to_logger(features, who+1, score) # score = 1 # tsumo is a valid sample for our training if (who == fromWho): # tsumo (win the score, score>0) if (features is not None) and (player_seat is not None) and (score > 0): self.feature_to_logger(features, who + 1, score) score = -1 else: player_seat, features = self.execute_extraction(tag, table) if not dry_run: tile = self.decoder.parse_tile(self.stop_tag) print('Hand: {}'.format(table.player.format_hand_for_print(tile))) # to rebuild all caches table.player.draw_tile(tile) tile = table.player.discard_tile() # real run, you can stop debugger here table.player.draw_tile(tile) tile = table.player.discard_tile() print('Discard: {}'.format( TilesConverter.to_one_line_string([tile])))
def reproduce(self, dry_run=False): draw_tags = ['T', 'U', 'V', 'W'] discard_tags = ['D', 'E', 'F', 'G'] player_draw = draw_tags[self.player_position] player_draw_regex = re.compile('^<[{}]+\d*'.format(''.join(player_draw))) draw_regex = re.compile('^<[{}]+\d*'.format(''.join(draw_tags))) discard_regex = re.compile('^<[{}]+\d*'.format(''.join(discard_tags))) table = Table() # previous_tag = None for tag in self.round_content: if dry_run: print(tag) if not dry_run and tag == self.stop_tag: break if 'INIT' in tag: values = self.decoder.parse_initial_values(tag) shifted_scores = [] for x in range(0, 4): shifted_scores.append(values['scores'][self._normalize_position(x, self.player_position)]) table.init_round( values['round_number'], values['count_of_honba_sticks'], values['count_of_riichi_sticks'], values['dora_indicator'], self._normalize_position(self.player_position, values['dealer']), shifted_scores, ) hands = [ [int(x) for x in self.decoder.get_attribute_content(tag, 'hai0').split(',')], [int(x) for x in self.decoder.get_attribute_content(tag, 'hai1').split(',')], [int(x) for x in self.decoder.get_attribute_content(tag, 'hai2').split(',')], [int(x) for x in self.decoder.get_attribute_content(tag, 'hai3').split(',')], ] # DEL: we can't only initialize the main player, we must initialize # other players as well. #table.player.init_hand(hands[self.player_position]) # ADD: initialize all players on the table table.players[0].init_hand(hands[self.player_position]) table.players[1].init_hand(hands[(self.player_position+1)%4]) table.players[2].init_hand(hands[(self.player_position+2)%4]) table.players[3].init_hand(hands[(self.player_position+3)%4]) # ADD: when restart a new game, we need to reinitialize the config self.extract_features.__init__() # We must deal with ALL players. #if player_draw_regex.match(tag) and 'UN' not in tag: if draw_regex.match(tag) and 'UN' not in tag: tile = self.decoder.parse_tile(tag) # CHG: we must deal with ALL players #table.player.draw_tile(tile) if "T" in tag: table.players[0].draw_tile(tile) elif "U" in tag: table.players[1].draw_tile(tile) elif "V" in tag: table.players[2].draw_tile(tile) elif "W" in tag: table.players[3].draw_tile(tile) #print("After draw `W`:", table.players[3].tiles) if discard_regex.match(tag) and 'DORA' not in tag: tile = self.decoder.parse_tile(tag) player_sign = tag.upper()[1] # TODO: I don't know why the author wrote the code as below, the # player_seat won't work if we use self._normalize_position. This # might be a tricky part, and we need to review it later. #player_seat = self._normalize_position(self.player_position, discard_tags.index(player_sign)) # Temporally solution to modify the player_seat player_seat = (discard_tags.index(player_sign) + self.player_position)%4 #print("updated player seat:",player_seat) if player_seat == 0: table.players[player_seat].discard_tile(DiscardOption(table.players[player_seat], tile // 4, 0, [], 0)) else: # ADD: we must take care of ALL players tile_to_discard = tile is_tsumogiri = tile_to_discard == table.players[player_seat].last_draw # it is important to use table method, # to recalculate revealed tiles and etc. table.add_discarded_tile(player_seat, tile_to_discard, is_tsumogiri) #print("seat:",player_seat) #print("tiles:", TilesConverter.to_one_line_string(table.players[player_seat].tiles), " discard?:", TilesConverter.to_one_line_string([tile_to_discard])) table.players[player_seat].tiles.remove(tile_to_discard) # DEL #table.add_discarded_tile(player_seat, tile, False) if '<N who=' in tag: meld = self.decoder.parse_meld(tag) #player_seat = self._normalize_position(self.player_position, meld.who) # Again, we change the player_seat here player_seat = (meld.who + self.player_position) % 4 table.add_called_meld(player_seat, meld) #if player_seat == 0: # CHG: we need to handle ALL players here if True: # we had to delete called tile from hand # to have correct tiles count in the hand if meld.type != Meld.KAN and meld.type != Meld.CHANKAN: table.players[player_seat].draw_tile(meld.called_tile) if '<REACH' in tag and 'step="1"' in tag: who_called_riichi = self._normalize_position(self.player_position, self.decoder.parse_who_called_riichi(tag)) table.add_called_riichi(who_called_riichi) # This part is to extract the features that will be used to train # our model. # if '<AGARI' in tag: # print(previous_tag) # print(tag) # print("~~~~~~~~~\n") if '<D' in tag: #features = self.extract_features.get_is_waiting_features(table) features = self.extract_features.get_waiting_tiles_features(table) if features is not None: features_list = features.split(";") assert len(features_list)==6, "<D> Features format incorrect!" table_info = features_list[0] player_info = features_list[1] logger2.info(table_info + ";" + player_info) if '<E' in tag: #features = self.extract_features.get_is_waiting_features(table) features = self.extract_features.get_waiting_tiles_features(table) if features is not None: features_list = features.split(";") assert len(features_list)==6, "<E> Features format incorrect!" table_info = features_list[0] player_info = features_list[2] logger2.info(table_info + ";" + player_info) if '<F' in tag: #features = self.extract_features.get_is_waiting_features(table) features = self.extract_features.get_waiting_tiles_features(table) if features is not None: features_list = features.split(";") assert len(features_list)==6, "<F> Features format incorrect!" table_info = features_list[0] player_info = features_list[3] logger2.info(table_info + ";" + player_info) if '<G' in tag: #features = self.extract_features.get_is_waiting_features(table) features = self.extract_features.get_waiting_tiles_features(table) if features is not None: features_list = features.split(";") assert len(features_list)==6, "<G> Features format incorrect!" table_info = features_list[0] player_info = features_list[4] logger2.info(table_info + ";" + player_info) # previous_tag = tag if not dry_run: tile = self.decoder.parse_tile(self.stop_tag) print('Hand: {}'.format(table.player.format_hand_for_print(tile))) # to rebuild all caches table.player.draw_tile(tile) tile = table.player.discard_tile() # real run, you can stop debugger here table.player.draw_tile(tile) tile = table.player.discard_tile() print('Discard: {}'.format(TilesConverter.to_one_line_string([tile])))