+1 (315) 557-6473 

Create a Program to Create Fish Card Game in Python Assignment Solution.


Instructions

Objective
For the purpose of this program, you are required to write a C++ assignment that simulates a fish card game. The game will be created using Python language, and the objective is to design a program that allows players to play the game interactively. The fish card game typically involves players collecting sets of four cards of the same rank, and the player who collects the most sets wins the game. Your task is to implement the game logic, including card shuffling, dealing, and player turns.

Requirements and Specifications

program to create fish card game in python
program to create fish card game in python 1
program to create fish card game in python 2
program to create fish card game in python 3
program to create fish card game in python 4
program to create fish card game in python 5
program to create fish card game in python 6
program to create fish card game in python 7
program to create fish card game in python 8
program to create fish card game in python 9

Source Code

from random import randint

cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J","Q","K","A"]

deck = []

# Take all elements from cards and add them to deck 4 times

for i in range(4):

deck += cards

user = []

comp = []

def draw_card() -> str:

"""

This function takes a card from the list 'deck' and returns it

:return: card -> str

"""

# randomly select index of card

index = randint(0, len(deck)-1)

# Get card and delete from deck

card = deck.pop(index)

# Return card

return card

def deal_cards() -> None:

"""

This function deals 7 cards for both user and computer. The cards are dealt alternatively to user, computer, user, computer, etc...

:return: None

"""

# Deal cards to user and computer

for i in range(7):

# Alternatively, deal one to user, then one to comp, etc

user.append(draw_card())

comp.append(draw_card())

def go_fish(hand: list) -> None:

"""

This function adds a new card to user or computer, depending of the hand passed as parameter

:param hand: List of cards (str)

:return: None

"""

# print statement

print("Go fish!")

# Check if the given hand is equal to user or comp

isUser = None

if hand == user:

isUser= True

else:

isUser = False

# Draw card

card = draw_card()

# Add to hand

hand.append(card)

# Sort hand

hand.sort()

# Display info to player

if isUser:

print("You drew a " + card)

else:

print("Computer drew a card")

def give_cards(giver: list, taker: list, card: str) -> tuple:

"""

This function checks if the card is in the giver's hand. If it is in the giver's hand, all instances

of that card are then removed from giver's hand and added to taker's hand

:param giver: list of giver's cards

:param taker: list of taker's cards

:param card: card

:return: Return the updated hands (lists) of giver and taker inside a tuple (giver, taker)

"""

print("The card " + card + " is being transferred.")

# Iterate through the cards in giver

for i, cardi in enumerate(giver):

if cardi == card: # The card in giver at index i is equal to card

# Remove from giver

del giver[i]

# Add to taker

taker.append(card)

Sort taker

taker.sort()

# Now return as tuple

return (giver, taker)

def display() -> None:

"""

This function shows to user the cards that he/she has in his/her hand, and displays the number

of cards in computer's hand

:return:None

"""

print("The user hand is: ", end="")

for i, card in enumerate(user):

print(card, end="")

if i < len(user)-1:

print(", ", end="")

print("")

print(f"Computer has {len(comp)} cards in its hand.")

def play_again() -> bool:

"""

This function asks to user of he/she wants to play again

The function uses an infinite loop that is broken only when the user enters a valid option

:return: None

"""

# Start infinite loop

while True:

option = input("Do you want to play? (Y/N): ")

if option.lower() == "y":

return True

elif option.lower() == "n":

return False

else:

print("Invalid input.")

def ask_user() -> None:

"""

This function asks to user for a card to be drawn from computer's hand

If the card is in computer's hand, then all instances of that card are added to user's hand and

removed from computer's hand

:return: None

"""

display()

# ask to user which card he/she wants to take from comp

card = input("Enter the card to be requested to the computer: ").upper()

# Check if the card is in computer's hand

if card in comp: # The comp has that card

giver, taker = give_cards(comp, user, card)

print("Computer has the card!")

else:

print("Computer does not have that card!")

go_fish(user)