Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE USE PYTHON FOR THIS PROBLEM!! In a typical card game, each player gets a hand of cards. The deck is shuffled, and cards are

PLEASE USE PYTHON FOR THIS PROBLEM!!

In a typical card game, each player gets a hand of cards. The deck is shuffled, and cards are dealt one at a time from the deck and added to the players' hands. In some games, cards can be removed from a hand, and new cards can be added. The game is won or lost depending on the value (Ace, 2, 3, ..., King) of the cards that a player receives. Our card game will deal two hands of 2-5 cards per hand. Your program will determine who has the highest scoring hand.

The starting code provided:

includes five list:

Two lists with values assigned and three empty lists.

requires you to write algorithms to

Create a deck of cards

Shuffle a deck of cards

Create a hand from a deck of cards

Determine who is the winner in a game of cards.

In this program, called CardGame, you will create a deck of 52 cards, represented by the lists suitNames and rankNames. You will print out all the cards in the deck, shuffle the whole deck of cards (the elements in the list), print out the shuffled cards in the deck, create a hand of cards and print each hand. You will deal two hands of cards and determine who is the winner of the card game.

To create the deck of cards, use the suitNames and rankNames lists to initialize each String element in the 52-element list to a sequence of two characters, each representing suit and then rank.

For full credit, do NOT initialize each element with 52 different statements.

Be sure to print the shuffled and unshuffled deck.

Clarification: Cards 1-13 represent the clubs, 14-26 represent the hearts, then spades, then diamonds. In all suits, card identities ascend in step with the card number, with Ace being high: 13 is the 2 of hearts, 14 is the 3 of hearts, and 25 is the Ace of hearts.

deck[0] (before shuffling) represents the 2 of Clubs (C2)

deck[14] would be the 3 of Hearts (H3)

deck[25] would be the Ace of Hearts (HA)

Hint: use a nested for loop where suitNames controls the outer loop and rankNames controls the inner loop. Append the card abbreviations (3 characters max each) in the list called deck.

To shuffle a deck of cards the elements in the lists must continuously randomly generate an index number of the list and swap the element at that index with the first element (at index 0). Swap how many times you like, but choosing a number greater than ~50 would be good as the deck holds 52 elements.

Hint: using the range function to create a loop that will execute at least 50 times. Use the randInt() function within the Random class discussed in our class to create a random number between 0-51. Use this number to determine which indices will be used to swap its card with the card in indices 0

DO NOT USE ANY PREDEFINED METHODS TO SHUFFLE CARDS. THIS MUST BE YOUR OWN ALGORITHM. YOU MUST ADAPT THE ALGORITHM AS DESCRIBED ABOVE.

From the shuffled deck list, create a hand of cards by first prompting the user for the number of cards to deal between 2 and 5. You must check the user's input for the correct value. If the value is incorrect, tell the user it is incorrect and ask for a new number. Take the number of cards entered by the user and from the shuffled deck of cards place those number of cards in the (represented by lists, hand1 and hand2).

Hint: use the pop command to take from the shuffled deck of cards and place into the hand list.

Cards that were taken from the deck to put in the hand list must never be used again. You can do this by using the pop command to take the card off the shuffled list.

Always make sure you have cards left in the deck of cards when creating a hand. Again, keep track of the length of your list to see if you have enough cards in your deck to deal 2 hands.

note: the game should prompt the user to continue playing as long as there are cards in the deck (in accordance with the output below)

To determine who won the card game, add each card value together in hand1 to create a total hand1 value, then use the same algorithm to determine the total hand2 value. The hand with the highest score wins the game.

Create two variables to hold the total face value of each hand, and be sure to print out the hand values and indicate the winning hand for the user.

Hints:

Hint: first determine the card value by looking at the second character in the card. Example: hand1 = [H4,DK,S8,CA] hand1[0][1] is equal to 4, hand1[1][1] = K, hand1[2][1] =8 and hand1[3][1] = A

See the chart below for the value of the second character in the card. Keep in mind that the second character of the card is a character, you must cast to an int.

Second Character In Card

Card value

A

11

K,Q,J or 10

10

2 - 9

Number is card value

Below is starting code for the CardGame:

"""

put your name

write a description of the program

"""

import random

suitNames=["C","H","S","D"];

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

cardDeck = []

hand1 = []

hand2 = []

#create card deck

print(cardDeck)

#shuffle card deck

print(cardDeck)

#prompt user for number of cards in hand

#create hand1

print("Hand 1 ",hand1)

#create hand2

print("Hand 2 ",hand2)

#find hand value of hand 1

hand1total = 0

print(hand1total)

#find hand value of hand 2

hand2total = 0

print(hand2total)

#print the hand that has the highest value print Tie Score if score is the same

SEE SAMPLE OUTPUT BELOW

Sample Output:

Unshuffled deck of cards

2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD

Shuffled deck of cards (many different answers)

9S 3C 4H 5C 6C 7C 9H JS 2C 6S 8C 8D 9C 2H 3H KH 7H QS 4S 8H QC 10H JH AH 3D 7D 2S QH KC 6H 9D 7S 8S AC 10S JC 3S 4C AS 2D 10C 4D 5D 6D 5S KS 5H 10D JD QD KD AD

Hand1

hand1 = ['H10','CK','SA','CQ']

hand1total = 41

Hand2

hand2 = ['HK','D7','D9','S4']

Hand2total = 30

SUGGESTION I WOULD HARDCODE HAND1 AND HAND2 IN YOUR PROGRAM TO CHECK IF YOUR LOGIC IS WORKING CORRECTLY. REMOVE THE HARDCODED LISTS BEFORE YOU UPLOAD YOUR PROGRAM

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

Database Theory Icdt 99 7th International Conference Jerusalem Israel January 10 12 1999 Proceedings Lncs 1540

Authors: Catriel Beeri ,Peter Buneman

1st Edition

3540654526, 978-3540654520

More Books

Students also viewed these Databases questions