├── License ├── README.md ├── blackjack.py ├── cards.py └── db.py /License: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Justin-2028 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Blackjack Application 2 | 3 | One of my first Python projects, this blackjack application will be sure to satisfy your gambling needs. 4 | -------------------------------------------------------------------------------- /blackjack.py: -------------------------------------------------------------------------------- 1 | import cards 2 | import db 3 | import locale as lc 4 | 5 | result = lc.setlocale(lc.LC_ALL, "") 6 | if result[0] == "C": 7 | lc.setlocale(lc.LC_ALL, "en_US") 8 | 9 | 10 | def display_title(): 11 | print("BLACKJACK!") 12 | print("Blackjack payout is 3:2") 13 | print("Enter 'x' for bet to exit") 14 | print() 15 | 16 | 17 | def get_starting_money(): 18 | try: 19 | money = db.read_money() 20 | except FileNotFoundError: 21 | money = 0 22 | if money < 5: 23 | print("You were out of money.") 24 | print("We gave you 100 so you could play.") 25 | db.write_money(100) 26 | return 100 27 | else: 28 | return money 29 | 30 | 31 | def get_bet(money): 32 | while True: 33 | try: 34 | bet = float(input("Bet amount: ")) 35 | except ValueError: 36 | print("Invalid amount. Try again.") 37 | continue 38 | 39 | if bet < 5: 40 | print("The minimum bet is 5.") 41 | elif bet > 1000: 42 | print("The maximum bet is 1,000.") 43 | elif bet > money: 44 | print("You don't have enough money to make that bet.") 45 | else: 46 | return bet 47 | 48 | 49 | def display_cards(hand, title): 50 | print(title.upper()) 51 | for card in hand: 52 | print(card[0], "of", card[1]) 53 | print() 54 | 55 | 56 | def play(deck, player_hand): 57 | while True: 58 | choice = input("Hit or Stand? (h/s): ") 59 | print() 60 | 61 | if choice.lower() == "h": 62 | cards.add_card(player_hand, cards.deal_card(deck)) 63 | if cards.get_points(player_hand) > 21: 64 | break 65 | display_cards(player_hand, "YOUR CARDS: ") 66 | elif choice == "s": 67 | break 68 | else: 69 | print("Not a valid choice, Try again.") 70 | return player_hand 71 | 72 | 73 | def buy_more_chips(money): 74 | while True: 75 | try: 76 | amount = float(input("Amount: ")) 77 | except ValueError: 78 | print("Invalid amount. Try again.") 79 | continue 80 | 81 | if 0 < amount <= 10000: 82 | money += amount 83 | return money 84 | else: 85 | print("Invalid amount, must be from 0 to 10,000.") 86 | 87 | 88 | def main(): 89 | display_title() 90 | money = get_starting_money() 91 | print("Money:", lc.currency(money, grouping=True)) 92 | print() 93 | 94 | while True: 95 | if money < 5: 96 | print("You are out of money.") 97 | buy_more = input("Would you like to buy more chips? (y/n) ") 98 | if buy_more == "y": 99 | money = buy_more_chips(money) 100 | print("Money:", lc.currency(money, grouping=True)) 101 | db.write_money(money) 102 | else: 103 | break 104 | 105 | bet = get_bet(money) 106 | if bet == "x": 107 | break 108 | 109 | deck = cards.get_deck() 110 | cards.shuffle(deck) 111 | 112 | dealer_hand = cards.get_empty_hand() 113 | player_hand = cards.get_empty_hand() 114 | 115 | cards.add_card(player_hand, cards.deal_card(deck)) 116 | cards.add_card(player_hand, cards.deal_card(deck)) 117 | cards.add_card(dealer_hand, cards.deal_card(deck)) 118 | 119 | display_cards(dealer_hand, "\nDEALER'S SHOW CARD: ") 120 | display_cards(player_hand, "YOUR CARDS: ") 121 | 122 | player_hand = play(deck, player_hand) 123 | 124 | cards.add_card(dealer_hand, cards.deal_card(deck)) 125 | while cards.get_points(dealer_hand) <= 21 and cards.get_points(dealer_hand) < 17: 126 | cards.add_card(dealer_hand, cards.deal_card(deck)) 127 | display_cards(dealer_hand, "DEALER'S CARDS: ") 128 | 129 | print("YOUR POINTS: " + str(cards.get_points(player_hand))) 130 | print("DEALER'S POINTS: " + str(cards.get_points(dealer_hand))) 131 | 132 | players_points = cards.get_points(player_hand) 133 | dealer_points = cards.get_points(dealer_hand) 134 | 135 | if players_points > 21: 136 | print("\nSorry, you busted. You lose.") 137 | money -= bet 138 | elif dealer_points > 21: 139 | print("\nYay! Dealer busted. You win!") 140 | money += bet 141 | else: 142 | if players_points == 21 and len(player_hand) == 2: 143 | if dealer_points == 21 and len(dealer_hand) == 2: 144 | print("\nDang, dealer has blackjack too. You push") 145 | else: 146 | print("\nBlackjack! You win!") 147 | money += bet * 1.5 148 | money = round(money, 2) 149 | elif players_points > dealer_points: 150 | print("\nHooray! You win!") 151 | money += bet 152 | elif dealer_points > players_points: 153 | print("\nSorry, You lose.") 154 | money -= bet 155 | elif players_points == dealer_points: 156 | print("\nYou push.") 157 | else: 158 | print("\nSorry, I am not sure what happened.") 159 | 160 | print("Money:", lc.currency(money, grouping=True)) 161 | 162 | db.write_money(money) 163 | 164 | again = input("\nPlay again? (y/n): ") 165 | print() 166 | if again.lower() != "y": 167 | print("Come again soon!") 168 | break 169 | 170 | print() 171 | print("Bye, have a nice day!") 172 | 173 | 174 | if __name__ == "__main__": 175 | main() 176 | -------------------------------------------------------------------------------- /cards.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python3 2 | 3 | import random 4 | 5 | 6 | def get_deck(): 7 | deck = [] 8 | ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", 9 | "Jack", "Queen", "King"} 10 | suits = {"Clubs", "Diamonds", "Hearts", "Spades"} 11 | for suit in suits: 12 | for rank in ranks: 13 | if rank == "Ace": 14 | value = 11 15 | elif rank == "Jack" or rank == "Queen" or rank == "King": 16 | value = 10 17 | else: 18 | value = int(rank) 19 | 20 | card = [rank, suit, value] 21 | deck.append(card) 22 | return deck 23 | 24 | 25 | def shuffle(deck): 26 | random.shuffle(deck) 27 | 28 | 29 | def deal_card(deck): 30 | card = deck.pop() 31 | return card 32 | 33 | 34 | def get_empty_hand(): 35 | hand = [] 36 | return hand 37 | 38 | 39 | def add_card(hand, card): 40 | hand.append(card) 41 | 42 | 43 | def get_points(hand): 44 | points = 0 45 | ace_count = 0 46 | for card in hand: 47 | if card[0] == "Ace": 48 | ace_count += 1 49 | points += card[2] 50 | 51 | if ace_count > 0 and points > 21: 52 | points = points - (ace_count * 10) 53 | if ace_count > 1 and points <= 11: 54 | points += 10 55 | return points 56 | 57 | 58 | def main(): 59 | deck = get_deck() 60 | shuffle(deck) 61 | for card in deck: 62 | print(card) 63 | print() 64 | 65 | hand = get_empty_hand() 66 | add_card(hand, deal_card(deck)) 67 | add_card(hand, deal_card(deck)) 68 | add_card(hand, deal_card(deck)) 69 | 70 | print("Hand:", hand) 71 | print("Points:", get_points(hand)) 72 | 73 | 74 | if __name__ == "__main__": 75 | main() 76 | -------------------------------------------------------------------------------- /db.py: -------------------------------------------------------------------------------- 1 | 2 | FILENAME = "money.txt" 3 | 4 | 5 | def write_money(money): 6 | try: 7 | with open(FILENAME, "w") as file: 8 | file.write((str(money))) 9 | except FileNotFoundError: 10 | print("Data file missing, resetting starting amount to 1000.") 11 | write_money(1000) 12 | return 1000 13 | else: 14 | return money 15 | 16 | 17 | def read_money(): 18 | try: 19 | with open(FILENAME, "r") as file: 20 | line = file.readline() 21 | money = float(line) 22 | return money 23 | except FileNotFoundError: 24 | print("Data file missing, resetting starting amount to 1000.") 25 | write_money(1000) 26 | return 1000 27 | --------------------------------------------------------------------------------