The rules for Blackjack
- If the total value of your cards exceeds 21, you lose (Bust).
- Jacks, Queens, and Kings are each valued at 10, and Aces can be valued as either 1 or 11, depending on which value is more advantageous.
- At the start, the dealer receives two cards, and the player also receives two cards.
- One of the dealer's cards is face-up, and the other card is face-down.
- To win, the player's total card value must be greater than the dealer's total card value but not exceed 21.
- The player may choose to receive additional cards if desired.
- If the dealer's total card value is 16 or less, the dealer must draw an additional card.
My first code
# 블랙잭 게임 프로젝트 with 파이썬
import random
# 카드, 사용자, 컴퓨터 리스트 정렬
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10]
user = []
computer = []
# 랜덤으로 카드를 불러오는 함수
def get_card():
card = random.choice(cards)
return card
# 승패를 결정하는 함수
def compare():
user_point = sum(user)
computer_point = sum(computer)
print(f"Your final hand: {user}, final score: {user_point}")
print(f"Computer's final hand: {computer}, final score: {computer_point}")
if user_point > 21:
print("You went over 21. You lose!")
elif computer_point > 21:
print("Computer went over 21. You win!")
elif user_point > computer_point:
print("You win!")
elif user_point == computer_point:
print("It's a draw!")
else:
print("You lose!")
print("############## Start Blackjack Game ##############")
# 첫번째 카드 분배
user.append(get_card())
computer.append(get_card())
print(f"computer card is {computer}")
# 두번째 카드 분재(여기서부터 컴퓨터 카드는 비공개)
user.append(get_card())
computer.append(get_card())
print(f"Your cards are {user}")
# 사용자가 카드를 더 받을지 선택
more = input("Do you want a card? Choose yes or no: ")
while True:
if more == "yes":
user.append(get_card())
print(f"Your cards are {user}")
if sum(user) > 21:
print("You went over 21. You lose!")
break
elif more == "no":
break
else:
print("Invalid input. Please choose 'yes' or 'no'.")
more = input("Do you want a card? Choose yes or no: ")
compare()
# 컴퓨터가 17 미만의 카드 합인 경우
if sum(computer) < 17:
computer.append(get_card())
I created a simple Blackjack game using Python for the first time.
As you know if you read my code I failed to apply one rule to the code.
Rules I failed to apply: : 'Aces can be worth either 1 or 11, depending on which value is better.'
I reviewed the course materials from the teacher who assigned the project and identified the missing parts and the sections I couldn't implement.
Good code
import random
import os
def deal_card():
"""Returns a random card from the deck."""
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
card = random.choice(cards)
return card
def calculate_score(cards):
"""Take a list of cards and return the score calculated from the cards"""
if sum(cards) == 21 and len(cards) == 2:
return 0
if 11 in cards and sum(cards) > 21:
cards.remove(11)
cards.append(1)
return sum(cards)
def compare(user_score, computer_score):
if user_score > 21 and computer_score > 21:
return "You went over. You lose 😤"
if user_score == computer_score:
return "Draw 🙃"
elif computer_score == 0:
return "Lose, opponent has Blackjack 😱"
elif user_score == 0:
return "Win with a Blackjack 😎"
elif user_score > 21:
return "You went over. You lose 😭"
elif computer_score > 21:
return "Opponent went over. You win 😁"
elif user_score > computer_score:
return "You win 😃"
else:
return "You lose 😤"
def play_game():
print("----------Start Game----------")
user_cards = []
computer_cards = []
is_game_over = False
for _ in range(2):
user_cards.append(deal_card())
computer_cards.append(deal_card())
while not is_game_over:
user_score = calculate_score(user_cards)
computer_score = calculate_score(computer_cards)
print(f" Your cards: {user_cards}, current score: {user_score}")
print(f" Computer's first card: {computer_cards[0]}")
if user_score == 0 or computer_score == 0 or user_score > 21:
is_game_over = True
else:
user_should_deal = input("Type 'y' to get another card, type 'n' to pass: ")
if user_should_deal == "y":
user_cards.append(deal_card())
else:
is_game_over = True
while computer_score != 0 and computer_score < 17:
computer_cards.append(deal_card())
computer_score = calculate_score(computer_cards)
print(f" Your final hand: {user_cards}, final score: {user_score}")
print(f" Computer's final hand: {computer_cards}, final score: {computer_score}")
print(compare(user_score, computer_score))
while input("Do you want to play a game of Blackjack? Type 'y' or 'n': ") == "y":
os.system('cls')
play_game()
a problem with my code
1. The code is dirty.
2. I misunderstood the rules of the game, so I didn't include Blackjack.
3. I didn't include code to ask if the user would like to play the game again
I knew that analysis of requirements was important in university lectures.
However, I did not follow the requirements of this project well.
Through direct experience, I was able to realize how important it is to analyze requirements before coding.
I believe my coding skill will be better!!
'Python' 카테고리의 다른 글
Fourth Python Project: Turtle Racing Game (0) | 2024.08.04 |
---|---|
Third Python Project: Quiz game (1) | 2024.08.03 |
Second Python Project: Number guessing game (0) | 2024.08.03 |
Second Python Project: Number Guessing Game (0) | 2024.07.21 |
Reason for studying python (0) | 2024.05.13 |