This is mostly my own work, so I'm not even sure if I'm doing things correctly, being a novice.
Code: Select all
##blackjack experiment##
init python:
class Card:
def __init__(self, suit, val):
self.suit = suit
self.value = val
class Deck:
def __init__(self):
self.cards = []
self.build()
def build(self):
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(1, 14):
self.cards.append(Card(s, v))
def show(self):
for c in self.cards:
c.show()
def shuffle(self):
for i in range(len(self.cards) - 1, 0, -1):
r = renpy.random.randint(0, i)
self.cards[i], self.cards[r] = self.cards[r], self.cards[i]
def drawCard(self):
drawn = self.cards.pop()
if drawn == 11:drawn = "J"
if drawn == 12:drawn = "Q"
if drawn == 13:drawn = "K"
if drawn == 14:drawn = "A"
return drawn
class Player:
def __init__(self, name):
self.name = name
self.hand = []
def draw(self, deck):
self.hand.append(deck.drawCard())
return self
def showHand(self):
cards = ""
for card in self.hand:
cards += "{} of {}".format(card.value, card.suit) + " "
return cards
def discard(self):
del self.hand[:]
def total(hand):
total = 0
for card in hand:
if card == "J" or card == "Q" or card == "K":
total += 10
elif card == "A":
if total >= 11:
total += 1
else:
total += 11
else:
total += card
return total
define deck = Deck()
define player1 = Player("Your")
define dealer = Player("Dealer")
default discard_pile = []
default first_bet = True
label start:
if len(deck.cards) == 0:
$ del discard_pile[:]
$ discard_pile = []
$ deck.shuffle()
else:
pass
label hand_start:
$ player1.draw(deck)
$ player1.draw(deck)
$ dealer.draw(deck)
$ dealer.draw(deck)
$ player1_cards = player1.showHand()
$ dealer_cards = dealer.showHand()
$ first_bet = True
"Discard pile: [discard_pile]"
"[player1.name] cards are [player1_cards]"
"[dealer.name] cards are [dealer_cards]"
label turn:
menu:
"Hit":
$ player1.draw(deck)
$ player1_cards = player1.showHand()
$ first_bet = False
"[player1_cards]"
if player1.total < 21:
jump turn
elif player1.total == 21:
jump results
elif player1.total > 21:
jump bust
"Stand":
jump results
"Double down" if first_bet:
$ player1.draw(deck)
$ player1_cards = player1.showHand()
jump results
return
label bust:
"Over 21, Bust"
call reset_hand
jump hand_start
label results:
if dealer.total == player1.total:
call reset_hand
jump push
elif dealer.total > player1.total:
call reset_hand
jump lose
elif dealer.total < player1.total:
call reset_hand
jump win
label push:
"Push"
jump hand_start
label lose:
"You lose"
jump hand_start
label win:
"You win"
jump hand_start
label reset_hand:
$ player1_discard = player1.showHand()
$ dealer_discard = dealer.showHand()
$ discard_pile.append(dealer_discard)
$ discard_pile.append(player1_discard)
$ player1.discard()
$ dealer.discard()
return
Tonight, I added the ability to add up the value of the cards drawn in hopes of creating a blackjack game. I got as far as ensuring the player could draw more cards as desired (but haven't worked on the dealer yet) and it should automatically send you to the correct label (win, lose, bust, blackjack, etc)
Instead, it seems to draw the cards out in reverse order every single time, starting with the King of Hearts and I don't know why.
The scoring system also doesn't seem to work, always sending the player to "lose" and sometimes giving weird results.
Cards 11 -14 are supposed to be replaced with J - A respectively, but aren't and I'm unsure why.
What puzzles me most is why the "discard_pile" list shows [] when shown as text and when populated, shows ['u'<card>]