Skip to content

Instantly share code, notes, and snippets.

@NekoTony
Created December 8, 2017 20:22
Show Gist options
  • Save NekoTony/c807498f47ff672538671581c653d1a3 to your computer and use it in GitHub Desktop.
Save NekoTony/c807498f47ff672538671581c653d1a3 to your computer and use it in GitHub Desktop.
Blackjack created by Nerdietony - https://repl.it/@Nerdietony/Blackjack
from random import choice, sample
def game(user, alice):
cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'] * 4
p1cards, botcards, cards = card(cards)
first, second = sample([user, alice], 2)
while True:
p1 = True if first is user else False
bot = True if first is alice else False
p1score = sum([points(x, "user") for x in p1cards])
botscore = sum([points(x, "bot") for x in botcards])
bothit = True
currentgame = True
print("The game has started and cards have been passed out. You have an {} and I have an {}.".format(p1cards[0], botcards[0]))
if p1score == 21:
return print("You win, blackjack!")
if botscore == 21:
return print("You win, blackjack!")
while (p1score < 22) or (botscore < 22):
if p1:
print("Your turn!")
resp = input("Your cards are {}. Your total score is {}. Do you want to hit? `Y/n`".format(mycards(p1cards), p1score))
if resp.lower() == "y":
thecard = choice(cards)
p1cards += thecard
cards.remove(thecard)
p1score += points(thecard, "user")
elif resp.lower() == "n":
currentgame = False
break
else:
print("Not a valid responce, try again.")
continue
if p1score == 21:
print("Blackjack! You won!")
break
elif p1score >= 22:
print("Oh, well you have went over with {}.".format(sum([points(x, "user") for x in p1cards])))
break
else:
print("My turn!")
if bothit:
doihit = choice([True, False])
if doihit:
thecard = choice(cards)
cards.remove(thecard)
botscore += points(thecard, "bot")
else:
bothit = False
if botscore == 21:
print("Blackjack! I won!")
break
elif botscore > 22:
print("Oh, well I have went over with {}.".format(sum([points(x, "bot") for x in botcards])))
break
p1 = not p1
bot = not bot
if currentgame is False:
break
p1w = True
botw = True
if p1:
if p1score >= 22:
p1w = False
else:
if botscore >= 22:
botw = False
if botw is True and p1w is True:
if botscore > p1score:
p1w = False
elif p1score > botscore:
botw = False
elif p1score == botscore:
botw = False
p1w = False
if p1w is True:
print("Player 1 has woo!")
elif botw is True:
print("I have won!")
elif botw is False and p1 is False:
print("It's a tied.")
break
def mycards(cards):
if len(cards) == 2:
return "{} and {}".format(cards[0], cards[1])
else:
return "{}".format(", ".join(cards))
def card(cards):
p1cards_1 = choice(cards)
cards.remove(p1cards_1)
botcards_1 = choice(cards)
cards.remove(botcards_1)
p1cards_2 = choice(cards)
cards.remove(p1cards_2)
botcards_2 = choice(cards)
cards.remove(botcards_2)
return ([p1cards_1, p1cards_2], [botcards_1, botcards_2], cards)
def points(card, player):
try:
return int(card)
except:
if card in ['Jack', 'Queen', 'King']:
return 10
if card == "Ace":
if player is "bot":
return choice([1, 11])
if player is "user":
while True:
num = input("You have an ace! Do you want it to be 1 point or 11?")
if num not in ["1", "11"]:
print("You must pick only 1-11")
continue
else:
break
return int(num)
game("player1", "bot1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment