本文整理汇总了Python中cards.Deck.shuffle方法的典型用法代码示例。如果您正苦于以下问题:Python Deck.shuffle方法的具体用法?Python Deck.shuffle怎么用?Python Deck.shuffle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cards.Deck
的用法示例。
在下文中一共展示了Deck.shuffle方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_shuffle
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def test_shuffle():
deck = Deck()
deck.shuffle()
card1 = deck.draw_card()
card2 = deck.draw_card()
assert card1.rank != "King" and card1.suit != "Spades" and card2.rank != "King" and card2.suit != "Hearts"
示例2: test_2elements
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def test_2elements(self):
card1 = Card("a", "b")
card2 = Card("c", "d")
x = Deck([card1, card2])
x.shuffle()
# NOTE: don't check order because it has a 50% chance of not changing
self.assertSetEqual(set(x), set([card1, card2]))
示例3: test_full
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def test_full(self):
x = Deck()
x.reset()
before = list(x)
x.shuffle()
after = list(x)
self.assertTrue(before != after)
self.assertSetEqual(set(x), set(before))
示例4: __init__
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def __init__(self):
self.grid = []
deck = Deck()
deck.shuffle()
n = 0
for i in range(1,5):
self.grid.append([])
for j in range(1, 14):
card = deck.cards[n]
if card.rank[0] == "A":
card = None
self.grid[i-1].append(card)
n += 1
示例5: main
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def main():
"""Creates a deck, deals and prints the cards,
then creates a second deck, shuffles, deals and prints."""
deck = Deck()
print("NUMBER OF CARDS:", len(deck))
print("THE CARDS IN A NEW DECK:")
while not deck.isEmpty():
print(deck.deal())
deck = Deck()
deck.shuffle()
print("\nTHE CARDS IN A SHUFFLED DECK:")
while not deck.isEmpty():
print(deck.deal())
示例6: CardDemo
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
class CardDemo(Frame):
def __init__(self):
"""Sets up the window and widgets."""
Frame.__init__(self)
self.master.title("Card Demo")
self.grid()
self._deck = Deck()
self._backImage = PhotoImage(file = Card.BACK_NAME)
self._cardImage = None
self._imageLabel = Label(self, image = self._backImage)
self._imageLabel.grid(row = 0, column = 0, rowspan = 3)
self._textLabel = Label(self, text = "")
self._textLabel.grid(row = 3, column = 0)
self._dealButton = Button(self,
text = "Deal",
command = self._deal)
self._dealButton.grid(row = 0, column = 1)
self._shuffleButton = Button(self,
text = "Shuffle",
command = self._shuffle)
self._shuffleButton.grid(row = 1, column = 1)
self._newButton = Button(self,
text = "New Deck",
command = self._new)
self._newButton.grid(row = 2, column = 1)
def _deal(self):
"""If the deck is not empty, deals and displays the
next card. Otherwise, returns the program to its
initial state."""
card = self._deck.deal()
if card != None:
self._cardImage = PhotoImage(file = card.fileName)
self._imageLabel["image"] = self._cardImage
self._textLabel["text"] = str(card)
else:
self._new()
def _shuffle(self):
self._deck.shuffle()
def _new(self):
"""Returns the program to its initial state."""
self._deck = Deck()
self._cardImage = None
self._imageLabel["image"] = self._backImage
self._textLabel["text"] = ""
示例7: DeckGUI
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
class DeckGUI(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, title = "Testing a Deck Display")
self.setSize(350, 200)
self.deck = Deck()
self.card = self.deck.deal()
self.image = PhotoImage(file = self.card.fileName)
self.cardImageLabel = self.addLabel("", row = 0, column = 0)
self.cardImageLabel["image"] = self.image
self.cardNameLabel = self.addLabel(row = 1, column = 0,
text = self.card)
self.button = self.addButton(row = 0, column = 1,
text = "Next Card",
command = self.nextCard)
self.button = self.addButton(row = 1, column = 1,
text = "Shuffle",
command = self.shuffleDeck)
self.button = self.addButton(row = 2, column = 1,
text = "New Deck",
command = self.newDeck)
def nextCard(self):
"""Deals a card from the deck and displays it,
or displays the back side if empty."""
if len(self.deck) == 0:
self.image = PhotoImage(file = Card.BACK_NAME)
self.cardNameLabel["text"] = 'Empty'
else:
self.card = self.deck.deal()
self.image = PhotoImage(file = self.card.fileName)
self.cardNameLabel["text"] = self.card
self.cardImageLabel["image"] = self.image
def shuffleDeck(self):
"""Shuffles the existing deck of cards."""
self.deck.shuffle()
def newDeck(self):
"""Creates a new Deck object, assigns it to the window's deck, and
updates the card images appropriately."""
self.deck = Deck()
示例8: Rules
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
class Rules(Game):
"""Rules for the game of Oh Hell!"""
def __init__(self):
self.deckStyle = 'French'
self.deck = Deck(self.deckStyle)
self.playerLimit = [3,4,5,6,7]
self.handLimit = [11,13,15,17,19,21,23]
super(Rules, self).__init__()
self.handCounter = 0
self.hands = [Hand(n, self.handLimit) for n in range(self.handLimit)]
def limitHands(self):
i = 1
while self.playerLimit * i < len(self.deck.cards):
print i
i = i + 1
# pass
self.handLimit = 'xx'
def setupDeck(self):
for cards in [(0,2),(1,2),(2,2),(3,2),(0,3),(1,3),(2,3),(3,3),(0,4),(1,4)]:
card = Card(self.deckStyle, cards[0], cards[1])
self.deck.remove(card)
def deal(self):
self.setupDeck()
self.deck.shuffle()
cards = self.deck.cards
for player in self.players:
for n in range(self.hands[self.handCounter].cardLimit):
player.hand.append(cards.pop())
for player in self.players:
print str(player), ':'
holding = u""
for card in player.hand:
holding = holding + card.__unicode__() + ' '
print holding
print '\n\n\n'
示例9: play_game
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def play_game(table, discards, deck, dealer):
global human
if check_score(dealer, dealer):
return
# Recursively plays the game
else:
print "Dealing the deck...\n"
time.sleep(2)
deck.shuffle()
deal(deck, dealer.next_player)
human.sort_hand()
starter = find_starter(dealer)
# Play 13 rounds (one for each card)
for roundNum in range(13):
# Round schedule
print_hand(starter)
table, trump = make_move(table, starter, None, False, roundNum)
table, discards = calculate_points(table, discards, trump)
print_score(starter, starter)
# Reshuffle the deck
deck = Deck(discards)
deck.shuffle()
deal(deck, starter.next_player)
play_game(table, discards, deck, dealer.next_player)
示例10: status
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
# Main Program
#
def status(owner):
print owner.getLabel(), " hand: (", owner.total(), "): ", owner
if owner.isaBlackjack():
print "That's Blackjack!"
elif owner.isaBust():
print "That is a Bust!"
print "\t\t\t Caesar - Todor's Palace"
print "\t\t\t ***********************"
deck = Deck()
deck.shuffle()
dealer = BlackjackHand('Dealer')
player_name = raw_input(" Please enter your name: ")
player = BlackjackHand(player_name)
deck.deal(dealer, 2)
deck.deal(player, 2)
print
print
status(dealer)
status(player)
if player.isaBlackjack() == True or player.isaBust() == True:
choice = 0
示例11: Game
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
class Game(object):
"""
Handler for the game progress.
"""
# Messages
__str_cards = "Tableau:\n%s\n\nDiscards: %4s %4s %4s %4s\n"
__str_option_draw = "d - draw a card"
__str_option_simple = "s - simple calculation"
__str_option_advanced = "a - advanced calculation"
__str_option_quit = "q - quit"
__str_choose = "Choose an option"
__str_options_all = "%s: %s, %s, %s, %s: " % (__str_choose,
__str_option_draw,
__str_option_simple,
__str_option_advanced,
__str_option_quit)
__str_options_short = "%s: %s, %s, %s: " % (__str_choose,
__str_option_simple,
__str_option_advanced,
__str_option_quit)
__str_calc_simple = "The total score (simple algorithm) is: %2s"
__str_calc_advanced = "The total score (advanced algorithm) is: %2s"
__str_card_dealt = "Card dealt: %4s"
__str_choose_loc = "Choose location (1 - 20) to place the new card: "
# Error messages
__str_err_nint = "Error: input is not an integer"
__str_err_oor = "Error: input is out of range"
__str_err_pos_full = "Error: a card was already placed in this spot"
__str_err_invalid_choice = "Error: an invalid choice was made"
# Options for the player
__option_quit = "Qq"
__option_deal = "Dd"
__option_calc_simple = "Ss"
__option_calc_advanced = "Aa"
def __init__(self):
""" Initializes the game """
self._deck = Deck()
self._deck.shuffle()
self._tableau = Tableau()
def play(self):
""" Starts the game """
self._print_cards()
while True:
# Ask user what to do
if self._tableau.is_full():
inp = raw_input(self.__str_options_short)
else:
inp = raw_input(self.__str_options_all)
if len(inp) == 0:
print self.__str_err_invalid_choice
elif inp in self.__option_quit:
break
elif inp in self.__option_deal:
self._play_step()
print
self._print_cards()
elif inp in self.__option_calc_simple:
print self.__str_calc_simple % \
self._tableau.calc_score_simple()
elif inp in self.__option_calc_advanced:
print self.__str_calc_advanced % \
self._tableau.calc_score_advanced()
else:
print self.__str_err_invalid_choice
def _print_cards(self):
""" Prints the game board and discards """
discards = tuple(self._tableau[self._tableau.board_end + 1:
self._tableau.end + 1])
print self.__str_cards % ((self._tableau,) + discards)
def _play_step(self):
""" One step of the game - deal card and place it """
card = self._deck.deal()
print self.__str_card_dealt % card
pos = self._get_pos_from_user()
self._tableau[pos] = card
def _get_pos_from_user(self):
""" Get a (valid) board position from the user """
while True:
# Iterate until value is valid
inp = raw_input(self.__str_choose_loc)
try:
pos = int(inp)
except ValueError:
print self.__str_err_nint
continue
if pos < self._tableau.start or pos > self._tableau.end:
print self.__str_err_oor
continue
if self._tableau.pos_full(pos):
print self.__str_err_pos_full
continue
return pos
示例12: blackjackGame
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def blackjackGame(player_name):
deck = Deck()
deck.shuffle()
player = BlackjackHand(player_name)
dealer = BlackjackHand('Dealer')
deck.deal(dealer, 2)
deck.deal(player, 2)
print
print
status(dealer)
status(player)
if player.isaBlackjack() == True or player.isaBust() == True:
choice = 0
if player.isaBust() == False:
out = player.getLabel() + ", type 1 for a hit and 0 to stay >> "
choice = raw_input(out)
while choice != '1' and choice != '0':
out = player.getLabel() + ", type 1 for a hit and 0 to stay >> "
choice = raw_input(out)
choice = int(choice)
while dealer.isaBust() == False and player.isaBust() == False \
and (choice == 1 or dealer.total() < 17):
while choice == 1:
deck.deal(player, 1)
print player.getLabel(), " hand: (", player.total(), "): ", player
if player.isaBlackjack():
print "That's Blackjack!"
choice = 0
elif player.isaBust():
print "That is a Bust!"
choice = 0
else:
choice = raw_input(out)
while choice != '1' and choice != '0':
choice = raw_input(out)
choice = int(choice)
print
print player.getLabel(), " total is ", player.total(), " and ", \
dealer.getLabel(), " total is ", dealer.total(), ". "
if player.isaBust():
print player.getLabel(), " went bust and loses the hand."
print
print dealer.getLabel(), "'s turn. Type any key to continue. "
key = raw_input()
if dealer.total() >= 17:
print dealer.getLabel(), " stays with ", dealer.total()
else:
print dealer.getLabel(), " takes a hit. "
deck.deal(dealer, 1)
status(dealer)
key = raw_input()
while dealer.total() < 17:
print dealer.getLabel(), " takes a hit. "
deck.deal(dealer, 1)
status(dealer)
key = raw_input()
print
print player.getLabel(), " total is ", player.total(), " and ", \
dealer.getLabel(), " total is ", dealer.total(), ". "
if dealer.isaBust():
print dealer.getLabel(), " went bust and loses the hand."
print
print player.getLabel(), "'s turn. Type any key to continue. "
key = raw_input()
if player.isaBust() == True and dealer.isaBust() == True:
out = player.getLabel() + ", press any key to continue. "
elif player.isaBlackjack() == False and player.isaBust() == False:
out = player.getLabel() + ", type 1 for a hit and 0 to stay >> "
choice = raw_input(out)
while choice != '1' and choice != '0':
out = player.getLabel() + ", type 1 for a hit and 0 to stay >> "
choice = raw_input(out)
choice = int(choice)
print
print
print " ~~ Game Over! ~~ "
print
print player.getLabel(), " has ", player.total(), ". "
print dealer.getLabel(), " has ", dealer.total(), ". "
print
if player.isaBust():
print player.getLabel(), " went bust. "
print dealer.getLabel(), " wins this hand! "
elif dealer.isaBust():
print dealer.getLabel(), " went bust. "
print player.getLabel(), " wins this hand! "
else:
if dealer > player:
print dealer.getLabel(), " wins this hand! "
elif dealer < player:
print player.getLabel(), " wins this hand! "
else:
print "It's a draw! "
示例13: test_1element
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def test_1element(self):
card = Card("a", "b")
x = Deck([card])
x.shuffle()
self.assertEqual(x, [card])
示例14: test_empty
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
def test_empty(self):
x = Deck()
x[:] = []
x.shuffle()
self.assertEqual(x, [])
示例15: Table
# 需要导入模块: from cards import Deck [as 别名]
# 或者: from cards.Deck import shuffle [as 别名]
class Table(object):
def __init__(self, minimum=1, maximum=500, payout=1, hit_soft_17=True,
insurance_allowed=True, insurance_payout=2,
num_decks=6, reshuffle_each_hand=True,
blackjack_payout=1.5, max_split_hands=3,
surrender_allowed=True, hit_split_ace=False,
resplit_aces=False, split_A10_is_blackjack=False,
double_after_split=False, double_after_hit= False):
# Establish table rules
self.min = minimum if minimum > 0 else 1
self.standard_bet = self.min
self.max = maximum if maximum > minimum else 500
self.payout = payout if payout > 0 else 1
self.hit_soft_17 = hit_soft_17
self.insurance_allowed = insurance_allowed
self.insurance_payout = insurance_payout if insurance_payout > 0 else 2
self.num_decks = num_decks if num_decks > 0 else 6
self.reshuffle_each_hand = reshuffle_each_hand
self.blackjack_payout = blackjack_payout if blackjack_payout > 0 else 1.5
self.max_split_hands = max_split_hands if max_split_hands >= 2 else 3
self.surrender_allowed = surrender_allowed
self.hit_split_ace = hit_split_ace
self.resplit_aces = resplit_aces
self.split_A10_is_blackjack = split_A10_is_blackjack
self.double_after_split = double_after_split
self.double_after_hit = double_after_hit
# Initialize necessary variables
if debug:
print "Established table"
self.init_deck()
self.dealer_hand = Hand("Dealer")
def init_deck(self):
self.deck = Deck(self.num_decks)
self.deck.shuffle()
def buy_in(self, name, chips, bet_method, play_method):
if debug:
print name + " bought into the table with $" + str(chips)
self.player_name = name
self.player_chips = chips
self.player_hands = [Hand(name)]
self.player_surrendered = False
self.bets = []
self.win_streak = 0
self.in_play = [False]
self.bet_method = bet_method
self.play_method = play_method
self.doubled = [False]
def add_funds(self, chips):
self.player_chips = self.player_chips + chips
def __str__(self):
if len(self.dealer_hand.cards) > 0:
ret = "Dealer is showing: " + str(self.dealer_hand.cards[0]) + "\n"
for h in range(len(self.player_hands)):
ret = ret + str(self.player_hands[h]) + " w/ value " + str(hand_value(self.player_hands[h])) + " and $" + str(self.bets[h]) + " bet"
return ret
else:
return "Waiting for hand to be dealt\n"
def deal(self):
if self.reshuffle_each_hand or self.deck.cards_left() < 4:
# if debug:
# print "Shuffling decks"
self.init_deck()
# if debug:
# print "Dealing"
self.deck.move_cards(self.dealer_hand, 2)
self.deck.move_cards(self.player_hands[0], 2)
self.player_hands[0].sort()
self.in_play[0] = True
def get_bets(self):
bet = self.bet_method(self.player_chips)
if bet == "rebet":
self.place_bet(self.standard_bet)
else:
while not self.place_bet(bet):
bet = self.bet_method()
if bet == "rebet":
self.place_bet(self.standard_bet)
def place_bet(self, bet):
if bet < self.min:
if debug:
print "Must bet at least the minimum: " + str(self.min)
return False
elif bet > self.max:
if debug:
print "Must bet less than the maximum: " + str(self.max)
return False
elif bet > self.player_chips:
if debug:
print "Don't have funds to place bet. Current chip count: " + str(self.player_chips)
return False
else:
#.........这里部分代码省略.........