简体   繁体   中英

Creating a Blackjack Game in Python

I'm new to programming and I'm joining here to ask questions, contribute (when I have more knowledge under my belt), and basically just learn and figure out if programming is right for me.

I am currently learning Python through a course on Udemy and I'm working on a milestone project to create a Blackjack game. The only rule is for me to use Object Oriented Programming and to create classes for things like the Card and Deck.

I've watched some tutorials on OOP, but I still do not feel confident in implementing them, so I was hoping I could share what I've written and hopefully have a better understanding through interactions here.

To start, I created a plan to break the project down into smaller tasks as it was overwhelming to me. Here's what I planned:

  1. Create a deck with 52 cards

  2. Assign numerical values to each card (Ace can be 1 or 11)

  3. Shuffle the deck

  4. Dealer and player are handed 2 cards each as a start

  5. Player is asked if he/she wants to hit or stand

  6. Dealer has to hit until he reaches 17

  7. Compare "points" to see who has more

  8. At any point, if anyone exceeds 21, that person loses

I am currently stuck at trying to figure out the first part. I'm basically trying to create a list with all 52 cards in it using OOP and for loops, but I can't seem to get things right.

I'm sharing my code below which was written in Python 2. Any ideas how to proceed?

Thank you, Paul

Edit #1: Thank you everyone for the comments and feedback. I think I am getting a little closer now. Can someone please let me know if I am doing this right and how I can print the list I used to create my deck such that I am able to see each of the 52 cards in a list? I tried using the str method but it appears I'm doing it wrongly.

import random

rank = [2,3,4,5,6,7,8,9,10,'Jack','Queen','King','Ace']
suit = ['Diamonds','Clubs','Hearts','Spade']

class Card(object):

    def __init__(self,rank,suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        return self.suit + self.rank

    def grab_suit(self):
        return self.suit

    def grab_rank(self):
        return self.rank

    def draw(self):
        print (self.suit + self.rank)

class Deck(object):
    def __init__(self):
        self.cards = []    
        for i in rank:
            for j in suit:
                self.cards.append(Card(i,j))

    def __str__(self):
        return self.cards()

Usually around here people like you to have a specific question that you're asking, rather than, "give me some pointers", so be prepared for complaints that this question is too broad. That said, I'll give you a couple pointers.

You've got suit and rank arrays defined outside of any class (also you've got their names flipped, but whatever). That's not OOP. I would put those inside the Deck class, since that's where they're used. Think of it this way: each Card doesn't need to know all the possible ranks and suits, it just needs to know its own rank and suit when it's created by the deck it belongs to.

I'm not sure what's going on with self.deck = self . What are you trying to do there? In the next line you've got self.deck = [] , and you're going to fill it with Card objects. That sounds ok, however, it's not a good idea to name a local variable the same as the name of the class it belongs to. That's going to get confusing. I would suggest calling it cards , because it's going to be an array full of cards that belong to the deck. Your loops look ok, but right here: self.deck.append(Card) you're not creating the Card correctly. The card needs to know what rank and suit you want to assign to that particular card. I'll let you look into how to do that yourself. Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM