def see_card(self, player, card): ''' Updates self.seen to include newly placed card by player. ''' if player not in range(c.NUM_PLAYERS): raise ValueError(c.red(c.PLAYER_NUMBER_ERR)) if not isinstance(card, Card): raise TypeError(c.red(c.CARD_CLASS_ERR)) if player != self.num: self.seen[player].append(card)
def __init__(self, rank, suit): if rank.upper() not in c.RANKS: raise ValueError(c.red(c.RANK_ERR)) if suit.upper() not in c.SUITS: raise ValueError(c.red(c.SUIT_ERR)) self.rank = rank.upper() self.suit = suit.upper() self.rank_strength = c.RANK_STRENGTHS[self.rank] self.suit_strength = c.SUIT_STRENGTHS[self.suit] # overall card strength = (4 * rank_strength + 1 * suit_strength) - 4 # -4 so that card strength ranges from 1 to 52, instead of 5 to 56. self.card_strength = (4 * self.rank_strength + 1 * self.suit_strength) - 4
def __init__(self, category): self.subitem_main_menu = category existing_item = str(input(f'Existing name: ')).strip().title() if CheckDuplication.check(self.subitem_main_menu, existing_item) is None: print(red('Not found or nonexistent')) else: new_item = str(input(f'Type the new name: ')).strip().title() print(f'Modifying an item from {self.subitem_main_menu}:') UpdatingDB(self.subitem_main_menu, existing_item, new_item)
def __init__(self, category, item): try: if CheckDuplication.check(category, item) is not True: print(red('Registry not in the system')) else: cursor = msdb.cursor() deleting_query = f"DELETE FROM {category} WHERE name='{item}'" cursor.execute(deleting_query) print(green('Successfully deleted.')) except: TryDBMessage.message()
def __init__(self, dealt, num, name=None): ''' dealt: a Deck of cards dealt to this player num: this player's player number, which ranges from 0 to 3 name: an optional name field that defaults to f'Player {num}' ''' if not isinstance(dealt, Deck): raise TypeError(c.red(c.DEALT_CARDS_ERR)) if num not in range(4): raise ValueError(c.red(c.PLAYER_NUMBER_ERR)) if name is not None and not isinstance(name, str): raise TypeError(c.red(c.NAME_ERR)) self.num = num self.name = name if name is not None else f'Player {num}' # split dealt cards up into a dict of {suit: cards} self.hand = dealt.get_cards_by_suit() # number of sets this player has won self.score = 0
def __init__(self, category, new_register): try: if CheckDuplication.check(category, new_register) is True: print(red('Already registered in the system')) else: cursor = msdb.cursor() insert = f"INSERT INTO {category} (Name) VALUES (%s)" record = new_register cursor.execute(insert, (record, )) msdb.commit() print( green( f' {new_register} successfully registered as a {category}' )) except: TryDBMessage.message()
def play_round(starter_num): ''' starter_num: number of first player to place a card for this round Play one round in bridge, where all 4 players place a card each. Returns player number of this round's winner. ''' global ROUNDS_PLAYED if starter_num not in range(4): raise ValueError(c.red(c.PLAYER_NUMBER_ERR)) pile = {} # starter (either Human / Bot) plays a card start_card = PLAYERS[starter_num].play_card(None) # start_card is added to pile pile[starter_num] = start_card # let Bots see card update_seen_card(starter_num, start_card) # go through next three players who need to play the same suit for num in range(starter_num + 1, starter_num + 4): # restart from 0 if player number > 3 num %= 4 if isinstance(PLAYERS[num], BridgeBot): time.sleep(1) # same process as above card = PLAYERS[num].play_card(round_suit=start_card.suit) pile[num] = card update_seen_card(num, card) winner = max(pile, key=lambda x: pile[x].get_bridge_strength(start_card.suit)) PLAYERS[winner].score += 1 ROUNDS_PLAYED += 1 return winner
def __init__(self, cards=None, sort_cards=False): ''' Creates a deck from optional input list of Cards. If empty list is passed, an empty Deck is created. If nothing is passed, a full sorted Deck is created. ''' FULL_DECK = [Card(rank, suit) for rank in c.RANKS for suit in c.SUITS] if cards is None: cards = FULL_DECK else: cards = sorted(cards) if sort_cards else cards if not isinstance(cards, list) or not all( isinstance(c, Card) for c in cards): raise TypeError(c.red(c.DECK_INPUT_ERR)) # UserList uses 'self.data' as variable name for built-in list functions self.data = cards
def play_card(self, round_suit=None): ''' round_suit is None if player is starting the round. Naive approach used - comments inline. ''' if round_suit is not None and round_suit.upper() not in c.SUITS: raise ValueError(c.red(c.SUIT_ERR)) # if starting the round, play a random non-trump card if possible if round_suit is None: # try to pick a card that's not in c.TRUMP_SUIT try: # only suits with at least 1 card choices = [ s for s in self.hand if self.hand[s] and s != c.TRUMP_SUIT ] random_suit = choice(choices) # except if only c.TRUMP_SUIT cards are left except IndexError: random_suit = c.TRUMP_SUIT c.TRUMP_STARTED = True card = self.hand[random_suit].pop_random_card() # if not starting the round else: # play highest card in sorted hand in round_suit if available if self.hand[round_suit]: card = self.hand[round_suit].pop(-1) # else play the smallest c.TRUMP_SUIT card if available elif self.hand[c.TRUMP_SUIT]: card = self.hand[c.TRUMP_SUIT].pop(0) c.TRUMP_STARTED = True # else play the smallest card in any other nonempty suit else: random_suit = choice([s for s in self.hand if self.hand[s]]) card = self.hand[random_suit].pop(0) print(c.yellow(f'{self.name} plays {card}')) return card
def play_card(self, round_suit=None): ''' Takes user input to play a card. round_suit is None if starting the round. ''' if round_suit is not None and round_suit.upper() not in c.SUITS: raise ValueError(c.red(c.SUIT_ERR)) # show user's cards on the screen (a dict of suits to cards) print('\nYour hand:\n') pprint(self.hand) # prompt user for input while True: user_input = input(c.USER_PROMPT).upper() # check input for type errors if not isinstance(user_input, str) or len(user_input) < 2: print(c.red(c.USER_INPUT_ERR)) continue user_rank = user_input[:-1] user_suit = user_input[-1] # check if inputs are valid ranks and suits if user_rank not in c.RANKS or user_suit not in c.SUITS: print(c.red(c.RANK_ERR)) print(c.red(c.SUIT_ERR)) continue # selected card card = Card(user_rank, user_suit) # error if user does not have that card if card not in self.hand[card.suit]: print(c.red(c.USER_NO_CARD_ERR)) continue # if user is starting the round if round_suit is None: # error if user is playing a trump card when not c.TRUMP_STARTED if card.suit == c.TRUMP_SUIT and not c.TRUMP_STARTED: print(c.red(c.USER_TRUMP_NOT_STARTED_ERR)) continue # if user is not starting the round and card.suit != round_suit if round_suit is not None and card.suit != round_suit: # error if user still has a card in round_suit if len(self.hand[round_suit]) > 0: print(c.red(c.USER_ROUND_SUIT_ERR)) print(c.red(f'Round suit: {c.SUITS[round_suit]}')) continue # if user plays a trump card, c.TRUMP_STARTED = True if card.suit == c.TRUMP_SUIT and not c.TRUMP_STARTED: c.TRUMP_STARTED = True # checks complete and chosen card is valid break # remove card from user's hand self.hand[card.suit].remove(card) print(c.yellow(f'\n{self.name} plays {card}')) return card