suits = ['♠', '♥', '♦', '♣'] colors = ['black', 'red', 'red', 'black'] ranks = "A23456789TJQK" class Card: """A card is not an agent itself, but it manages two agents which are always next to each other, representing the card as something like '♠A'. """ display = False def __init__(self, position, index, hidden=False, selected=False): """When creating a card, you pass an index, which should be a number between 0 and 51. If you imagine a deck of playing cards in order (all the spades from A-K, then all the hearts from A-K, etc.), then the index is all we need to identify which card this is. See the CardSuit and CardRank classes below for the math we can use to convert an index value to a suit or a rank. """ self.index = index self.name = str(index) self.agents = [CardSuit(index, hidden, selected), CardRank(index, hidden, selected)] self.set_position(position) def __str__(self): return ''.join(a.character for a in self.agents) def __repr__(self): return f"" def value(self): """Returns how many points this card is worth. Currently treats aces as worth 1. Later we might need a way of considering that an ace could be worth 1 or 11 in blackjack. """ rank_index = self.index % 13 return rank_index + 1 def set_position(self, position): """When the Card's position gets set, it updates the position of its suit and rank. In this way, we have one agent (Card) which manages the positions of two agents on the screen. """ x, y = position suit, rank = self.agents self.position = position suit.position = position rank.position = (x+1, y) def set_z(self, z): suit, rank = self.agents suit.z = z rank.z = z def hide(self): for agent in self.agents: agent.hide() def show(self): for agent in self.agents: agent.show() def select(self): for agent in self.agents: agent.select() def deselect(self): for agent in self.agents: agent.deselect() class CardSuit: def __init__(self, index, hidden=False, selected=False): self.name = f"{index}_suit" self.index = index self.suit_color = colors[index // 13] self.hidden = hidden self.selected = selected self.update_display() def select(self): self.selected = True self.update_display() def deselect(self): self.selected = False self.update_display() def show(self): self.hidden = False self.update_display() def hide(self): self.hidden = True self.update_display() def update_display(self): if self.hidden: self.character = "▒" fg = "blue" else: self.character = suits[self.index //13] fg = self.suit_color if self.selected: bg = "yellow" else: bg = "gray" self.color = fg + "_on_" + bg class CardRank: def __init__(self, index, hidden=False, selected=False): self.name = f"{index}_rank" self.index = index self.suit_color = colors[index // 13] self.hidden = hidden self.selected = selected self.update_display() def select(self): self.selected = True self.update_display() def deselect(self): self.selected = False self.update_display() def show(self): self.hidden = False self.update_display() def hide(self): self.hidden = True self.update_display() def update_display(self): if self.hidden: self.character = "▒" fg = "blue" else: self.character = ranks[self.index % 13] fg = self.suit_color if self.selected: bg = "yellow" else: bg = "gray" self.color = fg + "_on_" + bg