def new_game(self): try: self.ui_2.close( ) # при нажатии на новую игру оконо закрывается и начинается новая игра except: pass d = DeckGenerator() d.shuffle() t = TableCard(d) t.generate_pyramid() self.deck = t.pyramid_deck_linear self.stack = t.additional_deck # выполняется все то же самое что и и верху. создаются карты мешаются и создается пирамида for card in range(21, 28): self.deck[card].status = True for i in self.stack: i.status = True for i in range(len(self.deck)): eval('self.ui.label_{}.setVisible(True)'.format(i)) eval('self.ui.label_{}.setPixmap(self.deck[{}].pixmap)'.format( i, i)) self.index_stac = 0 self.ui.label_28.setPixmap(self.stack[self.index_stac].pixmap) self.dict_card = dict(zip(self.main_card_list[:28], self.deck)) self.z = []
def __init__(self): # Set up the player's wallet. self.wallet = Wallet() # Create a way to generate new cards. self.deck_generator = DeckGenerator() # Reset the game. self.reset_game()
def test(): # ---------------- Test ---------------- import sys app = QApplication(sys.argv) d = DeckGenerator() d.shuffle() t = TableCard(d) t.generate_pyramid() print(t.pyramid_deck_linear) print(t.pyramid_deck[0][0])
def _start_new_game(self): """Start new game, with new objects.""" D = DeckGenerator() D.shuffle() T = TableCard(D) T.generate_pyramid() gL = GameLogic(T) gL.level = self._level for item in T.pyramid_deck[-1]: item.status = True return Game(D, T, gL)
def test(): # ---------------- Test ---------------- d = DeckGenerator() d.shuffle() t = TableCard(d) t.generate_pyramid() for i in t.pyramid_deck: for j in i: j.status = True print(t) for i in t.additional_deck.deck: i.status = True print(t.additional_deck.deck.index(i), i) gl = GameLogic(t)
def initUI(self): # -- Global d = DeckGenerator() d.shuffle() self.deck = d.deck t = TableCard(d) t.generate_pyramid() gl = GameLogic(t) self.g_logic = gl self.table = self.g_logic.table self.pyramid_cards = self.table.pyramid_deck_linear # open bottom pyramid row for card in self.table.pyramid_deck[-1]: card.status = True # set pixmap for pyramid cards for row in range(len(self.table.pyramid_deck)): for card in range(len(self.table.pyramid_deck[row])): eval('self.ui.label_{}.setPixmap(self.table.pyramid_deck[row]' '[card].pixmap)'.format(str(row) + str(card))) # add labels_ui self.cards_labels.append(eval('self.ui.label_{}'.format( str(row) + str(card)))) # for add cards self.addCardLeft.mousePressEvent = self.add_deck_clicked self.addCardRightF.mouseMoveEvent = self.mouseMoveEvent self.addCardRightF.mouseReleaseEvent = self.mouseReleaseEvent self.addCardLeft.setPixmap( QPixmap(':/images/images/back.png')) self.addCardRightF.setVisible(False) # by default - hide self.addCardRightB.setPixmap(QPixmap(':/images/images/transparent_back' '.png')) # set event for all labels for lbl in self.cards_labels: lbl.mouseMoveEvent = self.mouseMoveEvent lbl.mouseReleaseEvent = self.mouseReleaseEvent self.labels_coord.append(lbl.pos()) self.ui.mousePressEvent = self.mousePressEvent # for all screen # -- actions new_game_action = self.ui.actionNew_Game new_game_action.triggered.connect(self.start_new_game) exit_action = self.ui.actionExit exit_action.triggered.connect(self.show_quit_message) cheat_action = self.ui.actionCheat cheat_action.triggered.connect(self.cheat) self.ui.show()
def __init__(self, parent=None): QWidget.__init__(self, parent) # Эти строчки идут по дефолту self.ui = uic.loadUi('pyramid.ui') self.palette = QPalette() img = QImage('images/index.jpg') scaled = img.scaled(self.ui.size()) self.palette.setBrush(QPalette.Window, QBrush(scaled)) # Присваеваем фону картинку self.ui.setPalette(self.palette) self.ui.label_29.setPixmap(QPixmap( 'images/close.png')) # Присваеваем закрытой карте ее заднюю часть self.main_card_list = [ self.ui.label_0, self.ui.label_1, self.ui.label_2, self.ui.label_3, self.ui.label_4, self.ui.label_5, self.ui.label_6, self.ui.label_7, self.ui.label_8, self.ui.label_9, self.ui.label_10, self.ui.label_11, self.ui.label_12, self.ui.label_13, self.ui.label_14, self.ui.label_15, self.ui.label_16, self.ui.label_17, self.ui.label_18, self.ui.label_19, self.ui.label_20, self.ui.label_21, self.ui.label_22, self.ui.label_23, self.ui.label_24, self.ui.label_25, self.ui.label_26, self.ui.label_27, self.ui.label_28 ] self.ui.mousePressEvent = self.mousePressEvent # Перевызначаю стандартный MausEvent self.mouse_x_y = [] for i in self.main_card_list: self.mouse_x_y.append([i.x(), i.y() ]) # С каждой карты списываем ее координату d = DeckGenerator() d.shuffle() t = TableCard(d) t.generate_pyramid() self.deck = t.pyramid_deck_linear self.stack = t.additional_deck for card in range(21, 28): self.deck[card].status = True for i in self.stack: i.status = True for i in range(len(self.deck)): eval('self.ui.label_{}.setPixmap(self.deck[{}].pixmap)'.format( i, i)) self.index_stac = 0 self.ui.label_28.setPixmap( self.stack[self.index_stac].pixmap ) # 28 лейблу присвоили его лицо (стек слева сверху) self.dict_card = dict(zip(self.main_card_list[:28], self.deck)) self.ui.pushButton.clicked.connect(self.next_card) self.ui.actionNew_game.triggered.connect(self.new_game) self.ui.actionExit.triggered.connect(self.exit) self.ui.actionWin_the_game.triggered.connect(self.win) self.ui.show() self.a, self.z = [], []
while self._pyramid[0][0].rank is not None: # if top card is not None self._menu() command = input('\nCommand: ').strip() if self._enter_command(command) is None: system('cls') continue else: system('cls') input('\nGame Over') print(game.instruction()) input('Press any key to continue...') # wait user system('cls') self._start_new_game().start() d = DeckGenerator() # create deck d.shuffle() t = TableCard(d) # create pyramid and additional card t.generate_pyramid() gl = GameLogic(t) for i in t.pyramid_deck[-1]: # open bottom row i.status = True ###################### Game ################### game = Game(d, t, gl) print(game.instruction()) input('Press any key to continue...') # wait user system('cls') game.start()
class Blackjack(): def __init__(self): # Set up the player's wallet. self.wallet = Wallet() # Create a way to generate new cards. self.deck_generator = DeckGenerator() # Reset the game. self.reset_game() def reset_game(self): """ Reset all game variables for a new round. """ # The player always goes first. self.current_turn = 'player' # Set the current cards to an empty array. self.cards = {'dealer': [], 'player': []} # Set the totals for each player. self.totals = {'dealer': 0, 'player': 0} # Create a new new deck of cards. self.deck = self.deck_generator.get_new_deck() def ask_bet(self): """ Ask a player for the current round's bet. """ balance = self.wallet.get_balance() message = '\nYou have {balance} remaining. Enter your bet: '.format( balance=balance) while True: try: self.bet = int(input(message)) except: print('Please enter a number. Try again.') continue else: if self.bet > balance: print( 'You cannot bet more than you have. Please enter a smaller number.' ) continue else: print('You bet {} for this round.'.format(self.bet)) break def draw_card(self): """ Draw a single card and return it. """ index_options = len(self.deck) - 1 random_index = randint(0, index_options) return self.deck.pop(random_index) def draw_initial_cards(self): """ Draw initial cards for player and dealer. """ # Draw cards for player and dealer. for current in ('dealer', 'player'): for i in range(0, 2): card = self.draw_card() self.cards[current].append(card) @staticmethod def format_card(card): """ Displays a single card tuple """ card_value = str(card[1]).capitalize() card_suit = card[0].capitalize() return "{} of {}".format(card_value, card_suit) def display_cards(self): """ Display all cards for the player and dealer. """ message = "{}'s cards: {}" for player in ['dealer', 'player']: # Format the cards correctly. cards = [self.format_card(card) for card in self.cards[player]] # If it's the player's turn, do not display the second card of the dealer. if self.current_turn == 'player' and player == 'dealer': cards[1] = '--hidden--' # Print the message. print(message.format(player.capitalize(), ' | '.join(cards))) # Empty line. print() def player_input(self): """ Ask player for input. """ while True: action = input( "Do you want to Hit or Stay? Enter 'h' for hit, and 's' for stay: " ) if action not in ['h', 's']: print( "You can only enter 'h' for hit, and 's' for stay. Please try again.\n" ) else: break return action def tally_up_cards(self): """ Tally up all the cards for each player. """ for player in ['dealer', 'player']: self.totals[player] = self.check_cards(player) def check_cards(self, player): """ Check the cards for the specified player. """ total = 0 for card in self.cards[player]: # Number cards can simply be added. if isinstance(card[1], int): total = total + card[1] # People cards have a value of 10. if card[1] in ['jack', 'queen', 'king']: total = total + 10 # Handle aces separately. # Find the best combination for aces. # 1 ace: 1 or 11 # 2 aces: 1,1, or 11,1 (all other combos go over) # 3 aces: 1,1,1 or 11,1,1 (all other combos go over) # 4 aces: 1,1,1,1 or 11,1,1,1 (all other combos go over) aces = list(filter(lambda card: card[1] == 'ace', self.cards['player'])) if len(aces) == 1: if (total + 11) > 21: total = total + 1 else: total = total + 11 elif len(aces) == 2: if (total + 12) > 21: total = total + 2 else: total = total + 12 elif len(aces) == 3: if (total + 13) > 21: total = total + 3 else: total = total + 13 elif len(aces) == 4: if (total + 14) > 21: total = total + 4 else: total = total + 14 return total def display_totals(self): """ Display the totals for this round. """ print() print("---- Results for this round ----") self.display_cards() print("Dealer total is: {}".format(self.totals['dealer'])) print("Player total is: {}".format(self.totals['player'])) def player_result(self, outcome): """ Player has either won or lost this round. """ self.display_totals() if outcome == 'win': self.wallet.add_money(self.bet * 2) message = 'You won this round and doubled your bet of {}.' elif outcome == 'lose': self.wallet.subtract_amount(self.bet) message = 'You lost this round your bet of {}.' print(message.format(self.bet)) print('Coins in your wallet are now: {}'.format( self.wallet.get_balance())) def play_game(self): # Play until game is not continued. continue_game = True while continue_game: # Play another round. self.play_round() # Once complete, ask if the player wants to play again. while True: # If user is out of money, he cannot play again. if self.wallet.balance == 0: print('You are broke and cannot play any more.') continue_game = False break # If user has money left, ask if he wants to play play_again = input('Do you want to play anther round (y/n): ') if play_again.lower() not in ['y', 'n']: print('Invalid choice. Please enter y or n only.') elif play_again == 'y': print('Alright. Let\'s play again.') break else: print('Thank you for playing.') if self.wallet.balance > self.wallet.initial_balance: print('You made some money. Congrats') else: print( 'You lost money, but at least you are not broke.') continue_game = False break def play_round(self): """ Play another round. """ # Reset everything. self.reset_game() # Ask player for their bet. self.ask_bet() # Draw initial set of cards. self.draw_initial_cards() # Print out first cards that were dealt. self.display_cards() # Tall up initial cards. self.tally_up_cards() # Indicate if round is complete. round_complete = False # Indicate if player already won this turn. player_won = False # Edge case: Player starts with a blackjack. if self.totals['player'] == 21: # If the player has more 21, he instantly won. print("BLACKJACK!!!!") player_won = True self.player_result('win') return True # Ask player for actions. while True: player_action = self.player_input() if player_action == 'h': # Draw a new card. card = self.draw_card() self.cards[self.current_turn].append(card) self.display_cards() self.tally_up_cards() if self.totals['player'] == 21: # If the player has more 21, he instantly won. print("BLACKJACK!!!!") player_won = True self.player_result('win') break elif self.totals['player'] > 21: # If the player has more than 21, he instantly lost. self.player_result('lose') round_complete = True break else: print('You finished your turn. Now let the dealer play.') break # Dealer gets to play if player did not win already. if not player_won and not round_complete: self.current_turn = 'dealer' self.display_cards() while True: # If the dealer has more than the player and less than 21, dealer wins. if self.totals['dealer'] < 21 and self.totals[ 'dealer'] > self.totals['player']: self.player_result('lose') break elif self.totals['dealer'] == 21: print('Dealer has Blackjack') self.player_result('lose') break elif self.totals['dealer'] > 21: self.player_result('win') break else: print('Dealer will hit again') # Wait for effect. sleep(3) # Let the dealer pick a card. card = self.draw_card() self.cards[self.current_turn].append(card) self.display_cards() self.tally_up_cards()