LeapZipBlog: Programmingassignmenthelper's blog: How Can We Do My Python Assignment for You?

How Can We Do My Python Assignment for You?

May 16, 2019 by Programmingassignmenthelper  

My very first assignment at University was to program a game of 21, although it was a cut down version. The assignment was in Pascal, so I have converted the code into Python to demonstrate a simple assignment. The basic rules of BlackJack (21, Pontoon) are that you have a deck of cards, and face cards count as 10 (Jack, Queen, King), and the Ace can count as 1 or 11. You see your cards, but not the dealers (you see one of the dealers cards) and you decide whether to stop where you are or to draw a new card. Once you have finished, the dealer takes their turn, and will draw if the value of the hand is less than 17. I’ve not included comments in the code, but I would do if it was a real assignment.

# Blackjack (21 or Pontoon)

import random

class Card:

    deck = []

    def __init__(self, rank, suit):
        if rank == "1":
            rank = "10"
        self.rank = rank
        self.suit = suit
        if rank in "JQK":
            self.val = 10
        elif rank == "A":
            self.val = 11
        else:
            self.val = int(rank)

    def __str__(self):
        return "%s%s" % (self.rank, self.suit)

    def __repr__(self):
        return str(self)

class Deck:

    def __init__(self):
        self.__deck = [Card(rank, suit) for suit in "CDHS" for rank in "234567891JQKA"]
        self.shuffle()

    def draw(self):
        card = self.__deck[self.__index]
        self.__index += 1
        return card

    def shuffle(self):
        random.shuffle(self.__deck)
        self.__index = 0

    def show_hand(self, hand):
        return "%s (%d)" % (",".join(str(card) for card in hand), self.score(hand))

    def score(self, hand):
        total = 0
        aces = 0
        for card in hand:
            total += card.val
            if card.val == 11:
                aces += 1
        while total > 21 and aces:
            total -= 10
            aces -= 1
        return total


def main():
    pot = 20
    while pot > 0:
        bet = int(input("You have %d available, how much do you want to bet? (0 to end) " % pot))
        if not bet:
            break
        if bet < 1:
            bet = 1
        if bet > pot:
            bet = pot
        deck = Deck()
        player = [deck.draw(), deck.draw()]
        dealer = [deck.draw(), deck.draw()]
        pot -= bet
        while True:
            print("You have:", deck.show_hand(player))
            print("Dealer shows:", dealer[0])
            move = input("(s)tick, (d)raw: ")
            if move in "sS":
                break
            if move in "dD":
                player.append(deck.draw())
            player_score = deck.score(player)
            if player_score > 21:
                break

        player_score = deck.score(player)
        if player_score > 21: