From 396576cb70cf39d651bfb9d417edaf55f75dcd0c Mon Sep 17 00:00:00 2001 From: JPN11 Date: Fri, 7 Feb 2025 23:44:53 +0000 Subject: [PATCH 1/2] 'add main.py' --- Python_Blackjack/main.py | 176 +++++++++++++++++++++++++++++++++++++++ python-mini-project | 1 + 2 files changed, 177 insertions(+) create mode 100644 Python_Blackjack/main.py create mode 160000 python-mini-project diff --git a/Python_Blackjack/main.py b/Python_Blackjack/main.py new file mode 100644 index 00000000..dc517fb6 --- /dev/null +++ b/Python_Blackjack/main.py @@ -0,0 +1,176 @@ +import random + +cards = [ + {"name": "Ace", "value": 11}, + {"name": "2", "value": 2}, + {"name": "3", "value": 3}, + {"name": "4", "value": 4}, + {"name": "5", "value": 5}, + {"name": "6", "value": 6}, + {"name": "7", "value": 7}, + {"name": "8", "value": 8}, + {"name": "9", "value": 9}, + {"name": "10", "value": 10}, + {"name": "Jack", "value": 10}, + {"name": "Queen", "value": 10}, + {"name": "King", "value": 10}, +] + + +class Player: + def __init__(self, name): + self.name = name + self.hand = [] + self.bet = 0 + self.money = 1000 # Each player starts with $1000 + + def deal_card(self): + draw = random.choice(cards) + self.hand.append(draw) + if draw["name"] == "Ace": + ace_value = int( + input( + f"{self.name}, you were dealt an Ace. Do you want to count Ace as 11 or 1? [11/1] " + ) + ) + while ace_value not in [11, 1]: + ace_value = int( + input( + f"Invalid choice, {self.name}. Do you want to count Ace as 11 or 1? [11/1] " + ) + ) + draw["value"] = ace_value # Update the value of Ace in the hand + + def calculate_hand_value(self): + total_value = sum(card["value"] for card in self.hand) + # Adjust for Aces if total value exceeds 21 + aces_count = sum(1 for card in self.hand if card["name"] == "Ace") + while total_value > 21 and aces_count: + total_value -= 10 # Count Ace as 1 instead of 11 + aces_count -= 1 + return total_value + + def hit_or_stay(self): + choice = input(f"{self.name}, do you want to hit? [y/n] ") + if choice.lower() == "y": + self.deal_card() + print(f"{self.name} got a {self.hand[-1]['name']}") + elif choice.lower() == "n": + pass + else: + print("Invalid choice. Please enter y or n.") + self.hit_or_stay() + + def place_bet(self): + while True: + try: + self.bet = int(input(f"{self.name}, enter your bet: ")) + if self.bet > self.money: + print("You don't have enough money to bet that much.") + else: + self.money -= self.bet # Deduct the bet from player's money + break + except ValueError: + print("Invalid bet. Please enter a number.") + + +class Game: + def __init__(self): + self.players = [] + self.gameover = False + + def remove_broke_players(self): + # Create a list of players to remove + players_to_remove = [player for player in self.players if player.money <= 0] + + # Remove each player from the game + for player in players_to_remove: + print(f"{player.name} is out of money and has been removed from the game.") + self.players.remove(player) + + def dealer_turn(self, dealer): + dealer_value = dealer.calculate_hand_value() + print(f"Dealer's hand value is {dealer_value}") + + for player in self.players: + player_value = player.calculate_hand_value() + if player_value > 21: + print(f"{player.name} busts! Dealer wins.") + print(f"{player.name} now has ${player.money}") + elif dealer_value > 21: + print(f"Dealer busts! {player.name} wins.") + player.money += player.bet * 2 + print(f"{player.name} now has ${player.money}") + elif player_value > dealer_value: + print(f"{player.name} wins!") + player.money += player.bet * 2 + print(f"{player.name} now has ${player.money}") + elif player_value < dealer_value: + print("Dealer wins!") + print(f"{player.name} now has ${player.money}") + else: + print("It's a tie!") + player.money += player.bet # Player gets their bet back + print(f"{player.name} now has ${player.money}") + + # Remove players with no money + self.remove_broke_players() + + # Check if there are players left + if len(self.players) == 0: + print("No players left. Game over.") + self.gameover = True + return # Skip the play again prompt + + if len(self.players) == 1: + self.gameover = True + + + # Ask if players want to play again + play_again = input("Do you want to play again? [y/n] ") + if play_again.lower() == "y": + for player in self.players: + player.hand = [] + player.bet = 0 + else: + self.gameover = True + + + def main(self): + print(r""".------..------..------..------..------..------..------..------..------. +|B.--. ||L.--. ||A.--. ||C.--. ||K.--. ||J.--. ||A.--. ||C.--. ||K.--. | +| :(): || :/\: || (\/) || :/\: || :/\: || :(): || (\/) || :/\: || :/\: | +| ()() || (__) || :\/: || :\/: || :\/: || ()() || :\/: || :\/: || :\/: | +| '--'B|| '--'L|| '--'A|| '--'C|| '--'K|| '--'J|| '--'A|| '--'C|| '--'K| +`------'`------'`------'`------'`------'`------'`------'`------'`------'""") + print("Starting money: 1000") + player_num = int(input("How many players? ")) + for _ in range(player_num): + player_name = input("Enter player name: ") + self.players.append(Player(player_name)) + + while not self.gameover: + dealer = Player("Dealer") + dealer.deal_card() + dealer.deal_card() + print(f"Dealer's up card is {dealer .hand[0]['name']}") + + for player in self.players: + player.place_bet() + player.deal_card() + player.deal_card() + print( + f"{player.name}'s hand is {player.hand[0]['name']} and {player.hand[1]['name']}" + ) + player.hit_or_stay() + print(f"{player.name}'s hand value is {player.calculate_hand_value()}") + + while dealer.calculate_hand_value() < 17: + dealer.deal_card() + self.dealer_turn(dealer) + + +# To start the game, create an instance of Game and call the main method +if __name__ == "__main__": + game = Game() + game.main() \ No newline at end of file diff --git a/python-mini-project b/python-mini-project new file mode 160000 index 00000000..c092b63f --- /dev/null +++ b/python-mini-project @@ -0,0 +1 @@ +Subproject commit c092b63f24ee927dc1a55a0fe28b176e7a6b6521 From ccb563265261f87c7b95c732b2bb65778f85f09f Mon Sep 17 00:00:00 2001 From: JPN11 Date: Fri, 7 Feb 2025 23:52:01 +0000 Subject: [PATCH 2/2] 'update README.md' --- Python_Blackjack/README.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Python_Blackjack/README.md diff --git a/Python_Blackjack/README.md b/Python_Blackjack/README.md new file mode 100644 index 00000000..29fb367e --- /dev/null +++ b/Python_Blackjack/README.md @@ -0,0 +1,36 @@ + +![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99) +![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103) + +# Blackjack + + + +## 🛠️ Description + +Blackjack Card Game in Python + +## ⚙️ Languages or Frameworks Used + +Python + +## 🌟 How to run + +You can type this command on your teminal to run the scripts. +`python main.py` + +if this doesn't work try, +`python3 Python_Blackjack/main.py` + +To stop it you can wait for the game to end. + +## 📺 Demo + +Coming soon + +## 🤖 Author + +This script is by JPN11 + +JPN11 -> https://github.com/JPN11 +This is my github profile. \ No newline at end of file