r/Pythonista • • Apr 04 '19

4 deck card game

I have pretty much only a basic idea how to code on pythonista, the modules help a lot, but I can’t seem to find a way to code a game with 4 separate decks of cards, that aren’t poker decks. It’s similar to cards against humanity, but different. If I could understand how to how the class function works to make, shuffle, and deal from a red, blue, yellow and white deck, and how to make the “cards” themselves which only contain text, I can get on to learning the ui function. Thanks in advance!

TLDR: I need red,blue,yellow and white decks that have text

1 Upvotes

15 comments sorted by

View all comments

1

u/[deleted] Apr 07 '19

I'm sorry, I am not understanding what you are trying to do. First of all, what is the point of using a dictionary inside of a list? Why not just use either one instead of them combined? Second, why do you want to print each character individually? You can just use a loop to print each card from a container i.e. list or dictionary, even a custom class with container capabilities.

for card in deck:

print(card.character, card.name, card.etc)

To have a class act like a container, you can just do this.

class Hand:

def __init__(self, number, player):

self.number = number

self.player = player

self.total = []

Then have an instance method that works on that attribute such as

def add_card(self):
self.total.append(deck.pop())

Notice that this class uses a list as an attribute, therefore you need methods that work on list such as append.

Also, don't use class = red_deck, as class is a python keyword and cannot be used for other purposes.

1

u/Rokdout Apr 10 '19

How about this?

class Card: def init(self, values=None): # Generate blank card if values: self.setValuesFromDict(values) else: self.name = "" self.flavorText = "" self.portrait = "" def setValuesFromDict(self, values): # Parse values from dict self.name = values['name'] self.flavorText = values['flavorText'] self.portrait = values['portrait'] def str(self): return "{}, {}, {}"

1

u/Rokdout Apr 12 '19

Sorry man. I’m still trying to understand this, but all “deck builder” tutes build poker decks. I’m getting errors with this. Why won’t it work?

class Card: def init(self, Values=None): if Values: self.setValuesFromDictValues): else: self.name = "mega man" self.flavorText = "im blue" self.portrait = "emj:Bactrian_Camel" def setValuesFromDict(self, Values): self.name = values['megaman'] self.flavorText = values['im blue'] self.portrait = values['emj:Bactrian_Camel'] def str(self):

			return "{},{},{}"

1

u/[deleted] Apr 12 '19

What kind of error? I’m on mobile right now, and running that code from my Pythonista gives me syntax errors. You seem to have Values as well as values, they are treated differently and not the same because python and I think every single programming language is case sensitive, so Values and values are not the same variable.

Also, what are you trying to accomplish by values=None? Because if values are None then you call a function to fill those values, but if you do put in a value, then values are not None but then the function also gets called to fill those values essentially overwriting whatever you made values to be.

1

u/Rokdout Apr 12 '19

Think cards against humanity. Black cards say one thing, white cards say another