Пример #1
0
 def test_multiplayer_rankings_complex(self):
     """
     Tests a complex multi-player game of blackjack in which some players bust,
     some do not, verifying that the final ranking is correctly ordered.
     """
     g = Game(['bob', 'jane', 'joe', 'katy'])
     ace = Card('ace', [1, 11])
     king = Card('king', [10])
     five = Card('five', [5])
     # bob --> bust w/ 25
     self.assertTrue(g.hit(king))
     self.assertTrue(g.hit(king))
     self.assertFalse(g.hit(five))
     # jane --> stay w/ 15
     self.assertTrue(g.hit(king))
     self.assertTrue(g.hit(five))
     g.stay()
     # joe --> stay w/ 20
     self.assertTrue(g.hit(king))
     self.assertTrue(g.hit(king))
     g.stay()
     # katy --> stay w/ 21
     self.assertTrue(g.hit(king))
     self.assertTrue(g.hit(king))
     self.assertTrue(g.hit(ace))
     g.stay()
     # get the winner
     self.assertListEqual(g.get_rankings(), ['katy', 'joe', 'jane', 'bob'])
Пример #2
0
def test_score_wrong_match_and_card_not_already_seen():
    game = Game(Terminal(), Difficulty.NORMAL)
    game.board[0][0] = Card(CardValue.THREE, Suit.SPADES)
    game.board[0][1] = Card(CardValue.TWO, Suit.HEARTS)
    game.selected = [(0, 0), (0, 1)]

    assert game.calculate_score() == 0
    game.difficulty = Difficulty.HARD
    assert game.calculate_score() == -1
Пример #3
0
  def test_player_deck(self):
    deck = Deck()
    player = Player("Mustapha", deck)
    #::
    cards = [Card(Rank.ACE, Suite.HEART), Card(Rank.TWO, Suite.PIKES)]
    player.addToDeckBottom(cards)

    self.assertEqual(player.popDeckTopCard(), cards[0])
    self.assertEqual(player.popDeckTopCard(), cards[1])
Пример #4
0
def test_score_wrong_match_second_card_when_card_already_seen():
    game = Game(Terminal(), Difficulty.NORMAL)
    seen_card = Card(CardValue.TWO, Suit.HEARTS)
    game.board[0][0] = Card(CardValue.ACE, Suit.CLUBS)
    game.board[0][1] = seen_card
    game.selected = [(0, 0), (0, 1)]
    game.seen[seen_card] = [(0, 1)]

    assert game.calculate_score() == -1
    game.difficulty = Difficulty.HARD
    assert game.calculate_score() == -2
Пример #5
0
 def test_blackjack(self):
     """
     Tests whether a blackjack (21 valued hand) is correctly identified
     """
     h = Hand()
     card1 = Card('ace', [1, 11])
     card2 = Card('king', [10])
     h.add_card(card1)
     self.assertFalse(h.has_blackjack())
     h.add_card(card2)
     self.assertTrue(h.has_blackjack())
Пример #6
0
def test_score_wrong_match_when_location_of_match_seen():
    game = Game(Terminal(), Difficulty.NORMAL)
    card = Card(CardValue.ACE, Suit.CLUBS)
    other_card = Card(CardValue.TWO, Suit.HEARTS)
    game.board[0][0] = card
    game.board[0][1] = other_card
    game.selected = [(0, 0), (0, 1)]
    game.seen[card] = [(0, 2)]

    assert game.calculate_score() == -1
    game.difficulty = Difficulty.HARD
    assert game.calculate_score() == -2
Пример #7
0
 def test_singleplayer(self):
     """
     Tests a simple single-player game of blackjack
     """
     g = Game(['bob'])
     ace = Card('ace', [1, 11])
     king = Card('king', [10])
     self.assertTrue(g.hit(ace))
     self.assertTrue(g.hit(king))
     g.stay()
     winner = g.get_winner()
     self.assertEqual(winner, 'bob')
Пример #8
0
 def test_get_priority_nonbust(self):
     """
     Tests whether the correct priority is assigned to non-bust hands
     """
     h = Hand()
     card1 = Card('ace', [1, 11])
     card2 = Card('king', [10])
     h.add_card(card1)
     self.assertEqual(h.get_priority(), 11)
     h.add_card(card2)
     self.assertEqual(h.get_priority(), 21)
     h.add_card(card2)
     self.assertEqual(h.get_priority(), 21)
Пример #9
0
 def test_add_card_multiple_no_ace(self):
     """
     Tests the state of adding multiple non-ace cards to the hand
     """
     h = Hand()
     card1 = Card('two', [2])
     card2 = Card('king', [10])
     h.add_card(card1)
     h.add_card(card2)
     self.assertListEqual(list(h.get_possible_scores()), [12])
     self.assertListEqual(list(h.get_cards()), [card1, card2])
     expected_tree = BinaryTree(
         BinaryTreeNode(0, BinaryTreeNode(2, BinaryTreeNode(12))))
     self.assertTrue(h.hands.is_equivalent(expected_tree))
Пример #10
0
class TestCard(unittest.TestCase):
    def setUp(self):
        self.card = Card(0, 3, NumberSet(18))
        self.card1 = Card(0, 0, NumberSet(0))

    def test_getSize(self):
        self.assertEqual(self.card.getSize(), 3)
        self.assertEqual(self.card1.getSize(), 0)

    def test_getID(self):
        self.assertIsNotNone(self.card)
        self.assertIsNotNone(self.card1)
        self.assertEqual(self.card.getId(), 0)
        self.assertEqual(self.card1.getId(), 0)
Пример #11
0
 def test_multiplayer_bust(self):
     """
     Tests a simple two-player game of blackjack where one player busts and the other stays
     """
     g = Game(['bob', 'jane'])
     ace = Card('ace', [1, 11])
     king = Card('king', [10])
     self.assertTrue(g.hit(ace))
     self.assertTrue(g.hit(king))
     g.stay()
     self.assertTrue(g.hit(king))
     self.assertTrue(g.hit(king))
     self.assertFalse(g.hit(king))
     winner = g.get_winner()
     self.assertEqual(winner, 'bob')
Пример #12
0
 def test_is_bust_w_ace(self):
     """
     Tests whether a bust (all possible values > 21) is correctly identified, including aces.
     """
     h = Hand()
     card1 = Card('ace', [1, 11])
     card2 = Card('king', [10])
     h.add_card(card1)
     self.assertFalse(h.is_bust())
     h.add_card(card2)
     self.assertFalse(h.is_bust())
     h.add_card(card2)
     self.assertFalse(h.is_bust())
     h.add_card(card2)
     self.assertTrue(h.is_bust())
Пример #13
0
 def test_best_score_w_ace(self):
     """
     Tests whether the correct best score is returned, including aces.
     """
     h = Hand()
     card1 = Card('ace', [1, 11])
     card2 = Card('king', [10])
     h.add_card(card1)
     self.assertEqual(h.get_best_score(), 11)
     h.add_card(card2)
     self.assertEqual(h.get_best_score(), 21)
     h.add_card(card2)
     self.assertEqual(h.get_best_score(), 21)
     h.add_card(card2)
     self.assertEqual(h.get_best_score(), 31)
Пример #14
0
def test_move_sets_correct_seen_values():
    game = Game(Terminal(), Difficulty.NORMAL)
    card = Card(CardValue.THREE, Suit.SPADES)
    other_card = Card(CardValue.TWO, Suit.HEARTS)
    game.board[0][0] = card
    game.board[0][1] = other_card
    game.selected = [(0, 0), (0, 1)]
    game.seen[card] = [(0, 2)]
    game.move(False)

    assert game.seen[card] == [(0, 2), (0, 0)]
    assert game.seen[other_card] == [(0, 1)]
    seen = game.seen
    game.selected = [(0, 0), (0, 1)]
    game.move(False)
    assert game.seen == seen
Пример #15
0
 def build_card(self, card):
     """
     create a card form a string
     :param card: string of a card
     :return:
     """
     return Card(card[:-1], card[-1:])
Пример #16
0
 def __init__(self):
     """
     Constructor for the stock. Initially contains all 52 cards face-down.
     """
     super().__init__()
     # spades and clubs are black
     self.cards.extend([
         Card("black", rank, suit, True) for rank in RANKS
         for suit in BLACK_SUITS
     ])
     # hearts and diamonds are white
     self.cards.extend([
         Card("white", rank, suit, True) for rank in RANKS
         for suit in WHITE_SUITS
     ])
     self.shuffle()
Пример #17
0
 def __init__(self, terminal, difficulty):
     self.difficulty = difficulty
     self.term = terminal
     self.deck = [Card(value, suit) for value in CardValue for suit in Suit]
     self.high_score = 0
     self.display = GameDisplay(terminal)
     self.reset()
     self.seen = {}
Пример #18
0
def createPlayingDeck(suites: List[Suite], ranks: List[Rank]) -> Deck:
    deck = Deck()
    for rank in ranks:
        for suite in suites:
            card = Card(rank, suite)
            deck.pushToTop(card)

    return deck
Пример #19
0
def test_score_increases_on_match():
    game = Game(Terminal(), Difficulty.NORMAL)
    matched_card = Card(CardValue.ACE, Suit.CLUBS)
    game.board[0][0] = matched_card
    game.board[0][1] = matched_card
    game.selected = [(0, 0), (0, 1)]

    assert game.calculate_score() == 1
    game.difficulty = Difficulty.HARD
    assert game.calculate_score() == 3
Пример #20
0
def test_move_sets_matched_cards_correctly():
    game = Game(Terminal(), Difficulty.NORMAL)
    card = Card(CardValue.THREE, Suit.SPADES)
    game.board[0][0] = card
    game.board[0][1] = card
    game.selected = [(0, 0), (0, 1)]
    game.move(False)

    assert (0, 0) in game.matched
    assert (0, 1) in game.matched
Пример #21
0
    def __init__(self, player1name, player1Gender, player2name, player2Gender):

        # Creation of the card's instances
        spy_card = Card("Spy", 0, 2)
        guard_card = Card("Guard", 1, 6)
        priest_card = Card("Priest", 2, 2)
        baron_card = Card("Baron", 3, 2)
        handmaid_card = Card("Handmaid", 4, 2)
        prince_card = Card("Prince", 5, 2)
        chancellor_card = Card("Chancellor", 6, 2)
        king_card = Card("King", 7, 1)
        countess_card = Card(
            "Countess",
            8,
            1,
        )
        princess_card = Card("Princess", 9, 1)

        # List of cards we will put in the deck

        self.listOfCards = [
            spy_card, guard_card, priest_card, baron_card, handmaid_card,
            prince_card, chancellor_card, king_card, countess_card,
            princess_card
        ]

        # List of the card we will put on the side at the beginning of each round

        self.hiddenCard = None
        self.isolatedCards = []

        self.deck = []

        # Creation of the players
        self.player1 = Player(player1name, player1Gender, self.hiddenCard,
                              self.isolatedCards, self.listOfCards)
        self.player2 = Player(player2name, player2Gender, self.hiddenCard,
                              self.isolatedCards, self.listOfCards)

        # Assigns the opposite player
        self.player1.oppositePlayer(self.player2)
        self.player2.oppositePlayer(self.player1)
Пример #22
0
 def create_deck(self):
     """
     create the initial deck
     :return:
     """
     cards = []
     for rank in CardRanks:
         for card_type in CardTypes:
             cards.append(Card(rank.value, card_type.value))
     self.logger.debug("deck was created")
     return cards
Пример #23
0
 def test_add_card_one_not_ace(self):
     """
     Tests the state of adding 1 non-ace card to the hand
     """
     h = Hand()
     card = Card('king', [10])
     h.add_card(card)
     self.assertListEqual(list(h.get_possible_scores()), [10])
     self.assertListEqual(list(h.get_cards()), [card])
     expected_tree = BinaryTree(BinaryTreeNode(0, BinaryTreeNode(10)))
     self.assertTrue(h.hands.is_equivalent(expected_tree))
Пример #24
0
 def test_best_score_no_ace(self):
     """
     Tests whether the correct best score is returned, excluding aces.
     """
     h = Hand()
     card = Card('king', [10])
     h.add_card(card)
     self.assertEqual(h.get_best_score(), 10)
     h.add_card(card)
     self.assertEqual(h.get_best_score(), 20)
     h.add_card(card)
     self.assertEqual(h.get_best_score(), 30)
Пример #25
0
def main():
    #:: for a longer game play(longer because of the number of cards in deck),
    # uncomment and use this input below

    # suites = [Suite.HEART, Suite.TILES, Suite.CLOVERS, Suite.PIKES]
    # ranks  = [
    #   Rank.ACE, Rank.KING, Rank.QUEEN, Rank.JACK,
    #   Rank.TEN, Rank.NINE, Rank.EIGHT, Rank.SEVEN,
    #   Rank.SIX, Rank.FIVE, Rank.FOUR, Rank.THREE,
    #   Rank.TWO
    # ]
    # initial_deck = createPlayingDeck(suites, ranks)

    initial_deck_list = [
        Card(Rank.ACE, Suite.HEART),
        Card(Rank.ACE, Suite.CLOVERS),
        Card(Rank.ACE, Suite.PIKES),
        Card(Rank.TWO, Suite.CLOVERS)
    ]

    initial_deck = Deck()
    initial_deck.extend(initial_deck_list)

    #:: create a list of players.
    player_one = Player("Mario", Deck())
    player_two = Player("Luigi", Deck())
    players = [player_one, player_two]

    #:: create your shuffle strategy
    shuffleStrategy = SimpleShuffleStrategy()

    #:: create your ranking strategy
    rankingStrategy = RankScore()

    #:: create game
    game = Game(initial_deck, players, shuffleStrategy, rankingStrategy)

    #:: call game.start()
    game.start()
Пример #26
0
 def choose_cards_NN(self, table):
     probs = self.get_log_prob_pred(table)
     sort_index = numpy.argsort(probs)
     sort_index = sort_index.tolist()
     sort_index.reverse()
     own_color = self.color
     for c in 'Blue', 'Green':
         if c != own_color:
             rival_color = c
     for i in sort_index:
         id_candidate_from_hand, id_candidate_from_table = self.vector_index_to_ids(
             i)
         candidate_from_hand = Card(id_candidate_from_hand, own_color)
         if candidate_from_hand not in self.hand:
             continue
         if id_candidate_from_hand == 2:  # What happens when playing the Parrot
             if id_candidate_from_table < 13:
                 candidate_from_table = Card(id_candidate_from_table,
                                             rival_color)
                 if candidate_from_table not in table.queue:
                     continue
         elif id_candidate_from_hand == 5:  # What happens when playing the Chameleon
             if id_candidate_from_table < 13:
                 queue_ids = []
                 queue_ids[:] = (c.id for c in table.queue)
                 if id_candidate_from_table not in queue_ids:
                     continue
                 candidate_from_table = Card(id_candidate_from_hand, '')
                 # I think it's ok to leave blank the color of the mimicked card
         if id_candidate_from_table == 0 or id_candidate_from_table == 13:
             candidate_from_table = None
         try:
             return candidate_from_hand, candidate_from_table
         except UnboundLocalError:
             print(candidate_from_hand, id_candidate_from_table)
             exit(1)
Пример #27
0
 def choose_card_from_queue_random(self, table):
     self.chosen_target = None
     # Choose Parrot's target
     if self.chosen_card_from_hand.id == 2:
         opp_cards_in_queue = []
         opp_cards_in_queue[:] = (c for c in table.queue
                                  if c.color != self.color)
         if len(opp_cards_in_queue):
             self.chosen_target = random.choice(opp_cards_in_queue)
     # Choose Chameleon's target
     elif self.chosen_card_from_hand.id == 5:
         queue_ids = []
         queue_ids[:] = (c.id for c in table.queue
                         if c.id != 2 and c.id != 5)
         queue_ids = list(set(queue_ids))
         if len(queue_ids):
             target_id = random.choice(queue_ids)
             self.chosen_target = Card(target_id, "")
     return self.chosen_target
Пример #28
0
def arrDefCards(XmlElement):
    numCartasDefense = 0
    maxDefense = 5
    arrCards = []
    while numCartasDefense < 10:
        for card in XmlElement.findall("./deck/card/[defense='" +
                                       str(maxDefense) + "']"):
            # print(ALL.tostring(card, encoding='utf8').decode('utf8')) para visualizar lo que fue selecionado
            numCartasDefense = numCartasDefense + 1
            carta = Card(card.attrib['summonPoints'], card.attrib['type'],
                         card.find('name').text,
                         card.find('description').text,
                         card.find('attack').text,
                         card.find('defense').text)
            arrCards.append(carta)
            if numCartasDefense == 10:
                break
        maxDefense = maxDefense - 1
    return arrCards
Пример #29
0
def arrRandomCards(xmlElement):
    arrRandomNumber = randomNumArray()
    arrCards = []
    for i in range(len(arrRandomNumber)):
        num = arrRandomNumber[i]
        contador = 0
        while contador <= num:
            for card in xmlElement.findall("./deck/card"):
                contador = contador + 1
                if contador == num:
                    carta = Card(card.attrib['summonPoints'],
                                 card.attrib['type'],
                                 card.find('name').text,
                                 card.find('description').text,
                                 card.find('attack').text,
                                 card.find('defense').text)
                    arrCards.append(carta)
                    break
    return arrCards
Пример #30
0
 def load_card_set(self, filename):
     """
     :param filename: nom du fichier
     :type filename: str
     """
     #name attack health mana
     myfic = open(filename, "r", 1)
     tmp = {}
     for line in myfic:
         i = 0
         word = line.partition("|")
         while i < 7:
             tmp[i] = word[0]
             word = word[2].partition("|")
             i += 1
         tmp[i] = word[0].split('\n')[0]
         self.deck.append(
             Card(tmp[0], int(tmp[1]), int(tmp[2]), int(tmp[3]),
                  int(tmp[4]), int(tmp[5]), int(tmp[6]), int(tmp[7])))
     myfic.close()