Skip to content

'add main.py' #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Python_Blackjack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--Please do not remove this part-->
![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.
176 changes: 176 additions & 0 deletions Python_Blackjack/main.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions python-mini-project
Submodule python-mini-project added at c092b6