Watch udemy's Video about Object Orienting I thought it might be object-oriented, so I made blackjack instead of trying my skills!
--There is no concept such as a deck. --A is always treated as 1. ――No 5 cards, both bursts are won by the dealer.
import random
CARDS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
CARDS_DIC = {"A": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7,
"8": 8, "9": 9, "10": 10, "J": 10, "Q": 10, "K": 10}
class Hit(list): #Player or dealer hit
def __init__(self, list):
self.list = list
def player_hit(self):
self.list.append(random.choice(CARDS))
print(self.list[-1] + "I pulled")
show_hand(self.list)
def dealer_hit(self):
while True:
if sum_cards(self.list) >= 17 and sum_cards(self.list) <= 21:
return False
elif sum_cards(self.list) < 17:
self.list.append(random.choice(CARDS))
else:
return False
class Interface(list): #Branch
def __init__(self, list):
self.list = list
def hit_or_stand(self):
while True:
answer = input("hit or stand?:").lower()
if answer == "hit":
Hit(self.list).player_hit()
elif answer == "stand":
return False
else:
print("Please enter hit or stand")
if sum_cards(self.list) > 21:
print("Burst")
return False
def classification(player_list, dealer_list): #Judgment of victory or defeat
sum_player = sum_cards(player_list)
sum_dealer = sum_cards(dealer_list)
if 21 >= sum_player > sum_dealer or sum_dealer > 21 >= sum_player:
print(f"you: {sum_player}")
print(f"dealer: {sum_dealer}")
print("You win")
elif 21 >= sum_dealer > sum_player or sum_player > 21 >= sum_dealer or sum_player and sum_dealer > 21:
print(f"you: {sum_player}")
print(f"dealer: {sum_dealer}")
print("You lose")
else:
print(f"you: {sum_player}")
print(f"dealer: {sum_dealer}")
print("It's a draw")
def sum_cards(lists): #Aggregate cards
sum_card = 0
for list in lists:
sum_card += CARDS_DIC[list]
return sum_card
def show_hand(list): #Show the cards in your hand and the total of the cards
player_hand_info = "When".join(list)
print( "Your hand is" + player_hand_info + "is")
print("total" + str(sum_cards(list)) + "is")
def main():
# 1,Player draws a card(2 sheets)
player_hand = random.choices(CARDS, k=2)
player_interface = Interface(player_hand)
# 2,Dealer draws card(2 sheets)
dealer_hand = random.choices(CARDS, k=2)
dealer_hit = Hit(dealer_hand)
print("Game Start")
print()
# 3,Player chooses hit or stand(Until burst or stand)
print("It's your turn")
show_hand(player_hand)
player_interface.hit_or_stand()
print()
#4,Dealer chooses hit or stand(Until the total is 17 or more)
print("It's the dealer's turn")
dealer_hit.dealer_hit()
print()
# 5,Judge and finish
print("Judgment")
classification(player_hand, dealer_hand)#Main processing layer
if __name__ == "__main__":
main()
Game Start
It's your turn
Your hand is K and 9
19 in total
hit or stand?:hit
I drew 7
Your hand is K, 9 and 7
26 in total
Burst
It's the dealer's turn
Judgment
you: 26
dealer: 18
You lose
During development, I didn't understand the class, I feel that the result is strange code. (Compared to the code written by others, my code is in a half-finished state with no person class, only Hit class and Interface class)
I want to study object-oriented programming again and try again.
It's difficult to think object-oriented is difficult!
Recommended Posts