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:
Create a deck with 52 cards
Assign numerical values to each card (Ace can be 1 or 11)
Shuffle the deck
Dealer and player are handed 2 cards each as a start
Player is asked if he/she wants to hit or stand
Dealer has to hit until he reaches 17
Compare "points" to see who has more
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()