Hi. I just joined this forum because I really need some quick advice on a Blackjack game I'm working on. It's my first major endeavor, and I can't quite get it to work right. Here's the code.
#blackjack.py
from cards import *
from graphics import *
import random
DIM = 400
FILE = "blackjack.data"
BLACKJACK = 21
ACE = 1
ACE_HIGH = 11
CARD_X = 50
X_DIFF = 40
DLR_CARD_Y = 70
PLYR_CARD_Y = 340
def makeTable():
board = GraphWin("BlackJack!", DIM, DIM)
boardTop = Rectangle(Point(0,0), Point(DIM,DIM))
boardTop.setFill("green")
boardTop.draw(board)
quitbutton = Rectangle(Point(50,170), Point(120,220))
quitbutton.setFill("red")
quitbutton.draw(board)
quitText = Text(Point(85,195), "Quit")
quitText.setTextColor("black")
quitText.draw(board)
hitmeButton = Rectangle(Point(180,170), Point(250,220))
hitmeButton.setFill("blue")
hitmeButton.draw(board)
hitmeText = Text(Point(215,195), "Hit me")
hitmeText.setTextColor("white")
hitmeText.draw(board)
stayButton = Rectangle(Point(310,170), Point(380,220))
stayButton.setFill("yellow")
stayButton.draw(board)
stayText = Text(Point(345,195), "Stay")
stayText.setTextColor("black")
stayText.draw(board)
return board, quitbutton, hitmeButton, stayButton
def closeTable(window):
window.close()
def cardSums( cards ):
"""Return a list of all the possible sums of the values of
this list of cards, given that an Ace can be worth
either 1 (ACE) or 11 (ACE_HIGH)."""
results = []
values = []
values.extend(cards)
changed = True
while changed == True:
totalSum = 0
changed = False
for i in range(len(values)):
totalSum = totalSum + values
if values == ACE and changed == False:
values = ACE_HIGH
changed = True
results.append(totalSum)
return results
def maxUnder( sums, limit ):
"""Return the largest value in the sums list that is
less than or equal to limit. If there is no such value,
return None.
Precondition: the values in sums are in ascending order"""
result = None
i = 0
while i < len(sums) and sums <= limit:
result = sums
i += 1
return result
def makeDeck():
deck = []
card = None
for i in range(len(suits)):
for x in range(1, len(ranks) + 1):
rank = x
suit = i
card = Card(rank, suit)
deck.append(card)
random.shuffle(deck)
return deck
def recordStats(winner):
scores = [None, None]
outfile = open(FILE, 'w')
if winner == 0:
scores[1] += 1
elif winner == 1:
scores[0] += 1
outfile.write(str(scores[0]) + "\n")
outfile.write(str(scores[1]))
outfile.close()
##def readStats(win):
## infile = open(FILE, 'r')
## scores = [int(infile.readline().strip()), int(infile.readline().strip())]
## winner = raw_input("Who won the game? ")
##
def drawCards(player, hand, window):
#Observes the cards in the player's hand and draws each one
counter = 0
if player == 0:
for card in hand:
card.drawAt(Point(CARD_X + (X_DIFF * counter), DLR_CARD_Y), window)
counter += 1
if player == 1:
for card in hand:
card.drawAt(Point(CARD_X + (X_DIFF * counter) , PLYR_CARD_Y), window)
counter += 1
def makeHands( deck ):
#picks the cards from the deck and puts them into a list
hand1 = []
hand2 = []
hand1.append(deck[0])
del deck[0]
hand1.append(deck[0])
del deck[0]
hand2.append(deck[0])
del deck[0]
hand2.append(deck[0])
del deck[0]
return hand1, hand2
def buttonPressed(point, rectangle):
p1 = rectangle.getP1()
p2 = rectangle.getP2()
clickX = point.getX()
clickY = point.getY()
xP1 = p1.getX()
xP2 = p2.getX()
yP1 = p1.getY()
yP2 = p2.getY()
return clickX > xP1 and clickX < xP2 and \
clickY > yP1 and clickY < yP2
def getMouseClick(rectangles, window):
clicked = None
click = window.getMouse()
for rectangle in rectangles:
if buttonPressed(click, rectangle) == True:
clicked = rectangle
break
return clicked
def pickCard(player, deck, hand):
hand.append(deck[0])
del deck[0]
drawCards(player, deck, hand)
def cardValues(hand):
vals = []
value = None
rank = None
suit = None
for card in hand:
rank = card.getRank()
if rank == 11 or rank == 12 or rank == 13:
rank = 10
vals.append(rank)
return vals
def main():
win, quitBut, hitBut, stayBut = makeTable()
deck = makeDeck()
player, dealer = makeHands(deck)
drawCards(0, dealer, win)
drawCards(1, player, win)
print cardValues(player)
while maxUnder(cardSums(cardValues(player)),BLACKJACK) <= BLACKJACK:
buttonHit = getMouseClick( (quitBut, hitBut, stayBut), win)
while buttonHit != quitBut:
if buttonHit == hitBut:
pickCard(1, deck, player)
drawCards(1, player, win)
elif buttonHit == stayBut:
pickCard(0, deck, dealer)
drawCards(0, dealer, win)
else:
closeTable(win)
if maxUnder(cardsums(cardValues(player)),BLACKJACK) > BLACKJACK:
recordStats(0)
if __name__ == '__main__':
main()
I've been taught using the graphics.py class built by John Zelle (it came with a textbook). For some reason, the closeTable function always gets called when I run it.
I hope that the exclusion thing doesn't keep people from being able to answer. I can't in good conscience post the source codes for other classes without permission.
If you could offer some help, I'd appreciate it. Thanks!
- Weebl4551