Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem For this lab you will complete a program to play a very simplified version of the card game cribbage!. The game is played with
Problem For this lab you will complete a program to play a very simplified version of the card game cribbage!. The game is played with a standard 52-card deck. During the game, two players are both dealt? five cards from the deck according to the rules (outlined in part 3). The player that has the hand with the most points is the winner. In this exercise, you will use nested lists to represent cards within the deck and within each player's hand. For example, the list: [['diamonds', 7], ['hearts', 11], ['clubs', 8], ['spades', 5], ['spades', 1]] would be used to represent a player with a hand containing the 7 of diamonds, the jack of hearts, the 8 of clubs, the 5 of spades, and the ace of spades. That is, each card is represented with a two-element list where the first element is a string specifying the suit and the second element is an integer representing the card value. The deck and player hands are lists containing these two- element card lists as elements. For this lab, you will need to complete 3 functions: . deal_card (deck, hand) score_hand (hand) play (shuffled deck) Both deal card and score_hand are helper functions that you will call from within play to execute the game. Additionally, you are provided two additional helper functions to help you complete and test your code: generate deck () shuffle (deck) You do not need to edit these functions. They are provided to help you test and complete your code. generate_deck will create and return a list representing a standard 52-card deck. Each element of the returned list will be a two-element list. The first element containing a string representing the suit (spades, clubs, diamonds, or hearts) and the second element will be an integer between 1 and 13 inclusive, representing the card value. Values 2-10 represent number cards, 1 represents an ace, 11 a jack, 12 a queen, and 13 a king. Calling the shuffle function will return a random permutation of the cards. We will break down how to complete the different functions in the steps below. For the first part of the lab, you will complete the deal_card helper function. By completing this function, you will become more familiar with the concept of aliasing and why you need to be careful when modifying lists passed as inputs to functions! This function takes two lists as arguments. The first input list represents the game's deck of cards and the second represents a player's hand. The function should remove the first card from the deck list and append it to the end of the list representing the player's hand. Note this function should return None. That is, your function should not return either of the modified lists. This is weird, and you may be wondering how the modified lists get passed back to the code that called the function. The answer is that they do not need to be passed back and the reason for this lies in the aliasing property of lists. Aliasing is tricky and (spoiler alert!) this will be an important concept to understand in the remainder of APS106. After implementing the function, try to explain to yourself how and why this function works. Discuss it with your TAS and colleagues to see if your understanding is correct. The following code snippet illustrates the behaviour of the function. deck = [['spades',10],['hearts',2],['clubs',8]] # deck with 3 cards player_hand = [['diamonds',3]) # list representing a player's hand, currently with a single card print("Deck and hand before deal hand function call") print("\tdeck : ", deck) print("\thand : ",player_hand) deal_card (deck,player_hand) # Notice no equals sign! Nothing assigned from the function return print(" Deck and hand after deal_hand function call") print("\tdeck : ", deck) print("\thand : ",player_hand) Printed Output Deck and hand before deal hand function call deck : [['spades', 10], ['hearts', 2], ['clubs', 8]] hand : [['diamonds', 3]] Deck and hand after deal hand function call deck : [['hearts', 2], ['clubs', 8]] hand : [['diamonds', 3], ['spades', 10]] Notice that after the function call, the first element from the deck (the 10 of spades) list has been removed and has been appended to the end of the hand list. You may assume that the deck list will always have a minimum of one card. Part 2: Score Hand Is this part, you will complete the function score_hand (hand) which calculates the score for the list of five cards in a player's hand. The input parameter to this function is a list of five cards representing a player's hand. You may assume that the list input to the function will always contain five cards. The score will be calculated according to the hand scoring rules of cribbage, with very slight modifications. The score_hand function should calculate points according to the following: 1. Each pair (i.e. same card value) scores 2 points. If a hand contains three or four of a kind, 2 points are scored for every combination of pairs. So three of a kind scores 6 points and four of a kind scores 12 points. 2. If all five cards are the same suit, 5 points are scored" 3. A group of three cards with consecutive values (called a run or straight) scores three points. The suit of the cards does not matter. A run of four consecutive values scores 4 points and a run of five consecutive values scores 5 points. Other points regarding runs: a. Points are scored for every unique set of cards that produces a run. For example, the hand: ace of hearts, ace of spaces, two of diamonds, three of hearts, and three of spades would score 12 points for runs because the run 1-3 can be constructed with four distinct sets of cards (note the total score for the hand would be 16 as there are also two pairs in the hand). b. For the purposes of defining runs aces, jacks, queens, and kings can be interpreted as the values 1, 11, 12, and 13, respectively. c. A run cannot wrap around from a king to an ace (i.e. A queen, king, and an ace would not be considered a run) d. Points are not awarded for shorter runs within a longer run. For example, in a run of four cards, no points would be awarded for the two runs of three within the hand. 4. All combinations of cards that sum to 15 are worth 2 points. When summing card combinations, aces are counted as one and jacks, queens, and kings are counted as 10. Sample test cases: Points 4 Description 2 + 3 + 10 = 15 => 2 pts 5 + 10 = 15 => 2 pts Hand 10 of hearts 2 of hearts 3 of hearts 6 of hearts 5 of diamonds For the final part of the lab, you will need to complete the play function. This function should utilize both the functions you completed in parts 1 and 2. The rules for playing the game are as follows: 1. Deal 5 cards to both players in alternating order. That is, player 1 gets the first, third, fifth, seventh, and ninth card in the deck and player2 gets the second, fourth, sixth, eighth, and tenth card from the deck. 2. Calculate the score for both players 3. The player with the high score wins the game. If the scored is tied, neither player wins. The function should return a three-element list. The first element should be one of the three following strings identifying the outcome of the game: . "player1 wins" "player2 wins" "tie" The second element should be the score of player 1 and the third element should be the score of player 2. Examples: If playerl has a score of 14 and player 2 has a score of 5, the list returned from the function should be: ['playerl wins', 14, 5] If player 1 has a score of 2 and player 2 has a score of 10, the list returned from the function should be: ['player2 wins', 2, 10] If player 1 has a score of 4 and player 2 has a score of 2, the list returned from the function should be: ['tie', 2, 2] You can test your code with randomly shuffled decks using the following snippet of code deck = generate_deck() deck = shuffle (deck) print("deck: ", deck[:10]) # just print the first cards in the deck Problem For this lab you will complete a program to play a very simplified version of the card game cribbage!. The game is played with a standard 52-card deck. During the game, two players are both dealt? five cards from the deck according to the rules (outlined in part 3). The player that has the hand with the most points is the winner. In this exercise, you will use nested lists to represent cards within the deck and within each player's hand. For example, the list: [['diamonds', 7], ['hearts', 11], ['clubs', 8], ['spades', 5], ['spades', 1]] would be used to represent a player with a hand containing the 7 of diamonds, the jack of hearts, the 8 of clubs, the 5 of spades, and the ace of spades. That is, each card is represented with a two-element list where the first element is a string specifying the suit and the second element is an integer representing the card value. The deck and player hands are lists containing these two- element card lists as elements. For this lab, you will need to complete 3 functions: . deal_card (deck, hand) score_hand (hand) play (shuffled deck) Both deal card and score_hand are helper functions that you will call from within play to execute the game. Additionally, you are provided two additional helper functions to help you complete and test your code: generate deck () shuffle (deck) You do not need to edit these functions. They are provided to help you test and complete your code. generate_deck will create and return a list representing a standard 52-card deck. Each element of the returned list will be a two-element list. The first element containing a string representing the suit (spades, clubs, diamonds, or hearts) and the second element will be an integer between 1 and 13 inclusive, representing the card value. Values 2-10 represent number cards, 1 represents an ace, 11 a jack, 12 a queen, and 13 a king. Calling the shuffle function will return a random permutation of the cards. We will break down how to complete the different functions in the steps below. For the first part of the lab, you will complete the deal_card helper function. By completing this function, you will become more familiar with the concept of aliasing and why you need to be careful when modifying lists passed as inputs to functions! This function takes two lists as arguments. The first input list represents the game's deck of cards and the second represents a player's hand. The function should remove the first card from the deck list and append it to the end of the list representing the player's hand. Note this function should return None. That is, your function should not return either of the modified lists. This is weird, and you may be wondering how the modified lists get passed back to the code that called the function. The answer is that they do not need to be passed back and the reason for this lies in the aliasing property of lists. Aliasing is tricky and (spoiler alert!) this will be an important concept to understand in the remainder of APS106. After implementing the function, try to explain to yourself how and why this function works. Discuss it with your TAS and colleagues to see if your understanding is correct. The following code snippet illustrates the behaviour of the function. deck = [['spades',10],['hearts',2],['clubs',8]] # deck with 3 cards player_hand = [['diamonds',3]) # list representing a player's hand, currently with a single card print("Deck and hand before deal hand function call") print("\tdeck : ", deck) print("\thand : ",player_hand) deal_card (deck,player_hand) # Notice no equals sign! Nothing assigned from the function return print(" Deck and hand after deal_hand function call") print("\tdeck : ", deck) print("\thand : ",player_hand) Printed Output Deck and hand before deal hand function call deck : [['spades', 10], ['hearts', 2], ['clubs', 8]] hand : [['diamonds', 3]] Deck and hand after deal hand function call deck : [['hearts', 2], ['clubs', 8]] hand : [['diamonds', 3], ['spades', 10]] Notice that after the function call, the first element from the deck (the 10 of spades) list has been removed and has been appended to the end of the hand list. You may assume that the deck list will always have a minimum of one card. Part 2: Score Hand Is this part, you will complete the function score_hand (hand) which calculates the score for the list of five cards in a player's hand. The input parameter to this function is a list of five cards representing a player's hand. You may assume that the list input to the function will always contain five cards. The score will be calculated according to the hand scoring rules of cribbage, with very slight modifications. The score_hand function should calculate points according to the following: 1. Each pair (i.e. same card value) scores 2 points. If a hand contains three or four of a kind, 2 points are scored for every combination of pairs. So three of a kind scores 6 points and four of a kind scores 12 points. 2. If all five cards are the same suit, 5 points are scored" 3. A group of three cards with consecutive values (called a run or straight) scores three points. The suit of the cards does not matter. A run of four consecutive values scores 4 points and a run of five consecutive values scores 5 points. Other points regarding runs: a. Points are scored for every unique set of cards that produces a run. For example, the hand: ace of hearts, ace of spaces, two of diamonds, three of hearts, and three of spades would score 12 points for runs because the run 1-3 can be constructed with four distinct sets of cards (note the total score for the hand would be 16 as there are also two pairs in the hand). b. For the purposes of defining runs aces, jacks, queens, and kings can be interpreted as the values 1, 11, 12, and 13, respectively. c. A run cannot wrap around from a king to an ace (i.e. A queen, king, and an ace would not be considered a run) d. Points are not awarded for shorter runs within a longer run. For example, in a run of four cards, no points would be awarded for the two runs of three within the hand. 4. All combinations of cards that sum to 15 are worth 2 points. When summing card combinations, aces are counted as one and jacks, queens, and kings are counted as 10. Sample test cases: Points 4 Description 2 + 3 + 10 = 15 => 2 pts 5 + 10 = 15 => 2 pts Hand 10 of hearts 2 of hearts 3 of hearts 6 of hearts 5 of diamonds For the final part of the lab, you will need to complete the play function. This function should utilize both the functions you completed in parts 1 and 2. The rules for playing the game are as follows: 1. Deal 5 cards to both players in alternating order. That is, player 1 gets the first, third, fifth, seventh, and ninth card in the deck and player2 gets the second, fourth, sixth, eighth, and tenth card from the deck. 2. Calculate the score for both players 3. The player with the high score wins the game. If the scored is tied, neither player wins. The function should return a three-element list. The first element should be one of the three following strings identifying the outcome of the game: . "player1 wins" "player2 wins" "tie" The second element should be the score of player 1 and the third element should be the score of player 2. Examples: If playerl has a score of 14 and player 2 has a score of 5, the list returned from the function should be: ['playerl wins', 14, 5] If player 1 has a score of 2 and player 2 has a score of 10, the list returned from the function should be: ['player2 wins', 2, 10] If player 1 has a score of 4 and player 2 has a score of 2, the list returned from the function should be: ['tie', 2, 2] You can test your code with randomly shuffled decks using the following snippet of code deck = generate_deck() deck = shuffle (deck) print("deck: ", deck[:10]) # just print the first cards in the deck
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started