def initGame(self, args): # read rules from rules file rules_file = args['--rules'] if not rules_file: exit( "ERROR: Please specify a rules document through the -r option." ) self.readRules(rules_file) # make players num_people = args['--people'] num_npcs = args['--npcs'] # print welcome screen os.system('cls' if os.name == 'nt' else 'clear') print(art.text2art("Crazy Eights", chr_ignore=True)) print("Welcome to Crazy Eights!") print() print("Rules -- specified from `{}`".format(rules_file)) print("--------------------------------------------------") for effect in EFFECTS: self.printRule(effect) print() input("Press [Enter] to continue...") print() # make people print("Creating players...") print() if num_people: for i in range(int(num_people)): self.players.append(Person(self)) # make NPCs if num_npcs: for i in range(int(num_npcs)): self.players.append(NPC(self)) # # of players can't be over 6 MAX_PLAYERS = 6 if len(self.players) == 0: exit("ERROR: Number of players cannot be 0.") if len(self.players) > MAX_PLAYERS: exit( "ERROR: Cannot have more than {} players.".format(MAX_PLAYERS)) # init deck and discard pile self.deck = pd.Deck() self.deck.shuffle() self.discard = pd.Deck() self.discard.empty() # distribute hands NUM_STARTING_CARDS = 5 for player in self.players: player.drawCards(NUM_STARTING_CARDS) printCards(player.hand) self.gameLoop()
def get_new_deck(self, num_decks): """Creates a new shuffled deck Creates a shuffled deck with the number of decks specified """ deck = pydealer.Deck(jokers=True, num_jokers=2) for n in range(num_decks - 1): other_deck = pydealer.Deck(jokers=True, num_jokers=2) deck.add(other_deck.deal(54)) deck.shuffle() return deck
def test_three_players(self): deck = pydealer.Deck() cards = deck.get_list([ '10 of Spades', 'Jack of Spades', 'Queen of Spades', 'King of Spades' ]) t, j, q, k = cards players = create_player_set(3) game = KuhnGame(players, cards, 1) root = game.create_root_node() TKQ_node = root.get_children()[(t, k, q)] JKQ_node = root.get_children()[(j, k, q)] for node in [TKQ_node, JKQ_node]: self.assertTrue( np.allclose( node.play(PokerActions.RAISE_1).play( PokerActions.FOLD).play( PokerActions.FOLD).evaluation(), np.array([2, -1, -1]))) self.assertTrue( np.allclose( node.play(PokerActions.CHECK).play( PokerActions.RAISE_1).play(PokerActions.FOLD).play( PokerActions.CALL).evaluation(), np.array([-2, 3, -1])))
def run_game(): deck = pydealer.Deck() hand = pydealer.Stack() cards = deck.get_list([ '10 of Spades', 'Jack of Spades', 'Queen of Spades', 'King of Spades' ]) players = create_player_set(3) game = KuhnGame(players, cards, 1) root = game.create_root_node() #random.seed(1) #chance_sampling_cfr = ChanceSamplingCFR(root, players) #chance_sampling_cfr.run(iterations=1000) #chance_sampling_cfr.compute_nash_equilibrium() # read Nash-Equilibrum via chance_sampling_cfr.nash_equilibrium member # try chance_sampling_cfr.value_of_the_game() function to get value of the game (-1/18) #print(chance_sampling_cfr.value_of_the_game()) # vanilla cfr vanilla_cfr = VanillaCFR(root, players) for i in tqdm(range(10)): vanilla_cfr.run(iterations=100) vanilla_cfr.compute_nash_equilibrium()
def setup_game(group_tele_id): session = scoped_session(session_factory) s = session() player_tele_ids = s.query(Player.player_tele_id).filter(Player.group_tele_id == group_tele_id).all() random.shuffle(player_tele_ids) # Creates a deck of cards in random order deck = pydealer.Deck(ranks=pydealer.BIG2_RANKS) deck.shuffle() # Sets up players curr_player = -1 for i, player_tele_id in enumerate(player_tele_ids): player_cards = pydealer.Stack(cards=deck.deal(13)) player_cards.sort(ranks=pydealer.BIG2_RANKS) # Player with ♦3 starts first if player_cards.find("3D"): curr_player = i player = s.query(Player).filter(Player.player_tele_id == player_tele_id).first() player.player_id = i player.cards = player_cards s.commit() game = s.query(Game).filter(Game.group_tele_id == group_tele_id).first() game.curr_player = game.biggest_player = curr_player s.commit() session.remove()
def run_game(): deck = pydealer.Deck() cards = deck.get_list( ['Jack of Spades', 'Queen of Spades', 'King of Spades']) players = create_player_set(2) game = KuhnGame(players, cards, 1) root = game.create_root_node() #random.seed(1) #chance_sampling_cfr = ChanceSamplingCFR(root, players) #chance_sampling_cfr.run(iterations=1000) #chance_sampling_cfr.compute_nash_equilibrium() # read Nash-Equilibrum via chance_sampling_cfr.nash_equilibrium member # try chance_sampling_cfr.value_of_the_game() function to get value of the game (-1/18) #print(chance_sampling_cfr.value_of_the_game()) # vanilla cfr vanilla_cfr = VanillaCFR(root, players) root = game.create_root_node() j, q, k = cards KQ_node = root.get_children()[(k, q)] vanilla_cfr._cfr_utility_recursive( KQ_node.play(PokerActions.CHECK).play(PokerActions.CHECK), [1, 1]) for i in tqdm(range(10)): vanilla_cfr.run(iterations=100) vanilla_cfr.compute_nash_equilibrium() print(vanilla_cfr.value_of_the_game())
class CardDeck: deck = pd.Deck(rebuild=True, re_shuffle=True) deck.shuffle() # Define a new rank dictionary new_ranks = { "values": { "Ace": 1, "King": 10, "Queen": 10, "Jack": 10, "10": 10, "9": 9, "8": 8, "7": 7, "6": 6, "5": 5, "4": 4, "3": 3, "2": 2, } } # Create a discard pile discardPile = pd.Stack() # Add cards to discard pile def addToDiscard(self, cards): self.discardPile.add(cards)
def test_compare_stacks(self): """""" other_deck = pydealer.Deck() result = pydealer.tools.compare_stacks(self.deck, other_deck) self.assertEqual(result, True)
def __init__(self, bot=None, server=None): self.bot = bot self.server = server self.queue = [] self.ingame = [] self.dealer = Player(self.bot.user.id) self.dealer.score = 2000 self.deck = pydealer.Deck()
def get_chance_node(self): deck = pydealer.Deck() cards = deck.get_list( ['Jack of Spades', 'Queen of Spades', 'King of Spades']) players = create_player_set(2) game = KuhnGame(players, cards, 1) return game.create_root_node()
def test_evaluation(self): deck = pydealer.Deck() cards = deck.get_list( ['Jack of Spades', 'Queen of Spades', 'King of Spades']) j, q, k = cards players = create_player_set(2) game = KuhnGame(players, cards, 1) root = game.create_root_node() KQ_node = root.get_children()[(k, q)] QJ_node = root.get_children()[(q, j)] KJ_node = root.get_children()[(k, j)] QK_node = root.get_children()[(q, k)] JQ_node = root.get_children()[(j, q)] JK_node = root.get_children()[(j, k)] for node in [KQ_node, QJ_node, KJ_node]: self.assertTrue( np.allclose( node.play(PokerActions.RAISE_1).play( PokerActions.FOLD).evaluation(), np.array([1, -1]))) self.assertTrue( np.allclose( node.play(PokerActions.RAISE_1).play( PokerActions.CALL).evaluation(), np.array([2, -2]))) self.assertTrue( np.allclose( node.play(PokerActions.CHECK).play( PokerActions.RAISE_1).play( PokerActions.FOLD).evaluation(), np.array([-1, 1]))) self.assertTrue( np.allclose( node.play(PokerActions.CHECK).play( PokerActions.CHECK).evaluation(), np.array([1, -1]))) for node in [QK_node, JQ_node, JK_node]: self.assertTrue( np.allclose( node.play(PokerActions.RAISE_1).play( PokerActions.FOLD).evaluation(), np.array([1, -1]))) self.assertTrue( np.allclose( node.play(PokerActions.RAISE_1).play( PokerActions.CALL).evaluation(), np.array([-2, 2]))) self.assertTrue( np.allclose( node.play(PokerActions.CHECK).play( PokerActions.RAISE_1).play( PokerActions.FOLD).evaluation(), np.array([-1, 1]))) self.assertTrue( np.allclose( node.play(PokerActions.CHECK).play( PokerActions.CHECK).evaluation(), np.array([-1, 1])))
def new_deck(self): shoe = pydealer.Stack() for i in range(self.shoe_size): shoe.add(pydealer.Deck()) shoe.shuffle() return shoe
def __init__(self, **kwargs): """ Addition of kwargs :param kwargs: """ self.player_deck = pydealer.Deck() self.player_deck.shuffle() for key, value in kwargs.items(): self.key = value
def __init__(self, list_of_players): self.deck = pydealer.Deck() self.deck.shuffle() self.players = {} self.list_of_players = list_of_players self.n_players = len(list_of_players) for player_name in list_of_players: self.players[player_name] = Player(player_name, self.deck, self.n_players)
def restart(self): """ Restart the deck :return: """ self.player_deck.empty() self.player_hand.clear() self.dealer_hand.clear() self.player_deck = pydealer.Deck() self.player_deck.shuffle()
def startHand(gameId): numberOfPlayers = db.numberOfPlayersInGame(gameId) _, _, _, dealer, _, _, _, _, handNo, smallBlind, blindIncrement = db.getGame( gameId) blindsIncreased = False if blindIncrement != 0: if (handNo + 1) % blindIncrement == 0: smallBlind *= 2 blindsIncreased = True handNo += 1 db.updateGame( gameId, "handNo = " + str(handNo) + ", smallBlind = " + str(smallBlind)) smallBlind = smallBlind bigBlind = smallBlind * 2 deck = pd.Deck() deck.shuffle() # Deal cards and set playes to not be folded for i in range(numberOfPlayers): _, _, _, _, _, _, _, _, eliminated, _, _, _ = db.getPlayer(gameId, i) if eliminated == 0: # not eliminated hand = deck.deal(2) db.updatePlayer( gameId, i, "folded = 0, isChecked = 0, cards = \"" + str(hand[0]) + ":" + str(hand[1]) + "\", amountPutInPot = 0, amountPutInPotThisRound = 0") #dealer moves to left dealerPos, smallBlindPos, bigBlindPos, UTG = getNextStartingPlayers(gameId) handLog = "<b>Hand number " + str( handNo) + "</b>\r\n--------------------------------------\r\n" if blindsIncreased: handLog += "Blinds have increased to " + str( smallBlind) + " and " + str(bigBlind) + " chips.\r\n" handLog += "Dealer: " + db.getPlayer(gameId, dealerPos)[3] + "\r\n" #UpdateGame db.updateGame( gameId, "currentPlayer = " + str(UTG) + ", dealer = " + str(dealerPos) + ", board = '', phase = 0, pot = 0, betToMatch = " + str(bigBlind)) handLog += (player.addToPot(gameId, smallBlindPos, smallBlind, "sb") + "\r\n") handLog += (player.addToPot(gameId, bigBlindPos, bigBlind, "bb")) db.updateGame(gameId, "handLog = \"" + handLog + "\"") return UTG
def __init__(self, squares=[]): if len(squares) == 0: self.squares = [None for i in range(50)] else: self.squares = squares self.player = 'B' self.deck = pydealer.Deck() self.deck.shuffle() self.p1_hand = pydealer.Stack() self.p2_hand = pydealer.Stack() self.deal_cards()
def setUp(self): """""" self.ace_spades = pydealer.Card("Ace", "Spades") self.two_diamonds = pydealer.Card("2", "Diamonds") self.queen_hearts = pydealer.Card("Queen", "Hearts") self.seven_clubs = pydealer.Card("7", "Clubs") self.cards = [self.ace_spades, self.two_diamonds, self.queen_hearts, self.seven_clubs] self.names = ["Ace of Spades", "2 of Diamonds", "Queen of Hearts", "7 of Clubs"] self.deck = pydealer.Deck() self.stack = pydealer.Stack(cards=self.cards)
def character_generator(): mapping = {'Ace': 12, 'King': 10, 'Queen': 10, 'Jack': 8, '10': 8, '9': 8, '8': 6, '7': 6, '6': 6, '5': 6, '4': 6, '3': 6, '2': 4, 'Spades': 4, 'Hearts': 3, 'Diamonds': 2, 'Clubs': 1} deck = pydealer.Deck() deck.shuffle() # Deadlands rules are deal 12, toss two from 3 to A inclusive (2, Jkr must stay) # selection = deck.deal(12) # But for now, we'll just take 10 randomly and live with it. selection = deck.deal(10) traits = { 'deftness': Trait(mapping[selection[0].suit], mapping[selection[0].value]), 'nimbleness': Trait(mapping[selection[1].suit], mapping[selection[1].value]), 'quickness': Trait(mapping[selection[2].suit], mapping[selection[2].value]), 'strength': Trait(mapping[selection[3].suit], mapping[selection[3].value]), 'vigor': Trait(mapping[selection[4].suit], mapping[selection[4].value]), 'cognition': Trait(mapping[selection[5].suit], mapping[selection[5].value]), 'knowledge': Trait(mapping[selection[6].suit], mapping[selection[6].value]), 'mien': Trait(mapping[selection[7].suit], mapping[selection[7].value]), 'smarts': Trait(mapping[selection[8].suit], mapping[selection[8].value]), 'spirit': Trait(mapping[selection[9].suit], mapping[selection[9].value])} # deftness = Trait(mapping[selection[0].suit], mapping[selection[0].value]) # nimbleness = Trait(mapping[selection[1].suit], mapping[selection[1].value]) # quickness = Trait(mapping[selection[2].suit], mapping[selection[2].value]) # strength = Trait(mapping[selection[3].suit], mapping[selection[3].value]) # vigor = Trait(mapping[selection[4].suit], mapping[selection[4].value]) # # cognition = Trait(mapping[selection[5].suit], mapping[selection[5].value]) # knowledge = Trait(mapping[selection[6].suit], mapping[selection[6].value]) # mien = Trait(mapping[selection[7].suit], mapping[selection[7].value]) # smarts = Trait(mapping[selection[8].suit], mapping[selection[8].value]) # spirit = Trait(mapping[selection[9].suit], mapping[selection[9].value]) return traits
class gameSim(): player = [] dealer = [] deck = pydealer.Deck(rebuild=True, re_shuffle=True) def __init__(self): self.deck.shuffle() self.player = [new_ranks['values'][self.deck.deal(1)[0].value], new_ranks['values'][self.deck.deal(1)[0].value]] self.dealer = [new_ranks['values'][self.deck.deal(1)[0].value], new_ranks['values'][self.deck.deal(1)[0].value]] def new_hand(self): self.deck = pydealer.Deck(ranks=new_ranks) self.deck.shuffle() self.player = [new_ranks['values'][self.deck.deal(1)[0].value], new_ranks['values'][self.deck.deal(1)[0].value]] self.dealer = [new_ranks['values'][self.deck.deal(1)[0].value], new_ranks['values'][self.deck.deal(1)[0].value]] def hit(self, player): if player: self.player.append(new_ranks['values'][self.deck.deal(1)[0].value]) else: self.dealer.append(new_ranks['values'][self.deck.deal(1)[0].value]) def value(self, player): if player: return sum(self.player) else: return sum(self.dealer) def bust(self, player): return self.value(player) > 21 def win(self, player): return self.value(player) > self.value(not player) def run_dealer(self): while not self.bust(False) and not self.win(False): self.hit(False) def state(self): ret = [0]*10 ret[0:len(self.player)] = self.player ret[-1] = self.dealer[0] return [float(i) for i in ret] def reward(self): if self.bust(True): return -1 elif self.bust(False): return 1 elif self.win(False): return -1 else: return 1
def nextRound(gameId): _, board, currentPlayer, dealer, phase, pot, betToMatch, handLog, _, _, _ = db.getGame( gameId) # 0 - preflop # 1 - postflop # 2 - turn # 3 - river newPhase = phase + 1 numberOfPlayers = db.numberOfPlayersInGame(gameId) db.updateGame(gameId, "currentPlayer = " + str(dealer)) for i in range(numberOfPlayers): db.updatePlayer(gameId, i, "isChecked = 0, amountPutInPotThisRound = 0") db.updateGame(gameId, "phase = " + str(newPhase)) if not allAreAllIn(gameId): newCurrentPlayer = nextPlayer(gameId)[1] db.updateGame(gameId, "currentPlayer = " + str(newCurrentPlayer)) else: newCurrentPlayer = dealer db.updateGame(gameId, "currentPlayer = " + str(newCurrentPlayer)) deck = pd.Deck() deck.get_list( getAllCardsInPlay(gameId)) # remove all cards currently in play deck.shuffle() if newPhase == 1: newBoard = deck.deal(3) handLog = handLog + "\r\nFlop : " + cardToUnicode(str(newBoard[0])) + ", " \ + cardToUnicode(str(newBoard[1])) + ", " + cardToUnicode(str(newBoard[2])) db.updateGame( gameId, "board = \"" + str(newBoard[0]) + ":" + str(newBoard[1]) + ":" + str(newBoard[2]) + "\", betToMatch = 0, handLog = \"" + handLog + "\"") elif newPhase == 2 or newPhase == 3: newBoard = deck.deal(1) if newPhase == 2: handLog = handLog + "\r\nTurn : " + cardToUnicode(str(newBoard[0])) else: handLog = handLog + "\r\nRiver : " + cardToUnicode(str( newBoard[0])) db.updateGame( gameId, "board = \"" + board + ":" + str(newBoard[0]) + "\", betToMatch = 0, handLog = \"" + handLog + "\"") return newCurrentPlayer
def test_player_a_acts_first(self): deck = pydealer.Deck() cards = deck.get_list( ['Jack of Spades', 'Queen of Spades', 'King of Spades']) players = create_player_set(2) game = KuhnGame(players, cards, 1) root = game.create_root_node() for k in root.get_children(): child = root.get_children()[k] assert child.get_player_to_move() == players[0]
def start(number): global started, number_of_players, played, hands, deck started = True number_of_players = number played = [False] * number_of_players deck = pydealer.Deck() deck.shuffle() hands = {} for i in range(number_of_players): if number_of_players <= 4: hands[i] = deck.deal(7) else: hands[i] = deck.deal(5) hands[i].sort()
def main_game_loop(self, number_of_games, number_of_players, results_list): for game in range(1, number_of_games + 1): if game % 50 == 0: print(game, 'games played') self.number_of_players = number_of_players self.deck = pydealer.Deck() self.state_dict = self.create_state_dict(self.number_of_players, self.deck) winner = self.play_game() if not isinstance(winner, str): results_list.append(winner.name) else: self.print_if_human_playing( ['All players were eliminated. No winner this round.']) return results_list
def fresh_kq_node(self): deck = pydealer.Deck() cards = deck.get_list( ['Jack of Spades', 'Queen of Spades', 'King of Spades']) players = create_player_set(2) game = KuhnGame(players, cards, 1) root = game.create_root_node() vanilla_cfr = VanillaCFR(root, players) j, q, k = cards KQ_node = root.get_children()[(k, q)] return vanilla_cfr, KQ_node
def test_game_tree(self): deck = pydealer.Deck() cards = deck.get_list( ['Jack of Spades', 'Queen of Spades', 'King of Spades']) num_players = 2 game = KuhnGame(create_player_set(num_players), cards, 1) # see if simulations run successfully for i in range(100): curr_node = game.create_root_node() while not curr_node.is_terminal(): curr_node = random.choice( list(curr_node.get_children().values())) self.assertTrue( len(np.nonzero(curr_node.evaluation())[0]) == num_players)
def __init__(self): # deck of cards we are playing with! self.deck = pd.Deck(jokers=True, num_jokers=3) self.deck.shuffle() # player info self.num_players = 0 # initialised in new_game self.players = [] # initialised in new_game # other piles self.unopened = None self.top_cards = [] self.discarded = [] # who's playing self.curr_player = 0 self.curr_player_idx = 0 # starts the game!
def fresh_tkq_node(): deck = pydealer.Deck() cards = deck.get_list([ '10 of Spades', 'Jack of Spades', 'Queen of Spades', 'King of Spades' ]) players = create_player_set(3) game = KuhnGame(players, cards, num_deal=1) root = game.create_root_node() vanilla_cfr = VanillaCFR(root, players) t, j, q, k = cards TKQ_node = root.get_children()[(t, k, q)] return vanilla_cfr, TKQ_node
def retrace(): global data_comp data_comp = rd.random deck2 = pydealer.Deck() #picks random deck start point and begins comparison of secondary data print("deck made") deck2.shuffle() print("shuffled") play12 = pydealer.Stack() play22 = pydealer.Stack() comp12 = pydealer.Stack() comp22 = pydealer.Stack() hold2 = pydealer.Stack() #buffer hold states for transfer initialized and stored here play12 = deck.deal(26) play22 = deck.deal(26) #end buffer operations and return cards that were not compared to each hand return data_comp
class Game: # Deck variable is local to game object deck = pydealer.Deck() def __init__(self, listOfPlayers): self.listOfPlayers = listOfPlayers def listPlayers (self): for player in listOfPlayers: print(player) def init(listOfPlayers): deck.shuffle() for player in listOfPlayers: player.hand = deck.deal(7) hand.sort() print("hand for player", player, "is", player.hand) def printHands(abc): print("print ahnds")