def guest_menu(selected, card_id): """Try and access selected option in the guest menu (see guest_options list). Arguments: selected -- a valid integer matching an option from guest_options list card_id -- the card id or -1 if guest Returns: The card id or -1 if guest """ if selected_option == 0: exit_sbs() else: if selected_option == 1: current_card = Card(checksum_type="luhn") db.create_card_record(current_card.get_data()) current_card.created() elif selected_option == 2: card_id = login() if card_id != -1: print(constants.LOGIN_SUCCESS_MSG) else: print(constants.LOGIN_FAIL_MSG) else: print(constants.MENU_UNSUPPORTED_OPTION_MSG) return card_id
class TestCard(unittest.TestCase): def setUp(self): self.card = Card(5, '♥') def test_get_card(self): '''get_card() returns correct tuple''' self.assertEqual(self.card.get_card(), (5, '♥')) def test_to_string(self): '''to_string() returns correct string''' self.assertEqual(self.card.to_string(), '5♥ ')
def __init__(self): """ The constructor for the Deck class. It iterates through the enumerated card ranks and suits, creating a card object for each. """ deck = [] for suit in suits: for rank in range(2, 11): StandardCard = Card(suit, rank) deck.append(StandardCard) for value in face_cards: FaceCard = Card(suit, value) deck.append(FaceCard) self.cards = deck
def __init__(self): self.all_cards = [] for suit in suits: for rank in ranks: # create card object created_card = Card(suit, rank) self.all_cards.append(created_card)
def __init__ (self): self.cards = [] for suit in suits: for rank in ranks: self.cards.append(Card(suit, rank))
def reset_deck(self): self.cards = [] for suit in self.suits: for face in self.faces: new_card = Card(suit, face) self.cards.append(new_card) self.shuffle()
def __init__(self): ''' Initialize and shuffle deck ''' self.deck = [] for suit in ['♦', '♠', '♥', '♣']: for value in (list(range(2, 11)) + ['J', 'Q', 'K', 'A']): self.deck.append(Card(value=value, suit=suit)) shuffle(self.deck)
class TestCard(unittest.TestCase): def setUp(self): self.card = Card(1) def tearDown(self): del self.card def test_card_init(self): self.assertEqual(self.card.number, 1) def test_card_close_box(self): self.card.close_box(3) self.assertLess(self.card.field[3], 91) def test_card_is_empty(self): self.assertTrue(self.card.is_empty()) def test___str__(self): self.assertTrue(self.card.__str__())
def __init__( self, name='', number=1, who=0, ): self.number = number self.is_playing = True self.who = who # присваиваем имя игрока self.name = (name if name else 'Неизвестный ' + '#' + str(number)) # заполняем карточку игрока self.cards = Card() # можно потом сделать по нескольку карт на игрока
def __init__(self, pack=52): ''' deck init @param int pack ''' if pack == 52: ranks = [['Ace', 11, 'A'], ['Two', 2, '2'], ['Three', 3, '3'], ['Four', 4, '4'], ['Five', 5, '5'], ['Six', 6, '6'], ['Seven', 7, '7'], ['Eight', 8, '8'], ['Nine', 9, '9'], ['Ten', 10, '10'], ['Jack', 10, 'J'], ['Queen', 10, 'Q'], ['King', 10, 'K']] suits = [['Clubs', '♠'], ['Diamonds', '♦'], ['Hearts', '♥'], ['Spades', '♣']] for rank in ranks: for suit in suits: self.cards.append(Card(rank, suit))
def logged_in_menu(selected, card_id): """Try and access selected option in the logged in menu (see logged_in_options list). Arguments: selected -- valid integer matching an option from logged_in_options list card_id -- card id or -1 if guest Returns: The card id or -1 if guest """ if selected_option == 0: exit_sbs() else: # if found, the response contains the card data (id, number, pin, balance) response = db.get_card_data_by_id(card_id) current_card = Card(data=response) # show balance option if selected_option == 1: current_card.get_balance() # add income option elif selected == 2: add_income(current_card) # do transfer option elif selected == 3: do_transfer(current_card) # close card option elif selected == 4: print(constants.CARD_CLOSE_MSG) db.delete_card_record(current_card.id) card_id = -1 # logout option elif selected == 5: print(constants.LOGOUT_SUCCESS_MSG) card_id = -1 else: print(constants.MENU_UNSUPPORTED_OPTION_MSG) return card_id
def valid_number(number, number_length=16, algo="luhn"): """Check & return if a given card number passes the provided algorithm. Arguments: number -- the given card number to check age fail_msg -- the update fail message Keyword arguments: number_length -- the card number digits count algo -- the provided algorithm """ result = False if not number.isdigit(): return result if len(number) != number_length: return result data = (-1, number, 0000, -1) fake_card = Card(data=data) if algo == "luhn": check_digit = fake_card.number[-1:] to_check = fake_card.number[:len(fake_card.number) - 1] result = (check_digit == fake_card.luhn_algo(to_check)) return result
def do_transfer(card): """Attempt to do an amount transfer from one card to another. Arguments: card -- the card object """ print(constants.CARD_TRANSFER_MSG) receiver = str(input(constants.CARD_TRANSFER_NUMBER_MSG)) if not check_number(card, receiver): return False amount = str(input(constants.CARD_TRANSFER_AMOUNT_MSG)) if not check_amount(card, amount): return False is_successful = update_balance(card, int(amount) * -1, quiet=True) if is_successful: receiver_card = Card(data=db.get_card_data_by_number(receiver)) is_successful = update_balance(receiver_card, int(amount), quiet=True) if is_successful: print(constants.CARD_TRANSFER_AMOUNT_SUCCESS) else: print(constants.CARD_TRANSFER_AMOUNT_FAIL)
def test_vanilla(self): '''Vanilla case''' hand = Hand([Card(5, '♥'), Card('A', '♥')]) self.assertEqual(hand.get_score(), 16)
def test_weird_hand(self): '''Collapses previous single ace''' hand = Hand([Card(3, '♥'), Card('A', '♦'), Card(10, '♠'), Card(4, '♣'),]) self.assertEqual(hand.get_score(), 18)
def test_many_aces(self): '''Three aces and a five''' hand = Hand([Card(5, '♥'), Card('A', '♥'), Card('K', '♥'), Card('A', '♦'), Card('A', '♣')]) self.assertEqual(hand.get_score(), 18)
def test_pair_aces(self): '''Pair aces''' hand = Hand([Card('A', '♥'), Card('A', '♦')]) self.assertEqual(hand.get_score(), 12)
def setUp(self): self.card = Card(1)
def setUp(self): self.card = Card(5, '♥')
def initialize_board(): go = Card("Go", is_special=True) comm_chest = Card("Community Chest", is_community=True) luxury_tax = TaxCard("Luxury Tax", tax=100) income_tax = TaxCard("Income Tax", tax=200) chance = Card("Chance", is_chance=True) jail = Card("Jail/Visiting Jail", is_special=True) free_parking = Card("Free Parking", is_special=True) go_to_jail = Card("Go to Jail", is_go_jail=True) reading_rr = RailRoadCard("Reading Railroad") penn_rr = RailRoadCard("Pennsylvania Railroad") bno_rr = RailRoadCard("B. & O. Railroad") short_line_rr = RailRoadCard("Short Line") railroads = Group("Railroad", [reading_rr, penn_rr, bno_rr, short_line_rr], 0, is_color=False) electric_company = UtilityCard("Electric Company") water_works = UtilityCard("Water Works") utilities = Group("Utility", [electric_company, water_works], 0, is_color=False) med_ave = PropertyCard("Mediterranean Avenue", 60, [2, 10, 30, 90, 160, 250], 30) baltic_ave = PropertyCard("Baltic Avenue", 60, [4, 20, 60, 180, 320, 450], 30) brown = Group("Brown", [med_ave, baltic_ave], 50) oriental_ave = PropertyCard("Oriental Avenue", 100, [6, 30, 90, 270, 400, 550], 50) vermont_ave = PropertyCard("Vermont Avenue", 100, [6, 30, 90, 270, 400, 550], 50) conn_ave = PropertyCard("Connecticut Avenue", 120, [8, 40, 100, 300, 450, 600], 60) light_blue = Group("Light Blue", [oriental_ave, vermont_ave, conn_ave], 50) st_charles_place = PropertyCard("St. Charles Place", 140, [10, 50, 150, 450, 625, 750], 70) states_ave = PropertyCard("States Avenue", 140, [10, 50, 150, 450, 625, 750], 70) virginia_ave = PropertyCard("Virginia Avenue", 160, [12, 60, 180, 500, 700, 900], 80) pink = Group("Pink", [st_charles_place, states_ave, virginia_ave], 100) st_james_place = PropertyCard("St. James Place", 180, [14, 70, 200, 550, 750, 950], 90) ten_ave = PropertyCard("Tennessee Avenue", 180, [14, 70, 200, 550, 750, 950], 90) ny_ave = PropertyCard("New York Avenue", 200, [16, 80, 220, 600, 800, 1000], 100) orange = Group("Orange", [st_james_place, ten_ave, ny_ave], 100) kentucky_ave = PropertyCard("Kentucky Avenue", 220, [18, 90, 250, 700, 875, 1050], 110) indiana_ave = PropertyCard("Indiana Avenue", 220, [18, 90, 250, 700, 875, 1050], 110) illinois_ave = PropertyCard("Illinois Avenue", 240, [20, 100, 300, 750, 925, 1100], 120) red = Group("Red", [kentucky_ave, indiana_ave, illinois_ave], 150) atlantic_ave = PropertyCard("Atlantic Avenue", 260, [22, 110, 330, 800, 975, 1150], 130) ventnor_ave = PropertyCard("Ventnor Avenue", 260, [22, 110, 330, 800, 975, 1150], 130) marvin_gardens = PropertyCard("Marvin Gardens", 280, [24, 120, 360, 850, 1025, 1200], 140) yellow = Group("Yellow", [atlantic_ave, ventnor_ave, marvin_gardens], 150) pacific_ave = PropertyCard("Pacific Avenue", 300, [26, 130, 390, 900, 1100, 1275], 150) nc_ave = PropertyCard("North Carolina Avenue", 300, [26, 130, 390, 900, 1100, 1275], 150) penn_ave = PropertyCard("Pennsylvania Avenue", 320, [28, 150, 450, 1000, 1200, 1400], 160) green = Group("Green", [pacific_ave, nc_ave, penn_ave], 200) park_place = PropertyCard("Park Place", 350, [35, 175, 500, 1100, 1300, 1500], 175) boardwalk = PropertyCard("Boardwalk", 400, [50, 200, 600, 1400, 1700, 2000], 200) blue = Group("Blue", [park_place, boardwalk], 200) board = [go, med_ave, comm_chest, baltic_ave, income_tax, reading_rr, oriental_ave, chance, vermont_ave, conn_ave, jail, st_charles_place, electric_company, states_ave, virginia_ave, penn_rr, st_james_place, comm_chest, ten_ave, ny_ave, free_parking, kentucky_ave, chance, indiana_ave, illinois_ave, bno_rr, atlantic_ave, ventnor_ave, water_works, marvin_gardens, go_to_jail, pacific_ave, nc_ave, comm_chest, penn_ave, short_line_rr, chance, park_place, luxury_tax, boardwalk] groups = [railroads, utilities, brown, light_blue, pink, orange, red, yellow, green, blue] return board, groups