Playing card class, written using enumsBasic playing card objects for gamesPlaying Card Class - is this...
Check if the digits in the number are in increasing sequence in python
What is the most triangles you can make from a capital "H" and 3 straight lines?
What kind of hardware implements Fourier transform?
What is the wife of a henpecked husband called?
Why did the villain in the first Men in Black movie care about Earth's Cockroaches?
Does Windows 10's telemetry include sending *.doc files if Word crashed?
Quenching swords in dragon blood; why?
Why did other German political parties disband so fast when Hitler was appointed chancellor?
How to acknowledge an embarrassing job interview, now that I work directly with the interviewer?
What is this metal M-shaped device for?
Broken patches on a road
Can I write a book of my D&D game?
What's the most convenient time of year to end the world?
Cryptic with missing capitals
Word or phrase for showing great skill at something without formal training in it
How do you funnel food off a cutting board?
Is a debit card dangerous for an account with low balance and no overdraft protection?
Avoiding morning and evening handshakes
Why do neural networks need so many training examples to perform?
Would these multi-classing house rules cause unintended problems?
Compress command output by piping to bzip2
A universal method for left-hand alignment of a sequence of equalities
If I delete my router's history can my ISP still provide it to my parents?
Slow moving projectiles from a hand-held weapon - how do they reach the target?
Playing card class, written using enums
Basic playing card objects for gamesPlaying Card Class - is this right?Weekend Challenge - Poker Hand EvaluationOptimizing “Poker hands” challenge solutionDetermining winner and winning hand in poker (holdem)Class to represent playing cardDetermine playing cardBeginnings of a Poker hand classifierUsing enums in a card/deck classPlaying card objectBasic playing card objects for games
$begingroup$
I wrote a card class a while back in this post here: Previous Question
I know it has been a long time but I recently came back to the project and wrote it using an enum as suggested in the answer to increase readability and to make input less error prone. I've never really used enums in Python so the question is did I do it correctly or is there a better way to do it?
from enum import Enum
class Suit(Enum):
CLUB, HEART, DIAMOND, SPADE = range(1, 5)
class Rank(Enum):
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)
JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'
class Card(object):
"""Models a playing card, each Card object will have a suit, rank, and weight associated with each.
possible_suits -- List of possible suits a card object can have
possible_ranks -- List of possible ranks a card object can have
Suit and rank weights are initialized by position in list.
If card parameters are outside of expected values, card becomes joker with zero weight
"""
def __init__(self, suit, rank, in_deck = False):
if suit in Suit and rank in Rank:
self.suit = suit
self.rank = rank
self.suit_weight = suit.value
self.rank_weight = rank.value
else:
self.suit = "Joker"
self.rank = "J"
self.suit_weight = 0
self.rank_weight = 0
self.in_deck = in_deck
def __str__(self):
"""Returns abbreviated name of card
Example: str(Card('Spades', 'A') outputs 'AS'
"""
return str(self.rank.value) + str(self.suit.name[0])
def __eq__(self, other):
"""Return True if cards are equal by suit and rank weight"""
return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight
def __gt__(self, other):
"""Returns true if first card is greater than second card by weight"""
if self.suit_weight > other.suit_weight:
return True
if self.suit_weight == other.suit_weight:
if self.rank_weight > other.rank_weight:
return True
return False
def modify_weight(self, new_suit_weight = None, new_rank_weight = None):
"""Modifies weight of card object"""
if new_suit_weight:
self.suit_weight = new_suit_weight
if new_rank_weight:
self.rank_weight = new_rank_weight
def is_in_deck(self):
"""Return True if card is in a deck, else false"""
return self.in_deck
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def get_suit_weight(self):
return self.suit_weight
def get_rank_weight(self):
return self.rank_weight
python object-oriented playing-cards enum
$endgroup$
add a comment |
$begingroup$
I wrote a card class a while back in this post here: Previous Question
I know it has been a long time but I recently came back to the project and wrote it using an enum as suggested in the answer to increase readability and to make input less error prone. I've never really used enums in Python so the question is did I do it correctly or is there a better way to do it?
from enum import Enum
class Suit(Enum):
CLUB, HEART, DIAMOND, SPADE = range(1, 5)
class Rank(Enum):
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)
JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'
class Card(object):
"""Models a playing card, each Card object will have a suit, rank, and weight associated with each.
possible_suits -- List of possible suits a card object can have
possible_ranks -- List of possible ranks a card object can have
Suit and rank weights are initialized by position in list.
If card parameters are outside of expected values, card becomes joker with zero weight
"""
def __init__(self, suit, rank, in_deck = False):
if suit in Suit and rank in Rank:
self.suit = suit
self.rank = rank
self.suit_weight = suit.value
self.rank_weight = rank.value
else:
self.suit = "Joker"
self.rank = "J"
self.suit_weight = 0
self.rank_weight = 0
self.in_deck = in_deck
def __str__(self):
"""Returns abbreviated name of card
Example: str(Card('Spades', 'A') outputs 'AS'
"""
return str(self.rank.value) + str(self.suit.name[0])
def __eq__(self, other):
"""Return True if cards are equal by suit and rank weight"""
return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight
def __gt__(self, other):
"""Returns true if first card is greater than second card by weight"""
if self.suit_weight > other.suit_weight:
return True
if self.suit_weight == other.suit_weight:
if self.rank_weight > other.rank_weight:
return True
return False
def modify_weight(self, new_suit_weight = None, new_rank_weight = None):
"""Modifies weight of card object"""
if new_suit_weight:
self.suit_weight = new_suit_weight
if new_rank_weight:
self.rank_weight = new_rank_weight
def is_in_deck(self):
"""Return True if card is in a deck, else false"""
return self.in_deck
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def get_suit_weight(self):
return self.suit_weight
def get_rank_weight(self):
return self.rank_weight
python object-oriented playing-cards enum
$endgroup$
add a comment |
$begingroup$
I wrote a card class a while back in this post here: Previous Question
I know it has been a long time but I recently came back to the project and wrote it using an enum as suggested in the answer to increase readability and to make input less error prone. I've never really used enums in Python so the question is did I do it correctly or is there a better way to do it?
from enum import Enum
class Suit(Enum):
CLUB, HEART, DIAMOND, SPADE = range(1, 5)
class Rank(Enum):
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)
JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'
class Card(object):
"""Models a playing card, each Card object will have a suit, rank, and weight associated with each.
possible_suits -- List of possible suits a card object can have
possible_ranks -- List of possible ranks a card object can have
Suit and rank weights are initialized by position in list.
If card parameters are outside of expected values, card becomes joker with zero weight
"""
def __init__(self, suit, rank, in_deck = False):
if suit in Suit and rank in Rank:
self.suit = suit
self.rank = rank
self.suit_weight = suit.value
self.rank_weight = rank.value
else:
self.suit = "Joker"
self.rank = "J"
self.suit_weight = 0
self.rank_weight = 0
self.in_deck = in_deck
def __str__(self):
"""Returns abbreviated name of card
Example: str(Card('Spades', 'A') outputs 'AS'
"""
return str(self.rank.value) + str(self.suit.name[0])
def __eq__(self, other):
"""Return True if cards are equal by suit and rank weight"""
return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight
def __gt__(self, other):
"""Returns true if first card is greater than second card by weight"""
if self.suit_weight > other.suit_weight:
return True
if self.suit_weight == other.suit_weight:
if self.rank_weight > other.rank_weight:
return True
return False
def modify_weight(self, new_suit_weight = None, new_rank_weight = None):
"""Modifies weight of card object"""
if new_suit_weight:
self.suit_weight = new_suit_weight
if new_rank_weight:
self.rank_weight = new_rank_weight
def is_in_deck(self):
"""Return True if card is in a deck, else false"""
return self.in_deck
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def get_suit_weight(self):
return self.suit_weight
def get_rank_weight(self):
return self.rank_weight
python object-oriented playing-cards enum
$endgroup$
I wrote a card class a while back in this post here: Previous Question
I know it has been a long time but I recently came back to the project and wrote it using an enum as suggested in the answer to increase readability and to make input less error prone. I've never really used enums in Python so the question is did I do it correctly or is there a better way to do it?
from enum import Enum
class Suit(Enum):
CLUB, HEART, DIAMOND, SPADE = range(1, 5)
class Rank(Enum):
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN = range(2,11)
JACK, QUEEN, KING, ACE = 'J', 'Q', 'K', 'A'
class Card(object):
"""Models a playing card, each Card object will have a suit, rank, and weight associated with each.
possible_suits -- List of possible suits a card object can have
possible_ranks -- List of possible ranks a card object can have
Suit and rank weights are initialized by position in list.
If card parameters are outside of expected values, card becomes joker with zero weight
"""
def __init__(self, suit, rank, in_deck = False):
if suit in Suit and rank in Rank:
self.suit = suit
self.rank = rank
self.suit_weight = suit.value
self.rank_weight = rank.value
else:
self.suit = "Joker"
self.rank = "J"
self.suit_weight = 0
self.rank_weight = 0
self.in_deck = in_deck
def __str__(self):
"""Returns abbreviated name of card
Example: str(Card('Spades', 'A') outputs 'AS'
"""
return str(self.rank.value) + str(self.suit.name[0])
def __eq__(self, other):
"""Return True if cards are equal by suit and rank weight"""
return self.suit_weight == other.suit_weight and self.rank_weight == other.rank_weight
def __gt__(self, other):
"""Returns true if first card is greater than second card by weight"""
if self.suit_weight > other.suit_weight:
return True
if self.suit_weight == other.suit_weight:
if self.rank_weight > other.rank_weight:
return True
return False
def modify_weight(self, new_suit_weight = None, new_rank_weight = None):
"""Modifies weight of card object"""
if new_suit_weight:
self.suit_weight = new_suit_weight
if new_rank_weight:
self.rank_weight = new_rank_weight
def is_in_deck(self):
"""Return True if card is in a deck, else false"""
return self.in_deck
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def get_suit_weight(self):
return self.suit_weight
def get_rank_weight(self):
return self.rank_weight
python object-oriented playing-cards enum
python object-oriented playing-cards enum
edited 5 hours ago
200_success
130k16153417
130k16153417
asked 6 hours ago
CE3601CE3601
235
235
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214582%2fplaying-card-class-written-using-enums%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214582%2fplaying-card-class-written-using-enums%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown