Code Monkey home page Code Monkey logo

python-blackjack-game's Introduction

Python-Blackjack-game

To clone the repository and run it locally, follow these steps:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to clone the repository using the cd command.
  3. Use the git clone command:
    https://github.com/EpicNesh26/Python-Blackjack-game.git
  4. Once the repository is cloned, navigate into the repository directory using the cd command.
  5. You can now run the script locally. Depending on the programming language, you might need to have the necessary runtime or dependencies installed on your system.
  6. Execute the script using the appropriate command or interpreter for the specific language. For example, if it's a Python script, you can run it using the python command followed by the script name:

Introduction to Blackjack:

Blackjack, or 21, is a casino card game where players aim to beat the dealer by getting a hand value close to 21 without exceeding it. Number cards are worth their face value, face cards 10 points, and Aces 1 or 11 points

Method 1

This Python script simulates dealing cards from a 52-card deck. It uses the random module to shuffle the deck, which is a list of cards created from the suits ("Spades," "Clubs," "Hearts," "Diamonds") and ranks ("A," "2"-"10," "J," "Q," "K"). The deal function distributes a specified number of cards. The script shuffles the deck, deals four cards, and determines each card's value, printing the results.

# 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

Method 2

This Python script simulates a deck of cards and a dealing function. It uses the random module to shuffle the deck, consisting of suits ("Spades," "Clubs," "Hearts," "Diamonds") and ranks with corresponding values. The deck is built by combining suits and ranks, then shuffled. The deal function returns a specified number of cards from the top of the shuffled deck. The script demonstrates deck creation, shuffling, and card dealing, and prints the dealt card's suit and rank.

cards = []
suits = ["Spades","clubs","hearts","diamonds"]
ranks = [{"rank":"A","value":11,
"rank":"1","value":1,
"rank":"2","value":2,
"rank":"3","value":3,
"rank":"4","value":4,
"rank":"5","value":5,
"rank":"6","value":6,
"rank":"7","value":7,
"rank":"8","value":8,
"rank":"9","value":9,
"rank":"J","value":10,
"rank":"Q","value":10,
"rank":"K","value":10,}]
for suit in suits:
for rank in ranks:
cards.append([suit,rank])

Final Code

This Python script implements a text-based Blackjack game with several classes:

Card: Represents a playing card with a suit and rank. Deck: Represents a deck of cards, which can be shuffled and dealt. Hand: Represents a hand of cards, managing dealt cards and calculating their value. Game: Manages the overall Blackjack game. 'check_winner' function determines the winner based on the player's and dealer's hand values.

The main part of the script creates a Game object and calls its play method to start the game. The play method prompts the user to enter the number of games to play, then plays each game by dealing two cards to both the player and the dealer, allowing the player to hit or stand, and determining the winner based on the final hand values.

class Hand:
def __init__(self, dealer = False):
self.cards=[]
self.value = 0
self.dealer = dealer
def add_card(self, card_list):
self.cards.extend(card_list)
def calculate_value(self):
self.value=0
has_ace = False
for card in self.cards:
card_value=int(card.rank["value"])
self.value += card_value
if card.rank["rank"] == "A":
has_ace = True
if has_ace and self.value > 21:
self.value -=10

python-blackjack-game's People

Contributors

epicnesh26 avatar

Stargazers

 avatar Joker  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.