Accessing a class/method from another program in Python - python

I have a program (blackjack.py) and it accesses another program's (cards.py and games.py) within its code. Most of this is from a book, so I'm having trouble understanding how it works.
Heres the code for cards.py:
class Card(object):
""" A playing card. """
RANKS = ["A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"]
SUITS = ["c", "d", "h", "s"]
def __init__(self, rank, suit, face_up = True):
self.rank = rank
self.suit = suit
self.is_face_up = face_up
def __str__(self):
if self.is_face_up:
rep = self.rank + self.suit
else:
rep = "XX"
return rep
def flip(self):
self.is_face_up = not self.is_face_up
class Hand(object):
""" A Hand of playing cards. """
def __init__(self):
self.cards = []
def __str__(self):
if self.cards:
rep = ""
for card in self.cards:
rep += str(card) + "\t"
else:
rep = "<empty>"
return rep
def clear(self):
self.cards = []
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)
class Deck(Hand):
""" A deck of playing cards. """
def populate(self):
for suit in Card.SUITS:
for rank in Card.RANKS:
self.add(Card(rank, suit))
def shuffle(self):
import random
random.shuffle(self.cards)
def deal(self, hands, per_hand = 1):
for round in range(per_hand):
for hand in hands:
if self.cards:
top_card = self.cards[0]
self.give(top_card, hand)
else:
print "Can't continue deal. Out of cards!"
if __name__ == "__main__":
print "This is a module with classes for playing cards."
raw_input("\n\nPress the enter key to exit.")
I'm writing an error check for the blackjack.py and I need to gather the number of cards that have been used so far. I think I can do that by accessing the number of values in cards[]. The problem is, I am not sure on how to do that.
This is all in theory though. I will include the `blackjack.py' code as well, so you all can see what I am trying to do, and help me determine if my logic is flawed.
blackjack.py code
Any and all input is appreciated.

While I'm not entirely clear on your intended structure here, you have a couple of options.
First, in order to use any functions or classes from cards.py in your blackjack.py module, you can use the import statement to import them. There are two styles of doing this:
# blackjack.py
import cards
Would give you access to everything in the cards module, and you'd call each function/class by prefacing it with cards.<name>. So, if you wanted to initialize an instance of the Deck class,
# blackjack.py
import cards
mydeck = cards.Deck()
The other way is the from <X> import <Y>, which gives you access to the functions and classes without having to add the prefix. Example:
# blackjack.py
from cards import Deck # or, to import everything, "from cards import *"
mydeck = Deck()
Either of these methods would give you an instance of the cards.Deck class.
Option 0
You can already tell this within your Deck class, since it subclasses from Hand, so every time you give a card it's taking out out of the Deck's cards attribute. Thus, the number of cards given out would simply be:
class Deck(Hand):
# ...
def number_cards_used(self):
return 52 - len(self.cards)
Alternatively, if you can't edit cards.py, you can simply get the number of cards left from your given Deck by:
# blackjack.py
def get_number_cards_used_from_deck(deck):
return 52 - len(deck.cards)
In use:
# blackjack.py
import cards
mydeck = cards.Deck()
# ...
# Do other operations with deck
# ...
cards_used = get_number_cards_used_from_deck(mydeck)
Option 1
If you can isolate all of and only those hands being played simultaneously, you can implement a method card_count:
class Hand(object):
# ...
# other code
# ....
def card_count(self):
return len(self.cards)
Then, if you have a list of all the hands, for example, you could do something like:
sum(map(lambda h: h.card_count(), list_of_hands))
Option 2
Within your Deck class, since it subclasses Hand, you could simply keep another running list of the cards that have been given out that would often be refreshed. This would look like:
class Deck(Hand):
# ...
# other code
# ...
def __init__(self):
self.populate()
self.used_cards = []
def give(self, card, other_hand):
self.used_cards.append(card)
super(Deck, self).give(card, other_hand)
def number_cards_used(self):
return len(self.used_cards)
There are other methods, of course.

Related

Python Blackjack Drawing a Card from a Deck Problem

Question: I am attempting to make a blackjack game. I am at the part where I am trying to make a player class with a hand size which contains an empty list. My intuition tells me that I need to be targeting the deck where the cards are and to use the append and the pop methods. I have already written a function in another class called the deal cards method. Am I able to use functions from other classes inside a class? Or do I have to come up with some way of my class targeting the deck and getting the cards?
# Work on the player class give them the ability to have a hand and to deal the
# cards into that hand
from random import shuffle
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return str(self.rank) + " of " + self.suit
class Deck:
deck = []
def __init__(self):
suits = "Hearts", "Diamonds", "Clubs", "Spades"
ranks = 2,3,4,5,6,7,8,9,10,"J","Q","K","A"
for suit in suits:
for rank in ranks:
card= Card(rank,suit)
self.deck.append(card)
# need to be able to deal cards
def deal_card(self):
dealt_card = self.deck.pop()
return dealt_card # return gives back the value to whomever called it.
# needs to be able to shuffle
def shuffle(self):
shuffle(self.deck)
# display the deck
def display(self):
for card in self.deck:
print(card)
class Player:
def __init__(self, name, isdealer):
self.name = name
self.hand = []
self.isdealer = isdealer
def draw_cards(self):
player_cards = dealt_card();
#scratchpad thinking area
# self.hand is an empty list. I want to get cards from the deck and put them in that list! SO I need to target
# the deck somehow.
# maybe with pop?
# I would use append to add the result to my hand.
# Work on the player class give them the ability to have a hand and to deal the
# cards into that hand
def main(): # main can be called blackjack or gameplay
# Welcome the player and explain the rules
print("""Welcome to Blackjack! Here are the Rules
Try to get as close to 21 without going over.
Kings, Queens, and Jacks are worth 10 points.
Aces are worth 1 or 11 points.
Cards 2 through 10 are worth their face value.
(H)it to take another card.
(S)tand to stop taking cards.
The dealer stops hitting at 17""")
# Run a game of blackjack
# create a deck of cards outside of the main.
deck = Deck()
deck.shuffle()
deck.display()
# Make player 1 and the dealer
# while True:
# return cards to the deck
# Shuffle the deck of cards close to the start to start a new game.
# Deal 2 cards to the players
# Loop: display hands and scores
# Ask them to hit or stand.
# Determine Winner
# If the program is run (instead of imported), run the game:
if __name__ == '__main__':
main()
I have tried using the function from the deck class but I am unsure I can use methods from other classes. I know that append can be used to add to strings. I know I need to target the deck somehow but it is in another class.
Am I able to use functions from other classes inside a class? Or do I have to come up with some way of my class targeting the deck and getting the cards?
There are several ways to approach this dilemma:
You could have a method in the Deck class that takes a player instance as argument. It then has access to both the deck and the player's hand
You could have a method in the Player class that takes the deck instance as argument. This is similar, but approaching it from the opposite side
You could create yet another class, like BlackJack that would have attributes for the deck and the players, and then its methods can access both deck and player without issue. This looks like a nicer solution, but it seems less compatible with the main function you have. It would mean that all of the logic in main would move into that new class.
In below code I have taken the first approach in the deal method.
I also took some other decisions:
Make Deck a subclass of list, because a deck is mostly like a list with maybe just one or two extra methods. This means a deck instance no longer needs a deck attribute, but can apply pop, append, ..etc directly to self.
Make Player a subclass of Deck, because a player is mostly like a deck (a "hand") with a name and a score. This means there is no more hand attribute, as self is that hand.
Add a value attribute to the Card class, which would calculate the value of the card (1 for Ace, ...etc) at construction.
Add a score method to the Player class which sums up the card values, adding 10 if the hand includes at least one ace and that would not bust the score.
I also implemented the game loop with hitting/standing/quitting, and reporting the winner
Maybe some of this might not be acceptable for the code challenge you might be taking, but I thought these were the right thing to do:
from random import shuffle
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
# Add a value to use in the blackjack scoring
self.value = rank if isinstance(rank, int) else (1 if rank == "A" else 10)
def __str__(self):
return f"{self.rank} of {self.suit}"
class Deck(list): # Inherit from list as a deck is mostly a list
def __init__(self):
suits = "Hearts", "Diamonds", "Clubs", "Spades"
ranks = 2,3,4,5,6,7,8,9,10,"J","Q","K","A"
for suit in suits:
for rank in ranks:
card = Card(rank, suit)
self.append(card)
def deal(self, player): # Pass player as argument so to act on deck & player
player.append(self.pop())
return player[-1]
def collect(self, player):
self.extend(player)
player.clear()
def __str__(self):
return ", ".join(map(str, self))
class Player(Deck): # Inherit from Deck, as a player really is a deck with a name
def __init__(self, name, isdealer):
self.name = name
self.isdealer = isdealer
def score(self):
total = 0
hasAce = False
for card in self:
total += card.value
hasAce = hasAce or card.value == 1
return total + 10 if hasAce and total < 12 else total
def __str__(self):
return f"{self.name} has {self.score()} ({super().__str__()})"
def main():
# Welcome the player and explain the rules
print("""Welcome to Blackjack! Here are the Rules
Try to get as close to 21 without going over.
Kings, Queens, and Jacks are worth 10 points.
Aces are worth 1 or 11 points.
Cards 2 through 10 are worth their face value.
(H)it to take another card.
(S)tand to stop taking cards.
The dealer stops hitting at 17
""")
deck = Deck()
# Make player 1 and the dealer
player = Player(input("What's your name? "), False)
dealer = Player("Dealer", True)
while True:
# return cards to the deck
deck.collect(player)
deck.collect(dealer)
shuffle(deck)
# Deal 2 cards to the players
deck.deal(player)
deck.deal(player)
deck.deal(dealer)
deck.deal(dealer)
# Loop: display hands and scores
print()
print("New game:")
print(dealer)
print(player)
# Ask them to hit or stand.
while player.score() <= 21:
choice = input(f"{player.name}, do you want to [h]it, [s]tand or [q]uit? ").upper()
if choice == "Q":
return
if choice == "S":
break
if choice == "H":
print(f"{player.name}, you receive {deck.deal(player)}")
print(player)
while dealer.score() <= 16:
print(f"{dealer.name} receives {deck.deal(dealer)}")
print(dealer)
# Determine Winner
if player.score() > 21 or player.score() <= dealer.score() <= 21:
print(f"{dealer.name} won this round")
else:
print(f"{player.name}, you won this round!")
if __name__ == '__main__':
main()

Problem debuging "list index out of range" error in Deck Class

I have a class here that makes a deck of cards. It has methods to deal a card and reset the deck to the original (in order and intact) state.
I get an error for line 23 out_card = self.cards[0]) when the program's grader runs it: IndexError: list index out of range
It says it is running an error when it tries to "deal" more than 52 cards. But when I try to run more than 52, it just returns a None type, like it's supposed to.
Not shown in code: the Card class. This is just a class I wrote whose __repr__ class turns Card(4, "C") to Card("4 of Clubs") for example.
import random
class Deck:
def __init__(self, shuffled = False):
cards = []
for suit in ["C", "D", "H", "S"]:
for rank in range(2,15):
cards.append(Card(rank, suit))
self.cards = cards
self.shuffled = shuffled
self.dealt_cards = 0
if self.shuffled == True:
random.shuffle(self.cards)
def shuffle(self):
random.shuffle(self.cards)
pass
def deal_card(self):
if self.dealt_cards <= 51:
out_card = self.cards[0]
if self.dealt_cards <= 50:
self.cards.pop(0)
self.dealt_cards += 1
return(out_card)
else:
return(None)
def __repr__(self):
return("deck(dealt " + str(self.dealt_cards) + ", shuffled=" + str(self.shuffled) + ")")
def reset(self):
self.cards = []
for suit in ["C", "D", "H", "S"]:
for rank in range(2,15):
cards.append(Card(rank, suit))
self.shuffled = False
self.dealt_cards = 0
Where could it go out of range? I couldn't find the problem. Python 3 for anyone wondering
I can't say exactly what raised the error, because it comes from the program I am taking's auto-grader. I can't say for sure, but I would think the code is something along the lines of:
for i in range(55):
print(my_deck.deal_card())
Make sure that you always use self when refering to class attributes from within the class methods.
Change the following in reset():
cards.append(Card(rank, suit))
To:
self.cards.append(Card(rank, suit))

How to create string representations of objects in a list?

I'm trying to create a game of Kings Cup (drinking game that involves cards) with Python. As it stands at the moment, when I print the deck and participants lists, it appears that the console is print out memory addresses. I would appreciate any help, pointers, tips, suggestions. Please and thank you.
import random
class Participant:
def __init__(self, name, gender):
self.name = name
self.gender = gender
self.cards = []
def drawCard(self):
self.cards += deck.pop()
def showCards(self):
print(self.name)
print(self.cards)
class Card:
def __init__(self, value, suite):
self.value = value
self.suite = suite
def getValue(self):
return self.value
def getSuite(self):
return self.suite
def __str__(self):
return ("{0} of {1}".format(self.value, self.suite))
deck = []
participants = []
def shuffleDeck():
for suite in ["Clubs", "Diamonds", "Hearts", "Spades"]:
for num in ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]:
deck.append(Card(num, suite))
random.shuffle(deck)
return deck
players = input("Enter the number of players: ")
numPlayers = int(players)
if numPlayers < 3:
print(players)
print("Not enough players.")
elif numPlayers > 12:
print(players)
print("That's too many players.")
else:
for player in range(numPlayers):
player += 1
name = input("Player %s's Name: " %player)
gender = input("Player %s's Gender: " %player)
participants.append(Participant(name, gender))
deck = shuffleDeck()
print(deck)
print(participants)
Define a representation for each instance. For example:
class Card:
def __init__(self, value, suite):
self.value = value
self.suite = suite
def getValue(self):
return self.value
def getSuite(self):
return self.suite
def __str__(self):
return ("{0} of {1}".format(self.value, self.suite))
def __repr__(self):
return ("{0} of {1}".format(self.value, self.suite))
Then
>> a = [Card(3,'Diamonds'),Card(2,'Spades')]
>> print(a)
[3 of Diamonds, 2 of Spades]
That said, see #abarnerts comments about formatting repr
Python stores information in memory with pointers. When you create a variable, you say: Hey python, remember that this variable points to this piece of memory.
When you print a string or a number, python will auto format it for you, but when more complex objects are involved, it doesn’t know what to do.
To inform python what to do, you need to create the magic function __str__ (2 underscores on each sides) which you already did.
The problem is, when you print a list, you don’t print each individual objects, you print what the list represents of each objects. It’s more useful like that, because you’re not storing simple objects like strings or ints, but complex and defined structures.
To tell python what to represent, you need to create the magic function __repr__. I strongly suggest that you do not copy the __str__ function. It would be preferred to print something like Card(*value*, *suite*). In other words: your init function.
Gift code:
def __repr__(self):
return '{}({}, {})'.format(self.__name__, self.value, self.suite)

Defining Classes in Python

Create a class Deck that represents a deck of cards. Your class should have the following methods:
constructor: creates a new deck of 52 cards in standard order.
shuffle: randomnizes the order of the cards.
dealCard: returns a single card from the top of the deck and removes the card from the deck
cardsLeft: returns the number of cards remaining in the deck.
Test your program by having it deal out a sequence of n cards from a shuffle deck where n is the user input.
class Deck:
def __init__(self):
self.cardList=[]
for suit in ["d","c","h","s"]:
for rank in range(1,14):
card=PlayingCard(suit, rank)
self.cardList.append(card)
def shuffle(self):
#I DON'T KNOW HOW TO SHUFFLE MY CARDS PLEASE HELP.
#self.cardList[pos1] = self.cardList[pos2]
#self.cardList[pos2] = self.cardList[pos1]
#these two lines above are not working
def dealCard(self):
return self.cardList.pop()
def cardsLeft(self):
return len(self.cardList)
Read the docs on random.shuffle. It should help you greatly! :)
from collections import namedtuple
from random import shuffle
PlayingCard = namedtuple('PlayingCard', "suit rank")
class Deck:
def __init__(self):
self.cardList = [PlayingCard(suit, rank) for suit in"dchs" for rank in range(1,14)]
def shuffle(self):
shuffle(self.cardList)
def dealCard(self):
return self.cardList.pop()
def cardsLeft(self):
return len(self.cardList)
d = Deck()
d.shuffle()
print [d.dealCard() for _ in range(5)]

Deck card class in python

I am working on creating a class for the first time, and I am thinking that I have done every thing to get it run, but I still get bunch of issues which is 'list' object has no attribute 'shffule' so the problem here is it will not shuffle the cards, and it will not tell the remaining cards, can any one tell me what am I doing wrong? Thanks in advance
import random
class card_deck:
def __init__(self, rank, suite, card):
self.rank= rank
self.suite = suite
def ranks(self):
return self.rank
def suites(self):
return self.suite
def cards(self,card):
suit_name= ['The suit of Spades','The suit of Hearts', 'The suit of Diamonds','Clubs']
rank_name=['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']
def value(self):
if self.rank == 'Ace':
return 1
elif self.rank == 'Jack':
return 11
elif self.rank == 'Queen':
return 12
elif self.rank == 'King':
return 13
def shffule(self):
random.shuffle(self.cards)
def remove(self,card):
self.cards.remove(card)
def cardremaining(self):
self.suite-self.rank
def main():
try:
deck=[]
for i in ['Spades','Hearts', ' Diamonds','Clubs']:
for c in ['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']:
deck.append((c, i))
deck.shffule
hand = []
user =eval(input('Enter a number of cards: 1-7 '))
print()
while user <1 or user >7:
print ("Only a number between 1-7:")
return main()
for i in range(user):
hand.append(deck[i])
print (hand)
except ValueError:
print("Only numbers")
main()
Apart from your code containing many small errors; I will try to answer your main problems.
If you are going to use shffule[sic] method of card_deck class, then you first need to create an instance of that class(whereas you tried to call that method with a list). Like this:
deck = card_deck(some_rank,some_suit,some_card)
deck.shffule() #Now you can call the method
Now, since you made it a class instance, you cannot get items from it like hand.append(deck[i])
Unless you defined the method __getitem__ in your class definition, like this:
#this will be in your class definition
def __getitem__(self,i):
return self.card_list[i] #Of course you have to define a list of cards in your class too.
In my opinion, you should spend a little more time trying to understand how is a class defined, how does methods work and how to access members of a class. After that you will be doing much better here
You are never actually creating an instance of the card_deck class. The expression
deck=[]
creates a variable named deck referring to an empty list.
In python, [a, b, c,...] is the syntax for creating list literals.
from collections import namedtuple
Card = namedtuple('Card', 'sign, value') # no need to write class to represent card
SIGNS = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
class Deck:
def __init__(self):
self.cards = [Card(sign, value) for sign in SIGNS for value in range(2,
11) +
'J Q K A'.split()]
def __repr__(self):
return str([str(card) for card in self.cards])
def __len__(self):
return len(self.cards)
def __getitem__(self, item):
return self.cards[item]
def __setitem__(self, key, value):
self.cards[key] = value
deck = Deck()
print deck[11] # indexing works, prints Card(sign='Hearts', value='K')
print len(deck) # prints 52
print deck[13:16] # slicing works
import random
random.shuffle(deck) # shuffle works using no extra code

Categories