Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi please I need help with this code here is an example of the output here is the code here is the code as a

Hi please I need help with this code
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
here is an example of the output
image text in transcribed
image text in transcribed
image text in transcribed
here is the code
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
here is the code as a text so you can copy and paste it
import random
"""
Your name:
Lab Name
Class #
Date:
"""
# *******************************************
# * Functions *
# *******************************************
# @ 1. Returns a shuffled list of 52 integers to represent a deck of cards
def createDeck():
# initialize a list for the deck
deck = []
# **** YOUR CODE HERE ****
#Return the list for the deck
return deck
# @ 2. Takes a list as a parameter and mixes it up (shuffles it)
def shuffle(deck):
# Create an empty list to store the shuffled integers in
shuffledList = []
# **** YOUR CODE HERE ****
# Return the shuffled list
return shuffledList
# Performs a players turn in the game. Searches listSearching for askCard.
# If it finds a match, it counts, removes the card from listSearching and
# adds it to listPlaying.
# Takes in parameters:
# 1. the integer for the card being asked for
# 2. the list of cards for the player asking
# 3. the list of cards for the player looking for the card in their hand
def turn(askCard, listPlaying, listSearching):
cardCount = 0
# @ 3. Loop backwards through the list
# **** YOUR CODE HERE ****
if cardCount > 0:
output = str(cardCount) + " " + str(askCard)
if (cardCount > 1):
output += "'s were given"
else:
output += " was given"
print output
else:
print ("No " + str(askCard) + "'s. Go fish.")
# @ 4. The there are cards left in the deck, add and display a random card to the
# playing hand and remove it from the deck.
# **** YOUR CODE HERE ****
# @ 5. Checks to see if the player has a book in their hand
def checkBook(hand):
# Initialize a book variable to 0
book = 0
# **** YOUR CODE HERE ****
# Return the value of the book variable
return book
# @ 6. Deals 5 cards to a player if they have no cards
def deal(hand):
print "Delete this line of code. It is only here so you can check your code"
# **** YOUR CODE HERE ****
# ==============================================================================
# *******************************************
# * Main Program *
# *******************************************
# Initialize Variables
deck = createDeck()
plyrsHand = []
compHand = []
numBooksPlyr = 0
numBooksComp = 0
currTurn = 1
deal(plyrsHand)
deal(compHand)
while (len(plyrsHand) > 0 and len(compHand) > 0 and len(deck) > 0):
plyrsHand.sort()
print ("Player's Hand: " + str(plyrsHand))
print ("Player's Books: " + str(numBooksPlyr))
print ("Computer's Books: " + str(numBooksComp))
compHand.sort()
#print ("Computer's Hand: " + str(compHand))
print
print ("Player's Turn:")
# Creates a random card for the player to ask for.
# Comment these 3 lines of code if you want to choose them yourself
ask = random.randint(0, len(plyrsHand) - 1)
print ("Player asks for any " + str(plyrsHand[ask]) + "'s")
turn(plyrsHand[ask], plyrsHand, compHand)
# Gets the card to ask for from the user
# Uncomment these 4 lines of code if you want to choose them yourself
"""
ask = int(input("Which card do you want to ask for? "))
while (ask not in plyrsHand):
ask = int(input("You can only ask for a value in your hand. Which card? "))
turn(ask, plyrsHand, compHand)
"""
deal(plyrsHand)
deal(compHand)
numBooksPlyr += checkBook(plyrsHand)
currTurn *= -1
print
#input("Press enter to continue...")
print
print ("Computer's Turn:")
ask = random.randint(0, len(compHand) - 1)
print ("Computer asks if you have any " + str(compHand[ask]) + "'s")
turn(compHand[ask], compHand, plyrsHand)
deal(compHand)
deal(plyrsHand)
numBooksComp += checkBook(compHand)
currTurn *= -1
print
#input("Press enter to continue...")
print
if (numBooksPlyr > numBooksComp):
print ("The Player Won!")
elif (numBooksComp > numBooksPlyr):
print ("The Computer Won!")
else:
print ("It was a tie.")
print ("Player's Books: " + str(numBooksPlyr))
print ("Computer's Books: " + str(numBooksComp))
RUN CODE ASSIGNMENT DOCS GRADE MORE 5 points Status: Not Submitted Computer Programming | Unit 8 Lab 4 This program will recreate the card game Go Fish. The main program has been completed, but the functions that support it still need to be finished. Rules of the game Goal: Obtain as many sets of 4-of-a-kind as possible. Game Play Each player is dealt 5 cards. Remaining cards are spread out in the middle. Moving clockwise, players take turns asking one other player in the group for a card of a particular rank. You can only ask for a rank that you currently have in your hand. For example: if you have a 7 in your hand you could ask, "Sarah, do you have any 75?" If you ask someone for a card, they must give up all the cards of that rank to you. Then you get to ask for another card from any of the players. If you ask someone for a card and they don't have any, they will say "Go Fish!", and you can pull one card from the table. . RUN CODE ASSIGNMENT DOCS | GRADE MORE If you ask someone for a card and they don't have any, they will say "Go Fish!", and you can pull one card from the table. . Once you have obtained a set of 4-of-a-kind, remove them from your hand and place them in front of you. If you ever run out of cards in your hand, pick up 5 new cards from the middle of the table. The game is over when there are no more cards on the table. The winner is the person who has the most complete sets of 4-of-a-kind. To Do: The following steps are numbered and correspond to the comments in the code. The comments will have the number and an a symbol to help find them easier. 1. Complete the createDeck() function. A deck of cards consists of 52 cards. There are 4 suits and 13 different values for each suit. For this lab, we will not really be using suits, but rather have 4 of the same of each value in the deck. The values will range from 2 to 14 (11, 12, 13, and 14 are really the Jack, Queen, King and Ace). We will use a list if integers to represent the deck. So our deck will have 4 2's, 43's. 4 4's, and so on. Here is the algorithm for creating a deck: Create an empty list for the deck Loop though the 4 suits (a loop that iterates 4 times) Loop through the 13 cards (integers) for each suit (2-14) Add the current card (integer) to the deck RUN CODE ASSIGNMENT DOCS GRADE | MORE Loop through the 13 cards (integers) for each suit (2-14) Add the current card (integer) to the deck Shuffle the list by calling the shuffle function. (list = shuffle(list)) Return the list for the deck 2. Complete the shuffle() function. A shuffled deck has a random order of the cards. This function will take in a list as a parameter and then return a shuffled version of that list. Here is the alogrithm for shuffling: Create an empty list to store the shuffled integers in While the length of the list parameter is greater than a Create a random integer from 8 to the last index of the list parameter Add the integer at the random index of the list parameter to the shuffled list Remove the integer at the random index of the list parameter from the list parameter Return the shuffled list RUN CODE ASSIGNMENT DOCS GRADE MORE 3. Complete the loop in the turn() function to search for the card being asked for. When a player is asked for a card, they must search for that card in their hand and give the other player all cards that match. To do this, we will be searching a list for a given value, incrementing a count of how many we found so far, removing it from one list (listSearching) and adding it to another (listPlaying) Since we are removing elements from the list and continuing to search it, we must search this list from the last index to the first index, otherwise we might skip an element Here is the alogrithm for searching: Loop backwards (last to first) through the indices of listSearching. Check if the card being asked for is the same as the card at the current index. If so, Increment the count Remove the card being asked for from list Searching Add the card being asked to listPlaying RUN CODE ASSIGNMENT DOCS GRADE MORE 4. Complete the else part of the if statement in turn() that makes the player go fish if it is not in the other player's hand. If a player asks for a card, but the other player does not have it, then they have to pick the next card from the deck. This next card is always the first card in the deck. Hint: what index always has the first element in a list? Here is the alogrithm for searching: Check if there are cards left in the deck, if so Check if it is the player's turn (this is done for you), if so Display that first card from the deck has been chosen Add the first card from the deck to listPlaying Remove the first card from the deck RUN CODE ASSIGNMENT DOCS GRADE MORE Remove the first card from the deck 5. Complete the checkBook() function. The player's have a book if they have 4 of the same value in their hand. If they do, they rmeove these cards from thewir hand and increment how many books they have. This function has a list parameter, hand, that is the current player's hand being checked. Here is the alogrithm for looking for a book in a hand: Initialize a book variable to e For each card in the hand Get the count for how many times that current card apears in the hand Check if the count is equal to 4, if so Loop 4 times Remove the current card from the hand Set the book variable to 1 Display a message that they got a book of the card's value Return the value of the book variable RUN CODE ASSIGNMENT DOCS GRADE MORE 6. Complete the deal() function. Each time a player has no cards left, they are dealt 5 cards or how many cards are left in the deck if less than 5. (Hint to find the minimum bewteen 2 values, you can use the min() function. ex: min(x, y) will give you x if it is the lower value.) This function has a list parameter, hand, that is the current player's hand being dealt to. Here is the algorithm for dealing the first 5 cards from the deck: Check if the hand is empty, if so Loop for 5 or the number of cards left in the deck times, whichever is less Add the first card from the deck to the hand Remove the first card from the deck RUN CODE ASSIGNMENT DOCS GRADE MORE Example Output Player's Hand: [2, 6, 10, 12, 13] Player's Books: Computer's Books: Player's Turn: Player asks for any 6's No 6's. Go fish. You fished a(n) 3 Computer's Turn: Computer asks if you have any 8's No 8's. Go fish. Player's Hand: [2, 3, 6, 10, 12, 13] Player's Books: Computer's Books: Player's Turn: Player asks for any 6's No 6's. Go fish. You fished a(n) 9 RUN CODE ASSIGNMENT DOCS GRADE MORE Player's Turn: Player asks for any 6's No 6's. Go fish. You fished a(n) 3 Computer's Turn: Computer asks if you have any 8's No 8's. Go fish. Player's Hand: [2, 3, 6, 10, 12, 13] Player's Books: Computer's Books: Player's Turn: Player asks for any 6's No 6's. Go fish. You fished a(n) 9 Computer's Turn: Computer asks if you have any 2's 1 2 was given RUN CODE ASSIGNMENT DOCS GRADE MORE Player's Turn: Player asks for any 6's No 6's. Go fish. You fished a(n) 3 Computer's Turn: Computer asks if you have any 8's No 8's. Go fish. Player's Hand: [2, 3, 6, 10, 12, 13] Player's Books: Computer's Books: Player's Turn: Player asks for any 6's No 6's. Go fish. You fished a(n) 9 Computer's Turn: Computer asks if you have any 2's 1 2 was given Lab 08-04 Go Fish Save Submit + Continue # # *** 1 import random 2 3 4 - Your name: 5 Lab Name 6 Class # 7 - Date: 8 9 10 11 # 12 Functions 13 14 15 # @ 1. Returns a shuffled list of 52 integers to represent a deck of c. 16- def createDeck(): 17 # initialize a list for the deck 18 deck = [] 19 20 # **** YOUR CODE HERE **** 22 #Return the list for the deck 23 return deck 24 25 # @ 2. Takes a list as a parameter and mixes it up (shuffles it) 26. def shuffle(deck): 27 tan the hill, it rain 21 NNN 0 Lab 08-04 Go Fish Save Submit + Continue 25 # @ 2. Takes a list as a parameter and mixes it up (shuffles it) 26. def shuffle(deck): 27 # Create an empty list to store the shuffled integers in shuffled List = [] 28 29 30 # **** YOUR CODE HERE **** 31 32 # Return the shuffled list 33 return shuffled List 34 35 # Performs a players turn in the game. Searches listSearching for asko 36 # If it finds a match, it counts, removes the card from listSearching 37 # adds it to listPlaying. 38 - # Takes in parameters: 39 # 1. the integer for the card be

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions

Question

When insurance is fair, in a sense, it is also free.

Answered: 1 week ago