예제 #1
0
 def test_play_card(self):
     deck = Deck()
     cards_in_deck_before_playing = len(deck.cards_in_deck)
     cards_played_before_playing = len(deck.dropped_cards)
     card_to_play = deck.retrieve_card()
     deck.play_card(card_to_play)
     cards_in_deck_after_playing = len(deck.cards_in_deck)
     cards_played_after_playing = len(deck.dropped_cards)
     assert cards_in_deck_before_playing - cards_in_deck_after_playing == 1 and cards_played_after_playing - cards_played_before_playing == 1
예제 #2
0
 def simulate_game(self, player_1, player_2):
     # Starts a new game
     game = Game(players=[copy.deepcopy(player_1), copy.deepcopy(player_2)])
     score = Score(name_player_1=game.players[0].name,
                   name_player_2=game.players[1].name)
     while True:
         # Give cards
         deck = Deck()
         game, deck = self._give_cards_to_players(game, deck)
         game_round, score = self._simulate_round(game, deck, score)
         game.results.append(
             dict(player_1={
                 "points":
                 game_round.score.player_1['points'],
                 "cut":
                 game_round.cut
                 if game.players[0].name == game_round.winner else None
             },
                  player_2={
                      "points":
                      game_round.score.player_2['points'],
                      "cut":
                      game_round.cut
                      if game.players[1].name == game_round.winner else None
                  },
                  moves=[move.to_dict() for move in game_round.moves]))
         game, winner_index = self._find_winner(game, game_round, score)
         game.winner = winner_index
         if winner_index is not None:
             break
     return game
예제 #3
0
 def _simulate_round(game: Game, deck: Deck, score: Score):
     game_round = Round()
     current_player = 0
     while True:
         # Starts a new move
         current_player = 0 if current_player == 1 else 1
         game.players[current_player].played_dropped_card = False
         deck, move = game.players[current_player].make_move(deck, score)
         deck.retrieve_card(move.retrieved_card)
         deck.play_card(move.played_card)
         game_round.add_move(move)
         if move.cut:
             # When a player "cuts" a step-game has finished, scores are registered and then a check is
             # needed to figure out if some player reached the max amount of points (that means they lost)
             score.player_1['points'] += game.players[0].rest_value
             score.player_2['points'] += game.players[1].rest_value
             game_round.register_score(copy.deepcopy(score))
             game.players[current_player].cut = None
             break
     return game_round, score
 def test_initial_cards_allocation(self):
     player_1 = self._build_player('Jack')
     player_2 = self._build_player('Mary')
     games_simulator = GamesSimulator()
     for _ in range(self.REPRESENTATIVE_GAMES_AMOUNT):
         deck = Deck()
         game = Game(
             players=[copy.deepcopy(player_1),
                      copy.deepcopy(player_2)])
         game, deck = games_simulator._give_cards_to_players(game, deck)
         assert len(game.players[0].cards) == game.CARDS_IN_HAND and len(
             game.players[1].cards) == game.CARDS_IN_HAND
예제 #5
0
 def test_retrieving_card(self):
     deck = Deck()
     card_to_retrieve = deck.cards_in_deck[-1]
     retrieved_card = deck.retrieve_card()
     assert retrieved_card == card_to_retrieve and len(deck.cards_in_deck) == 47
예제 #6
0
 def test_retrieving_card_when_no_card_left(self):
     deck = Deck()
     deck.cards_in_deck = Cards()
     deck.retrieve_card()
     assert len(deck.cards_in_deck) == 47
예제 #7
0
def get_deck():
    deck = Deck()
    return flask.jsonify(
        {"cards": [card.to_string() for card in deck.cards_in_deck]})