Example #1
0
class TestGame(unittest.TestCase):

    def setUp(self):
        names = ['Kelly', 'Leo', 'Ball', 'Hoa']
        self.game = Game(names, 'config_test.json')
        # self.game.current_kyoku = MagicMock()

    def test_init(self):
        self.assertEqual(self.game.players[0].name, 'Kelly')
        self.assertEqual(self.game.players[0].jikaze, Jihai.TON)
        self.assertEqual(self.game.players[1].name, 'Leo')
        self.assertEqual(self.game.players[1].jikaze, Jihai.NAN)
        self.assertEqual(self.game.players[2].name, 'Ball')
        self.assertEqual(self.game.players[2].jikaze, Jihai.SHAA)
        self.assertEqual(self.game.players[3].name, 'Hoa')
        self.assertEqual(self.game.players[3].jikaze, Jihai.PEI)
        self.assertEqual(self.game.bakaze, Jihai.TON)

    def test_start_game(self):
        ...

    def test_tobu(self):
        with patch('mahjong.kyoku.Kyoku.start') as mock_func:
            def f():
                self.game.players[0].points = -2_500
                return False, 0, 0
            mock_func.side_effect = f
            self.game.start_game()
        mock_func.assert_called()

    def test_all_last(self):
        # test if the points are the same
        with patch('mahjong.kyoku.Kyoku.start') as mock_func:
            def f():
                self.game.players[0].points = 30_000
                self.game.players[1].points = 29_000
                self.game.players[2].points = 21_000
                self.game.players[3].points = 20_000
                self.game.bakaze = Jihai.TON
                self.game.kyoku_num = 4
                return True, 0, 0
            mock_func.side_effect = f
            self.game.start_game()
        mock_func.assert_called()

    def test_complete_game(self):
        ...

    def test_ryuukyoku(self):
        ...
Example #2
0
    def __init__(self, config: dict = None):
        """init config

        Args:
            config (dict, optional): DEFAULT_CONFIG. Defaults to None.
        """
        self.config = config
        if not self.config:
            self.config = DEFAULT_CONFIG
        self.name = 'Mahjong'
        self.seed = config['seed']
        self.game = Game()

        self.agents = None
        self.snapshot = None
Example #3
0
from color import *
from mahjong.game import Game
from setting import Setting

pygame.init()  # pygame 初始化,必须有,且必须在开头

clock = pygame.time.Clock()  # 用于控制循环刷新频率的对象
screen = pygame.display.set_mode((Setting.win_w, Setting.win_h))
screen.fill(Color.WHITE)
pygame.display.set_caption(Setting.game_name)
pygame.font.init()
pygame.mixer.init()



game = Game(screen=screen, clock=clock)
game.prepare()
game.play()
game.end()

# eric = PlayerHuman('Eric', is_viewer=True)
# players = [
#     eric,
#     PlayerAi('大乔'),
#     PlayerAi('貂蝉'),
#     PlayerAi('西施'),
# ]
# prevailing_wind = '东'
# hand = HandS(players=players, flower=True, prevailing_wind=prevailing_wind, screen=screen, clock=clock, viewer=eric)
# hand.prepare()
# hand.deal()
Example #4
0
class Env(object):
    """
    Mahjong Environment
    """
    def __init__(self, config: dict = None):
        """init config

        Args:
            config (dict, optional): DEFAULT_CONFIG. Defaults to None.
        """
        self.config = config
        if not self.config:
            self.config = DEFAULT_CONFIG
        self.name = 'Mahjong'
        self.seed = config['seed']
        self.game = Game()

        self.agents = None
        self.snapshot = None

    def set_agents(self, agents):
        self.agents = agents

    def reset(self):
        self.game.init_game(self.config)
        self.snapshot = self.game.new_game()

    def next(self):
        self.snapshot = self.game.next(self.snapshot)

    def save(self):
        self.game.save()

    def decide(self):
        for agent in self.agents:
            agent.decide(
                self.snapshot, self.game.round.trace, self.game.dealer.deck
            )  # self.game.round.trace to get the trace , self.game.dealer.deck to get deck

    def load(self, uuid: str, step: int):
        """
        加载历史对局
        Args:
            uuid (str): 局id
            step (int): 步骤
        """
        if not os.path.exists(f'logs/{uuid}_history.pickle') \
            or not os.path.exists(f'logs/{uuid}_trace.pickle') \
            or not os.path.exists(f'logs/{uuid}_seed.pickle'):
            print("wrong uuid")

        with open(f'logs/{uuid}_seed.pickle', 'rb') as handle:
            self.config = pickle.load(handle)
        self.game.init_game(self.config)
        self.snapshot = self.game.load_game(uuid, step)

    def step_back(self, step: int = 1):
        self.snapshot = self.game.step_back(step)

    def run(self):
        # history = []
        if self.config["show_log"]:
            self.snapshot.print()
        while not self.snapshot.is_finish:

            self.decide()
            # if self.config["show_log"]:
            #     self.snapshot.print_decides()
            self.next()
        # if self.config["show_log"]:
        #     self.snapshot.print()
        if self.config["seed"]:
            self.save()
Example #5
0
from mahjong import Game, Round, Hand
game = Game()
round = Round(game)
hand = Hand(round)
hand.shuffle()
hand.deal()
for p in game.players:
    print('|'.join(map(str, p.hand)))
    print(', '.join(map(str, p.bonus)))
#'''
#''' # uncomment this opening triple quote when commenting out one elsewhere
import sys
from mahjong.game import Game, Hand, UserIO, Question, HandEnding, HandResult

if '--game' in sys.argv:
    game = Game(extra_hands=False)
else:
    game = Hand(None)
print('Note: all indexes are **1-based**.')
print('This is a rudimentary text-based mahjong implementation.')
print("It's purely as a proof-of-concept/manual testing method.")
print("It requires trust that each player won't look at the other's privacy.")
print("Please don't actually use this. Make something even a tiny bit better.")
print("With that said, let's start.")

question = game.play()
while question is not None:
    if isinstance(question, UserIO):
        if question.question == Question.READY_Q:
            input('Hit Enter when ready for the next round.')
            question = question.answer()
Example #6
0
 def setUp(self):
     names = ['Kelly', 'Leo', 'Ball', 'Hoa']
     self.game = Game(names, 'config_test.json')