Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Blackjack/blackjack.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
174 lines (151 sloc)
5.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def start(): | |
f=open("deck.txt","r") | |
deck=[] | |
for line in f: | |
line=line.split(' ') | |
line.pop(-1) | |
deck.append(line) | |
import random | |
import time | |
l="\n" | |
s=[1,2] | |
dealercards=[] | |
for card in s: | |
dealer_card=deck[random.randint(0,len(deck)-1)] | |
deck.remove(dealer_card) | |
dealercards.append(dealer_card) | |
playercards=[] | |
for card in s: | |
player_card=deck[random.randint(0,len(deck)-1)] | |
deck.remove(player_card) | |
playercards.append(player_card) | |
def playerdeck(): | |
pos=0 | |
c=1 | |
print(l+"Your deck:") | |
for card in playercards: | |
print(str(c)+".\t"+str(playercards[pos][0])) | |
pos=pos+1 | |
c=c+1 | |
def dealerdeck(): | |
pos=0 | |
c=1 | |
print(l+"Dealer deck:") | |
for card in dealercards: | |
print(str(c)+".\t"+str(dealercards[pos][0])) | |
pos=pos+1 | |
c=c+1 | |
print("Dealer's Card 1: ????") | |
print("Dealer's Card 2: "+ str(dealercards[1][0])) | |
playerdeck() | |
dealer_total=int(dealercards[0][-1])+int(dealercards[1][-1]) | |
player_total=int(playercards[0][-1])+int(playercards[1][-1]) | |
def ace_check(): | |
count=-1 | |
for card in playercards: | |
count=count+1 | |
if 'Ace' in card[0]: | |
if len(card)==2: | |
continue | |
else: | |
pass | |
ace=input(l+"You have an Ace! Assign value of '1' or '11': ") | |
if ace=='1': | |
playercards[count].remove(card[-1]) | |
player_card=playercards[count] | |
return player_card | |
break | |
elif ace=='11': | |
playercards[count].remove(card[1]) | |
player_card=playercards[count] | |
return player_card | |
break | |
else: | |
print("Invalid answer. Computer assigns value") | |
playercards[count].remove(card[random.randint(1,2)]) | |
player_card=playercards[count] | |
return player_card | |
break | |
def dealer_ace_check(): | |
count=-1 | |
for card in dealercards: | |
count=count+1 | |
if 'Ace' in card[0]: | |
if dealer_total<21: | |
val=21-dealer_total | |
if val<=11: | |
dealercards[count].remove(card[1]) | |
dealer_card=dealercards[count] | |
return dealer_card | |
elif val>11: | |
dealercards[count].remove(card[-1]) | |
dealer_card=dealercards[count] | |
return dealer_card | |
for ace in s: | |
ace_check() | |
dealer_ace_check() | |
play="" | |
def total_check(): | |
if play=="stay": | |
if dealer_total>16 and player_total>16 and dealer_total==player_total: | |
print("DRAW") | |
elif dealer_total>player_total and dealer_total<=21: | |
print("PLAYER LOSS") | |
elif dealer_total<player_total and player_total<=21: | |
print("PLAYER WIN") | |
pass | |
if player_total==21 and dealer_total!=21: | |
print("PLAYER WIN") | |
elif player_total!=21 and dealer_total==21: | |
print("PLAYER LOSS") | |
elif player_total==21: | |
print("PLAYER WIN") | |
elif dealer_total==21: | |
print("PLAYER LOSS") | |
elif player_total>21: | |
print("PLAYER LOSS") | |
elif dealer_total>21: | |
print("PLAYER WIN") | |
while player_total<=21 and dealer_total<=21: | |
if player_total==21: | |
total_check() | |
break | |
play=str(input("Enter 'hit' or 'stay': ")) | |
if play=="stay" and player_total<=16: | |
print("Must hit when card total less than 17") | |
continue | |
if play=="hit": | |
pass | |
if play=="hit": | |
print("Player: Drawing a card...") | |
time.sleep(3) | |
player_card=deck[random.randint(0,len(deck)-1)] | |
deck.remove(player_card) | |
playercards.append(player_card) | |
ace_check() | |
playercards[-1]=player_card | |
player_total=player_total+int(player_card[1]) | |
play=="" | |
print(l) | |
playerdeck() | |
total_check() | |
if play=="stay": | |
while dealer_total<=16: | |
dealer_ace_check() | |
dealer_card=deck[random.randint(0,len(deck)-1)] | |
deck.remove(dealer_card) | |
dealercards.append(dealer_card) | |
dealer_total=dealer_total+int(dealer_card[1]) | |
print("Dealer: Drawing a card...") | |
time.sleep(3) | |
dealerdeck() | |
if dealer_total>=17: | |
dealerdeck() | |
total_check() | |
break | |
start() | |
####ISSUES | |
## 1) total_check(): Will print PLAYER LOSS after every hit when dealer_total is 21 from start. | |
## 2) dealer_ace_check(): No play input given when dealer has two aces bc total=22 | |
## 3) total_check(): Ace assigned 11 -> stay -> hit. Cards: Ace+3+7=21. Prints PLAYER WIN twice. | |
## 4) total_check(): Ace assigned 11 -> stay. Dealer cards: Ace+jack=21. Print PLAYER LOSS twice. | |