Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program will allow anywhere between 1 and 6 players (inclusive). Here is what yo

ID: 3795119 • Letter: P

Question

Program will allow anywhere between 1 and 6 players (inclusive). Here is what your output will look like:

Write code in python please

Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if the player gets 21 points.

Code so far: (DO NOT COPY AND PASTE CODE!)

Explanation / Answer

I have a different code for this assignment. You should format the output as you need. Place all the 3 files at the same folder and then try to execute.

--------------------------------------------------------------

games.py

class Player(object):
""" A player for a game. """
def __init__(self, name, score = 0):
self.name = name
self.score = score

def __str__(self):
rep = self.name + ": " + str(self.score)
return rep

def ask_yes_no(question):
"""Ask a yes or no question."""
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response

def ask_number(question, low, high):
"""Ask for a number within a range."""
response = None
while response not in range(low, high):
response = int(input(question))
return response

  
if __name__ == "__main__":
print("You ran this module directly (and did not 'import' it).")
input(" Press the enter key to exit.")

-----------------------------------------------------------------------------------------------------

cards.py

class Card(object):
""" A playing card. """
RANKS = ["A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"]
SUITS = ["c", "d", "h", "s"]

def __init__(self, rank, suit, face_up = True):
self.rank = rank
self.suit = suit
self.is_face_up = face_up

def __str__(self):
if self.is_face_up:
rep = self.rank + self.suit
else:
rep = "XX"
return rep

def flip(self):
self.is_face_up = not self.is_face_up
  
class Hand(object):
""" A hand of playing cards. """
def __init__(self):
self.cards = []

def __str__(self):
if self.cards:
rep = ""
for card in self.cards:
rep += str(card) + " "
else:
rep = "<empty>"
return rep

def clear(self):
self.cards = []

def add(self, card):
self.cards.append(card)

def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)

class Deck(Hand):
""" A deck of playing cards. """
def populate(self):
for suit in Card.SUITS:
for rank in Card.RANKS:
self.add(Card(rank, suit))

def shuffle(self):
import random
random.shuffle(self.cards)

def deal(self, hands, per_hand = 1):
for rounds in range(per_hand):
for hand in hands:
if self.cards:
top_card = self.cards[0]
self.give(top_card, hand)
else:
print("Can't continue deal. Out of cards!")

if __name__ == "__main__":
print("This is a module with classes for playing cards.")
input(" Press the enter key to exit.")

--------------------------------------------------------------------------------------------------------------

players.py

import cards, games   

class CardBJ(cards.Card):
""" A Blackjack Card. """
ACE_VALUE = 1

@property
def value(self):
if self.is_face_up:
v = CardBJ.RANKS.index(self.rank) + 1
if v > 10:
v = 10
else:
v = None
return v

class DeckBJ(cards.Deck):
""" A Blackjack Deck. """
def populate(self):
for suit in CardBJ.SUITS:
for rank in CardBJ.RANKS:
self.cards.append(CardBJ(rank, suit))
  

class HandBJ(cards.Hand):
""" A Blackjack Hand. """
def __init__(self, name):
super(HandBJ, self).__init__()
self.name = name

def __str__(self):
rep = self.name + ": " + super(HandBJ, self).__str__()
if self.total:
rep += "(" + str(self.total) + ")"
return rep

@property   
def total(self):
# if a card in the hand has value of None, then total is None
for card in self.cards:
if not card.value:
return None
  
# add up card values, treat each Ace as 1
t = 0
for card in self.cards:
t += card.value

# determine if hand contains an Ace
contains_ace = False
for card in self.cards:
if card.value == CardBJ.ACE_VALUE:
contains_ace = True
  
# if hand contains Ace and total is low enough, treat Ace as 11
if contains_ace and t <= 11:
# add only 10 since we've already added 1 for the Ace
t += 10   
  
return t

def is_busted(self):
return self.total > 21


class PlayerBJ(HandBJ):
""" A Blackjack Player. """
def is_hitting(self):
response = games.ask_yes_no(" " + self.name + ", do you want a hit? (Y/N): ")
return response == "y"

def bust(self):
print(self.name, "busts.")
self.lose()

def lose(self):
print(self.name, "loses.")

def win(self):
print(self.name, "wins.")

def push(self):
print(self.name, "pushes.")

  
class DealerBJ(HandBJ):
""" A Blackjack Dealer. """
def is_hitting(self):
return self.total < 17

def bust(self):
print(self.name, "busts.")

def flip_first_card(self):
first_card = self.cards[0]
first_card.flip()


class GameBJ(object):
""" A Blackjack Game. """
def __init__(self, names):
self.players = []
for name in names:
player = PlayerBJ(name)
self.players.append(player)

self.dealer = DealerBJ("Dealer")

self.deck = DeckBJ()
self.deck.populate()
self.deck.shuffle()

@property
def still_playing(self):
sp = []
for player in self.players:
if not player.is_busted():
sp.append(player)
return sp

def __additional_cards(self, player):
while not player.is_busted() and player.is_hitting():
self.deck.deal([player])
print(player)
if player.is_busted():
player.bust()

def play(self):
# deal initial 2 cards to everyone
self.deck.deal(self.players + [self.dealer], per_hand = 2)
self.dealer.flip_first_card() # hide dealer's first card
for player in self.players:
print(player)
print(self.dealer)

# deal additional cards to players
for player in self.players:
self.__additional_cards(player)

self.dealer.flip_first_card() # reveal dealer's first

if not self.still_playing:
# since all players have busted, just show the dealer's hand
print(self.dealer)
else:
# deal additional cards to dealer
print(self.dealer)
self.__additional_cards(self.dealer)

if self.dealer.is_busted():
# everyone still playing wins
for player in self.still_playing:
player.win()
else:
# compare each player still playing to dealer
for player in self.still_playing:
if player.total > self.dealer.total:
player.win()
elif player.total < self.dealer.total:
player.lose()
else:
player.push()

# remove everyone's cards
for player in self.players:
player.clear()
self.dealer.clear()
  

def main():
print("Welcome to Blackjack! ")
  
names = []
number = games.ask_number("How many players? (1 - 7): ", low = 1, high = 8)
for i in range(number):
name = input("Enter player name: ")
names.append(name)
print()
  
game = GameBJ(names)

again = None
while again != "n":
game.play()
again = games.ask_yes_no(" Do you want to play again?: ")


main()
input(" Press the enter key to exit.")

---------------------------------------------------------------------------------------

OUTPUT:

$python player.py
Welcome to Blackjack!

How many players? (1 - 7): '2'
Enter player name: '1'
Enter player name: '2'
()
1: Qh 5c (15)
2: 7d 5h (12)
Dealer: XX 4h

1, do you want a hit? (Y/N): 'y'
1: Qh 5c 8d (23)
('1', 'busts.')
('1', 'loses.')

2, do you want a hit? (Y/N): 'n'
Dealer: Ah 4h (15)
Dealer: Ah 4h Qc (15)
Dealer: Ah 4h Qc 9h (24)
('Dealer', 'busts.')
('2', 'wins.')

Do you want to play again?: 'n'


Press the enter key to exit.