Blackjack Console game program in Python

Console Games

Games that can be played using the console.

Here is the program for the Blackjack Game of the cards.

BlackJack Game

Blackjack is the American variant of a globally popular banking game known as Twenty-One.
in this game, one player has to make the total sum of their hands to lower than 21 to win while another player has the total sum is 21 or more,
All the cards greater than 10 are considered as the 10 and Ace is considered as the 11 or 1 depending on conditions.

The person who has less total of the cards is the winner (one player should have scored more than 21).


import random
class blackJack:
player1Hand = 0
player2Hand = 0
deck ={'Ace of Spades':1, '2 of Spades':2, '3 of Spades':3,
'4 of Spades':4, '5 of Spades':5, '6 of Spades':6,
'7 of Spades':7, '8 of Spades':8, '9 of Spades':9,
'10 of Spades':10, 'Jack of Spades':10,
'Queen of Spades':10, 'King of Spades': 10,
'Ace of Hearts':1, '2 of Hearts':2, '3 of Hearts':3,
'4 of Hearts':4, '5 of Hearts':5, '6 of Hearts':6,
'7 of Hearts':7, '8 of Hearts':8, '9 of Hearts':9,
'10 of Hearts':10, 'Jack of Hearts':10,
'Queen of Hearts':10, 'King of Hearts': 10,
'Ace of Clubs':1, '2 of Clubs':2, '3 of Clubs':3,
'4 of Clubs':4, '5 of Clubs':5, '6 of Clubs':6,
'7 of Clubs':7, '8 of Clubs':8, '9 of Clubs':9,
'10 of Clubs':10, 'Jack of Clubs':10,
'Queen of Clubs':10, 'King of Clubs': 10,
'Ace of Diamonds':1, '2 of Diamonds':2, '3 of Diamonds':3,
'4 of Diamonds':4, '5 of Diamonds':5, '6 of Diamonds':6,
'7 of Diamonds':7, '8 of Diamonds':8, '9 of Diamonds':9,
'10 of Diamonds':10, 'Jack of Diamonds':10,
'Queen of Diamonds':10, 'King of Diamonds': 10}

def deal_cards(self, numCards):
for x in range(numCards):
while len(self.deck):
if self.player1Hand >= 21 and self.player2Hand >= 21:
print("no one wins Tie")
print("Cards Value of Player 1 is ", self.player1Hand)
print("Cards Value of Player 2 is ", self.player2Hand)
exit()
break
elif self.player1Hand >= 21:
print("Player 2 wins")
print("Cards Value of Player 1 is ", self.player1Hand)
print("Cards Value of Player 2 is ", self.player2Hand)
exit()
break
elif self.player2Hand >= 21:
print("player 1 wins")
print("Cards Value of Player 1 is ", self.player1Hand)
print("Cards Value of Player 2 is ", self.player2Hand)
exit()
break
else:
card, value = random.choice(list(self.deck.items()))
self.deck.pop(card)
print("player 1 was dealt with ", card)
self.player1Hand += value
card, value = random.choice(list(self.deck.items()))
self.deck.pop(card)
print("player 2 was dealt with ", card)
self.player2Hand += value
break
print("Cards Value of Player 1 is ", self.player1Hand)
print("Cards Value of Player 2 is ", self.player2Hand)
def main(self):
num = int(input("How many times card will be dealt "))
self.deal_cards(num)

game = blackJack()
while True:
game.main()

the output of this program is
output


If you like this post check out my other post related to C, C++, PHP, Java, JavaScript etc.
Thanks for being here.

Comments

Popular posts from this blog

C/C++ program to check the Palindrome string.

Second Equation of motion, How to implement using C language

Third Equation of motion, How to implement using C language