def center_pile_collected(game_state,player_num,turned_cards,c): print("player num "+str(player_num)+" picked up cards") player_index=int(player_num)-1 if game_state._players[player_index]!=game_state._bot: #the bot is not the one that picked up the cards picked_cards=[] for card in turned_cards: if card not in picked_cards: picked_cards.append(card) for card in game_state._known_center_cards: if card not in picked_cards: picked_cards.append(card) game_state._players[player_index]._hand=[] for card in picked_cards: game_state._players[player_index]._hand.append(card) game_state._players[player_index]._hand.sort(key=lambda x:game_state.get_number_val(x['Value'])) game_state._players[player_index]._num_cards+=game_state._num_cards_center print("opponent now has "+str(game_state._players[player_index]._num_cards)+" cards") else: #the bot is the one that picked up the cards c.update_player_info() game_state._bot._hand=c.hand game_state._bot._hand.sort(key=lambda x:game_state.get_number_val(x['Value'])) game_state._bot.count_num_cards() game_state._bot._num_cards=len(game_state._bot._hand) game_state._bot.count_cycles_until_win_bot() print("my hand is now "+str(game_state._bot._num_cards)+" cards") game_state._num_played_cards+=game_state._num_cards_center game_state._num_played_cards-=game_state._players[player_index]._cards_played_into_center game_state._num_cards_center=0 for player in game_state._players: player._cards_played_into_center=0
def should_call_bluff(self, game_state, opponent, card_val, num_cards_played): # decides whether or not the bot should call bluff on another player. if card_val == "Ace": card_val = 1 elif card_val == "Jack": card_val = 11 elif card_val == "Queen": card_val = 12 elif card_val == "King": card_val = 13 if isinstance(card_val, list): card_val = int(card_val[1]) opponent_ind = int(opponent) - 1 print(str(game_state._bot._num_each_card)) k = game_state._bot._num_each_card[ card_val - 1] #should be the number of the sought card in own hand r = num_cards_played #should be the number of the sought card played by the opponent h = game_state._bot._num_cards #should be own hand size l = game_state._players[ opponent_ind]._num_cards #should be opponent's hand size j = game_state._bot._cycles_until_win #should be how many turns until bot wins i = game_state._num_cards_center + num_cards_played #should be number of cards in center pile print(str(h) + " " + str(l)) for card in game_state._known_center_cards: if game_state.get_number_val(card['Value']) == card_val: k += 1 for card in game_state._known_center_cards: for hand in game_state._bot._hand: if (game_state.get_number_val( card['Value']) == game_state.get_number_val( hand['Value'])): i -= 1 #immediately call bluff if it's the opponent's last card if (l == 0): return 1.0 #immediately call bluff if there appear to be more than four of a kind if (k + r > 4): return 1.0 #otherwise, calculate the probability of the player lying #considers both the probability of the opponent not having the card #as well as how many cards are in the center pile (risk) #and how close the bot is to winning (it will not lie if it can win #in the next two turns), as it returns a negative float then. prob = probfunc.ncr(4 - k, r) * probfunc.ncr( 48 - h + k, l - r) / probfunc.ncr(52 - h, l) return (1 - prob) * (1 / ((i + 0.1))) * ( 0.001 / (0.001 + 0.001 * 0.999 * math.exp(-0.5 * (h - 6.5)))) * (1 / (h + 0.1))
def should_call_bluff(self,game_state, opponent, card_val, num_cards_played): # decides whether or not the bot should call bluff on another player. if card_val=="Ace": card_val=1 elif card_val=="Jack": card_val=11 elif card_val=="Queen": card_val=12 elif card_val=="King": card_val=13 if isinstance(card_val,list): card_val=int(card_val[1]) opponent_ind=int(opponent)-1 print(str(game_state._bot._num_each_card)) k = game_state._bot._num_each_card[card_val-1] #should be the number of the sought card in own hand r = num_cards_played #should be the number of the sought card played by the opponent h = game_state._bot._num_cards #should be own hand size l = game_state._players[opponent_ind]._num_cards #should be opponent's hand size j = game_state._bot._cycles_until_win #should be how many turns until bot wins i = game_state._num_cards_center+num_cards_played #should be number of cards in center pile print(str(h)+" "+str(l)) for card in game_state._known_center_cards: if game_state.get_number_val(card['Value'])==card_val: k+=1 for card in game_state._known_center_cards: for hand in game_state._bot._hand: if (game_state.get_number_val(card['Value']) == game_state.get_number_val(hand['Value'])): i-=1 #immediately call bluff if it's the opponent's last card if (l==0): return 1.0 #immediately call bluff if there appear to be more than four of a kind if (k+r > 4): return 1.0 if (j <= 2): return 0.0 #if these two cases aren't satisfied, then this gets passed to our ML model to determine if the bot should lie or not return callBluffPredictOutput.predict(np.array([[k, r, h, l, j, i]]))
def center_pile_collected(game_state, player_num, turned_cards, c): player_index = int(player_num) - 1 print("i know that player " + str(player_num) + " has " + str(game_state._known_center_cards)) have_card = False for card in turned_cards: have_card = False for known_card in game_state._players[player_index]._hand: if card == known_card: have_card = True if have_card == False: game_state._players[player_index]._hand.append(card) for card in game_state._known_center_cards: have_card = False for known_card in game_state._players[player_index]._hand: if card == known_card: have_card = True if have_card == False: game_state._players[player_index]._hand.append(card) game_state._players[player_index]._hand.sort( key=lambda x: game_state.get_number_val(x['Value'])) game_state._players[player_index].count_num_cards() game_state._players[player_index]._num_cards += ( game_state._num_cards_center + len(turned_cards)) game_state._num_played_cards += game_state._num_cards_center game_state._num_cards_center = 0 game_state._num_played_cards -= game_state._players[ player_index]._cards_played_into_center if game_state._players[player_index] == game_state._bot: c.update_player_info() game_state._bot._hand = c.hand game_state._bot._hand.sort( key=lambda x: game_state.get_number_val(x['Value'])) game_state._bot.count_num_cards() game_state._bot.count_cycles_until_win_bot() for player in game_state._players: player._cards_played_into_center = 0
def run_bot(): with open('data.csv', 'w') as csvFile: writer = csv.writer(csvFile) bluff_thresh=.3 #temp call_thresh=.3 #temp in_progress=False c=cheat.client.Client("My_Cheat_Bot") game_list = c.list_games() game_id= game_list[-1]['Id'] join_game(c,game_id) if c.wait_for_message()[0]=='GAME_STARTED': game_state=start_game(c) while True: #start playing the game here c.update_player_info() lie = [] print("time to play!") game_state._bot._hand = c.hand game_state._bot.count_cards(c.hand) if int(c.get_current_turn()['Position'])==game_state._bot_pos: data = [] data.append(len(c.hand)) print("playing cards...") value=c.get_current_turn()['CardValue'] play=decide_cards_to_play(value,game_state,bluff_thresh,data,lie) print(play) print(c.hand) c.play_cards(play) game_state._bot._sequence.append(game_state._bot._sequence.pop(0)) c.update_player_info() msg=c.wait_for_message() if msg[0]=='GAME_OVER': print('Gave Over') break else: msg=c.wait_for_message() if msg[0]=='GAME_OVER': print('Gave Over') break if msg[0]=='CARDS_PLAYED': x=c.get_current_turn() game_state._players[int(x['Position'])-1]._cards_played_into_center+=int(x['CardsDown']) #remove known cards from opponent if game_state._players[int(x['Position'])-1]._num_each_card[game_state.get_number_val(x['CardValue'])-1]!=0: for card in game_state._players[int(x['Position'])-1]._hand: if game_state.get_number_val(card['Value'])==game_state.get_number_val(x['CardValue']): del card game_state._players[int(x['Position'])-1]._num_each_card=0 print("deciding to call...") game_state._players[int(x['Position'])-1]._sequence.append(game_state._players[int(x['Position'])]._sequence[-1]) if decide_call_bluff(game_state,int(x['Position']),x['CardValue'],x['CardsDown'],call_thresh): print("i call cheat!") c.play_call() c.update_player_info() else: print("seems ok enough...") c.play_pass() c.update_player_info() msg=c.wait_for_message() called = 1 if msg[0]=='CALLED': called = 0 if msg[1][1]['WasLie']==False: center_pile_collected(game_state,int(msg[1][1]['CallPosition']),msg[1][1]['Cards']) else: x=c.get_current_turn() center_pile_collected(game_state,int(x['Position']),msg[1][1]['Cards']) msg=c.wait_for_message() if(len(lie) != 0): data.append(called) writer.writerow(data) if msg[0]=='GAME_OVER': print('Gave Over') break if msg[0]=='TURN_OVER': pass csvFile.close()