def display_row(contestant): ''' displays 6 cards in a row args: contestant = string: specifies which row cards are the be displayed on effect: ''' global dealer_hand_display global player_hand_display if contestant == 'player': vs = 1 save_loc = player_hand_display if contestant == 'dealer': vs = 0 save_loc = dealer_hand_display for i in range(6): card_disp = CardLabel(root) card_disp.grid(row=vs,column=(i)) root.columnconfigure(i, minsize=85) save_loc.append(card_disp) card_disp.display('blank')
def hit(self): ''' Appends a card to the players hand and then displays that card. If the player busts gameover() is called''' self._player.append(self._deck.deal(1)[0]) self.hits = CardLabel(root) self.hits.grid(row=3, column=len(self._player) - 1) self.hits.display('front', self._player[-1].num()) #print(self._player) if (BlackJack.total(self._player)) > 21: BlackJack.gameover(self, 0, 'You Busted!')
def stand(self): ''' this function is called when the player clicks the pass button. To stand in blackjack means you pass priority and allow the dealer to play out their hand ''' self._dealercard1.display('front', self._dealer[0].num()) while (BlackJack.total(self._dealer) < 22): if (BlackJack.total(self._dealer)) < 17: self._dealer.append(self._deck.deal(1)[0]) self.dealerhits = CardLabel(root) #self.dealerhits.after(2000) self.dealerhits.grid(row=1, column=len(self._dealer) - 1) self.dealerhits.display('front', self._dealer[-1].num()) else: return self.compare(self._dealer, self._player) return BlackJack.gameover(self, 1, 'Dealer Busted!')
def deal(self): ''' Adds cards to the game: Creates a deck, shuffles the deck, deals cards to the dealer and then the player ''' BlackJack.__init__( self) # Reinstantiates the class to reset the game space self._deck = Deck.Deck() # 1) Create Deck self._deck.shuffle() # 2) shuffle deck self._dealer = self._deck.deal(2) # 3) Create Dealers Hand self._dealercard1 = CardLabel(root) self._dealercard1.grid(row=1, column=0) self._dealercard1.display('back', self._dealer[0].num()) self._dealercard2 = CardLabel(root) self._dealercard2.grid(row=1, column=1) self._dealercard2.display('front', self._dealer[1].num()) self._player = self._deck.deal(2) # 4) Create Players hand for i in range(len(self._player)): self._playercards = CardLabel(root) self._playercards.grid(row=3, column=i) self._playercards.display('front', self._player[i].num())
def flip(): global n, x card = [one, two, three] if x == 3: n = (n + 1) % 3 x = 0 card[x].display(sides[n], randint(0,51)) x += 1 root = tk.Tk() CardLabel.load_images() root.rowconfigure(0, minsize=130) for i in range(3): root.columnconfigure(i, minsize=100) one = CardLabel(root) one.grid(row=0, column=0, padx = 10, pady = 10) two = CardLabel(root) two.grid(row=0, column=1, padx = 10, pady = 10) three = CardLabel(root) three.grid(row=0, column=2, padx = 10, pady = 10) button = tk.Button(root, text='Flip', command=flip) button.grid(row=1, column=1, pady = 10) if __name__ == "__main__": root.mainloop()
elif counter1 != counter2 and counter2 == counter3: #If first card flipped, then second card flips counter2 = (counter2 + 1) % 3 elif counter2 != counter3: #If the first and second card is flipped, the third card is counter3 = (counter3 + 1) % 3 if counter1 == 0 and counter2 == 0 and counter3 == 0: #Assigns new cards when cards flipped to back again rand_num1 = randint(0,51) rand_num2 = randint(0,51) rand_num3 = randint(0,51) a.display(flipping_status[counter1], rand_num1)#Displays first card b.display(flipping_status[counter2], rand_num2)#Displays second card c.display(flipping_status[counter3], rand_num3)#Displays third card CardLabel.load_images() #Loads the images of the cards a = CardLabel(root) #Makes 'a' a label of class CardLabel a.grid(row=0, column=0) b = CardLabel(root) #Makes 'b' a label of class CardLabel b.grid(row=0,column=1) c = CardLabel(root) #Makes 'c' a label of class CardLabel c.grid(row=0,column=2) button = Button(root,text='Flip', command=flipping_cards) #Flips the cards button.grid(row=1,column=1) if __name__ == '__main__': root.mainloop() #Calls mainloop
n has a pattern of 0,1,2,0,1,2,... in order to alternate between cards every click """ global a, n, card a = (a + 1) % 9 w = a // 3 n = a % 3 #print(a,n,w) card[n].display(side[w], randint(0, 51)) root = Tk() CardLabel.load_images() card = [] for i in range(3): card.append(CardLabel(root)) card[i].grid(row=0, column=i) root.rowconfigure(0, minsize=115) root.columnconfigure(0, minsize=85) button = Button(root, text="Flip", command=flip) button.grid(row=1, column=1, pady=10) if __name__ == "__main__": root.mainloop()
from tkinter import * from random import randint from CardLabel import * side = ["back", "front", "blank"] n = 0 def flip(): global n, label n = (n + 1) % 3 label.display(side[n], randint(0, 51)) root = Tk() CardLabel.load_images() label = CardLabel(root) label.grid(row=0, column=0) root.rowconfigure(0, minsize=115) root.columnconfigure(0, minsize=85) button = Button(root, text="Flip", command=flip) button.grid(row=1, column=0, pady=10) if __name__ == "__main__": root.mainloop()
# Spring 2014 # Note: see flip_one_frame.py for a better organization.... from tkinter import * from random import randint from CardLabel import * sides = ['back', 'front', 'blank'] # values that can be passed to display method n = 0 # index into list of sides def flip(): global n, label n = (n + 1) % 3 label.display(sides[n], randint(0,51)) # 2nd arg ignored if first is not 'front' root = Tk() CardLabel.load_images() # call after creating top level app label = CardLabel(root) # image on a new CardLabel is 'back' label.grid(row=0, column=0) # call x.grid() to place widget x in its parent's grid root.rowconfigure(0, minsize=115) # note the grid is part of the parent... root.columnconfigure(0, minsize=85) button = Button(root, text='Flip', command=flip) button.grid(row=1, column=0, pady = 10) if __name__ == '__main__': root.mainloop()
class BlackjackFrame(Frame): """ Subclass of Frame, creates a blackjack game. """ _player_hand = [] _dealer_hand = [] _player_move = 0 _dealer_move = 0 _player_wins = 0 _dealer_wins = 0 def __init__(self, parent): """ Buttons and card labels initialized. """ Frame.__init__(self, parent) self.configure(background = 'white') self._cards = Deck(BlackjackCard) self._cards.shuffle() self.pack() CardLabel.load_images() self._dc1 = CardLabel(self) self._dc1.grid(row = 0, column = 0) self._dc2 = CardLabel(self) self._dc2.grid(row = 0, column = 1) self._dc3 = CardLabel(self) self._dc3.grid(row = 0, column = 2) self._dc4 = CardLabel(self) self._dc4.grid(row = 0, column = 3) self._dc5 = CardLabel(self) self._dc5.grid(row = 0, column = 4) self._dc6 = CardLabel(self) self._dc6.grid(row = 0, column = 5) self._pc1 = CardLabel(self) self._pc1.grid(row = 1, column = 0) self._pc2 = CardLabel(self) self._pc2.grid(row = 1, column = 1) self._pc3 = CardLabel(self) self._pc3.grid(row = 1, column = 2) self._pc4 = CardLabel(self) self._pc4.grid(row = 1, column = 3) self._pc5 = CardLabel(self) self._pc5.grid(row = 1, column = 4) self._pc6 = CardLabel(self) self._pc6.grid(row = 1, column = 5) self._deal = Button(self, text = 'Deal', command = self.dealcb) self._deal.grid(row = 2, column = 0, padx = 10, pady = 10) self._hit = Button(self, text = 'Hit', command = self.hitcb) self._hit.grid(row = 2, column = 2, padx = 10, pady = 10) self._stand = Button(self, text = 'Stand', command = self.standcb) self._stand.grid(row = 2, column = 4, padx = 10, pady = 10) self.dealcb() def dealcb(self): """ Resets player and dealer hands with 2 cards each from top of deck and sets up starting layout for each hand. """ self._hit.configure(state = ACTIVE) self._stand.configure(state = ACTIVE) old = self._dealer_hand + self._player_hand self._cards.restore(old) self._player_hand = self._cards.deal(2) self._dealer_hand = self._cards.deal(2) self._player_move = 0 self._dealer_move = 0 self._dc1.display('back') self._dc2.display('front', self._dealer_hand[1]._id) self._dc3.display('blank') self._dc4.display('blank') self._dc5.display('blank') self._dc6.display('blank') self._pc1.display('front', self._player_hand[0]._id) self._pc2.display('front', self._player_hand[1]._id) self._pc3.display('blank') self._pc4.display('blank') self._pc5.display('blank') self._pc6.display('blank') Label(self, text = 'Dealer Wins: ').grid(row = 3, column = 2, padx = 10, pady = 10) self.dcount = Label(self, text = self._dealer_wins) self.dcount.update_idletasks() self.dcount.grid(row = 3, column = 3, padx = 10, pady = 10) Label(self, text = 'Player Wins: ').grid(row = 4, column = 2, padx = 10, pady = 10) self.pcounts = Label(self, text = self._player_wins) self.pcounts.grid(row = 4, column = 3, padx = 10, pady = 10) self.pcounts.update_idletasks() def hitcb(self): """ Appends card from top of deck to players hand, changes display of cards in players hand from blank to the new card. """ label = [self._pc3, self._pc4, self._pc5, self._pc6] if self._player_move < 4 and self.total(self._player_hand) <= 21: card = self._cards.deal(1) self._player_hand.extend(card) label[self._player_move].display('front', self._player_hand[self._player_move + 2]._id) self._player_move += 1 if self.total(self._player_hand) > 21: self._hit.configure(state = DISABLED) showinfo(title = 'Game Over', message = 'You Lose, Total Over 21') self._player_move = 0 self._dealer_wins += 1 def standcb(self): """ 'flips' over dealers first card to front, compares total points of player and dealer to determine winner. """ label = [self._dc3, self._dc4, self._dc5, self._dc6] while self.total(self._dealer_hand) < 17: card = self._cards.deal(1) self._dealer_hand.extend(card) label[self._dealer_move].display('front', self._dealer_hand[self._dealer_move + 2]._id) self._dealer_move += 1 self._hit.configure(state = DISABLED) self._stand.configure(state = DISABLED) self._dc1.display('front', self._dealer_hand[0]._id) dealer_total, player_total = self.total(self._dealer_hand), self.total(self._player_hand) if dealer_total <= 21 and player_total <= 21: if dealer_total > player_total: showinfo(title = 'Game Over', message = 'You Lose, Dealer Wins') self._dealer_wins += 1 elif player_total > dealer_total: showinfo(title = 'Game Over', message = 'You Win!') self._player_wins += 1 elif dealer_total > 21: showinfo(title = 'Game Over', message = 'You Win!') self._player_wins += 1 else: showinfo(title = 'Game Over', message = "It's a tie") else: showinfo(title = 'Game Over', message = "It's a tie") def total(self, hand): """ Calculates the total for a given hand. """ points = 0 n_aces = 0 for card in hand: points += card.points() if card.rank() == 12: n_aces += 1 while points > 21 and n_aces > 0: points -= 10 n_aces -= 1 return points
elif dealer_score < player_total or dealer_score > 21: showinfo("Game Over", "Player 1 Wins!") else: showinfo("Game Over", "Its a Tie!") total() """ This next section is filled with display info for the tkinter. The card labels are made with [Cardlabel(root) for i in range(6)] for better usability. """ root = Tk() CardLabel.load_images() Dcard = [CardLabel(root) for i in range(6)] Pcard = [CardLabel(root) for i in range(6)] dealerpos = 0 for x in Dcard: x.grid(row=0, column = dealerpos) x.display(side = 'blank') dealerpos += 1 playerpos = 0 for y in Pcard: y.grid(row=1, column = playerpos) y.display(side = 'blank') playerpos += 1
############################################################################################################### root = Tk() CardLabel.load_images() # Call after creating top level app ############################################################################################################### # Card labels dealer_label = [0]*6 player_label = [0]*6 for card in range(6): # Create labels for dealer dealer_label[card] = CardLabel(root) dealer_label[card].grid(row=0, column=card, padx=10, pady=10) dealer_label[card].display('blank') # Create labels for player player_label[card] = CardLabel(root) player_label[card].grid(row=1, column=card, padx=10, pady=10) player_label[card].display('blank') ############################################################################################################### # Score update label dealer_win = Label(root, text='Dealer\'s score: ') dealer_win.grid(row=0, column=6, sticky=W, padx=20, pady=10) player_win = Label(root, text='Player\'s score: ')
def __init__(self, parent): """ Buttons and card labels initialized. """ Frame.__init__(self, parent) self.configure(background = 'white') self._cards = Deck(BlackjackCard) self._cards.shuffle() self.pack() CardLabel.load_images() self._dc1 = CardLabel(self) self._dc1.grid(row = 0, column = 0) self._dc2 = CardLabel(self) self._dc2.grid(row = 0, column = 1) self._dc3 = CardLabel(self) self._dc3.grid(row = 0, column = 2) self._dc4 = CardLabel(self) self._dc4.grid(row = 0, column = 3) self._dc5 = CardLabel(self) self._dc5.grid(row = 0, column = 4) self._dc6 = CardLabel(self) self._dc6.grid(row = 0, column = 5) self._pc1 = CardLabel(self) self._pc1.grid(row = 1, column = 0) self._pc2 = CardLabel(self) self._pc2.grid(row = 1, column = 1) self._pc3 = CardLabel(self) self._pc3.grid(row = 1, column = 2) self._pc4 = CardLabel(self) self._pc4.grid(row = 1, column = 3) self._pc5 = CardLabel(self) self._pc5.grid(row = 1, column = 4) self._pc6 = CardLabel(self) self._pc6.grid(row = 1, column = 5) self._deal = Button(self, text = 'Deal', command = self.dealcb) self._deal.grid(row = 2, column = 0, padx = 10, pady = 10) self._hit = Button(self, text = 'Hit', command = self.hitcb) self._hit.grid(row = 2, column = 2, padx = 10, pady = 10) self._stand = Button(self, text = 'Stand', command = self.standcb) self._stand.grid(row = 2, column = 4, padx = 10, pady = 10) self.dealcb()
class BlackJack(Frame): ''' Creates Widgets: labels for players and buttons for the user to interact with and play the game ''' players = ['Dealer:', 'Player1:'] def __init__(self, master=None): Frame.__init__(self, master) CardLabel.load_images() # Create Labels dealerLabel = Label(root, text=self.players[0]) dealerLabel.grid(row=0, column=0) player1Label = Label(root, text=self.players[1]) player1Label.grid(row=2, column=0) # Set dimensions of the game space for i in range(6): root.columnconfigure(i, minsize=80) for i in [1, 3]: root.rowconfigure(i, minsize=106) # Buttons self._dealButton = Button(root, text='Deal', command=self.deal, padx=10) self._dealButton.grid(row=4, column=0, pady=0) self._hitButton = Button(root, text='Hit', command=self.hit, padx=14) self._hitButton.grid(row=4, column=1, pady=0) self._passButton = Button(root, text='Pass', command=self.stand, padx=10) self._passButton.grid(row=4, column=2, pady=0) def deal(self): ''' Adds cards to the game: Creates a deck, shuffles the deck, deals cards to the dealer and then the player ''' BlackJack.__init__( self) # Reinstantiates the class to reset the game space self._deck = Deck.Deck() # 1) Create Deck self._deck.shuffle() # 2) shuffle deck self._dealer = self._deck.deal(2) # 3) Create Dealers Hand self._dealercard1 = CardLabel(root) self._dealercard1.grid(row=1, column=0) self._dealercard1.display('back', self._dealer[0].num()) self._dealercard2 = CardLabel(root) self._dealercard2.grid(row=1, column=1) self._dealercard2.display('front', self._dealer[1].num()) self._player = self._deck.deal(2) # 4) Create Players hand for i in range(len(self._player)): self._playercards = CardLabel(root) self._playercards.grid(row=3, column=i) self._playercards.display('front', self._player[i].num()) def hit(self): ''' Appends a card to the players hand and then displays that card. If the player busts gameover() is called''' self._player.append(self._deck.deal(1)[0]) self.hits = CardLabel(root) self.hits.grid(row=3, column=len(self._player) - 1) self.hits.display('front', self._player[-1].num()) #print(self._player) if (BlackJack.total(self._player)) > 21: BlackJack.gameover(self, 0, 'You Busted!') def stand(self): ''' this function is called when the player clicks the pass button. To stand in blackjack means you pass priority and allow the dealer to play out their hand ''' self._dealercard1.display('front', self._dealer[0].num()) while (BlackJack.total(self._dealer) < 22): if (BlackJack.total(self._dealer)) < 17: self._dealer.append(self._deck.deal(1)[0]) self.dealerhits = CardLabel(root) #self.dealerhits.after(2000) self.dealerhits.grid(row=1, column=len(self._dealer) - 1) self.dealerhits.display('front', self._dealer[-1].num()) else: return self.compare(self._dealer, self._player) return BlackJack.gameover(self, 1, 'Dealer Busted!') def compare(self, dealer, player): ''' Compares Dealer hand to Players hand and makes the call to gameover() with appropriate results ''' dealertotal, playertotal = BlackJack.total(dealer), BlackJack.total( player) if dealertotal > playertotal: BlackJack.gameover(self, 0) elif dealertotal < playertotal: BlackJack.gameover(self, 1) elif dealertotal == 21 and 2 == len(dealer) < len(player): BlackJack.gameover(self, 0, "Dealer BlackJack!") elif playertotal == 21 and 2 == len(player) < len(dealer): BlackJack.gameover(self, 0, "Player BlackJack!") else: BlackJack.gameover(self, 2, "Push") def gameover(self, win=1, result="Highscore wins"): ''' called from many other methods; reports if the player wins or loses the game via message box and signals player to start new a game ''' global Score script = [" YOU LOSE... ", "YOU WIN!", "No winners"] msg = showinfo(message=(script[win], " Click the 'Deal' button to play again"), title=("GAME OVER:", result)) # This should be a message box self._hitButton['state'] = 'disabled' self._passButton['state'] = 'disabled' self._dealButton['bg'] = 'green' def total(hand): ''' calculates the total point value of a hand, allowing for smart handing of Aces dual, but mutually exclusive value state ''' values = { "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10, "J": 10, "Q": 10, "K": 10, "A": 11 } result = 0 numAces = 0 for i in range(len(hand)): result += values[hand[i].symb()] if hand[i].symb() == 'A': numAces += 1 while result > 21 and numAces > 0: result -= 10 numAces -= 1 return result
def display_cards(hand,restart=False,show_hand=False): ''' This creates the labels for each user and makes the first card of the comp uters hand visible if show hand is true. If restart is true then all lables are reset. ''' for i in range(len(hand)): a = CardLabel(root) if i == 0 and hand == computer_hand and not show_hand: a.display('back',hand[i].n) else: a.display('front',hand[i].n) if hand == computer_hand: a.grid(row=0,column=i+1) else: a.grid(row=1,column=i+1) if restart == True: for i in range(len(hand),6): a = CardLabel(root) a.display(1) if hand == computer_hand: a.grid(row=0,column=i+1) else: a.grid(row=1,column=i+1)
else: a.grid(row=1,column=i+1) if restart == True: for i in range(len(hand),6): a = CardLabel(root) a.display(1) if hand == computer_hand: a.grid(row=0,column=i+1) else: a.grid(row=1,column=i+1) CardLabel.load_images() root.bind("<Key>", key) #Key events deck_lbl = CardLabel(root) deck_lbl.grid(row=0,column=0) deck_lbl.display('back',0) deal_btn = Button(root,text="Deal",command=deal) hit_btn = Button(root,text="Hit",command=hit) hit_btn['state'] = 'disabled' pass_btn = Button(root,text="Pass",command=end_turn) pass_btn['state'] = 'disabled' deal_btn.grid(row=3,column=1) hit_btn.grid(row=3, column=2) pass_btn.grid(row=3,column=3) if __name__ == '__main__': root.mainloop()
from tkinter import * from random import randint from CardLabel import * from Card import * flipper_app = Tk() CardLabel.load_images() box = Frame(flipper_app) box.pack() card1 = CardLabel(box) card1.grid(row = 0, column = 1) card2 = CardLabel(box) card2.grid(row = 0, column = 2) card3 = CardLabel(box) card3.grid(row = 0, column = 3) move = 0 def flip(): global move side = ['front', 'front', 'front', 'blank', 'blank', 'blank', 'back', 'back', 'back'] label = [card1, card2, card3] * 3 if move < 9: label[move].display(side[move], randint(0,51)) flipper_app.update()
while result > 21 and numAces > 0: result -= 10 numAces -= 1 return result root = Tk() CardLabel.load_images() p_display = [] d_display = [] for i in range(6): d_display.append(CardLabel(root)) d_display[i].grid(row = 0, column = i) for i in range(6): p_display.append(CardLabel(root)) p_display[i].grid(row = 1, column = i) deal() root.rowconfigure(0, minsize = 115) root.columnconfigure(0, minsize=85) button = Button(root, text = "Deal", command = deal) button.grid(row= 3,column=0, pady =10)
root.rowconfigure(row, minsize=130) for col in range(6): root.columnconfigure(col, minsize=100) root.columnconfigure(6, minsize=180) # account balance enrty Amount = tk.Entry(root) Amount.grid(row=4, column=0, columnspan=2) Bet = tk.Entry(root) Bet.grid(row=4, column=2, columnspan=2) # top row labels onetop = CardLabel(root) onetop.grid(row=0, column=0, padx = 10, pady = 10) twotop = CardLabel(root) twotop.grid(row=0, column=1, padx = 10, pady = 10) threetop = CardLabel(root) threetop.grid(row=0, column=2, padx = 10, pady = 10) fourtop = CardLabel(root) fourtop.grid(row=0, column=3, padx = 10, pady = 10) fivetop = CardLabel(root) fivetop.grid(row=0, column=4, padx = 10, pady = 10) sixtop = CardLabel(root)
label1.display(sides[side], index) if n == 1: label2.display(sides[side], index) if n == 2: label3.display(sides[side], index) side += 1 if side == 3: side = 0 n = (n + 1) % 3 root = Tk() CardLabel.load_images() label1 = CardLabel(root) label1.grid(row=0, column=0) label2 = CardLabel(root) label2.grid(row=0, column=1) label3 = CardLabel(root) label3.grid(row=0, column=2) root.rowconfigure(0, minsize=115) root.columnconfigure(0, minsize=85) root.columnconfigure(1, minsize=85) root.columnconfigure(2, minsize=85) button = Button(root, text='Flip', command=flip) button.grid(row=1, column=1, pady = 10)