def test_get_card_returns_a_card(self): from src.deck import Deck from src.deck import Card testDeck = Deck(generate=True) cardFromDeck = testDeck.get_card() self.assertIsInstance(cardFromDeck, Card)
def test_get_card_returns_a_card_from_deck(self): from src.deck import Deck from src.deck import Card testDeck = Deck(generate=True) cardFromDeck = testDeck.get_card() self.assertIn(cardFromDeck, testDeck.__deck__)
class Game(): '''The main service that administers the rules of the game Go Fish''' def __init__(self): self.player1 = Player('Player 1') self.player2 = Player('Player 2') self.deck = Deck() self.player1_turn = True def deal(self): '''populates player hand's''' #loop 7 times for _ in range(7): #give a card to each player self.player1.accept_card(self.deck.get_card()) self.player2.accept_card(self.deck.get_card()) def player_turn(self, value): '''standard player turn''' #create our play object with # the value that was asked for play = Play(value) #assign to and from player depending of who's turn it is from_player, to_player = (self.player1, self.player2) if not self.player1_turn else ( self.player2, self.player1) #change the current turn early so we don't forget self.player1_turn = not self.player1_turn #check of the from player has any of the requested cards play.cards_from_opponent = self.check_for_card(value, from_player) #if that list had a value if play.was_successful(): #give the cards to the asker to_player.accept_cards(play.cards_from_opponent) #if the list was empty #check if the deck is empty elif not self.deck.is_empty(): #if not, draw a card play.drawn_card = self.deck.get_card() #give it to the asker to_player.accept_card(play.drawn_card) #if the drawn card matches the exact value, and the deck is not empty if play.drawn_card.value == value and not self.deck.is_empty(): #draw a bonus card play.bonus_card = self.deck.get_card() #give that to the asker to_player.accept_card(play.bonus_card) #capture if the deck is empty play.empty_deck = self.deck.is_empty() #return the play object return play def check_for_card(self, value, from_player): '''check if oppoenent has any cards of that value and return a list of them if they exist''' return from_player.give_cards(value) def auto_turn(self): '''Automatically perform player2's turn''' #determine which hand we are using card_bank = self.player2.hand if not self.player1_turn else self.player1.hand #calculate the max value for randint max_len = len(card_bank) #if the player's hand is empty if (max_len < 1): #set the card bank to the non-booked cards card_bank = self.get_non_books() #set the new max length max_len = len(card_bank) #pick a random index from our card bank index = randint(0, max_len - 1) #choose a card from the bank card = card_bank[index] #return the result of a player's turn with #this random value return self.player_turn(card.value) def get_non_books(self): '''Get a list of values that are not currently in closed books''' #list of ints 0-12 all_cards = list(range(13)) #remove any values that player 1 has on the table remove_player_one = self.remove_values(all_cards, self.player1.books) #remove any values that player 2 has on the table remove_player_two = self.remove_values(remove_player_one, self.player2.books) #return a list of cards for the remaining numbers (we don't care about suits) return list(map(lambda val: Card(0, val), remove_player_two)) def remove_values(self, all_cards, books): '''remove the elements of all_cards that are included in books''' #loop over the player1 books for book in books: try: #if the value of the first card is still in all i = all_cards.index(book[0].value) #remove it all_cards.pop(i) except: #if the value wasn't in all, move on continue return all_cards def tally_scores(self): '''Check to see who won, return a message with the result''' #count p1's books player1_score = len(self.player1.books) #cound p2's books player2_score = len(self.player2.books) #if the scores are the same, its a tie! if player1_score == player2_score: #return this early return 'Tie game!' #if p1 > p2 if player1_score > player2_score: #return this early return 'Player 1 wins!' #if we made it here, player 2 won return 'Player 2 wins!' def game_over(self): '''Check if the game is over yet''' #both player's hands and the deck must be empty return not len(self.player1.hand) > 0 \ and not len(self.player2.hand) > 0 \ and not len(self.deck.cards) > 0
class Game(): def __init__(self): self.player = Player() self.dealer = Dealer() self.deck = Deck() def run(self): # Deal cards self.new_run() # Print board self.print_board() # Player 1 goes first self.players_turn() self.print_board() # dealers turn self.dealer_turn() # check who won winner = self.check_winner() print(winner) def new_run(self): # Deal cards to player self.__deal_to_plyr__() # deal cards to dealer self.__deal_to_dealer__() def __deal_to_plyr__(self): for i in range(0,2): self.player.add_card(self.deck.get_card(), None) def __deal_to_dealer__(self): self.dealer.add_card(self.deck.get_card(), None) self.dealer.add_card(self.deck.get_hidden_card(), None) def players_turn(self): while True: # Get players's decision decision = self.player.play() # If stand then break the loop if decision == 2: break elif decision == 1: #IF the player hits self.hit_player() self.print_board() elif decision == 3: # Split for the player self.split_player() self.print_board() elif decision == 4: # Insurer the player pass # Check if bust if self.player.check_if_bust(): break def hit_player(self): # If player is in a split if self.player.__get_split_status__(): self.player.add_card(card1=self.deck.get_card(), card2=self.deck.get_card()) else: self.player.add_card(card1=self.deck.get_card(), card2=None) def split_player(self): self.player.split() self.hit_player() def insure_player(self): pass def hit_dealer(self): self.dealer.add_card(card1=self.deck.get_card(), card2=None) def dealer_reveal(self, inTest=False): # Reveal hidden card self.dealer.unhide_card() if not inTest: # Print board self.print_board() print("Dealer revealed hidden card") time.sleep(2) def dealer_turn(self): # Reveal hidden card self.dealer_reveal() while True: # Get players's decision decision = self.dealer.play() # If stand then break the loop if decision == 2: break elif decision == 1: #IF the dealer hits self.hit_dealer() self.print_board() print("Dealer chose to hit") time.sleep(1) # CHECK WINNER BY BUST def __check_tie_by_both_bust__(self, player_bust_status, dealer_bust_status): if (player_bust_status and dealer_bust_status): return True else: return False def __check_player_bust_and_dealer_hasnt__(self, player_bust_status, dealer_bust_status): if (player_bust_status and not dealer_bust_status): return True else: return False def __check_dealer_bust_and_player_hasnt__(self, player_bust_status, dealer_bust_status): if (not player_bust_status and dealer_bust_status): return True else: return False def __winner_by_bust__(self, player_bust_status, dealer_bust_status): # Tie if both bust if self.__check_tie_by_both_bust__(player_bust_status, dealer_bust_status): return self.tie() # If player bust and dealer hasn't if self.__check_player_bust_and_dealer_hasnt__(player_bust_status, dealer_bust_status): return self.bust() # If dealer bust and player hasn't if self.__check_dealer_bust_and_player_hasnt__(player_bust_status, dealer_bust_status): return self.winner() return None # CHECK WINNER BY VALUE def __check_plyr_hand_value_larger_than_dealer__(self, plyrHandValue, dealerHandValue): if plyrHandValue > dealerHandValue: return True else: return False def __check_player_and_dealer_hands_are_equal__(self, plyrHandValue, dealerHandValue): if plyrHandValue == dealerHandValue: return True else: return False def __winner_by_value__(self, plyrHand1Value, plyrHand2Value, dealerHandValue, playerSplit=False): # Check hand 1 or hand2 is larger than dealers if self.__check_plyr_hand_value_larger_than_dealer__(plyrHand1Value, dealerHandValue) or self.__check_plyr_hand_value_larger_than_dealer__(plyrHand2Value, dealerHandValue): return self.winner() # # Check hand 2 is larger than dealers # elif self.__check_plyr_hand_value_larger_than_dealer__(plyrHand2Value, dealerHandValue) and playerSplit: # return self.winner() # Check if hand1 or is tied with dealer elif self.__check_player_and_dealer_hands_are_equal__(plyrHand1Value, dealerHandValue) or self.__check_player_and_dealer_hands_are_equal__(plyrHand2Value, dealerHandValue): return self.tie() # # Check if hand2 is tied with dealer # elif self.__check_player_and_dealer_hands_are_equal__(plyrHand2Value, dealerHandValue) and playerSplit: # return self.tie() # Otherwise player lost else: return self.loose() def check_winner(self): player_bust_status = self.player.check_if_bust() dealer_bust_status = self.dealer.check_if_bust() dealer_hand_value = self.dealer.get_hand_value() # hand_values = self.player.get_hand_value() hand1_value = self.player.hand1.get_hand_value() hand2_value = self.player.hand2.get_hand_value() player_in_split = self.player.__get_split_status__() # Check winner by bust winnerByBustOutcome = self.__winner_by_bust__(player_bust_status, dealer_bust_status) if winnerByBustOutcome: return winnerByBustOutcome # Check Winner By Value winnerByValueOutcome = self.__winner_by_value__(hand1_value, hand2_value, dealer_hand_value, player_in_split) if winnerByValueOutcome: return winnerByValueOutcome return "UNKNOWN OUTCOME" def winner(self): return "You Win" def tie(self): return "Game Tied" def loose(self): return "You Lose" def bust(self): return "BUST - " + self.loose() def print_board(self): # Clear screen self.clear_screen() # print Dealer print("Dealer") print(self.dealer.get_hand_output()) # print("\n\n") # print Player print("Player 1") print(self.player.get_hand_output()) print("") def clear_screen(self): if platform.system()=="Windows": subprocess.Popen("cls", shell=True).communicate() else: #Linux and Mac print("\033c", end="")