# PUT YOUR NAME HERE
# PUT YOUR SBU ID NUMBER HERE
# PUT YOUR NETID (BLACKBOARD USERNAME) HERE
#
# IAE 101 (Fall 2020)
# HW 3, Problem 1
# DON'T CHANGE OR REMOVE THIS IMPORT
from random import shuffle
# DON'T CHANGE OR REMOVE THESE LISTS
# The first is a list of all possible card ranks: 2-10, Jack, King, Queen, Ace
# The second is a list of all posible card suits: Hearts, Diamonds, Clubs, Spades
ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
suits = ["H", "D", "C", "S"]
# This class represents an individual playing card
class Card():
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
# DON'T CHANGE OR REMOVE THIS
# This function creates a string out of a Card for easy printing.
def __str__(self):
return "[" + self.suit + ", " + self.rank + "]"
# This class represents a deck of playing cards
class Deck():
def __init__(self):
self.cards = []
for r in ranks:
for s in suits:
c = Card(s,r)
self.cards.append(c)
# DON'T CHANGE OR REMOVE THIS
# This function will shuffle the deck, randomizing the order of the cards
# inside the deck.
# It takes an integer argument, which determine how many times the deck is
# shuffled.
def shuffle_deck(self, n = 5):
for i in range(n):
shuffle(self.cards)
# This function will deal a card from the deck. The card should be removed
# from the deck and added to the player's hand.
def deal_card(self, player):
player.hand.append(self.cards.pop())
# DON"T CHANGE OR REMOVE THIS
# This function constructs a string out of a Deck for easy printing.
def __str__(self):
res = "[" + str(self.cards[0])
for i in range(1, len(self.cards)):
res += ", " + str(self.cards[i])
res += "]"
return res
# This class represents a player in a game of Blackjack
class Player():
def __init__(self, name):
self.name = name
self.hand = []
self.status = True
def value(self):
summ,ct = 0,0
for i in range(len(self.hand)):
if self.hand[i].rank=='A':
ct += 1
continue
if self.hand[i].rank=='J' or self.hand[i].rank=='Q' or self.hand[i].rank=='K':
summ += 10
continue
summ += int(self.hand[i].rank)
for i in range(ct):
if summ>10:
summ += 1
else:
summ += 11
return summ
def choose_play(self):
if self.value()<17> return 'Hit'
self.status = False
return 'Stay'
# DON'T CHANGE OR REMOVE THIS
# This function creates a string representing a player for easy printing.
def __str__(self):
res = "Player: " + self.name + "\n"
res += "\tHand: " + str(self.hand[0])
for i in range(1, len(self.hand)):
res += ", " + str(self.hand[i])
res += "\n"
res += "\tValue: " + str(self.value())
return res
# This class represents a game of Blackjack
class Blackjack():
def __init__(self, players):
self.players = players
self.deck = Deck()
self.deck.shuffle_deck(10)
for _ in range(2):
for p in self.players:
self.deck.deal_card(p)
def play_game(self):
while True:
for p in self.players:
if p.status:
v = p.choose_play()
if v=='Hit':
self.deck.deal_card(p)
if p.value()>21:
p.status = False
print('Player',p.name,'has busted')
flag = 1
for p in self.players:
if p.status==True:
flag = 0
if flag:
break
# DON'T CHANGE OR REMOVE THIS
# This function creates a string representing the state of a Blackjack game
# for easy printing.
def __str__(self):
res = "Current Deck:\n\t" + str(self.deck)
res = "\n"
for p in self.players:
res += str(p)
res += "\n"
return res
# DO NOT DELETE THE FOLLOWING LINES OF CODE! YOU MAY
# CHANGE THE FUNCTION CALLS TO TEST YOUR WORK WITH
# DIFFERENT INPUT VALUES.
if __name__ == "__main__":
# Uncomment each section of test code as you finish implementing each class
# for this problem. Uncomment means remove the '#' at the front of the line
# of code.
# Test Code for your Card class
c1 = Card("H", "10")
c2 = Card("C", "A")
c3 = Card("D", "7")
print(c1)
print(c2)
print(c3)
print()
# Test Code for your Deck class
d1 = Deck()
d1.shuffle_deck(10)
print(d1)
print()
# Test Code for your Player class
p1 = Player("Alice")
p2 = Player("Bob")
d1.deal_card(p1)
d1.deal_card(p2)
print(p1.value())
print(p2.value())
d1.deal_card(p1)
d1.deal_card(p2)
print(p1.value())
print(p2.value())
d1.deal_card(p1)
d1.deal_card(p2)
print(p1.value())
print(p2.value())
print(p1)
print(p2)
print(p1.choose_play())
print(p2.choose_play())
print()
# Test Code for your Blackjack class
players = [Player("Summer"), Player("Rick"), Player("Morty"), Player("Jerry")]
game = Blackjack(players)
print(game)
game.play_game()
print(game)
select = {}
for p in game.players:
if p.value()<=21:
select[p.name] = p.value()
if not bool(select):
print('No winners')
else:
max_value = max(select.values())
winners = [name for name, v in select.items() if v == max_value]
if len(winners)==1:
print('Winner is',winners[0])
if len(winners)>1:
print('There is a tie between:')
for w in range(len(winners)-1):
print(winners[w],end=', ')
print('and',winners[-1])