Skip to content
Advertisement

Trying to program my computer player to be smarter in the card game “Go Fish”. More specific info in body

”’ This is the card game “Go Fish”, and I am trying to make my computer player smarter. As of right now, when player 2 asks for a card, it does so with the random.choice function like this (player_2_choice = random.choice(player_2_hand). I want to try and restrict the available choices by comparing player_2_hand to two different lists. One is for_player_2 = [], which is a collection of cards that player1 has asked for and player2 didn’t have. If player2 didn’t have the card, that means the card is still in player1’s hand, so the computer player should know to ask for that card if they draw it later. The other list is remembered_cards = [], which is a collection of cards that player2 asks player1 for and player1 doesn’t have the card in their hand, so player2 should know not to ask for that card for a set amount of turns because player2 knows that player1 doesn’t have that card. Will post most of the program and comment as much as I can to help explain what I am trying to do. Just looking for suggestions on what I could do. Not allowed to use classes, thats part of another assignment. When making the comparisons between for_player_2 and player_2_hand, and remembered_cards and player_2_hand, I need to only compare the first letter of each element to find the match-Example: for_player_2 = [‘2H’] player_2_hand = [‘2C’, ‘3C’, ‘4c’, ‘KC’, ‘AC’] — The check would see that 2H is for_player_2 and the 2C in player_2_hand match, so that would be the value stored in player_2_choice. Then that would be the card player_2 asks for. ”’

import random
deck = build_deck()  #build_deck() builds a deck of cards
shuffle_deck(deck) #shuffles deck
player_1_hand = deal_hand(deck, 7) #Deals 7 cards to player1, card example: 'AS'
player_2_hand = deal_hand(deck, 7) #Deals 7 cards to player2
player_1_pairs = remove_pairs(player_1_hand) #Removes pairs at beginning of game
player_2_pairs = remove_pairs(player_2_hand) #Removes pairs at beginning of game
for_player_2 = [] # Will be added to later
remembered_cards = [] #Will be added to later
while True:
    if len(player_1_hand) == 0 or len(player_2_hand) == 0 or len(deck) == 0:
        break
    # Player 1's turn
    while True:
        if len(player_1_hand) == 0:
            break
        print("Your cards ", player_1_hand)
        desired_card = input("What card would you like from your opponent?")
        if desired_card not in player_1_hand:
            print("Invalid option, please choose again.")
            continue
        if check_guess(player_2_hand, player_1_hand, desired_card, player_1_pairs) == True:
            #This function above checks to see if the guess is in the other player's hand
            print("Well done!!!")
        else:
            print("Go Fish")
            player_1_hand.append(deal_top_card(deck)) #Deals top card of deck to player
            d_card = player_1_hand[-1] #Last card in hand is the card we just drew
            # Since we had to go fish, the desired card is added to the list for_player_2
            for_player_2.append(desired_card)
            if check_d_card(player_1_hand, player_1_pairs, desired_card, d_card) == True:
                print("Congratulations, you drew the match to your pair!!!")
                continue
            elif check_d_card(player_1_hand, player_1_pairs, desired_card, d_card) == False:
                for i in range(len(player_1_hand)):
                    for j in range(i + 1, len(player_1_hand)):
                        # If card ranks match
                        card1 = player_1_hand[i]
                        card2 = player_1_hand[j]
                        if card1[0] == card2[0]:
                            #Add the pairs to the player pair list
                            player_1_pairs.extend([card1, card2])
                            #Remove pairs from player's hand
                            player_1_hand.remove(card1)
                            player_1_hand.remove(card2)
                            break
            print(f"Player 1 has {len(player_1_hand)} cards in their hand")
            break
    #Player 2's turn
    while True:
        if len(player_2_hand) == 0:
            break
        #This is the section I am trying to figure out mainly
        #Want check and see if matching element between for_player_2 and player_2_hand
        cardFound = False
        for card in for_player_2:
            for hand_card in player_2_hand:
                if hand_card[0] == card[0]:
                    player_2_choice = card
                    print("Player 2 asked for ", card)
                    cardFound = True
                    break
        if not cardFound:
            #This is where the comparison needs to be made between 
            #remembered_cards and player_2_hand
        player_2_choice = random.choice(player_2_hand) # This is original but I want to change
        print("Player 2 asked for", player_2_choice) # Original
        #After first check between for_player_2 and player_2_hand I want to check another
        #Next I want to check if there is a matching element between remembered_cards
        #And player_2_hand because remembered_cards are the cards player2 asked for
        #and player1 didn't have, so player2 shouldn't ask for that card again for 
        #a set amount of turns. Not sure if that is possible.
        
        if player_2_choice not in player_2_hand:
            continue
        if check_guess(player_1_hand, player_2_hand, player_2_choice, player_2_pairs) == True:
            print("Player 2 got a pat on the back for guessing correctly!!!")
        else:
            print("Player 2 had to go fishing!!!")
            #Here is where the failed player_2_choice gets added to remembered cards
            remembered_cards.append(player_2_choice)
            player_2_hand.append(deal_top_card(deck)) #Deals top card of deck to player2
            d_card = player_2_hand[-1]
            if check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card) == True:
                print("Player 2 got a pat on the back for drawing the match to their pair")
                continue
            elif check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card) == Fals:
                for i in range(len(player_2_hand)):
                    for j in range(i + 1, len(player_2_hand)):
                        #If card ranks match
                        card1 = player_2_hand[i]
                        card2 = player_2_hand[j]
                        if card1[0] == card2[0]:
                            player_2_pairs.extend([card1, card2])
                            player_2_hand.remove(card1)
                            player_2_hand.remove(card2)
                            break
            print(f"Player 2 has {len(player_2_hand)} cards in their hand")
            break
        

Advertisement

Answer

import random
deck = build_deck()  #build_deck() builds a deck of cards
shuffle_deck(deck) #shuffles deck
player_1_hand = deal_hand(deck, 7) #Deals 7 cards to player1, card example: 'AS'
player_2_hand = deal_hand(deck, 7) #Deals 7 cards to player2
player_1_pairs = remove_pairs(player_1_hand) #Removes pairs at beginning of game
player_2_pairs = remove_pairs(player_2_hand) #Removes pairs at beginning of game
for_player_2 = [] # Will be added to later
remembered_cards = {} # <========== converted to a Set for better preformance
while True:
    if len(player_1_hand) == 0 or len(player_2_hand) == 0 or len(deck) == 0:
        break
    # Player 1's turn
    while True:
        if len(player_1_hand) == 0:
            break
        print("Your cards ", player_1_hand)
        desired_card = input("What card would you like from your opponent?")
        if desired_card not in player_1_hand:
            print("Invalid option, please choose again.")
            continue
        if check_guess(player_2_hand, player_1_hand, desired_card, player_1_pairs) == True:
            #This function above checks to see if the guess is in the other player's hand
            print("Well done!!!")
        else:
            print("Go Fish")
            player_1_hand.append(deal_top_card(deck)) #Deals top card of deck to player
            d_card = player_1_hand[-1] #Last card in hand is the card we just drew
            # Since we had to go fish, the desired card is added to the list for_player_2
            for_player_2.append(desired_card)
            if check_d_card(player_1_hand, player_1_pairs, desired_card, d_card) == True:
                print("Congratulations, you drew the match to your pair!!!")
                continue
            elif check_d_card(player_1_hand, player_1_pairs, desired_card, d_card) == False:
                for i in range(len(player_1_hand)):
                    for j in range(i + 1, len(player_1_hand)):
                        # If card ranks match
                        card1 = player_1_hand[i]
                        card2 = player_1_hand[j]
                        if card1[0] == card2[0]:
                            #Add the pairs to the player pair list
                            player_1_pairs.extend([card1, card2])
                            #Remove pairs from player's hand
                            player_1_hand.remove(card1)
                            player_1_hand.remove(card2)
                            break
            print(f"Player 1 has {len(player_1_hand)} cards in their hand")
            break
    #Player 2's turn
    while True:
        if len(player_2_hand) == 0:
            break
        #This is the section I am trying to figure out mainly
        #Want check and see if matching element between for_player_2 and player_2_hand
        cardFound = False
        for card in for_player_2:
            for hand_card in player_2_hand:
                if hand_card[0] == card[0]:
                    player_2_choice = card
                    # not_needed #print("Player 2 asked for ", card)
                    cardFound = True
                    break
        if not cardFound:
            #This is where the comparison needs to be made between 
            #remembered_cards and player_2_hand
            player_2_choice = random.choice( # random choose
                list( # random.choice requires list (as an argument)
                    set(player_2_hand). # convert player_2_hand to a Set to use difference method
                        difference(remembered_cards) # remove cards that is in remembered_cards
                ) # randomly choose from the result list
            )

        # not_needed #player_2_choice = random.choice(player_2_hand) # This is original but I want to change
        print("Player 2 asked for", player_2_choice) # Original
        #After first check between for_player_2 and player_2_hand I want to check another
        #Next I want to check if there is a matching element between remembered_cards
        #And player_2_hand because remembered_cards are the cards player2 asked for
        #and player1 didn't have, so player2 shouldn't ask for that card again for 
        #a set amount of turns. Not sure if that is possible.
        
        if player_2_choice not in player_2_hand:
            continue
        if check_guess(player_1_hand, player_2_hand, player_2_choice, player_2_pairs) == True:
            print("Player 2 got a pat on the back for guessing correctly!!!")
        else:
            print("Player 2 had to go fishing!!!")
            #Here is where the failed player_2_choice gets added to remembered cards
            remembered_cards.add(player_2_choice)
            player_2_hand.append(deal_top_card(deck)) #Deals top card of deck to player2
            d_card = player_2_hand[-1]
            if check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card) == True:
                print("Player 2 got a pat on the back for drawing the match to their pair")
                continue
            elif check_d_card(player_2_hand, player_2_pairs, player_2_choice, d_card) == Fals:
                for i in range(len(player_2_hand)):
                    for j in range(i + 1, len(player_2_hand)):
                        #If card ranks match
                        card1 = player_2_hand[i]
                        card2 = player_2_hand[j]
                        if card1[0] == card2[0]:
                            player_2_pairs.extend([card1, card2])
                            player_2_hand.remove(card1)
                            player_2_hand.remove(card2)
                            break
            print(f"Player 2 has {len(player_2_hand)} cards in their hand")
            break
        
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement