What's new

Need assistance on blackjack python project implementing hit/stand feature

Fever

Active member
Staff member
im just working on a blackjack project and im new to coding so its a little tough trying to add new functions such as the hit/stand function. I have tried making a hit/stand function but im not sure how to actually add a new card to the total of the players cards and the dealer also. im just staring by adding the players to try and get some result but a am just stuck at this rate and have no idea how i can properly implement this. I have a sep file that stores the list of the deck and shuffle but is imported

import playing_cards
import random
player_hand = []
dealers_hand = []
hands = [[]]



#STAGE 1 - testing purpose only

# Testing code for step 1
##card = playing_cards.deal_one_card()
##print(card)




#Stage 2 - testing purpose only (PLAYERS HAND)

# Deal first card
card = playing_cards.deal_one_card()

# Append it to the player_hand list
player_hand.append(card)
# Deal second card
card = playing_cards.deal_one_card()
# Append it to the player_hand list
player_hand.append(card)
#ADDING UP BOTH CARDS

# Display the player's hand to the screen using a simple print statement
#print("Player's hand is ", player_hand)


#Stage 4 and 5

suits = {'C': 'Club', 'H': 'Heart', 'D': 'Diamond', 'S': 'Spade'}
names = {'2': '2', '3': '3', '4': '4',
'5':'5', '6': '6', '7': '7', '8': '8', '9': '9', 'T': '10','J': 'Jack', 'Q': 'Queen', 'K': 'King', 'A': 'Ace'}


def score_hand(player_hand):
" Scores hand with adjustment for Ace "
value = {}
for i in range(10):
value[str(i)] = i
value['J'] = 10
value['Q'] = 10
value['K'] = 10
value['A'] = 11
value['T'] = 10

score = sum(int(value[card[0]]) for card in player_hand)
if score > 21:

# Adjust for Aces
adjustment = sum(10 if card[0]=='A' else 0 for card in player_hand)
score -= adjustment



return score

#ask about this

def show_result(player_hand):

player_score = int(score_hand(player_hand))

if player_score > 21:
print('*** Player Bust! ***')
elif player_score == 21:
print('*** Blackjack! Player Wins! ***')


##else:
## push(player_hand,dealer_hand)
##def push(player_score,dealers_score):
## print("Dealer and Player tie! It's a push.")
##else:
## // Logic to continue game





def hit_stand(show_hand,player_hand):

while True:
x = input("Would you like to Hit or Stand? Enter 'h' or 's'")

if x[0].lower() == 'h':
hit(show_hand,player_hand) # hit() function defined above

elif x[0].lower() == 's':
print("Player stands. Dealer is playing.")
playing = False









def show_hand(player_hand):

score_hand(player_hand)
score = f"Player's hand is {score_hand(player_hand)}: "
cards = ' | '.join([f"{names[card[0]]} of {suits[card[1]]}" for card in player_hand])

return score + cards





for hand in hands:
print(show_hand(player_hand))
show_result(player_hand)




#Stage 3 - Testing purpose only (DEALERS HAND)

### Deal first card
card = playing_cards.deal_one_card()
### Append it to the player_hand list
dealers_hand.append(card)
### Deal second card
card = playing_cards.deal_one_card()
### Append it to the player_hand list
dealers_hand.append(card)
### Display the player's hand to the screen using a simple print statement


#Stage 4 and 5

def score_hand(dealers_hand):
" Scores hand with adjustment for Ace "
value = {}
for i in range(10):
value[str(i)] = i
value['J'] = 10
value['Q'] = 10
value['K'] = 10
value['A'] = 11
value['T'] = 10

score = sum(int(value[card[0]]) for card in dealers_hand)
if score > 21:
# Adjust for Aces
adjustment = sum(10 if card[0]=='A' else 0 for card in dealers_hand)
score -= adjustment

return score

#ask about this

def show_result(dealers_hand):

dealers_score = int(score_hand(dealers_hand))

if dealers_score > 21:
print('*** Dealer Bust! ***')
elif dealers_score == 21:
print('*** Blackjack! Dealer Wins! ***')


def show_hand(dealers_hand):

score = f"Dealers's hand is {score_hand(dealers_hand)}: "
cards = ' | '.join([f"{names[card[0]]} of {suits[card[1]]}" for card in dealers_hand])

return score + cards


for hand in hands:
print(show_hand(dealers_hand))
print('')
show_result(dealers_hand)
hit_stand(show_hand, player_hand)

Continue reading...
 
Top