Пример #1
0
 def test_rule_game(self):
     controller1 = GameController([RulePlayer() for _ in range(4)], CardDeck(0))
     controller1.play()
     self.assertEqual(controller1.game.current_action, GameAction.GAME_OVER)
     for r in controller1.game.rounds:
         is_game_json_valid(game_to_json(r))
     controller2 = GameController([RulePlayer() for _ in range(4)], CardDeck(0))
     controller2.play()
     self.__check_two_games_equal(controller1.game, controller2.game)
Пример #2
0
 def test_valid_moves_new_game(self):
     cards = CardDeck().deal_cards()
     players = [PlayerCards(i, i, cards[i]) for i in range(4)]
     game = Round(players, cards[4], [], 0, Suite(0))
     p_round = PartialRound.from_round(game, 0)
     valid_cards = vm.valid_trick_moves(p_round)
     self.assertEqual(len(valid_cards), 5)
Пример #3
0
 def test_mc_sim(self):
     cards = CardDeck(0).deal_cards()
     players_cards = [PlayerCards(i, i, cards[i]) for i in range(4)]
     flipped_card = cards[4][0]
     mc_sim = MC_Simulation(players_cards[0], create_random_players(),
                            flipped_card, 0, flipped_card.suite, None)
     mc_sim.simulate()
Пример #4
0
 def test_is_trump(self):
     cards = CardDeck().cards
     trump = Suite(0)
     trump_cards = [c for c in cards if vm.is_trump(c, trump)]
     self.assertEqual(len(trump_cards), 7)
     # Check for the left bower
     left_bower = [c for c in trump_cards if c.suite != trump]
     self.assertEqual(len(left_bower), 1)
     self.assertEqual(left_bower[0].face_card, FaceCard.JACK)
Пример #5
0
 def test_trump_value(self):
     cards = CardDeck().cards
     trump = Suite(0)
     trump_values = [vm.trump_value(c, trump) for c in cards]
     self.assertEqual(set(trump_values), set([i for i in range(8)]),
                      "Incorrect values of trump")
     self.assertEqual(trump_values.count(0), 17)
     for i in range(1, 8):
         self.assertEqual(trump_values.count(i), 1)
Пример #6
0
    def test_min_max_sim(self):
        cards = CardDeck(0).deal_cards()
        players_cards = [PlayerCards(i, i, cards[i]) for i in range(4)]
        game_sim = GameTreeSimulation(players_cards, create_min_max_players(),
                                      cards[4], 0, Suite.DIAMOND)
        game_tree = game_sim.simulate()
        game_stats = GameTreeStatistics(game_tree)

        self.assertEqual(game_stats.tree_depth, 16)
        self.assertEqual(game_stats.num_nodes, 24367)
 def __init__(self,
              players: [PlayerInterface],
              runs: int = 1000,
              card_deck: CardDeck = CardDeck(0)):
     self.players = players
     self.runs = runs
     self.card_deck = card_deck
     self.rounds = []
     self.results = []
     self.sim_time = None
     self.score_time = None
Пример #8
0
    def test_random_simulation_reproducible(self):
        cards = CardDeck(0).deal_cards()
        players_cards = [PlayerCards(i, i, cards[i]) for i in range(4)]

        mc_sim_1 = GameTreeSimulation(players_cards, create_random_players(),
                                      cards[4], 0, Suite.CLUB)
        mc_tree_1 = mc_sim_1.simulate()
        mc_pickle_1 = pickle.dumps(mc_tree_1)

        mc_sim_2 = GameTreeSimulation(players_cards, create_random_players(),
                                      cards[4], 0, Suite.CLUB)
        mc_tree_2 = mc_sim_2.simulate()
        mc_pickle_2 = pickle.dumps(mc_tree_2)

        self.assertEqual(mc_pickle_1, mc_pickle_2)
    def test_known_cards_in_hand(self):
        """Idea to test function is to simulate a whole bunch of games and check at each point
        if the set of known cards and possible cards calculated is valid
        """
        whole_deck = sorted([
            Card(suite, face_card) for face_card in FaceCard for suite in Suite
        ])
        controller = GameController([RandomPlayer(0) for _ in range(4)],
                                    CardDeck(0))
        controller.play()
        for g_round in controller.game.rounds:
            if len(g_round.tricks) > 0:
                player_cards = g_round.players_cards[0]
                possible_cards = possible_cards_in_hand(
                    player_cards.hand, player_cards.index, player_cards.order,
                    g_round.kitty[0], g_round.trump, g_round.tricks)
                known_cards, possible_cards = update_possible_cards(
                    player_cards.hand, player_cards.index, g_round.tricks,
                    possible_cards)

                self.assertTrue(
                    len(list(chain(*list(possible_cards)))) == 0,
                    "Entire game should be played, possible cards are 0")
                self.assertListEqual(sorted(list(chain(*list(known_cards)))),
                                     whole_deck)
                self.assertListEqual(sorted(known_cards[4]),
                                     sorted(g_round.kitty))

                for i in range(5):
                    possible_cards = possible_cards_in_hand(
                        player_cards.hand, player_cards.index,
                        player_cards.order, g_round.kitty[0], g_round.trump,
                        g_round.tricks)
                    known_cards, possible_cards = update_possible_cards(
                        player_cards.hand, player_cards.index, g_round.tricks,
                        possible_cards)
                    # No repeats in known cards
                    all_known_cards = list(chain(*list(known_cards)))
                    self.assertEqual(len(all_known_cards),
                                     len(set(all_known_cards)))
                    # all cards accounted for
                    self.assertEqual(
                        set(
                            list(chain(*list(possible_cards))) +
                            all_known_cards), set(whole_deck))
import cProfile

from euchre.data_model import CardDeck, PlayerCards, Suite
from euchre.game_simulations import GameTreeSimulation

# Profile MonteCarlo Call, takes about 70 seconds on a full min max
cards = CardDeck().deal_cards()
players = [PlayerCards(i, i, cards[i]) for i in range(4)]
mc_sim = GameTreeSimulation(players, cards, 0, Suite.DIAMOND)
cProfile.run('mc_sim.simulate()')




 def test_game_to_json_simple(self):
     cards = CardDeck().deal_cards()
     players = [PlayerCards(i, i, cards[i]) for i in range(4)]
     game = Round(players, cards[4])
     json_game_str = game_to_json(game)
     is_game_json_valid(json_game_str)
from euchre.game_controller import GameController
from euchre.data_model import CardDeck
from euchre.players.RandomPlayer import RandomPlayer
from euchre.players.CommandLinePlayer import CommandLinePlayer

if __name__ == '__main__':
    controller = GameController([RandomPlayer(0)
                                 for _ in range(3)] + [CommandLinePlayer()],
                                CardDeck(0))
    controller.play()
Пример #13
0
 def test_repeatability_game(self):
     controller1 = GameController([RandomPlayer(0) for _ in range(4)], CardDeck(0))
     controller1.play()
     controller2 = GameController([RandomPlayer(0) for _ in range(4)], CardDeck(0))
     controller2.play()
     self.__check_two_games_equal(controller1.game, controller2.game)
Пример #14
0
 def test_web_player(self):
     controller = GameController([RandomPlayer(0) for _ in range(4)], CardDeck(0))
     controller.play()
     self.assertEqual(controller.game.current_action, GameAction.GAME_OVER)
     for r in controller.game.rounds:
         is_game_json_valid(game_to_json(r))
Пример #15
0
 def __init__(self, players: [PlayerInterface], current_dealer: int, card_deck: CardDeck = None):
     self.players = players
     self.current_dealer = current_dealer
     self.card_deck = card_deck if card_deck is not None else CardDeck()
     self.cur_round = None
     self.web_player_start = 0