Ejemplo n.º 1
0
    def has_flush_draw(self):
        """Verifies if there is a flush draw.

        Returns a tuple of booleans, (high, low)
        (True, False) means there's a flush draw to the high card.
        (False, True) means the same, to the low card.
        (True, True) means the hand is suited, and there's a flush draw.
        (False, False) means no flush draw.

        Should return (True, False) if there's a flush draw with a pair in hand.

        Can be used with any() or all()
        """
        if self.is_flush():
            return (False, False)

        high_card = max(self._cards)
        hc_suit = Card.get_suit_int(high_card)

        low_card = min(self._cards)
        lc_suit = Card.get_suit_int(low_card)

        suits = list(map(Card.get_suit_int, self._cards + self._board._cards))

        high_f_d = len([suit for suit in suits if suit == hc_suit]) == 4
        low_f_d = len([suit for suit in suits if suit == lc_suit]) == 4

        if any([high_f_d, low_f_d]) and self.pair_in_hand():
            return (True, False)

        return (high_f_d, low_f_d)
Ejemplo n.º 2
0
    def has_backdoor_flush(self):
        """Verifies if there is a backdoor flush.

        Returns a tuple of booleans, (high, low)
        (True, False) means there's a backdoor flush to the high card.
        (False, True) means the same, to the low card.
        (True, True) means the hand is suited, and there's a backdoor flush
            with a card on the flop.
        (False, False) means no backdoor flush draw.

        Can be used with any() or all()
        """

        high_card = max(self._cards)
        hc_suit = Card.get_suit_int(high_card)

        low_card = min(self._cards)
        lc_suit = Card.get_suit_int(low_card)

        suits = list(map(Card.get_suit_int, self._cards + self._board._cards))

        high_bd_f_d = len([suit for suit in suits if suit == hc_suit]) == 3
        low_bd_f_d = len([suit for suit in suits if suit == lc_suit]) == 3

        if any([high_bd_f_d, low_bd_f_d]) and self.pair_in_hand():
            return (True, False)

        return (high_bd_f_d, low_bd_f_d)
Ejemplo n.º 3
0
    def make_is_suited(self, hero_player):
        if Card.get_suit_int(hero_player.hand[0]) == Card.get_suit_int(
                hero_player.hand[1]):
            return np.array(1).reshape(1, -1)

        else:
            return np.array(0).reshape(1, -1)
Ejemplo n.º 4
0
    def encode_state_v1(self, table, hero_player):
        state_arrays = []
        for x in hero_player.hand:
            card_0_rank = Card.get_rank_int(x)
            card_0_suit = Card.get_suit_int(x)
            state_arrays.append(self.get_card_dummies(card_0_rank,
                                                      card_0_suit))

        state = np.concatenate([state_arrays], axis=1).reshape(1, 1, -1)

        # Put in  dummy variables for undealt cards
        table_card_ranks = self.table_card_ranks[:]
        table_card_suits = self.table_card_suits[:]

        for idx, x in enumerate(table.board):
            table_card_ranks[idx] = Card.get_rank_int(x)
            table_card_suits[idx] = Card.get_suit_int(x)

        print(table.board)
        print(table_card_ranks)
        print(table_card_ranks == self.table_card_ranks)
        for card_rank, card_suit in zip(table_card_ranks, table_card_suits):
            state_arrays.append(self.get_card_dummies(card_rank, card_suit))

        state_arrays = np.concatenate(state_arrays, axis=1)

        bet_and_stack = []

        bet_and_stack.append(
            np.array(hero_player.playing_position).reshape(1, -1))
        bet_and_stack.append(
            np.array(table.current_pot / table.total_chips).reshape(1, -1))
        bet_and_stack.append(
            np.array(hero_player.stack / table.total_chips).reshape(1, -1))
        bet_and_stack.append(
            np.array(table.current_bet / table.total_chips).reshape(1, -1))
        bet_and_stack.append(
            np.array(hero_player.stack / table.big_blind).reshape(1, -1))

        bet_and_stack = np.concatenate(bet_and_stack, axis=1)

        state = np.concatenate([state_arrays, bet_and_stack],
                               axis=1).reshape(1, -1)

        fold_state = np.copy(state)
        # fold_state = np.zeros(shape=np.shape(state))

        return state, fold_state
Ejemplo n.º 5
0
	def revealCards(self, gameObject, cards):
		data = {}
		data['agenda'] = "cardReveal"
		data['data'] = []
		for card in cards:
			data['data'].append(gameObject.convertNotation(Card.get_rank_int(card), Card.get_suit_int(card)))
		comms.broadcastToPlayers(gameObject.players, data);
Ejemplo n.º 6
0
    def encode_state_simple(self, table, hero_player):
        state_arrays = []

        for x in hero_player.hand:
            card_0_rank = (float(Card.get_rank_int(x)) + 1) / 13
            state_arrays.append(card_0_rank)

        if Card.get_suit_int(hero_player.hand[0]) == Card.get_suit_int(
                hero_player.hand[1]):
            is_suited = 1

        else:
            is_suited = 0

        state_arrays.append(is_suited)

        card_connectivity = abs(
            Card.get_rank_int(hero_player.hand[0]) / 13 -
            Card.get_rank_int(hero_player.hand[1]) / 13)**0.25
        state_arrays.append(card_connectivity)

        # state = np.concatenate([state_arrays], axis=1)
        state_arrays = np.array(state_arrays).reshape(1, -1)
        # print(state_arrays)

        bet_and_stack = []
        bet_and_stack.append(
            np.array(table.current_pot / hero_player.prev_stack).reshape(
                1, -1))
        bet_and_stack.append(
            np.array(hero_player.stack / hero_player.prev_stack).reshape(
                1, -1))

        bet_and_stack.append(
            np.array(hero_player.playing_position).reshape(1, -1))
        bet_and_stack = np.concatenate(bet_and_stack, axis=1)

        state = np.concatenate([state_arrays, bet_and_stack],
                               axis=1).reshape(1, -1)

        fold_state = np.copy(state)
        fold_state[0][0:6] = 0.0

        fold_q_value = hero_player.stack / hero_player.prev_stack

        return state, fold_state
Ejemplo n.º 7
0
def serialize_card(card):
    suit_int = Card.get_suit_int(card)
    rank_int = Card.get_rank_int(card)

    s = Card.INT_SUIT_TO_CHAR_SUIT[suit_int]
    r = Card.STR_RANKS[rank_int]

    return s + r
Ejemplo n.º 8
0
def get_rank_of_hand_cards(card1, card2):
    rank = 0
    features = convert_card_to_feature(card1)
    features.update(convert_card_to_feature(card2))

    suit_1 = Card.get_suit_int(card1)
    suit_2 = Card.get_suit_int(card2)
    rank_1 = Card.get_rank_int(card1)
    rank_2 = Card.get_rank_int(card2)

    features[53] = 1 if suit_1 == suit_2 else 0
    features[54] = 1 if rank_1 == rank_2 else 0
    features[55] = rank_1 / 13
    features[56] = rank_2 / 13
    features[57] = abs(rank_1 - rank_2) / 12

    if suit_1 == suit_2:  # same suit
        if rank_1 >= 8 and rank_2 >= 8:  # pokers >= 10
            rank = 10
        elif (rank_1 >= 8 or rank_2 >= 8) and abs(
                rank_1 - rank_2) < 5:  # |one of poker >= 10| and |gap < 5|
            rank = 9
        elif abs(rank_1 - rank_2) < 5:  # gap < 5
            rank = 8
        else:
            rank = 6
    elif rank_1 == rank_2:  # same rank
        if rank_1 >= 8:
            rank = 7
        elif rank_1 >= 5:
            rank = 5
        else:
            rank = 3
    else:
        if rank_1 >= 8 and rank_2 >= 8:  # pokers >= 10
            rank = 4
        elif (rank_1 >= 8 or rank_2 >= 8) and abs(
                rank_1 - rank_2) < 5:  # |one of poker >= 10| and |gap < 5|
            rank = 2
        elif rank_1 >= 8 or rank_2 >= 8 or abs(rank_1 - rank_2) < 5:  # gap < 5
            rank = 1
        else:
            rank = 0

    features[58] = rank / 10
    return rank, features
Ejemplo n.º 9
0
 def get_current_ai_state_id(self):
     # current_state_id encodes the current state as follows:
     # current_state_id[0]: Indicates whether or not the player's two cards are the same suit
     # current_state_id[1]: Value of the player's high card
     # current_state_id[2]: Value of the player's low card
     # current_state_id[3]: The actions taken, as stored in self.action_round
     if Card.get_rank_int(self.player_cards[0]) >= Card.get_rank_int(
             self.player_cards[1]):
         high_card = Card.get_rank_int(self.player_cards[0])
         low_card = Card.get_rank_int(self.player_cards[1])
     else:
         high_card = Card.get_rank_int(self.player_cards[1])
         low_card = Card.get_rank_int(self.player_cards[0])
     is_same_suit = Card.get_suit_int(
         self.player_cards[0]) == Card.get_suit_int(self.player_cards[1])
     current_state_id = (int(is_same_suit), high_card, low_card,
                         tuple(self.action_round))
     return current_state_id
Ejemplo n.º 10
0
 def cards_to_suits(cards):
     suit_dummy = np.zeros(4)
     suit_binaries = np.array([Card.get_suit_int(x) for x in cards])
     suit_dummy[0] = sum(suit_binaries == 1)
     suit_dummy[1] = sum(suit_binaries == 2)
     suit_dummy[2] = sum(suit_binaries == 4)
     suit_dummy[3] = sum(suit_binaries == 8)
     suit_dummy_std = (suit_dummy - 1.5) / 2  # Hacky "standardisation"
     return suit_dummy_std
Ejemplo n.º 11
0
 def cards_to_suits(cards):
     suit_dummy = np.zeros(4)
     suit_binaries = np.array([Card.get_suit_int(x) for x in cards])
     suit_dummy[0] = sum(suit_binaries == 1)
     suit_dummy[1] = sum(suit_binaries == 2)
     suit_dummy[2] = sum(suit_binaries == 4)
     suit_dummy[3] = sum(suit_binaries == 8)
     suit_dummy_std = (suit_dummy - 1.5) / 2  # Hacky "standardisation"
     return suit_dummy_std
Ejemplo n.º 12
0
    def make_preflop_probability(self, hero_player, table):
        if table.stage == "PREFLOP":
            card_0_rank = Card.get_rank_int(hero_player.hand[0])
            card_0_suit = Card.get_suit_int(hero_player.hand[0])

            card_1_rank = Card.get_rank_int(hero_player.hand[1])
            card_1_suit = Card.get_suit_int(hero_player.hand[1])

            if card_0_suit == card_1_suit:
                hero_score = self.preflop_suited_array[card_0_rank,
                                                       card_1_rank]

            else:
                hero_score = self.preflop_unsuited_array[card_0_rank,
                                                         card_1_rank]

        else:
            hero_score = -1

        return np.array(hero_score).reshape(1, -1)
Ejemplo n.º 13
0
    def make_hand_suit_dummies(self, hero_player):
        card_dummies = []

        for x in hero_player.hand:
            suit_int = Card.get_suit_int(x)
            suit_int = np.array(suit_int).reshape(-1, 1)
            suit_dummies = self.suit_enc.transform(suit_int).toarray()[:, :-1]
            card_dummies.append(suit_dummies)

        card_dummies = np.concatenate(card_dummies, axis=1)

        return card_dummies
Ejemplo n.º 14
0
def get_hole_power(hole):  
    ha = Card.get_rank_int(hole[0])
    hb = Card.get_rank_int(hole[1])
    
    power = ha + hb

    if Card.get_suit_int(hole[0]) == Card.get_suit_int(hole[1]):
        power += 25
            
    if ha == hb:
        power += 50
        
    if ha >= 8:
        power += ha * 3
        
    if hb >= 8:
        power += hb * 3

    if power >= 100:
        power = 100

    return power
Ejemplo n.º 15
0
    def handle_hold_rank(self, hand):
        card_1 = Card.get_rank_int(hand[0]) + 2
        card_2 = Card.get_rank_int(hand[1]) + 2

        if card_1 > card_2:
            fst_card = card_1
            sec_card = card_2
        else:
            fst_card = card_2
            sec_card = card_1

        card_list = [fst_card, sec_card]
        if Card.get_suit_int(hand[0]) == Card.get_suit_int(hand[1]):
            card_list.append('s')

        rank = 9
        for key in TWO_CARD_RANK_EVA.keys():
            value = TWO_CARD_RANK_EVA[key]
            if card_list in value:
                rank = int(key)
                break

        return rank
Ejemplo n.º 16
0
def convert_card_to_feature(card):
    features = {}
    suit = Card.get_suit_int(card)
    # print(suit)
    rank = Card.get_rank_int(card)
    if suit == 1:
        index = rank
    elif suit == 2:
        index = rank + 13
    elif suit == 4:
        index = rank + 26
    elif suit == 8:
        index = rank + 39
    features[index] = 1
    return features
Ejemplo n.º 17
0
    def make_board_suit_dummies(self, table):
        card_dummies = []
        table_card_suits = self.table_card_suits[:]

        for idx, x in enumerate(table.board):
            suit_int = Card.get_suit_int(x)
            table_card_suits[idx] = suit_int

        for x in table_card_suits:
            x = np.array(x).reshape(-1, 1)
            suit_dummies = self.suit_enc.transform(x).toarray()[:, :-1]
            card_dummies.append(suit_dummies)

        card_dummies = np.concatenate(card_dummies, axis=1)

        return card_dummies
Ejemplo n.º 18
0
def get_lowest_unpairing_card(hand):
    """Add the worst possible card to an incomplete hand. Something
    that cannot pair up or make a flush or make a straight."""
    existing_ranks = set([Card.get_rank_int(x) for x in hand])
    remaining_ranks = list(set(range(12)) - existing_ranks)
    selected_rank = remaining_ranks[0]

    would_be_hand = hand + [Card.new(Card.STR_RANKS[selected_rank] + 'h')]
    if is_straight(would_be_hand):
        selected_rank = remaining_ranks[1]  # Don't make a straight

    selected_rank_str = Card.STR_RANKS[selected_rank]

    last_suit = Card.get_suit_int(hand[-1])
    last_suit_index = [1, 2, 4, 8].index(last_suit)
    selected_suit = [1, 2, 4, 8][(last_suit_index + 1) % 4]
    selected_suit_str = Card.INT_SUIT_TO_CHAR_SUIT[selected_suit]

    selected_card_str = selected_rank_str + str(selected_suit_str)
    return Card.new(selected_card_str)
Ejemplo n.º 19
0
def get_lowest_unpairing_card(hand):
    """Add the worst possible card to an incomplete hand. Something
    that cannot pair up or make a flush or make a straight."""
    existing_ranks = set([Card.get_rank_int(x) for x in hand])
    remaining_ranks = list(set(range(12)) - existing_ranks)
    selected_rank = remaining_ranks[0]

    would_be_hand = hand + [Card.new(Card.STR_RANKS[selected_rank] + 'h')]
    if is_straight(would_be_hand):
        selected_rank = remaining_ranks[1]    # Don't make a straight

    selected_rank_str = Card.STR_RANKS[selected_rank]

    last_suit = Card.get_suit_int(hand[-1])
    last_suit_index = [1, 2, 4, 8].index(last_suit)
    selected_suit = [1, 2, 4, 8][(last_suit_index + 1) % 4]
    selected_suit_str = Card.INT_SUIT_TO_CHAR_SUIT[selected_suit]

    selected_card_str = selected_rank_str + str(selected_suit_str)
    return Card.new(selected_card_str)
Ejemplo n.º 20
0
	def endRound(self, gameObject, winners):
		#winners=[0,1]
		data = {}
		data['agenda'] = "playerWonRound"
		data['data'] = {'winnerSid': [], 'earnings': [], 'playerSid':[], 'playerHands':[], 'currentCash':[]}

		if(len(winners)==1):
			#only 1 winner
			gameObject.players[winners[0]].money+=self.pot
			print("Player " + str(winners[0]) + " WON")
			data['data']['winnerSid'] = gameObject.players[winners[0]].id	#front checks len(data['playerSid'])
			data['data']['earnings'] = self.pot
		else:
			#OH LORD HELP US, CE BO TO DELALO
			mainPot=0
			sidePot=0
			leftoverMainPot=0
			leftoverSidePot=0
			retVal=self.getMinBetAndSecondMinBetAndItsIndexes()
			minBetIndex=retVal[0]
			minBet=retVal[1]
			secondMinIndex=retVal[2]
			secondMinBet=retVal[3]
			sidePotWinners=[]
			for i in range(len(self.roundPlayers)):	#get main pot
				#calculate mainPot
				if(self.playerStatus[i] is not None):
					mainPot+=minBet

				#advanced black magic sidePot calculations
				if(self.playerStatus[i] is not None and self.roundPlayers[i].currentBet > minBet):
					sidePot+=(secondMinBet-minBet)
					if(self.roundPlayers[i].currentBet>secondMinBet):
						self.roundPlayers[i].returnMoney((self.roundPlayers[i].currentBet-secondMinBet))
						sidePotWinners.push(i)
						self.roundPlayers[i].currentBet=secondMinBet #needed?
			
			if(len(winners)==0):
				mainPotSplit=mainPot
			else:
				mainPotSplit=math.floor(mainPot/len(winners))
				leftoverMainPot=mainPot%len(winners)
			if(len(sidePotWinners)==0):
				sidePotSplit=sidePot
				sidePotWinners=winners
			else:
				sidePotSplit=math.floor(sidePot/len(sidePotWinners))
				leftoverSidePot=sidePot%len(sidePotWinners)
			
			for j in range(len(winners)):
				if(self.roundPlayers[j].currentBet==minBet):
					gameObject.players[winners[j]].money+=mainPotSplit
				if(self.roundPlayers[j].currentBet > minBet):
					gameObject.players[winners[j]].money+=sidePotSplit
			#split leftover earnings amongst players (remainder at pot/ammountOfWinners; example=> 15/2=7=>remainder:1; first winners gets the leftover money)
			#requires in-depth testing
			mainLeftoverEarnings=[]
			for p in winners:
				mainLeftoverEarnings.append(0)
			mainPotPI=0
			while(leftoverMainPot>0):
				mainLeftoverEarnings[mainPotPI]+=1
				leftoverMainPot-=1
				mainPotPI+=1
				if(mainPotPI>len(winners)):
					mainPotPI=0
			sideLeftoverEarnings=[]
			for p in sidePotWinners:
				sideLeftoverEarnings.addpend(0)
			sidePotPI=0
			while(leftoverSidePot>0):
				sideLeftoverEarnings[sidePotPI]+=1
				leftoverSidePot-=1
				sidePotPI+=1
				if(sidePotPI>len(sidePotWinners)):
					sidePotPI=0

			mainPotPI2=0
			sidePotPI2=0
			for p in winners:
				earnings=mainPotSplit
				if(mainPotPI2<len(winners)):
					earnings+=mainLeftoverEarnings[mainPotPI2]
					mainPotPI2+=1
				data['data']['winnerSid'].append(gameObject.players[p].id)
				if(p in sidePotWinners):
					earnings+=sidePotSplit
					if(sidePotPI2<len(sideLeftoverEarnings)):
						earnings+=sideLeftoverEarnings[sidePotPI2]
						sidePotPI2+=1
				data['data']['earnings'].append(earnings)

			#split the pot-DEPRECATED

			#potSplit=self.pot / len(winners)
			#print("Pot split between: ")
			#for p in winners:
			#	gameObject.players[winnerIndex].money+=potSplit
			#	print("Player " + str(winners[0]) + " ")
			#	data['playerSid'].append(gameObject.players[p])
			#	data['earnings'].append(potSplit)
		
		for player in gameObject.players:
			data['data']['playerSid'].append(player.id)
			convertedHand = player.hand
			convertedHand[0] = gameObject.convertNotation(Card.get_rank_int(convertedHand[0]), Card.get_suit_int(convertedHand[0]))
			convertedHand[1] = gameObject.convertNotation(Card.get_rank_int(convertedHand[1]), Card.get_suit_int(convertedHand[1]))
			data['data']['playerHands'].append(convertedHand)
			data['data']['currentCash'].append(player.money)
		print(data)
		comms.broadcastToPlayers(gameObject.players, data);

		gameObject.roundCounter+=1
		print("\r\n\r\n")
Ejemplo n.º 21
0
 def get_card_suit(self, card):
     return Card.get_suit_int(card)
Ejemplo n.º 22
0
 def suits(self):
     """Return a list of the cards' suits, in order."""
     return [Card.INT_SUIT_TO_CHAR_SUIT[Card.get_suit_int(card)] for card in self._cards]
Ejemplo n.º 23
0
 def hand_is_suited(self):
     return Card.get_suit_int(self._cards[0]) == Card.get_suit_int(
         self._cards[1])
Ejemplo n.º 24
0
def generate_odds():
    card_ints = [i for i in range(13)]
    pair_combos = combinations_with_replacement(card_ints, 2)
    eval = Evaluator()

    pair_scores = np.zeros(shape=(13, 13))

    pair_suits = [8, 8]
    for x in pair_combos:
        deck = Deck()
        deck_match_idxs = []
        hero_cards = [None, None]

        if x[0] == x[1]:
            continue

        # Find cards in deck
        for deck_idx, card in enumerate(deck.cards):

            if x[0] == Card.get_rank_int(card) and pair_suits[0] == Card.get_suit_int(card):
                hero_cards[0] = card
                deck_match_idxs.append(deck_idx)

            if x[1] == Card.get_rank_int(card) and pair_suits[1] == Card.get_suit_int(card):
                hero_cards[1] = card
                deck_match_idxs.append(deck_idx)

        # Remove hero cards from deck
        deck.cards = [i for idx, i in enumerate(deck.cards) if idx not in deck_match_idxs]

        # Repeat x times
        num_wins = 0
        num_losses = 0
        while num_wins + num_losses < 10000:
            # Draw villan cards
            villan_cards = deck.draw(2)

            # Draw five card board
            board = deck.draw(5)


            # Find winner
            hero_rank = eval.evaluate(hero_cards, board)
            villan_rank = eval.evaluate(villan_cards, board)

            if hero_rank < villan_rank:
                num_wins += 1

            elif hero_rank > villan_rank:
                num_losses += 1

            else:
                None

            # Put villan and board cards back into deck
            deck.cards.extend(villan_cards)
            deck.cards.extend(board)


            # Shuffle deck for next draw
            shuffle(deck.cards)


        pair_scores[x[0], x[1]] = num_wins / (num_wins + num_losses)
        pair_scores[x[1], x[0]] = num_wins / (num_wins + num_losses)
        np.savetxt('./suited_pair_scores.csv', pair_scores, delimiter=',', fmt='%5f')
        print(pair_scores)
Ejemplo n.º 25
0
	def startRound(self, gameObject):
		self.board=[]
		self.pot=0
		self.sidePot=0
		print("MAIN POT: "+ str(self.pot)+ " SIDE POT: "+ str(self.sidePot))
		playerCount = len(self.roundPlayers)
		if(playerCount <= 1): #check if there are more than 1 players in round
			print("ENDING GAME - NOT ENOUGH PLAYERS LEFT")
			return False #DONE implementiraj ce ni nobenega ker so leavali
		
		#deal initial hands
		for i in range(2):
			for player in self.roundPlayers:
				player.hand.append(self.deck.draw(1))
		#game start & initial cards
		for player in self.roundPlayers:
			data = {}
			data['agenda'] = "roundStart"
			data['status'] = "ok"
			currentHand = player.hand
			data['data'] = [
				gameObject.convertNotation(Card.get_rank_int(currentHand[0]), Card.get_suit_int(currentHand[0])),
				gameObject.convertNotation(Card.get_rank_int(currentHand[1]), Card.get_suit_int(currentHand[1]))
			]
			comms.send(player.socket, data);

		#Preflop phase
		print("--Preflop--")
		result = self.preflopPhase(gameObject, (gameObject.roundCounter % len(self.roundPlayers)))
		#check if all but 1 folded else go to next phase      
		if(not result): #only 1 player remains active; end game
			lastPlayerIndex=self.getLastPlayer()
			if(lastPlayerIndex is not False):
				self.endRound(gameObject, [lastPlayerIndex])
				return True   
			else:
				return False         
		#Flop phase
		print("--Flop--")
		self.board=self.deck.draw(3)    #3 cards to board
		self.revealCards(gameObject, self.board)
		result=self.bettingPhase(gameObject, (gameObject.roundCounter % len(self.roundPlayers)))
		#check if all but 1 folded else go to next phase
		if(not result): #only 1 player remains active; end game
			lastPlayerIndex=self.getLastPlayer()
			if(lastPlayerIndex is not False):
				self.endRound(gameObject, [lastPlayerIndex])
				return True   
			else:
				return False
		#Turn phase
		print("--Turn--")
		drawnCard = self.deck.draw(1)
		self.revealCards(gameObject, [drawnCard])
		self.board.append(drawnCard)
		result=self.bettingPhase(gameObject, (gameObject.roundCounter % len(self.roundPlayers)))
		#check if all but 1 folded else go to next phase
		if(not result): #only 1 player remains active; end game
			lastPlayerIndex=self.getLastPlayer()
			if(lastPlayerIndex is not False):
				self.endRound(gameObject, [lastPlayerIndex])
				return True   
			else:
				return False
		#River phase
		print("--River--")
		drawnCard = self.deck.draw(1)
		self.revealCards(gameObject, [drawnCard])
		self.board.append(drawnCard)
		result=self.bettingPhase(gameObject, (gameObject.roundCounter % len(self.roundPlayers)))
		#end hand comparison
		if(not result): #only 1 player remains active; end game
			lastPlayerIndex=self.getLastPlayer()
			if(lastPlayerIndex is not False):
				self.endRound(gameObject, [lastPlayerIndex])
				return True
			else:
				return False
		# allHands = []
		playerCount = len(self.roundPlayers)
		for i in range(playerCount):
			if(self.playerStatus != None):
				self.playerHandScores.append(evaluator.evaluate(self.board, self.roundPlayers[i].hand))
				self.playerHandClasses.append(evaluator.get_rank_class(self.playerHandScores[i]))
		# 		allHands.append(self.roundPlayers[i].hand)
			else:
				self.playerHandScores.append(None)
				self.playerHandClasses.append(None)
				# allHands.append(self.roundPlayers[i].hand)
		#print(str(Card.get_suit_int(self.board[0]))+" "+str(Card.get_rank_int(self.board[0])))
		# print("BOARD: ")
		# Card.print_pretty_cards(self.board)        
		# for i in range(playerCount):
		# 	if(self.playerStatus[i]!=None):
		# 		print("Player " + str(i) + ": ")
		# 		Card.print_pretty_cards(self.roundPlayers[i].hand)
		# 	else:
		# 		print("Player " + str(i) + ": FOLDED")


		winners=self.getWinnerIndexes()      
		self.endRound(gameObject, winners)
		#DONE(test): ob koncu igre klici removePlayer() nad vsemi igralci ki so ostali zavoljo konsistence
		# evaluator.hand_summary(self.board, allHands)      
		return True
Ejemplo n.º 26
0
    def encode_state_by_table(self, table, hero_player):
        state_arrays = []

        # Unpack card ranks
        for x in hero_player.hand:
            # card_0_rank = (float(Card.get_rank_int(x)) + 1) / 13
            card_0_rank = Card.get_rank_int(x) + 1
            card_0_rank = self.min_max_scaling(-1, 1, 0, 13, card_0_rank)
            state_arrays.append(card_0_rank)

        c_1_suit = Card.get_suit_int(hero_player.hand[0])
        c_1_suit = self.min_max_scaling(-1, 1, 0, 9, c_1_suit)
        c_2_suit = Card.get_suit_int(hero_player.hand[1])
        c_2_suit = self.min_max_scaling(-1, 1, 0, 9, c_2_suit)

        state_arrays.append(c_1_suit)
        state_arrays.append(c_2_suit)

        # state = np.concatenate([state_arrays], axis=1).reshape(1, 1, -1)

        # Put in  dummy variables for undealt cards
        table_card_ranks = self.table_card_ranks[:]
        table_card_suits = self.table_card_suits[:]

        for idx, x in enumerate(table.board):
            table_card_ranks[idx] = Card.get_rank_int(x) + 1
            table_card_ranks[idx] = self.min_max_scaling(
                -1, 1, 0, 13, table_card_ranks[idx])

            table_card_suits[idx] = Card.get_suit_int(x)
            table_card_suits[idx] = self.min_max_scaling(
                -1, 1, 0, 9, table_card_suits[idx])

        # is preflop
        is_preflop = 0
        is_flop = 0
        is_turn = 0
        is_river = 0

        if table.stage == "PREFLOP":
            is_preflop = 1
        elif table.stage == "FLOP":
            is_flop = 1
        elif table.stage == "TURN":
            is_turn = 1
        elif table.stage == "RIVER":
            is_river = 1

        else:
            print("error")
            exit()

        state_arrays.append(is_preflop)
        state_arrays.append(is_flop)
        state_arrays.append(is_turn)
        state_arrays.append(is_river)

        hero_score = 7462
        # evaluate hand if not preflop
        if not is_preflop:
            board = [x for x in table.board if x != 13]
            hero_score = self.evaluator.evaluate(board, hero_player.hand)

        hero_score = self.min_max_scaling(-1, 1, 0, 7462, hero_score)

        state_arrays.append(hero_score)

        state_arrays = np.array(state_arrays)
        state_arrays = state_arrays.reshape(1, -1)

        bet_and_stack = []
        bet_and_stack.append(
            np.array(hero_player.playing_position).reshape(1, -1))

        bet_total_ratio = table.current_bet / table.total_chips
        stack_contrib_ratio = hero_player.stack / table.total_chips
        win_stack_ratio = (hero_player.stack +
                           table.current_pot) / table.total_chips

        bet_total_ratio = self.min_max_scaling(-1, 1, 0, 1, bet_total_ratio)
        stack_contrib_ratio = self.min_max_scaling(-1, 1, 0, 2,
                                                   stack_contrib_ratio)
        win_stack_ratio = self.min_max_scaling(-1, 1, 1, 2, win_stack_ratio)

        bet_and_stack.append(np.array(bet_total_ratio).reshape(1, -1))
        bet_and_stack.append(np.array(stack_contrib_ratio).reshape(1, -1))
        bet_and_stack.append(np.array(win_stack_ratio).reshape(1, -1))

        bet_and_stack = np.concatenate(bet_and_stack, axis=1)

        state = np.concatenate([state_arrays, bet_and_stack],
                               axis=1).reshape(1, -1)

        # fold_state = np.copy(state)
        fold_state = np.zeros(shape=np.shape(state))
        fold_state[-1][-1] = stack_contrib_ratio

        # Fold state doesn't matter what the hero cards are, score, position

        return state, fold_state
Ejemplo n.º 27
0
    def encode_state_by_hand(self, table, hero_player):
        state_arrays = []

        # Unpack card ranks
        for x in hero_player.hand:
            # card_0_rank = (float(Card.get_rank_int(x)) + 1) / 13
            card_0_rank = Card.get_rank_int(x)
            card_0_rank = self.min_max_scaling(-1, 1, 0, 12, card_0_rank)
            state_arrays.append(card_0_rank)

        c_1_suit = Card.get_suit_int(hero_player.hand[0])
        # c_1_suit = self.min_max_scaling(-1, 1, 0, 9, c_1_suit)
        c_2_suit = Card.get_suit_int(hero_player.hand[1])
        # c_2_suit = self.min_max_scaling(-1, 1, 0, 9, c_2_suit)

        # state_arrays.append(c_1_suit)
        # state_arrays.append(c_2_suit)

        if c_1_suit == c_2_suit:
            state_arrays.append(1)

        else:
            state_arrays.append(-1)

        # state = np.concatenate([state_arrays], axis=1).reshape(1, 1, -1)

        # is preflop
        is_preflop = 0
        is_flop = 0
        is_turn = 0
        is_river = 0

        if table.stage == "PREFLOP":
            is_preflop = 1
        elif table.stage == "FLOP":
            is_flop = 1
        elif table.stage == "TURN":
            is_turn = 1
        elif table.stage == "RIVER":
            is_river = 1

        else:
            print("error")
            exit()

        state_arrays.append(is_preflop)

        hero_score = 7462
        # evaluate hand if not preflop
        if not is_preflop:
            board = [x for x in table.board if x != 13]
            hero_score = self.evaluator.evaluate(board, hero_player.hand)
            hero_score = self.min_max_scaling(0, 1, 0, 7462, hero_score)

        else:
            card_0_rank = Card.get_rank_int(hero_player.hand[0])
            card_1_rank = Card.get_rank_int(hero_player.hand[1])

            if c_1_suit == c_2_suit:
                hero_score = self.preflop_suited_array[card_0_rank,
                                                       card_1_rank]

            else:
                hero_score = self.preflop_unsuited_array[card_0_rank,
                                                         card_1_rank]

            # if hero_score == 0:
            #     print(c_1_suit, c_2_suit)
            #     print(card_0_rank, card_1_rank)
            #     print(self.preflop_unsuited_array)
            #     exit()

        state_arrays.append(hero_score)

        state_arrays = np.array(state_arrays)
        state_arrays = state_arrays.reshape(1, -1)

        bet_and_stack = []
        bet_and_stack.append(
            np.array(hero_player.playing_position).reshape(1, -1))

        # pot_total_ratio = table.current_pot/table.total_chips
        # stack_total_ratio = hero_player.stack/table.total_chips
        bet_total_ratio = table.current_bet / table.total_chips
        # stack_bb_ratio = table.big_blind/hero_player.stack
        stack_contrib_ratio = hero_player.stack / hero_player.prev_stack
        win_stack_ratio = (hero_player.stack +
                           table.current_pot) / hero_player.prev_stack

        # bet_total_ratio = self.min_max_scaling(-1, 1, 0, 1, bet_total_ratio)
        # stack_contrib_ratio = self.min_max_scaling(-1, 1, 0, 2, stack_contrib_ratio)
        # win_stack_ratio = self.min_max_scaling(-1, 1, 1, 2, win_stack_ratio)

        # bet_and_stack.append(np.array(pot_total_ratio).reshape(1, -1))
        # bet_and_stack.append(np.array(stack_total_ratio).reshape(1, -1))
        bet_and_stack.append(np.array(bet_total_ratio).reshape(1, -1))
        # bet_and_stack.append(np.array(stack_bb_ratio).reshape(1, -1))
        bet_and_stack.append(np.array(stack_contrib_ratio).reshape(1, -1))
        bet_and_stack.append(np.array(win_stack_ratio).reshape(1, -1))

        # bet_and_stack.append(np.array(table.current_pot/table.total_chips).reshape(1, -1))
        # bet_and_stack.append(np.array(hero_player.stack/table.total_chips).reshape(1, -1))
        # bet_and_stack.append(np.array(table.current_bet/table.total_chips).reshape(1, -1))
        # bet_and_stack.append(np.array(hero_player.stack/table.big_blind).reshape(1, -1))
        # bet_and_stack.append(np.array(hero_player.stack/hero_player.contribution_to_pot).reshape(1, -1))

        bet_and_stack = np.concatenate(bet_and_stack, axis=1)

        # print(bet_and_stack)

        state = np.concatenate([state_arrays, bet_and_stack],
                               axis=1).reshape(1, -1)

        # fold_state = np.copy(state)
        fold_state = np.zeros(shape=np.shape(state))
        fold_state[-1][-1] = stack_contrib_ratio

        # Fold state doesn't matter what the hero cards are, score, position

        return state, fold_state
Ejemplo n.º 28
0
 def get_pokers(self):
     res = []
     for poker in self.pokers:
         res.append((Card.get_rank_int(poker), Card.get_suit_int(poker)))
     return res