Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Black Jack is a popular casino game played with a standard 52 card deck. A single player plays against the dealer. (The rules below vary

Black Jack is a popular casino game played with a standard 52 card deck. A single player plays against the "dealer". (The rules below vary slightly from the game as it is commonly played in casinos).

Basic rules: In the first part of the game, the player is dealt cards repeatedly until one of the following events happens:

The player's score (i.e. the sum of the values of the player's cards, explained below) reaches 21. In this case, the player wins immediately.

The player's score exceed 21. In this case, the player loses immediately.

The player decides to "stay", i.e. not take any more cards.

If the player has neither won or lost at this point, it is the dealer's turn to draw cards. The dealer keeps drawing cards until the sum of her card values is 17 or higher. Then she stops and the winner is determined as follows:

If the dealer's score reaches 21, she wins.

If the dealer's score exceeds 21, the player wins.

If the sum of the card values of the player and the dealer are equal, the game is a "push" and nobody wins.

Otherwise, the person with the highest score wins.

Card values: The card deck consists of 52 cards. There are four "suits": spades (), hearts (), diamonds () and clubs (). For each suit, there are 13 cards with different ranks: 9 number cards with the values 2-10, three "face" cards Jack ("J"), Queen("K"), and King ("K"), and an Ace card ("A"). In black jack, all face cards have the value 10. The value of an Ace card depends on the context in which the card is dealt. If the player is dealt an Ace, the card's value is 11 unless this would result in a total score of more than 21. In that case, the value of the Ace is 1. The same rule applies if it is the dealer's turn.

Download the skeleton code problem3.py.image text in transcribed

(a) Write the class Card, which represents a single playing card. The class should define two attributes, suit and rank. Both should be strings and they should be initialized with the parameters passed to the __init__(self, suit, value) method, so you can use the following line to create a new card: some_card = Card('','A') another_card = Card('','10') Write a suitable __str__(self) method for your class that returns a single string representing the card's suit and rank.

(b) Write the value(self,total) method of the Card class, that returns the game value of the card as an int, given the current score. If the card is a number card, that number should be the value. If it is a face card, the value should be 10. If it is an Ace, the value depends on the current score of either the player or the dealer, which is passed to the method as a parameter (1 or 11, see above).

(c) Outside of your Card class, write the function make_deck(), that returns a list of all possible 52 cards. Instead of listing each card in your code explicitly, use nested for loops to create the cards. Before returning the list, use the random.shuffle(some_list) method, which shuffles the element of some_list in place (i.e. it modifies some_list, rather than returning a new list).

(d) Write a main() function that plays a single game of Black Jack according to the rules specified above. First, get a new list of cards by calling the make_deck() method. To deal a card, remove the first element from the list. Keep track of the player's score. Call the card's value method to determine the value of the card. After each card, evaluate if the player has won or lost and otherwise ask her if she wants another card. If the player decides to "stay", perform the dealer's turn as described above (continue to use the same deck).

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 And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 1 Lncs 8055

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013 Edition

3642402844, 978-3642402845

More Books

Students also viewed these Databases questions

Question

Understand how customers respond to effective service recovery.

Answered: 1 week ago