Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help adjusting the provided Python Script file that will do the following: Take principles of showing Card Pictures and integrate it into the

I need help adjusting the provided Python Script file that will do the following: Take principles of showing Card Pictures and integrate it into the Black Jack game so that the user can see the cards instead of just normal text. Some of the GUI interface notes you should keep in mind are: There needs to be a button for "Stand" ie no more cards for the player. There needs to be a button for "Hit" ie give the player one card There needs to be a button for "Game Over" that ends the program There is one more addition to the game. On the "Game Over" button, the program will display a dialog box telling the user how many games they played, how many the won, how many they lost, and the number of 21s they scored. The dialog box will wait for the user to click OK before closing out the program entirely. You will need to consider and take into account what happens when the player or the dealer gets more and more cards. One helpful item on this is that absolute maximum they can have would be A-A-A-A-2-2-2-2-3-3-3-X. The X card would bust them no matter what at that point. Yes, you are going to let them "Hit" if they are on 21 but be nice and ask them are they Sure since they are on 21. Do not forget that when you start a hand, a single card from the Dealer is Face down and all the other cards are face up. When the player stands, you flip the face down card to face up. The Black Jack game has most of the Game Logic, you have to add in that GUI and the little bit to deal with counting hands, wins, losses and 21s. To ensure it to be working and functional for grading, include all the files and the appropriate folder structure when you zip up the program for submission. That even means adding in the images and folders needed. The script is to use solid programming practices like comments, self documenting variable names (also known as meaningful variable names) and easy to read and neat code. You are to place a comment block at the very top of the script containing your name, the semester, the due date of the exam, and the instructor's name each on separate lines. """ File: blackjack.py This module defines the Blackjack, Player, and Dealer classes. """ from cards import Deck, Card class Player(object): """This class represents a player in a blackjack game.""" def __init__(self, cards): self.cards = cards def __str__(self): """Returns string rep of cards and points.""" result = ", ".join(map(str, self.cards)) result += " " + str(self.getPoints()) + " points" return result def hit(self, card): self.cards.append(card) def getPoints(self): """Returns the number of points in the hand.""" count = 0 for card in self.cards: if card.rank > 9: count += 10 elif card.rank == 1: count += 11 else: count += card.rank # Deduct 10 if Ace is available and needed as 1 for card in self.cards: if count <= 21: break elif card.rank == 1: count -= 10 return count def hasBlackjack(self): """Dealt 21 or not.""" return len(self.cards) == 2 and self.getPoints() == 21 class Dealer(Player): """Like a Player, but with some restrictions.""" def __init__(self, cards): """Initial state: show one card only.""" Player.__init__(self, cards) self.showOneCard = True def __str__(self): """Return just one card if not hit yet.""" if self.showOneCard: return str(self.cards[0]) else: return Player.__str__(self) def hit(self, deck): """Add cards while points < 17, then allow all to be shown.""" self.showOneCard = False while self.getPoints() < 17: self.cards.append(deck.deal()) class Blackjack(object): def __init__(self): self.deck = Deck() self.deck.shuffle() self.player = Player([self.deck.deal(), self.deck.deal()]) self.dealer = Dealer([self.deck.deal(), self.deck.deal()]) def play(self): print("Player: ", self.player) print("Dealer: ", self.dealer) while True: choice = input("Do you want a hit? [y/n]: ") if choice in ("Y", "y"): self.player.hit(self.deck.deal()) points = self.player.getPoints() print("Player: ", self.player) if points >= 21: break else: break playerPoints = self.player.getPoints() if playerPoints > 21: print("You bust and lose") else: self.dealer.hit(self.deck) print("Dealer: ", self.dealer) dealerPoints = self.dealer.getPoints() if dealerPoints > 21: print("Dealer busts and you win") elif dealerPoints > playerPoints: print("Dealer wins") elif dealerPoints < playerPoints and playerPoints <= 21: print("You win") elif dealerPoints == playerPoints: if self.player.hasBlackjack() and not self.dealer.hasBlackjack(): print("You win") elif not self.player.hasBlackjack() and self.dealer.hasBlackjack(): print("Dealer wins") else: print("There is a tie") def main(): """Instantiate the model and play the game.""" game = Blackjack() game.play() if __name__ == "__main__": main()

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

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions