def test_deck_replenisher_is_empty_after_after_replenish(self): replenisher = Deck([2,3,4]) deck = Deck([1]) deck.set_replenisher(replenisher) self.assertEqual(1, deck.draw()) deck.draw() self.assertTrue(replenisher.is_empty())
def __init__(self): self.discard_pile = Deck() self.hand = [] self.deck = Deck() self.comm = None self.deck.set_replenisher(self.discard_pile) self.played = [] self.turns = 0 self.listeners = []
def test_can_replenish_deck(self): replenisher = Deck([2]) deck = Deck([1]) deck.set_replenisher(replenisher) self.assertEqual(1, deck.draw()) self.assertEqual(2, deck.draw()) self.assertIsNone(deck.draw())
def get_engine_with_ordered_cards(): engine = BattlelineEngine(MockPlayer("yankeeBot"), MockPlayer("rebelBot"), Output()) # reinitialize the deck with a non shuffled deck to make things more reliable # don't do this in production code, the deck should be shuffled in real # code engine.troop_deck = Deck(sorted(engine.get_troop_cards(), key=lambda x: ( x[1], x[0]), reverse=True), shuffleDeck=False) return engine
def __init__(self, player1, player2, output): """ Constructor @param player1 the first player @param player2 the second player """ self.player1 = player1 self.player2 = player2 self.troop_deck = Deck(self.get_troop_cards()) self.output_handler = output self.board_logic = BoardLogic(self) self.last_move = None
def test_notification_of_shuffle(self): deck = Deck([1,2], shuffleDeck = False) listener = Mock() listener.hit_function = False def notify(notification): self.assertEquals(notification.type, "shuffle-deck") listener.hit_function = True listener.notify = notify deck.add_shuffle_listener(listener) deck.shuffle() self.assertTrue(listener.hit_function)
def test_deck_is_not_empty_by_default(self): listOfCards = [1] self.assertFalse(Deck(listOfCards, False).is_empty())
def test_getting_cards_returns_copy(self): deck = Deck([1,2,3], shuffleDeck=False) cards = deck.get_cards() self.assertEquals([1,2,3], cards) deck.draw() self.assertEquals([1,2,3], cards)
def test_can_draw_card_from_deck(self): listOfCards = [1] drawnCard = Deck(listOfCards, False).draw() self.assertEqual(drawnCard, 1)
def test_can_add_to_deck(self): deck = Deck([1]) deck.add(2) self.assertEqual([1,2], deck.get_cards()) self.assertEqual(2, deck.draw()) self.assertEqual(1, deck.draw())
def test_deck_is_empty_before_replenish(self): replenisher = Deck([2]) deck = Deck([1]) deck.set_replenisher(replenisher) self.assertEqual(1, deck.draw()) self.assertTrue(deck.is_empty())
def test_empty_deck_is_empty(self): self.assertTrue(Deck().is_empty())
class Player(object): def __init__(self): self.discard_pile = Deck() self.hand = [] self.deck = Deck() self.comm = None self.deck.set_replenisher(self.discard_pile) self.played = [] self.turns = 0 self.listeners = [] def set_communication(self, comm): self.comm = comm def close_communication(self): if self.comm: self.comm.close() def send_message_and_await_response(self, message): self.comm.send_message(message) return self.comm.get_response() def send_message(self, message): self.comm.send_message(message) def add_event_listener(self, listener): self.listeners.append(listener) self.deck.add_shuffle_listener(listener) def gain_cards(self, cards): for card in cards: self.discard_pile.add(card) for l in self.listeners: l.notify(Notification("gained-cards", cards = cards)) def get_discard_pile(self): return self.discard_pile.get_cards() def get_top_discard_card(self): return self.discard_pile.get_cards()[-1] if self.discard_pile else None def draw_cards(self, num_to_draw): for _ in range(num_to_draw): card = self.deck.draw() if card is None: break self.add_to_hand(card) def add_to_hand(self, card): self.hand.append(card) def put_card_on_top_of_deck(self, card): self.deck.add(card) def get_hand(self): return list(self.hand) def get_deck_cards(self): return self.deck.get_cards() def __get_cards_to_discard(self): return self.hand+self.played def cleanup(self, top_discard = ""): for l in self.listeners: l.notify(Notification("played-cards", cards = self.played)) for card in self.__get_cards_to_discard(): if card != top_discard: self.discard(card) for card in self.__get_cards_to_discard(): self.discard(card) for l in self.listeners: l.notify(Notification("discard-card", card = self.get_top_discard_card())) assert self.hand == [] assert self.played == [] def discard(self, card): if self.is_in_hand(card): self.hand.remove(card) elif card in self.played: self.played.remove(card) else: raise CardNotInHandException(card) self.discard_pile.add(card) def discard_multiple(self, cards): for card in cards: self.discard(card) def is_in_hand(self, card): return card in self.hand def trash(self, cards): for card in cards: if not self.is_in_hand(card): raise CardNotInHandException(card) self.hand.remove(card) for l in self.listeners: l.notify(Notification("trashed-cards", cards = cards)) def play_card(self, card): if card not in self.hand: raise CardNotInHandException(card) self.hand.remove(card) self.played.append(card) def get_played_cards(self): return list(self.played) def get_number_of_turns_taken(self): return self.turns def mark_turn_taken(self): self.turns += 1 def get_score(self): all_cards = self.get_hand() + self.get_deck_cards() + self.get_discard_pile() return sum([get_victory_points(card) for card in all_cards]) def reveal(self, cards): for l in self.listeners: l.notify(Notification("revealed-cards", cards = cards))
def test_deck_shuffle_does_not_change_actual_cards(self): listOfCards = list(range(1, 10)) localDeck = Deck(listOfCards, False) localDeck.shuffle() self.assertEqual(set(localDeck.deck), set(listOfCards))
def test_returns_none_when_drawing_from_empty_deck(self): listOfCards = [1] localDeck = Deck(listOfCards, False) self.assertIsNotNone(next(localDeck)) self.assertIsNone(next(localDeck))
def test_deck_is_empty_after_drawing_all_cards(self): listOfCards = [1, 2] localDeck = Deck(listOfCards, False) next(localDeck) next(localDeck) self.assertTrue(localDeck.is_empty())
def test_can_draw_different_cards_from_deck(self): listOfCards = [1, 2] localDeck = Deck(listOfCards, False) self.assertNotEqual(next(localDeck), next(localDeck))