def test_hand(): mj_set = MjSet() mj_set.shuffle() # ai = PlayerAiPro("bot02") ai = PlayerAiAdv("bot02") for _ in range(13): ai.draw(mj_set) ai.sort_concealed() print("concealed: " + Rule.convert_tiles_to_str(ai.concealed)) mj_set.shuffle() for _ in range(100): tile = ai.draw(mj_set) print(f"draw: {tile}") if Rule.is_mahjong(ai.concealed): print("Mahjong!!!") break tile = ai.decide_discard() print(f"discard: {tile}") ai.discard(tile) ai.sort_concealed() print("concealed: " + Rule.convert_tiles_to_str(ai.concealed)) print("discarded: " + Rule.convert_tiles_to_str(ai.discarded)) print(f"strategies: {ai.strategies}") print(f"strategies_time: {ai.strategies_time}") print("draw count: " + str(ai.draw_count))
def test_decide_discard_by_left_meld_and_eye(): mj_set = MjSet() ai = PlayerAiAdv("bot02") for _ in range(3): ai.draw(mj_set) mj_set.shuffle() for _ in range(11): ai.draw(mj_set) ai.sort_concealed() print("ai.concealed_str =", ai.concealed_str) result = ai.decide_discard_by_left_meld_and_eye() for x in result: print(x, Rule.convert_tiles_to_str(Rule.convert_arr_to_tiles(result[x])))
def play(self): current = self._dealer # the current player player = current before = None # the previous player current_discard = None # discard tile by previous player for player in self._players: print(f'{player.position}: {player}') print("=" * 40) have_winner = False while not have_winner and not self.out_of_tiles: if current_discard: wind = current.position player = current for index in range(3): # test for hu by other if player.try_mahjong(current_discard): player.concealed.append(current_discard) print(f"winner is {player}, by {before}") self._winner = player self.firer = before self._state_machine.mahjong() have_winner = True self.refresh_screen(state='mahjong') break wind = Suit.get_next_wind(wind) player = self.positions[wind] if have_winner: break # interrupted by exposed kong / pong / chow interrupted = False if current_discard: # Melding another for current, +1, +2 players player = None # try kong ( must have tiles ): if self.mj_set.tiles: wind = current.position player = current for index in range(3): try: if player.try_exposed_kong(tile=current_discard, owner=before, mj_set=self._mj_set): self.refresh_screen('drawing') # self._sound_kong.play() interrupted = True break except OutOfTilesError as e: self.withdraw() except HaveWinnerError as e: self._winner = player self.mahjong_on_kong = True self.firer = player self._state_machine.mahjong() have_winner = True self.refresh_screen(state='mahjong') break wind = Suit.get_next_wind(wind) player = self.positions[wind] if self._winner: break # try pong: if not interrupted: wind = current.position player = current for index in range(3): if player.try_exposed_pong(tile=current_discard, owner=before): self.refresh_screen() self._sound_pong.play() interrupted = True break wind = Suit.get_next_wind(wind) player = self.positions[wind] # try chow: if not interrupted: # wind = current.position player = current if player.try_exposed_chow(current_discard, before): self.refresh_screen() self._sound_chow.play() interrupted = True if not interrupted: before.put_on_desk(current_discard) before.discarding = None # end if current_discard: if interrupted: current = player else: # test for out of tiles if not self.mj_set.tiles: self.withdraw() break # draw new_tile = current.draw(self._mj_set) if not new_tile: self.withdraw() break self.refresh_screen(state='drawing') # test for hu by self if current.try_mahjong(tile=None): print(f"winner is {current}, by self!") self._winner = current self.firer = current self._state_machine.mahjong() self.refresh_screen(state='mahjong') break # self kong try: if current.try_conceal_kong(self.mj_set): pass except OutOfTilesError as e: self.withdraw() except HaveWinnerError as e: self._winner = current self.mahjong_on_kong = True self.firer = current self._state_machine.mahjong() have_winner = True self.refresh_screen(state='mahjong') break if self.winner: break # test for exposed kong from exposed pong result_of_try = False try: result_of_try = current.try_exposed_kong_from_exposed_pong( mj_set=self.mj_set) except OutOfTilesError as e: self.withdraw() except HaveWinnerError as e: self._winner = current self.mahjong_on_kong = True self.firer = current self._state_machine.mahjong() have_winner = True self.refresh_screen(state='mahjong') break if self._winner: break if result_of_try: # try rob kong by others self.refresh_screen() # others try mahjong player = None wind = current.position for index in range(3): wind = Suit.get_next_wind(wind) player = self.positions[wind] if player.try_mahjong(tile=new_tile): print(f"winner is {player}, by rob {current}!") player.concealed.append(new_tile) self._winner = player self.firer = current self._state_machine.mahjong() self.refresh_screen(state='mahjong') self.robbing_a_kong = True break if self._winner: break if isinstance(current, PlayerAiAdv): wall = self._mj_set.tiles tile = current.decide_discard(players_count=len(self.players), wall=wall) else: tile = current.decide_discard() current.discard(tile) print(current, 'discard tile:', tile) current_discard = tile current.sort_concealed() # (f'{current} discard {tile}') # (Rule.convert_tiles_to_str(current.concealed)) # next player next = self._get_next_player(current) before = current current = next self.refresh_screen() self.sound_discard() # end while self.refresh_screen(state='scoring') print("tiles left :", len(self.mj_set.tiles)) for player in self.players: if player == self.winner: print(f"winner {player}: ", Rule.convert_tiles_to_str(player.concealed)) else: print(f"{player}: ", Rule.convert_tiles_to_str(player.concealed)) left = Rule.convert_tiles_to_arr(self.mj_set.tiles) left.sort()
def play(self): current = self._dealer # the current player player = current before = None # the previous player current_discard = None # discard tile by previous player for player in self._players: print(f'{player.position}: {player}') print("=" * 40) have_winner = False while not have_winner: if current_discard: wind = current.position player = current for index in range(3): # test for hu by other test = player.concealed[:] test.append(current_discard) if Rule.is_mahjong(test): player.concealed.append(current_discard) print(f"winner is {player}, by {before}") self._winner = player self._state_machine.mahjong() have_winner = True break wind = Suit.get_next_wind(wind) player = self.positions[wind] if have_winner: break # interrupted by exposed kong / pong / chow interrupted = False if current_discard: # Melding another for current, +1, +2 players player = None # try kong ( must have tiles ): if self.mj_set.tiles: wind = current.position player = current for index in range(3): if player.try_exposed_kong(tile=current_discard, owner=before, mj_set=self._mj_set): interrupted = True break wind = Suit.get_next_wind(wind) player = self.positions[wind] # try pong: if not interrupted: wind = current.position player = current for index in range(3): if player.try_exposed_pong(tile=current_discard, owner=before): interrupted = True break wind = Suit.get_next_wind(wind) player = self.positions[wind] # try chow: if not interrupted: wind = current.position player = current if player.try_exposed_chow(current_discard, before): interrupted = True if not interrupted: before.put_on_desk(current_discard) # end if current_discard: if interrupted: current = player else: # test for out of tiles if not self.mj_set.tiles: print("out of tiles!") self._winner = None self._state_machine.withdraw() break # draw current.draw(self._mj_set) # test for hu by self if Rule.is_mahjong(current.concealed): print(f"winner is {current}, by self!") self._winner = current self._state_machine.mahjong() break # self kong current.try_conceal_kong(self.mj_set) if isinstance(current, PlayerAiAdv): wall = self._mj_set.tiles tile = current.decide_discard(players_count=len(self.players), wall=wall) else: tile = current.decide_discard() print(current, 'discard:', tile) current.discard(tile) current.sort_concealed() current_discard = tile # (f'{current} discard {tile}') # (Rule.convert_tiles_to_str(current.concealed)) # next player next = self._get_next_player(current) before = current current = next # end while print("tiles left :", len(self.mj_set.tiles)) for player in self.players: if player == self.winner: print(f"winner {player}: ", Rule.convert_tiles_to_str(player.concealed)) else: print(f"{player}: ", Rule.convert_tiles_to_str(player.concealed)) left = Rule.convert_tiles_to_arr(self.mj_set.tiles) left.sort() print(self.players[0].strategies) print(self.players[0].strategies_time)