Question: I have my working code now need to edit to a GUI using sqlite and tkinter and I'm lost. Looking to store money and users
I have my working code now need to edit to a GUI using sqlite and tkinter and I'm lost. Looking to store money and users name in DB with start time stop time and duration played. Code below:
##################################
## Blackjack##
## Python Programming##
## Chester Facey##
## 4/02/2020##
##################################
import random, os, sys
cardName = { 1: 'Ace', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', 11: 'Jack', 12: 'Queen', 13: 'King' }
cardSuit = { 'c': 'Clubs', 'h': 'Hearts', 's': 'Spades', 'd': 'Diamonds' }
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return(cardName[self.rank]+" Of "+cardSuit[self.suit])
def getRank(self):
return(self.rank)
def getSuit(self):
return(self.suit)
def BJValue(self):
if self.rank > 9:
return(10)
else:
return(self.rank)
def showHand(hand):
for card in hand:
print(card)
def showCount(hand):
print("__________________________")
print("Your Hand Total: "+str(handCount(hand)))
print("__________________________")
def handCount(hand):
handCount=0
for card in hand:
handCount += card.BJValue()
return(handCount)
def gameEnd(score):
print("****Final Score****", " Computer: "+str(score['computer'])+" You: "+str(score['player1']))
sys.exit(0)
deck= []
suits = [ 'c','h','d','s' ]
score = { 'computer': 0, 'player1': 0 }
hand= { 'computer': [],'player1': [] }
for suit in suits:
for rank in range(1,14):
deck.append(Card(rank,suit))
keepPlaying = True
while keepPlaying:
random.shuffle(deck)
random.shuffle(deck)
random.shuffle(deck)
hand['player1'].append(deck.pop(0))
hand['computer'].append(deck.pop(0))
hand['player1'].append(deck.pop(0))
hand['computer'].append(deck.pop(0))
playplayer1 = True
bustedplayer1 = False
while playplayer1:
os.system('clear')
print()
print("Blackjack!")
print("__________________________")
print("Games Won:")
print("__________________________")
print(" Computer: "+str(score['computer'])+" You: "+str(score['player1']))
print()
print("__________________________")
print()
print('Computer Shows: '+ str(hand['computer'][-1]))
print("__________________________")
print()
print('Your Hand: ')
showHand(hand['player1'])
showCount(hand['player1'])
print()
inputCycle = True
userInput = ''
while inputCycle:
userInput = input("(H)it, (S)tand, or (Q)uit: ").upper()
if userInput == 'H' or 'S' or 'Q':
inputCycle = False
if userInput == 'H':
hand['player1'].append(deck.pop(0))
if handCount(hand['player1']) > 21:
playplayer1 = False
bustedplayer1 = True
elif userInput == 'S':
playplayer1 = False
else:
gameEnd(score)
playComputer = True
bustedComputer = False
while not bustedplayer1 and playComputer:
print(handCount(hand['computer']))
if handCount(hand['computer'])<17:
hand['computer'].append(deck.pop(0))
else:
playComputer = False
if handCount(hand['computer'])>21:
playComputer = False
bustedComputer = True
if bustedplayer1:
print('Player Busted')
score['computer'] += 1
elif bustedComputer:
print('Computer Busted')
score['player1'] += 1
elif handCount(hand['player1']) > handCount(hand['computer']):
print('Player Wins')
score['player1'] += 1
else:
print('Computer Wins')
score['computer'] += 1
print()
print('Computer Hand:')
showHand(hand['computer'])
showCount(hand['computer'])
print()
print('Player Hand:')
showHand(hand['player1'])
showCount(hand['player1'])
print()
if input("(Q)uit or enter for next round").upper() == 'Q':
gameEnd(score)
deck.extend(hand['computer'])
deck.extend(hand['player1'])
del hand['computer'][:]
del hand['player1'][:]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
