patternpythonMinor
War card game using classes
Viewed 0 times
warcardgameusingclasses
Problem
I would like some feedback to the code I wrote for the card game War. While programming it, I wanted to make classes that I could easily use for other card games too (hence why some functions are actually unused).
```
from random import shuffle
import time
class card:
def __init__(self, suit, rank):
self.suit = suit.lower() # make sure that the suit is spelled in all lowercase characters
self.rank = rank
def isBlackOrRed(self):
# Function to check if a card is black or red
# Hearts and Diamonds are red cards, all other are black cards
if self.suit == "hearts" or self.suit == "diamonds":
return "red"
else:
return "black"
def isFaceCard(self):
# Function to check if a card is a face card
# Face cards have the ranks 11, 12 and 13
# or, Jack, Queen and King
faceCards = [11, 12, 13]
for faceCard in faceCards:
if self.rank == faceCard:
return True
return False
def getDescription(self):
# Returns a two item list
return [self.rank, self.suit]
def __repr__(self):
# Returns a nice explanation of the card
# for example: "ace of diamonds"
rank = self.rank
if rank == 1:
rank = "ace"
elif rank == 11:
rank = "jack"
elif rank == 12:
rank = "queen"
elif rank == 13:
rank = "king"
return "{} of {}".format(rank, self.suit)
#############
class cardStack:
# Class for both decks and hands
def __init__(self, name=""):
# Optional name variable, by default it's empty
self.stack = []
self.name = name
def giveFullDeck(self):
# Gives the stack a full set of cards (52 cards)
suits = ["clubs", "diamonds", "hearts", "spades"]
for suit in suits:
for rank in range(1, 14):
self.stack.append(card(suit, rank)) # U
```
from random import shuffle
import time
class card:
def __init__(self, suit, rank):
self.suit = suit.lower() # make sure that the suit is spelled in all lowercase characters
self.rank = rank
def isBlackOrRed(self):
# Function to check if a card is black or red
# Hearts and Diamonds are red cards, all other are black cards
if self.suit == "hearts" or self.suit == "diamonds":
return "red"
else:
return "black"
def isFaceCard(self):
# Function to check if a card is a face card
# Face cards have the ranks 11, 12 and 13
# or, Jack, Queen and King
faceCards = [11, 12, 13]
for faceCard in faceCards:
if self.rank == faceCard:
return True
return False
def getDescription(self):
# Returns a two item list
return [self.rank, self.suit]
def __repr__(self):
# Returns a nice explanation of the card
# for example: "ace of diamonds"
rank = self.rank
if rank == 1:
rank = "ace"
elif rank == 11:
rank = "jack"
elif rank == 12:
rank = "queen"
elif rank == 13:
rank = "king"
return "{} of {}".format(rank, self.suit)
#############
class cardStack:
# Class for both decks and hands
def __init__(self, name=""):
# Optional name variable, by default it's empty
self.stack = []
self.name = name
def giveFullDeck(self):
# Gives the stack a full set of cards (52 cards)
suits = ["clubs", "diamonds", "hearts", "spades"]
for suit in suits:
for rank in range(1, 14):
self.stack.append(card(suit, rank)) # U
Solution
Following PEP 8, here are some things that can improve your code:
as @101001010100101010110101010010 has said in his answer too:
Use multiline strings for printing.
- Name classes with the CapWords convention. (instead of
cardStackuseCardStack)
- Use multiline comments for docstrings.
- Use inline comments sparingly. It seems like you are using it for everything except docstrings.
- Put all relevant "magic" definitions after
__init__such as__str__. (This is my opinion).
as @101001010100101010110101010010 has said in his answer too:
Use multiline strings for printing.
Context
StackExchange Code Review Q#131174, answer score: 2
Revisions (0)
No revisions yet.