I followed a tutorial to code blackjack in python. It generates a deck, and plays one round of blackjack, then ends the program. Once the player and the dealer have received all of their cards, and the outcome is declared, I want to print all of the remaining cards in the deck to verify it is working properly. This way I can move on to playing with more than one deck confidently.
class Card:
def __init__(self,value,suit):
self.cost = value
self.value = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'][value-1]
self.suit = '♥♦♣♠'[suit]
def price(self):
if self.cost >= 10:
return 10
elif self.cost == 1:
return 11
return self.cost
def show(self):
print('_________')
print(f'| {self.value:<2} |')
print('| |')
print(f'| {self.suit} |')
print('| |')
print(f'| {self.value:>2} |')
print('_________')
import random
from card import Card
class Deck:
def __init__(self):
self.cards = []
def generate(self):
for i in range(1, 14):
for j in range(4):
self.cards.append(Card(i, j))
def draw(self, iteration):
cards = []
for i in range(iteration):
card = random.choice(self.cards)
self.cards.remove(card)
cards.append(card)
return cards
def count(self):
return len(self.cards)
def remaining(self):
from deck import Deck
class Player:
def __init__(self, isDealer, deck):
self.cards = []
self.isDealer = isDealer
self.deck = deck
self.score = 0
def hit(self):
self.cards.extend(self.deck.draw(1))
self.check_score()
if self.score > 21: #Useless
return 1
return 0
def deal(self):
self.cards.extend(self.deck.draw(1))
self.check_score()
if self.score == 21: #Useless
return 1
return 0
def check_score(self):
a_counter = 0
self.score = 0
for card in self.cards:
if card.price() == 11:
a_counter += 1
self.score += card.price()
while a_counter != 0 and self.score > 21:
a_counter -= 1
self.score -= 10
return self.score
def show(self):
if self.isDealer:
print("Dealer's Cards")
for i in self.cards:
i.show()
print("Score: " + str(self.score))
elif not self.isDealer:
print("Players Cards")
for i in self.cards:
i.show()
print("Score: " + str(self.score))
from deck import Deck
from player import Player
class Blackjack:
def __init__(self):
self.deck = Deck()
self.deck.generate()
self.player = Player(False, self.deck)
self.dealer = Player(True, self.deck)
def play(self):
self.player.deal()
self.dealer.deal()
self.player.hit()
self.dealer.show()
if self.dealer.check_score() == 11: #Don't forget about chips
print("Insurance?")
self.dealer.hit()
if self.player.check_score() == 21:
p_status = 1
else:
p_status = 0
if self.dealer.check_score() == 21:
d_status = 1
else:
d_status = 0
self.player.show()
if p_status == 1:
print("Player got Blackjack! Congrats!")
if d_status == 1:
self.dealer.show()
print("Dealer and Player got Blackjack! It's a push. (Tie)") #Won't work since dealer only has one card
return 1
cmd = ""
while cmd != "Stand":
bust = 0
cmd = input("Hit or Stand? ")
if cmd == "Hit":
bust = self.player.hit()
self.player.show()
if bust == 1:
print("Player busted. Good Game!")
return 1
print("\n")
self.dealer.show()
if d_status == 1:
print("Dealer got Blackjack! Better luck next time!")
return 1
while self.dealer.check_score() < 17:
if self.dealer.hit() == 1:
self.dealer.show()
print("Dealer busted. Congrats!")
return 1
self.dealer.show()
if self.dealer.check_score() == self.player.check_score():
print("It's a Push (Tie). Better luck next time!")
elif self.dealer.check_score() > self.player.check_score():
print("Dealer wins. Good Game!")
elif self.dealer.check_score() < self.player.check_score():
print("Player wins. Congradulations!")
self.deck.remaining()
b = Blackjack()
b.play()
Looks like a simple for loop will do the trick. All the other code in the deck class looks like it properly removes a card from the list. So:
def remaining(self):
print("Remaining cards in deck":)
for card in self.cards:
print(card)
Printing out the entire list as a whole is also a possibility.
print(self.cards)
In any case:
Just make sure that the card class has a __str__()
method.
def __str__(self):
return self.suit + self.value
This function gets called whenever a string representation of the class instance is requested. So, it can be used to return a string of what that representation must be.