Skip to content
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

Added Blackjack with 3 different methods #203

Closed
wants to merge 1 commit into from
Closed
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
106 changes: 106 additions & 0 deletions BlackJack/BlackJack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import random

# Define the suits and ranks
suits = ["Spades", "Clubs", "Hearts", "Diamonds"]
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
values = {
"A": 11, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10, "J": 10, "Q": 10, "K": 10
}

# Define the Card class
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank

def __str__(self):
return f"{self.rank} of {self.suit}"

# Define the Deck class
class Deck:
def __init__(self):
self.cards = [Card(suit, rank) for suit in suits for rank in ranks]
self.shuffle()

def shuffle(self):
random.shuffle(self.cards)

def deal(self):
return self.cards.pop()

# Define the Hand class
class Hand:
def __init__(self):
self.cards = []
self.value = 0
self.aces = 0

def add_card(self, card):
self.cards.append(card)
self.value += values[card.rank]
if card.rank == 'A':
self.aces += 1
self.adjust_for_ace()

def adjust_for_ace(self):
while self.value > 21 and self.aces:
self.value -= 10
self.aces -= 1

def __str__(self):
return f"{' '.join(map(str, self.cards))} (Value: {self.value})"

# Define the Game class
class Game:
def __init__(self):
self.deck = Deck()
self.player_hand = Hand()
self.dealer_hand = Hand()

def play(self):
print("Welcome to Blackjack!")

# Initial dealing
for _ in range(2):
self.player_hand.add_card(self.deck.deal())
self.dealer_hand.add_card(self.deck.deal())

# Show hands
print(f"Dealer's Hand: {self.dealer_hand.cards[0]} and <hidden>")
print(f"Player's Hand: {self.player_hand}")

# Player's turn
while self.player_hand.value < 21:
action = input("Do you want to [H]it or [S]tand? ").upper()
if action == 'H':
self.player_hand.add_card(self.deck.deal())
print(f"Player's Hand: {self.player_hand}")
else:
break

# Dealer's turn
while self.dealer_hand.value < 17:
self.dealer_hand.add_card(self.deck.deal())

# Show final hands
print(f"Dealer's Hand: {self.dealer_hand}")
print(f"Player's Hand: {self.player_hand}")

self.check_winner()

def check_winner(self):
if self.player_hand.value > 21:
print("Player busts! Dealer wins.")
elif self.dealer_hand.value > 21:
print("Dealer busts! Player wins.")
elif self.player_hand.value > self.dealer_hand.value:
print("Player wins!")
elif self.player_hand.value < self.dealer_hand.value:
print("Dealer wins!")
else:
print("It's a tie!")

# Run the game
if __name__ == "__main__":
game = Game()
game.play()
40 changes: 40 additions & 0 deletions BlackJack/Method1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import random

# Define the suits and ranks
suits = ["Spades", "Clubs", "Hearts", "Diamonds"]
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

# Create the deck of cards
deck = [{"suit": suit, "rank": rank} for suit in suits for rank in ranks]

def shuffle_deck(deck):
random.shuffle(deck)

def deal(deck, num_cards):
dealt_cards = []
for _ in range(num_cards):
card = deck.pop()
dealt_cards.append(card)
return dealt_cards

def display_cards(cards):
for card in cards:
print(f"{card['rank']} of {card['suit']}")

def blackjack_game(num_players=1, num_cards_per_player=2):
shuffle_deck(deck)
players_hands = []

# Deal cards to players
for _ in range(num_players):
player_hand = deal(deck, num_cards_per_player)
players_hands.append(player_hand)

# Display each player's hand
for i, player_hand in enumerate(players_hands):
print(f"Player {i + 1}'s hand:")
display_cards(player_hand)
print()

if __name__ == "__main__":
blackjack_game(num_players=2, num_cards_per_player=2)
31 changes: 31 additions & 0 deletions BlackJack/Method2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import random

# Define the suits and ranks
suits = ["Spades", "Clubs", "Hearts", "Diamonds"]
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

# Create the deck of cards
deck = [{"suit": suit, "rank": rank} for suit in suits for rank in ranks]

def shuffle_deck(deck):
random.shuffle(deck)

def deal(deck, num_cards):
dealt_cards = []
for _ in range(num_cards):
card = deck.pop()
dealt_cards.append(card)
return dealt_cards

def print_dealt_cards(dealt_cards):
for card in dealt_cards:
print(f"Dealt card: {card['rank']} of {card['suit']}")

# Shuffle the deck
shuffle_deck(deck)

# Deal four cards
dealt_cards = deal(deck, 4)

# Print the dealt cards' suit and rank
print_dealt_cards(dealt_cards)