73
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <time.h> #include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <fstream> #include <string> using namespace std; # define INPUT_FILE "inputf.txt" # define DEAL_FILE "deal.txt" # define DEALS_MONEY "deals_money.txt" # define RIVER_CARDS "river_cards.txt" # define CHECKPATTERN_FILE "checkpattern.txt" # define PRE_FLOP 1 # define FLOP 2 # define FOURTH_STREET 3 # define FIFTH_STREET 4 # define SHOW_DOWN 5 # define SKIP_NEWLINE while(*ptr == 10 || *ptr==13){ptr++;} # define FIRST (int)'A' # define CALL (int)'C' # define RISE (int)'R' # define FOLD (int)'F' # define FOLDED (int) '-' # define BLUFF (int)'B' # define VERY_GOOD 5 # define GOOD 4 # define AVG 3 # define BAD 2 # define VERY_BAD 1 # define HIGH 1 # define LOW 0 # define OFFENSIVE 1 # define DEFENSIVE 2 # define ULTRA_DEFENSIVE 3 struct player { int handCard1; int handCard2; int team; int bet; int money_remaining; }; short int players_data_move[7][5][500]={0}; /* [TEAM NUMBER][STAGE][ROUND] */ short int players_data_bet[7][5][500]={0}; /* [TEAM NUMBER][STAGE][ROUND] */

AI For Texam Hold'em poker

  • Upload
    -

  • View
    321

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: AI For Texam Hold'em poker

#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <time.h> #include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <fstream> #include <string> using namespace std; # define INPUT_FILE "inputf.txt" # define DEAL_FILE "deal.txt" # define DEALS_MONEY "deals_money.txt" # define RIVER_CARDS "river_cards.txt" # define CHECKPATTERN_FILE "checkpattern.txt" # define PRE_FLOP 1 # define FLOP 2 # define FOURTH_STREET 3 # define FIFTH_STREET 4 # define SHOW_DOWN 5 # define SKIP_NEWLINE while(*ptr == 10 || *ptr==13){ptr++;} # define FIRST (int)'A' # define CALL (int)'C' # define RISE (int)'R' # define FOLD (int)'F' # define FOLDED (int) '-' # define BLUFF (int)'B' # define VERY_GOOD 5 # define GOOD 4 # define AVG 3 # define BAD 2 # define VERY_BAD 1 # define HIGH 1 # define LOW 0 # define OFFENSIVE 1 # define DEFENSIVE 2 # define ULTRA_DEFENSIVE 3 struct player { int handCard1; int handCard2; int team; int bet; int money_remaining; }; short int players_data_move[7][5][500]={0}; /* [TEAM NUMBER][STAGE][ROUND] */ short int players_data_bet[7][5][500]={0}; /* [TEAM NUMBER][STAGE][ROUND] */

Page 2: AI For Texam Hold'em poker

short int pot_money[7][5][500]={0}; /* [TEAM NUMBER][STAGE][ROUND] */ short int players_hand_card_1[7]; /* [TEAM NUMBER] */ short int players_hand_card_2[7]; /* [TEAM NUMBER] */ short int river_cards[5],r_count=0; /* [STAGE] */ short int player_p_values[7][5]; /* [TEAM NUMBER][STAGE] */ short int checkPattern[6][5][4][3][7] ={0}; /* [PVALUE][ACTION][REACTION][STAGE][TEAMNUMBER] */ int round_number[5]; /* [STAGE] */ int p_ratio[6]; /* [PVALUE] */ int reaction_ratio[5]; /* [REACTION] */ bool players_folded[7]; /* [TEAM NUMBER] */ short int number_of_folds; /* Stores the Number of Folds */ short int play_mode; /* 1 :: OFFENSIVE | 2 :: DEFENSIVE | 3 :: ULTRA DEFENSIVE */ short int number_of_deals; /* Stores the Number of Deals */ player players[7]; int deal_number; /* Represents the nth deal */ bool is_show_down; /* Tells if its Show Down or not */ bool stagePlayed[6]={0}; /* Bool of Stages Played */ bool playersEliminated[7]; /* [TEAM NUMBER] */ short int num_players_eliminated = 0; void printPlayerDetails(player p); void read_file(char *name,char *data); int getInt(char *data); int decode_input_file(char *data); int decode_dealsMoney(char *data); void getString(char *str,char *data); int decode_dealFile(char *data,int *stage,int *main_pot); void read_showDown_handCards(char *data); void get_current_state_players(char *data,int stage,int self_teamNumber,int dealer); void set_play_mode(int self_team_number); int defensive_play(int stage,int *all_cards,int n_total_cards,int self_team_number,int dealer); bool is_bluffsMore(short int team_number); int get_minimum_raise(short int stage,short int team_number,short int dealer); short int get_rank(int self_team_number); void set_p_ratio(); void set_reaction_ratio(); void set_player_folded(); bool is_FirstTurn(short int dealer, short int self_team_number); short int checkPattern_p(int action,int reaction, int stage ,int team,int detail); short int checkPattern_action(int p_value,int action,int reaction, int stage ,int team,int detail); int catagorize_move(int p_move); void analyze_data(int self_teamNumber,int dealer); int offensive(int stage,int *cards,int total,int teamno,int dealer); int getMove(int stage,int phand,int *team,int teamno,int dealer);

Page 3: AI For Texam Hold'em poker

int bluff(int rise, int limit); int lowraise(int bet); int compare (const void * a, const void * b); int pcounter(int *cards,int hand,int total); int highcard(int *hole,int hand); int pairs(int *cards,int *allpairs,int total); int twoofakind(int *allpairs,int paircards,int *hole,int hand); int twopair(int *allpairs,int paircards,int *hole,int hand); int threeofakind(int *allpairs,int paircards,int *cards,int total,int *hole , int hand); int straight(int *cards,int total,int *hole,int hand); int flush(int *cards,int total,int *hole,int hand); int fullhouse(int *allpairs,int paircards,int *cards,int total,int *hole , int hand ); int fourofakind(int *allpairs,int paircards,int *hole,int hand); int striaghtflush(int *cards,int total,int *hole,int hand); int priver(int*cards,int hand,int total); int pgrader(int probab , int stage); int ppreflop(int hand1,int hand2); int pfuture(int *cards,int total); int futurestraight(int *cards,int total); int futureflush(int *cards, int total); int overallp(int*cards,int total,int stage); void start_deal(char *data,int self_teamNumber,int *players_left); int anyoppraisehigh(int stage,int round,int firstplayer,int myteamno,int dealer); void printPlayerDetails(player p) { cout << "\n Hand 1 = " << p.handCard1; cout << "\n Hand 2 = " << p.handCard2; cout << "\n Team Number = " << p.team; cout << "\n Current Round Bet = " << p.bet; } void read_file(char *name,char *data) { FILE *file; file = fopen(name, "rb"); fread(data,1000,1,file); fclose(file); } char *ptr; char *ptr_startReading; int getInt(char *data) { int temp=0; while(*ptr < '0' || *ptr > '9' ) ptr++; while(*ptr >= '0' && *ptr <= '9') { temp = temp*10 + (*ptr - '0');

Page 4: AI For Texam Hold'em poker

ptr++; } return temp; } int decode_input_file(char *data) { ptr = data; int t1,t2,t3; t1 = getInt(data); t2 = getInt(data); t3 = getInt(data); players[t3].team = t3; players[t3].handCard1 = t1; //cout << "\nHAND CARD 1 = " << t1; players[t3].handCard2 = t2; //cout << "\nHAND CARD 2 = " << t2; players[t3].bet = getInt(data); //cout << "\nTEAM NUMBER = " << t3; return t3; } int decode_dealsMoney(char *data) { ptr = data; player *p; int temp,deal; p = players; //deal = getInt(data); string read_str; ifstream fin; fin.open(DEALS_MONEY,ios::in); fin >> read_str; fin >> read_str; deal = atoi(read_str.c_str()); for(temp=1;temp<=6;temp++) { //p->team = getInt(data); //p->money_remaining = getInt(data); //p++; fin >> read_str; fin >> read_str; players[temp].money_remaining= atoi(read_str.c_str()); if(players[temp].money_remaining == 0) { playersEliminated[temp]=true; num_players_eliminated++; } }

Page 5: AI For Texam Hold'em poker

fin.close(); return deal; } void getString(char *str,char *data) { while(!(*ptr == '-' || *ptr == '_' || (*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z'))) ptr++; while((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z') || *ptr=='_' || *ptr == '-') { *str = *ptr; str++; ptr++; } *str='\0'; } int decode_dealFile(char *data,int *stage,int *main_pot) { ptr = data; int dealer = getInt(data); char str[100]; getString(str,data); //cout << "\n First Str = "; puts(str); getString(str,data); //cout << "\n Second Str = "; puts(str); if(!strcmp("Show_Down",str)) *stage = SHOW_DOWN; else if(!strcmp("Pre_Flop",str)) *stage = PRE_FLOP; else if(!strcmp("Flop",str)) *stage = FLOP; else if(!strcmp("Fourth_Street",str)) *stage = FOURTH_STREET; else if(!strcmp("Fifth_Street",str)) *stage = FIFTH_STREET; *main_pot = getInt(data); ptr_startReading = ptr; return dealer; } void read_showDown_handCards(char *data) { ofstream fout; string file_name,temp_file; int card;

Page 6: AI For Texam Hold'em poker

for(int xx=1;xx<=6;xx++) { file_name = "team"; string bet_name = "_bet.txt"; file_name = file_name + bet_name; temp_file = (char)(xx + 48); file_name = file_name+temp_file; fout.open(file_name.c_str(),ios::app); //fout.open("test.txt",ios::app); card = getInt(data);/* Temp storage for value we dont need */ fout << getInt(data) << "\n" << getInt(data) << "\n"; fout.close(); } } /* ptr continues from the previous point.Its a global variable */ void get_current_state_players(char *data,int stage,int self_teamNumber,int dealer) { cout << "\n Started Reading Rest of Deal File"; ptr = ptr_startReading; string stage_name; int count_new_cards; int river[3]; //int round_count; switch(stage) { case 1:stage_name = "Pre_Flop";count_new_cards = 0;break; case 2:stage_name = "Flop";count_new_cards = 3;break; case 3:stage_name = "Fourth_Street";count_new_cards = 1;break; case 4:stage_name = "Fifth_Street";count_new_cards = 1;break; case 5:stage_name = "Show_Down";count_new_cards = 0;break; } ofstream fout_river; string file_name; string temp_file; string move; int bet; int t_round = 1; int cur_stage = 1; ifstream fin; fin.open("deal.txt",ios::in); string t_str; while(t_str != "Pre_Flop") fin >> t_str; bool end_round=false; bool end_stage=false;

Page 7: AI For Texam Hold'em poker

fout_river.open(RIVER_CARDS,ios::out); fin >> t_str; int prevv=0; while(!fin.eof()) { int xx; xx=atoi(t_str.c_str()); if(cur_stage == 5) { //cout << "\nxx = " << xx; cout << "\n Reading Hand Cards"; fin >> t_str; players_hand_card_1[xx]=atoi(t_str.c_str()); fin >> t_str; players_hand_card_2[xx]=atoi(t_str.c_str()); if(xx==6) { /*cout << "\n"; for (int zz = 1;zz<=6;zz++) { cout <<zz<<" - " << players_hand_card_1[zz]; cout <<" " << players_hand_card_2[zz]; cout << "\n"; } getch();*/ fout_river.close(); return; } fin >> t_str; } else { if(xx == dealer) end_round = true; fin >> move; if(move == "End" || t_str == "End") { cout << "\nEnd Reached.Stage Should Change"; round_number[cur_stage] = t_round; cur_stage++; ofstream fout; fout.open("Round_Counts.txt",ios::app); fout << t_round << "\n"; fout.close(); t_round=1; switch(cur_stage) {

Page 8: AI For Texam Hold'em poker

case 1:stage_name = "Pre_Flop";count_new_cards = 0;break; case 2:stage_name = "Flop";count_new_cards = 3;break; case 3:stage_name = "Fourth_Street";count_new_cards = 1;break; case 4:stage_name = "Fifth_Street";count_new_cards = 1;break; case 5:stage_name = "Show_Down";count_new_cards = 0;break; } while(move != stage_name && move != "Show_Down") fin >> move; if(move == "Show_Down") { cur_stage = 5; count_new_cards=0; is_show_down = true; } stagePlayed[cur_stage] = true; for(int xx=0;xx<count_new_cards;xx++) { fin >> t_str; river[xx] = atoi(t_str.c_str()); fout_river << river [xx] << "\n"; } fin >> t_str; } else { if(move != "-") { fin >> t_str; bet = atoi(t_str.c_str()); } else bet = 0; cout << "\n Move = " << move << " " << bet; players_data_move[xx][cur_stage][t_round] = (int)move[0]; players_data_bet[xx][cur_stage][t_round] = bet; pot_money[xx][cur_stage][t_round] = prevv; cout << "\n Pot Money = " << prevv; //getch(); if(players_data_move[xx][cur_stage][t_round] != FOLD) prevv = bet; if(end_round == true) t_round++; /* } */ end_round = false; end_stage = false; fin >> t_str; } } }

Page 9: AI For Texam Hold'em poker

cout << "\nStage = " <<stage << " t_round = " << t_round; round_number[stage]=t_round; fout_river.close(); //getch(); return ; } void set_play_mode(int self_team_number) { int money_total=0; for(int xx=1;xx<=6;xx++) money_total=money_total+players[xx].money_remaining; int mean= money_total/(6-num_players_eliminated); cout << "\nMean Money = " << mean << "\n2/3rd of Mean = " <<(2*mean)/3 << "\nRank = " << get_rank(self_team_number); if(players[self_team_number].money_remaining >=500 && ((get_rank(self_team_number) <=(2*(6-num_players_eliminated))/3) || num_players_eliminated >=3)) play_mode = OFFENSIVE; else play_mode = DEFENSIVE; cout << "\nPlay Mode = "<<play_mode; //getch(); /* SET BRUTEFORE play_mode - Until Rank is Top 3 or until Money doesnt go below 500 or 800 deals */ // Remove the Comments below to Activate Ultra Defensive modee // /* play_mode = ULTRA_DEFENSIVE; if(get_rank(self_team_number) <=3 || players[self_team_number].money_remaining <= 500 || deal_number >= 800) play_mode = OFFENSIVE; */ } /* stage = The current Stage all_cards = All cards including hand card and river Card. (First two Cards as Hand Cards n_total_cards = Total Number of Cards = River Cards + 2 Hand Cards self_team_number = The team number who has to return a Reply dealer = the dealer for the current deal */ int defensive_play(int stage,int *all_cards,int n_total_cards,int self_team_number,int dealer) { /* An array that stores all the Cards , including Hand Cards (as 1st 2 cards) & and River Cards to call "overallp" function */

Page 10: AI For Texam Hold'em poker

/*static int all_cards[7]; */ /*all_cards[0] = players_hand_card_1[self_team_number]; */ /*all_cards[1] = players_hand_card_2[self_team_number]; */ /*for(int xx = 2;xx < 2+n_river_cards;xx++) */ /* all_cards[xx] = river_cards[xx-2]; */ int p_hand = overallp(all_cards, n_total_cards, stage); if(!(p_hand == VERY_GOOD || p_hand == GOOD)) /* The Case of Fold */ return -10;/*fold*/ return offensive(stage,all_cards,n_total_cards,self_team_number,dealer); } /* Returns true or 1 if the player bluffs atleast twice as more else false oe 0 */ bool is_bluffsMore(short int team_number) { int rise_counter=1; int bluff_counter=1; for(int xx = 1;xx<=6;xx++) for(int yy = 1;yy<5;yy++) for(int zz=1;zz<=round_number[yy];zz++) { if(players_data_move[xx][yy][zz] == RISE) rise_counter++; if(players_data_move[xx][yy][zz] == BLUFF) bluff_counter++; } if(rise_counter*2 <= bluff_counter) return true; else return false; } int get_minimum_raise(short int stage,short int team_number,short int dealer) { if(stage == 5) return 0; int cur_round=round_number[stage]; int cur_player=team_number-1; int cur_stage = stage; cout << "\nStart Round = " << cur_round; while(cur_round >= 0) { if(cur_player == 0)cur_player=6; if(cur_player == dealer)cur_round--; if(cur_round == 0) { cur_stage--; cur_round=round_number[cur_stage]; }

Page 11: AI For Texam Hold'em poker

cout << "\nCur Player = " << cur_player << " cur_stage = " << cur_stage << " cur_round = " <<cur_round; cout << "\nPot Money = " << pot_money[cur_player][cur_stage][cur_round]; cout << "\nRaise = " << (players_data_bet[cur_player][cur_stage][cur_round]-pot_money[cur_player][cur_stage][cur_round]); cout << "\nRise Amounts = " << players_data_bet[cur_player][cur_stage][cur_round]; if(players_data_move[cur_player][cur_stage][cur_round] == RISE) { cout << "\nFinally Rise = " << players_data_bet[cur_player][cur_stage][cur_round]-pot_money[cur_player][cur_stage][cur_round]; if(players_data_bet[cur_player][cur_stage][cur_round]-pot_money[cur_player][cur_stage][cur_round] == 0 || players_data_bet[cur_player][cur_stage][cur_round]-pot_money[cur_player][cur_stage][cur_round] == 5) return 10; //getch(); return (players_data_bet[cur_player][cur_stage][cur_round]-pot_money[cur_player][cur_stage][cur_round]); } cur_player--; } return 0; } /* Returns your rank on calling */ short int get_rank(int self_team_number) { short int rank=6; for(int xx = 1;xx<=6;xx++) { if(xx == self_team_number) continue; if(players[xx].money_remaining <=players[self_team_number].money_remaining) rank--; } return rank; } /* Sets the value of all elements in Array p_ratio as 0 */ void set_p_ratio() { for (int xx=0;xx<6; xx++) p_ratio[xx]=0; } /* Sets the value of all elements in Array reaction_ratio as 0 */ void set_reaction_ratio()

Page 12: AI For Texam Hold'em poker

{ for (int xx=0;xx<=4; xx++) reaction_ratio[xx]=0; } /* Sets the value of all elements in Array players_folded as false */ void set_player_folded() { for(int xx=1;xx<=6;xx++) players_folded[xx] = false; } /* Returns 1 or "true" , if our first turn, else returns 0 or "false" */ bool is_FirstTurn(short int dealer, short int self_team_number) { int s_pos = dealer+1; while(s_pos != self_team_number && !(s_pos == 7 && self_team_number == 1)) { if(s_pos == 7) s_pos = 1; if(players_folded[s_pos] == false) return false; s_pos++; } return true; } /* Function Returns the p_value */ /* VERY_GOOD 5 */ /* GOOD 4 */ /* AVG 3 */ /* BAD 2 */ /* VERY_BAD 1 */ short int checkPattern_p(int action,int reaction, int stage ,int team,int detail = LOW) { /* Action :: What the other opponent Do */ /* Reaction :: What this team does for the given action */ /* Stage :: Stage for which data is required */ /* Team :: Team number for which the data is required */ /* Detail :: How much deatil to check the Patter */ /* HIGH - Check exact match */ /* LOW - Ignore Stage */ /* Set p_ratio array as 0 */ set_p_ratio(); /* Declare the Array to store CheckPattern */ short int max;

Page 13: AI For Texam Hold'em poker

/* Read the CheckPattern File and save it in the Array */ FILE *file; file = fopen(CHECKPATTERN_FILE,"rb"); if(file != NULL) { fread(checkPattern,sizeof(int)*6*5*4*3*7,1,file); fclose(file); } if (detail == LOW) { max = 0; for(int xx = 1;xx<=5;xx++) /* For What P Value */ { p_ratio[xx] = checkPattern[xx][action][reaction][stage][team]; if(checkPattern[max][action][reaction][stage][team] <= checkPattern[xx][action][reaction][stage][team]) max = xx; } return max; } else { max = 0; for(int xx=1; xx<=5; xx++) /* For What P-Value */ for(int zz = 0; zz < 4;zz++) /* For Any Stage */ { p_ratio[xx] = checkPattern[xx][action][reaction][zz][team]; if(checkPattern[max][action][reaction][zz][team] <= checkPattern[xx][action][reaction][zz][team]) max = xx; } return max; } } /* Function Returns an Action */ /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ /* It is based on priority */ short int checkPattern_action(int p_value,int action, int stage ,int team,int detail = LOW) { /* p_value :: P_Value for which the reaction is reqd */ /* Action :: What the other opponent Do */ /* Reaction :: What this team does for the given action */ /* Stage :: Stage for which data is required */ /* Team :: Team number for which the data is required */

Page 14: AI For Texam Hold'em poker

/* Detail :: How much deatil to check the Patter */ /* HIGH - Check exact match */ /* LOW - Ignore Stage */ /* Declare the Array to store CheckPattern */ /* Set p_ratio array as 0 */ set_reaction_ratio(); short int max; /* Read the CheckPattern File and save it in the Array */ FILE *file; file = fopen(CHECKPATTERN_FILE,"rb"); if(file!=NULL) { fread(checkPattern,sizeof(int)*6*5*4*3*7,1,file); fclose(file); } if (detail == LOW) { max = 0; for(int xx = 0;xx<4;xx++) /* For What Reaction */ { reaction_ratio[xx] = checkPattern[p_value][action][xx][stage][team]; if(checkPattern[p_value][action][max][stage][team] <= checkPattern[p_value][action][xx][stage][team]) max = xx; } return max; } else { max = 0; for(int xx=1; xx<=5; xx++) /* For What Reaction */ for(int zz = 1; zz <= 5;zz++) /* For Any Value of P */ { reaction_ratio[xx] = checkPattern[p_value][action][xx][zz][team]; if(checkPattern[zz][action][max][stage][team] <= checkPattern[p_value][action][xx][zz][team]) max = xx; } return max; } } int catagorize_move(int p_move) {

Page 15: AI For Texam Hold'em poker

/* KEY INDEX */ /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ if(p_move == CALL) return 1; if(p_move == FOLD) return 2; if(p_move == BLUFF) return 4; if(p_move == RISE) return 3; return 0; /* For error */ } void analyze_data(int self_team_number,int dealer) { /*cout << "\n"; for (int xx = 1;xx<=6;xx++) { cout <<xx<<" - " << players_hand_card_1[xx]; cout <<" " << players_hand_card_2[xx]; cout << "\n"; }*/ r_count=0; string file_name = "team" ; string temp_file; string t_str; ifstream fin; ifstream file1; ifstream file2; /* Order for Check Pattern :: PValue - Action - Reaction - Stage -Team Number */ //int cumulative_round_count=0; int stage =1,max_stage; int move; int counter; number_of_folds=0; /* Get all the Data from the Help Files into the two arrays declared above */ /* Read the CheckPattern File */ cout << "\nCheck Pattern Read Start"; FILE *file; file = fopen(CHECKPATTERN_FILE,"rb"); if(file != NULL)

Page 16: AI For Texam Hold'em poker

{ fread(checkPattern,sizeof(int)*6*5*4*3*7,1,file); fclose(file); } //cout << "\nCHECKPATTERN = " << checkPattern[2][2][2][2][2]; cout << "\n Check Pattern read complete"; /* Read all the river Cards from the Help Files */ fin.open(RIVER_CARDS,ios::in); fin >> t_str; cout << "\nRiver Card Read Start"; while(!fin.eof()) { river_cards[r_count++]=atoi(t_str.c_str()); fin >>t_str; } fin.close(); cout << "\nRiver Card read complete\nRIVER CARDS :: "; for(int mm=0;mm<r_count;mm++) cout << river_cards[mm] << " "; /* This File contains the number of rounds played at each stage in the current Deal */ fin.open("Round_Counts.txt",ios::in); /* Set player_folded array to False */ set_player_folded(); number_of_folds=0; /* Read all the Round Counts to an Array */ fin >> t_str; //round_number[stage++]=atoi(t_str.c_str()); while (!fin.eof()) { round_number[stage++]=atoi(t_str.c_str()); fin >>t_str; } fin.close(); cout << "\n Read Round Numbers complete, stage = " << stage << "\n ROUND NUMBERS :: "; for(int mm=1;mm<stage;mm++) cout << round_number[mm] << " "; max_stage = stage; //cout << "\nSTAGE = " << stage; /* Read all the opponent moves and Bets from Help Files */ /*for(int xx = 1,yy=0;xx<=6;xx++) { stage=0; //cumulative_round_count = 0; counter = round_number[stage]; temp_file = (char)(xx + 48); file1.open((string)(file_name + "_move.txt"),ios::in); file2.open((string)(file_name + "_bet.txt"),ios::in);

Page 17: AI For Texam Hold'em poker

yy = 0; while(!file1.eof()) { file1 >> players_data_move[xx][round_number[stage]-counter][yy]; if(players_data_move[xx][round_number[stage]-counter][yy] == FOLD) { players_folded[xx]= true; number_of_folds++; } file2 >> players_data_bet[xx][round_number[stage]-counter][yy]; counter--; if(counter == 0) { yy=0; //cumulative_round_count = cumulative_round_count + round_number[stage]; stage++; counter = round_number[stage]; } yy++; } file1.close(); file2 >> players_hand_card_1[xx]; file2 >> players_hand_card_2[xx]; file2.close(); stage++; } */ for(int xx = 1;xx<=6;xx++) {for(int yy = 1;yy<5;yy++) {for(int zz=1;zz<=round_number[yy];zz++) {if(players_data_move[xx][yy][zz] == FOLD) { players_folded[xx]=true; number_of_folds++; }}}} cout << "\nAnalyze Fold Data Complete"; for(int xx=1;xx<=6;xx++) cout << xx<<":"<<players_folded[xx] << " "; players_hand_card_1[self_team_number] = players[self_team_number].handCard1; players_hand_card_2[self_team_number] = players[self_team_number].handCard2; //getch(); //cout << "\nWILL IT GO THERE ? :O "; if(is_show_down == false)/* If the Stage is not ShowDown , we dont have to analyze the Data. Just store them in Arrays */ return ; stage = 5; //cout << "\nCOMES HERE!!!! :D "; /* Calculate the P-Values of each player at every round */ // int preflop_river_cards[5];//Can use this array for sending to the p_value Function // int all_cards[7];

Page 18: AI For Texam Hold'em poker

for(int yy = 2;yy < 2+r_count;yy++) all_cards[yy] = river_cards[yy-2]; cout << "\nCalculating All P Values Started"; for(int xx = 1; xx<=6 ; xx++) { all_cards[0] = players_hand_card_1[xx]; all_cards[1] = players_hand_card_2[xx]; /* Preflop */ //if(max_stage >= 1) player_p_values[xx][1] = overallp(all_cards,2,1); //cout << "\n Calculated First pValue"; /* Flop */ //if(max_stage >= 2) if(stagePlayed[2] == true) player_p_values[xx][2] = overallp(all_cards,5,2); //cout << "\n Calculated Second pValue"; /* Fourth Street */ //if(max_stage >= 3) if(stagePlayed[3] == true) player_p_values[xx][3] = overallp(all_cards,6,3); //cout << "\n Calculated Third pValue"; /* Fifth Street */ //if(max_stage >= 4) if(stagePlayed[4] == true) player_p_values[xx][4] = overallp(all_cards,7,4); //cout << "\n Calculated Fourth pValue"; } /* All data has now been stored in the required arrays */ cout << "\nAll PValues Calculated"; for(int xx=1;xx<=6;xx++) { cout << "\nPlayer - " << xx << "\n"; for(int yy=1;yy<=4;yy++) cout << player_p_values[xx][yy] << " "; } //getch(); /* Start analyzing from the Dealer */ int current_round = 0; int number_of_rise = 0; int number_of_call = 0; int number_of_fold = 0; /* Loop to Analyze the Data Stored in all Array */ int loop_counter = 0; /* Counter for position in current Round */

Page 19: AI For Texam Hold'em poker

int loop_stage = 0; /* Counter for Current Stage */ int loop_start = dealer+3; /* Counter for Player Number */ int loop_pot_money = 10; /* Stores the Money in Pot for the Current Stage */ int player_bet; int player_move; bool isBluff; /* 1-Bluff 0-Not Bluff */ int player_action = (int)'A'; /* What other player did, based on priority */ while(loop_stage != 5) { if(loop_start >= 7) loop_start = loop_start-6; if(loop_start == dealer+1) loop_counter++; if(loop_counter == round_number[loop_stage]) /* Stage Over */ { loop_stage++; loop_counter=0; loop_pot_money = 0; player_action = (int)'A'; } player_bet = players_data_bet[loop_start][loop_stage][loop_counter] - loop_pot_money; pot_money[loop_start][loop_stage][loop_counter] = loop_pot_money; if(player_bet <=0) player_bet = 0; loop_pot_money = loop_pot_money + player_bet; player_move = players_data_move[loop_start][loop_stage][loop_counter]; if(player_move > player_action && player_action != FOLDED) player_action = player_move; if(player_bet != 0 && (player_p_values[loop_start][loop_stage] == BAD || player_p_values[loop_start][loop_stage] == VERY_BAD || player_p_values[loop_start][loop_stage] == AVG)) { isBluff = true; player_move = BLUFF; } else isBluff = false; checkPattern[player_p_values[loop_start][loop_stage]][catagorize_move(player_action)][catagorize_move(player_move)][loop_stage][loop_start]++; if(player_move == BLUFF) checkPattern[player_p_values[loop_start][loop_stage]][catagorize_move(player_action)][RISE][loop_stage][loop_start]++; loop_start++; } /* Write The CheckPattern back to File */ cout << "\nWRITE CHECKPATTERN FILE STARTED!"; file = fopen(CHECKPATTERN_FILE,"wb");

Page 20: AI For Texam Hold'em poker

fwrite(checkPattern,sizeof(int)*6*5*4*3*7,1,file); fclose(file); cout << "\nWRITE COMPLETE"; remove("bluff.txt"); //getch(); } /* this fnctn shud be called when need to play offensive and it will return the amount of bet */ int offensive(int stage,int *cards,int total,int teamno,int dealer) { /* initialize random seed: */ /* initialization for rand function */ srand ( time(NULL) ); int phand,rise = get_minimum_raise(stage,teamno,dealer);/* rise = minimum available raise */ phand = overallp(cards,total,stage); cout << "\nP Hand = " << phand; //getch(); int temp = is_FirstTurn(dealer,teamno);/* checking for first turn */ //cout << "\n First Turn True/False = " << temp; if(temp == 0)/* opponent raised or called */ { /* is p vgud */ if(phand == 5) { int check = anyoppraisehigh(stage,round_number[stage],dealer + 1,teamno,dealer);/* check if opp raised high */ if(check == 1) { /* check stage */ if(stage == 1) { int choice = rand() % 4 + 1; if((choice == 1)||(choice == 2)||(choice == 3)) return 0;/* call */ else { /* high raise fnctn */ int bet = bluff(rise,4);/* 4 for high raise fnctn */ return bet; } } else if(stage == 2) { int choice = rand() % 4 + 1; if((choice == 1)||(choice ==2)) {

Page 21: AI For Texam Hold'em poker

/* high raise fnctn */ int bet = bluff(rise,4); return bet; } else return 0; } else if(stage == 3) { int choice = rand() % 4 + 1; if(choice ==1) return 0; else { int bet = bluff(rise,4);/*high raise fnctn*/ return bet; } } else/*last stage */ { /* high raise fntn */ int bet = bluff(rise,4); return bet; } } else/* low raise or call */ { /* stage check */ if(stage == 4) { /* high raise fnctn */ int bet = bluff(rise,4); return bet; } else if(round_number[stage] == 1)/*new card opened */ { int choice = rand() % 2 + 1; if(choice == 1) return 0;/* call */ else { int bet = bluff(rise,4); return bet; } /* high raise fntn */ } else/*no new card */ { FILE *pt; int bluffcounter = 4;

Page 22: AI For Texam Hold'em poker

pt = fopen("bluff.txt","rb"); if(pt != NULL)/* prev raise */ { fscanf(pt,"%d",&bluffcounter); fclose(pt); if(bluffcounter <4) { int bet = bluff(rise,4); return bet; } } else { int choice = rand() % 2 +1; if(choice == 1) return 0;/* call */ else { /* high raise fntn */ int bet = bluff(rise,4); return bet; } } } } } /* p is gud */ if(phand == 4) { //cout << "\n Case of Pgood"; int value=1; int check = anyoppraisehigh(stage,round_number[stage],dealer + 1,teamno,dealer);/* check if opp raised high */ //cout << "\nDid Any opponent Raise High? " << check; if(check == 1) { int temp = teamno-1; int cur_round = round_number[stage]; do { if(temp == 0) temp = 6; if(temp == dealer) cur_round--; if(cur_round <=0) {value = 0;break;} if(players_data_move[temp][stage][cur_round] == RISE) {

Page 23: AI For Texam Hold'em poker

int rise = players_data_bet[temp][stage][cur_round] - pot_money[temp][stage][cur_round]; if(rise > 50) { int faltu; faltu = is_bluffsMore(temp); if(faltu==0) value = faltu; } } temp--; }while(temp != teamno); if(value == 0) { int pla = 6 - number_of_folds;/* gets no of players playing on the table */ if(pla <= 3)/* check pattern fntn */ { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); } else { int count = 0; int temp=teamno-1; int still_in_play[3]={0}; int zz=0; int cur_round = round_number[stage]; do { int value; if(temp==0) temp = 6; if(temp == dealer)

Page 24: AI For Texam Hold'em poker

cur_round--; if(cur_round <=0) break; if((players_data_move[temp][stage][cur_round] == CALL)&&(players_data_bet[temp][stage][cur_round] - pot_money[temp][stage][cur_round] > 50)) value = is_bluffsMore(temp); if(value == 0) { count++; still_in_play[zz++]=temp; } temp--; }while(temp!=teamno); still_in_play[zz++]=teamno; if(zz==3) {still_in_play[zz++]=0;} pla = count; /* no of players who raises high and doesn;t bluff */ if(pla <= 2) return getMove(stage,phand,still_in_play,teamno,dealer); else /* check pattern */ { int choice = rand() % 2 + 1; if(choice == 1) return 0; else return -10;/* fold */ } } } else/* means opponent bluffs a lot */ { int players = 6 - number_of_folds;/* no of players playing at the table */ if(players <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2)

Page 25: AI For Texam Hold'em poker

{still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else { return 0;/*call*/ } } } else /* low call or call*/ { int players = 6 - number_of_folds;/* no of players laying at the table */ //cout << "\nReached the case of chek = 0, players = "<<players; if(players <= 3) /*-----------editing left-----------*/ { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else { cout << "\nPlayers Greater than 3"; int lowcheck=0,count=0; int temp = teamno-1; if(temp == 0 ) temp = 6; int still_in_play[7]={0}; int zz =0; int cur_round = round_number[stage]; cout << "\ncur_round = " << cur_round; do { if(temp == 0)

Page 26: AI For Texam Hold'em poker

temp = 6; if(temp == dealer) cur_round--; if(cur_round == 0) break; cout << "\ntemp = " << temp << " cur_round = " << cur_round << " stage = " << stage; if(players_data_move[temp][stage][cur_round] == RISE) { count++; still_in_play[zz++] = temp; lowcheck = 1; } temp--; }while(temp != teamno); //cout << "\nNumber of Rise = " << count; //cout << "\nlow check = " << lowcheck; //getch(); still_in_play[zz++] = teamno; if(zz==3) {still_in_play[zz]=0;zz++;} if(lowcheck == 1) { //cout <<"\nEnter Low Check"; int pla = count;/*no of players who raised low */ if(pla <= 2) { //cout << "\n Here ????"; return getMove(stage,phand,still_in_play,teamno,dealer); } else { int choice = rand() % 2 + 1; if(choice == 1) return 0;/* call */ else { //cout << "\nLow Raise ? "; int bet = lowraise(rise);/* low raise call */ return bet; } } } else/*opponent called */ { int choice = rand()%2 + 1; if(choice == 1) return 0;/* call */

Page 27: AI For Texam Hold'em poker

else/* low raise */ { int bet = lowraise(rise); return bet; } } } } } if(phand == 3) { int bet,value; int check = anyoppraisehigh(stage,round_number[stage],dealer + 1,teamno,dealer);/* check if opp raised high */ if(check == 1)/* raises high */ { if((stage == 1)||(stage == 2)) { int temp=teamno-1;/*checking bluffs a lot*/ int cur_round = round_number[stage]; do { if(temp == 0) temp = 6; if(temp == dealer) cur_round--; if(cur_round <= 0) break; if(players_data_move[temp][stage][cur_round] == RISE) { int rise = players_data_bet[temp][stage][cur_round] - pot_money[temp][stage][cur_round]; if(rise <= 50) { int faltu; faltu = is_bluffsMore(temp); if(faltu==0) value = faltu; } } temp--; }while(temp != teamno); if(value == 1) { int choice = rand()%3 +1; if((choice == 1)||(choice == 2)) return 0;/* call */ else return -10;/*fold*/

Page 28: AI For Texam Hold'em poker

} else { int choice = rand()%3 +1; if((choice == 1)||(choice == 2)) return -10;/* fold */ else return 0;/*call*/ } } else/*stage 3rd or 4 */ { if(round_number[stage]==1)/*new cards opened */ { int choice = rand()%2 +1; if(choice == 1)/* bluff */ { int bet = bluff(rise,2); cout <<"\n1 Bet = " << bet; //getch(); return bet; } else return -10;/*fold*/ } else/* no new card opened */ { if(players_data_move[teamno][stage][round_number[stage]-1]==RISE) { int bet = bluff(rise,2);/* bluff if allowed */ if(bet != 0) {cout <<"\n1 Bet = " << bet; //getch(); return bet;} } else return -10; /* fold */ } } } else /* low raise or call */ { if((stage==1)||(stage==2)) { if(round_number[stage]==1)/*new card opened */ { int choice = rand()%2 +1; if(choice == 1)

Page 29: AI For Texam Hold'em poker

{ int bet = bluff(rise,2); cout <<"\n1 Bet = " << bet; //getch(); return bet; } else return 0;/*call*/ } else/* no new card opened */ { if(players_data_move[teamno][stage][round_number[stage]-1]==RISE) { int bet = bluff(rise,2); cout <<"\n1 Bet = " << bet; //getch(); return bet; } else { return 0;/*call*/ } } } else /* stage 3 or 4 */ { if(round_number[stage]==1)/* new card opened */ { int choice = rand()%3 +1; if(choice == 1) return 0;/* call */ else if(choice == 2) return -10;/*fold*/ else { int bet = bluff(rise,2); cout <<"\n1 Bet = " << bet; //getch(); return bet; } } else/* no new card opened */ { if(players_data_move[teamno][stage][round_number[stage]-1]==RISE) { int bet = bluff(rise,2);

Page 30: AI For Texam Hold'em poker

cout <<"\n1 Bet = " << bet; //getch(); return bet; } else { int choice = rand()%2 +1; if(choice == 1) return 0;/* call */ else return -10;/*fold*/ } } } } } if(phand == 2)/* p bad */ { int check = anyoppraisehigh(stage,round_number[stage],dealer + 1,teamno,dealer);/* check if opp raised high */ if(check == 1)/*opponent raised high*/ { if(stage == 1) { int choice = rand()%4 +1; if((choice == 1)||(choice == 2)||(choice == 3)) return 0;/* call */ else return -10;/*fold*/ } else if(stage == 2) { int choice = rand()%3 +1; if(choice == 1) return 0;/* call */ else return -10;/*fold*/ } else if(stage == 3) { int choice = rand()%4 +1; if((choice == 1)||(choice == 2)||(choice == 3)) return -10;/* fold */ else return 0;/*call*/ } else/*stage 4*/ return -10;/*fold*/ } else/*low raise or call */

Page 31: AI For Texam Hold'em poker

{ if((stage==1)||(stage==2)) { if(round_number[stage]==1)/*new card opened */ { int choice = rand()%2 +1; if(choice == 1) { int bet = bluff(rise,2); cout <<"\n1 Bet = " << bet; //getch(); return bet; } else return 0;/*call*/ } else/* no new card opened */ { if(players_data_move[teamno][stage][round_number[stage]-1]==RISE) { int bet = bluff(rise,2); cout <<"\n1 Bet = " << bet; //getch(); return bet; } else { return 0;/*call*/ } } } else /* stage 3 or 4 */ { if(round_number[stage]==1)/* new card opened */ { int choice = rand()%4 +1; if(choice == 1) return 0;/* call */ else if(choice == 2 || choice == 3) return -10;/*fold*/ else { int bet = bluff(rise,2); cout <<"\n1 Bet = " << bet; //getch(); return bet;

Page 32: AI For Texam Hold'em poker

} } else/* no new card opened */ { if(players_data_move[teamno][stage][round_number[stage]-1]==RISE) { int bet = bluff(rise,2); return bet; } else { int choice = rand()%2 +1; if(choice == 1) return 0;/* call */ else return -10;/*fold*/ } } } } } else/* phand == 1 *//* p is vbad */ { int check = anyoppraisehigh(stage,round_number[stage],dealer + 1,teamno,dealer);/* check if opp raised high */ if(check == 1) { cout << "\nYes Some player Rises"; //getch(); if(stage == 1) { int choice = rand() % 4 + 1; if ((choice==1)||(choice==2)||(choice==3)) return 0;/*call*/ else return -10;/*fold */ } else if(stage == 2) { int choice = rand()%4 +1; cout<<"\nchoice "<<choice<<"\n"; //getch(); if(choice == 2) return 0;/* call */ else return -10;/* fold */

Page 33: AI For Texam Hold'em poker

} else if(stage == 3) { int choice = rand() % 5 +1; if((choice == 1)||(choice == 2)||(choice == 3)||(choice == 4)) return -10;/* fold */ else return 0;/* call */ } else/* stage == 4 */ return -10;/* fold */ } else/*opponent raised low or called */ { cout << "\n Opponent Raised Low"; if(round_number[stage]==1)/* new card opened */ { if(stage == 4) { int choice = rand()%2 +1; if(choice == 1) { /* bluff fnctn */ int bet = bluff(rise,2); return bet; } else return -10;/* fold */ } else { int choice = rand() % 2 + 1; if (choice == 1) { /*bluff fnctn */ int bet = bluff(rise,2); return bet; } else return 0;/* call */ } } else/* no new cards opened */ { if(players_data_move[teamno][stage][round_number[stage]-1] == RISE)/*previous bluff*/ { int bet = bluff(rise,2); return bet; } else

Page 34: AI For Texam Hold'em poker

{ int choice = rand() % 2 + 1; if(choice == 1) return 0;/*call*/ else return -10;/*fold*/ } } } } } /*--------------------------- checked till here ----------------------*/ else/* first turn is ours */ { if(phand == 5)/* p hand is very gud */ { if(stage == 1) { int choice = rand() % 4 +1; if((choice == 1)||(choice == 2)) return 0;/* call */ else { /* high raise */ int bet = bluff(rise,4); return bet; } } else if((stage == 2)||(stage == 3)) { int choice = rand() % 3 + 1; if(choice == 1) { /* raise fnctn */ int bet = bluff(rise,4); return bet; } else return 0;/* call */ } else/* stage 4 */ { int choice = rand() % 2 + 1; if(choice == 1) return 0;/* call */ else { /* bluff fnctn */

Page 35: AI For Texam Hold'em poker

int bet = bluff(rise,2); return bet; } } } else if(phand == 4) { if(stage == 1) { int players = 6 - number_of_folds;/* no of players playing at the table atm */ if(players <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else/* players > 3 */ { int choice = rand() % 4 + 1; if((choice == 1)||(choice == 2)) return 0;/* call */ else if(choice == 3) { /* bluff fnctn */ int bet = bluff(rise,4); return bet; } else { /* raise fnctn */ int bet = lowraise(rise); return bet; } } } else if((stage == 3)||(stage == 2)) {

Page 36: AI For Texam Hold'em poker

int players = 6 - number_of_folds;/* no of players playing the current bet */ if(players <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else/* players > 3 */ { int choice = rand() % 3 + 1; if(choice == 1) { /* bluff fnctn */ int bet = bluff(rise,2); return bet; } else return 0;/* call */ } } else/* stage == 4 */ { int players = 6 - number_of_folds;/* no of players playing the deal */ if(players <=3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1));

Page 37: AI For Texam Hold'em poker

if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /*check pattern */ } else/* players > 3 */ { int choice = rand() % 3 + 1; if (choice == 1) { /* bluff fnctn */ int bet = bluff(rise,2); return bet; } else return 0;/* call */ } } } else if(phand = 3)/* phand == avg */ { if(stage == 1) { int players = 6 - number_of_folds;/* no of players playing the current deal */ if(players <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else/* players > 3 */ { int choice = rand() %3 + 1; if(choice == 1) { /* bluff fnctn */

Page 38: AI For Texam Hold'em poker

int bet = bluff(rise,2); return bet; } else return 0;/* call */ } } else if((stage == 2)||(stage == 3)) { int players = 6 - number_of_folds;/* no of players playing the current deal */ if(players <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else/* players > 3 */ { int choice = rand() %3 + 1; if(choice == 1) { /*bluff fnctn */ int bet = bluff(rise,2); return bet; } else return 0; } } else/* stage = 4 */ { if(players_data_move[teamno][stage][round_number[stage]-1] == RISE) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0};

Page 39: AI For Texam Hold'em poker

do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ /* can call or bluff */ /* never fold here */ } else/* previous wasn;t a bluff*/ { int players = 6 - number_of_folds;/* no of players playing the current deal */ if(players <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else/* players > 3 */ { int choice = rand() %3 + 1; if(choice == 1) { /*bluff fnctn */ int bet = bluff(rise,2); return bet; } else

Page 40: AI For Texam Hold'em poker

return 0;/*call*/ } } } } if(phand == 2)/* phand = bad */ { int players = 6 - number_of_folds;/* no of players playing the current deal */ if(stage == 1) { if(players <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else { int choice = rand() % 2 + 1; if(choice == 1) return 0;/* call */ else { /* bluff fnctn */ int bet = bluff(rise,2); return bet; } } } else if((stage == 2)||(stage == 3)) { if (players <=3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do {

Page 41: AI For Texam Hold'em poker

if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /*check pattern */ } else { int choice = rand() % 3 + 1; if(choice == 1) { /* bluff fnctn */ int bet = bluff(rise,2); return bet; } else return 0; /* call */ } } else/*stage = 4 */ { if (players <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else { int choice = rand()%4 + 1; if(choice == 1) {

Page 42: AI For Texam Hold'em poker

/* bluff fnctn */ int bet = bluff(rise,2); return bet; } else return 0; } } } else/* phand = 1 and vbad */ { int pla = 6 - number_of_folds;/* no of players playing the current deal */ if(stage == 1) { if(pla <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else { int choice = rand() % 3 + 1; if((choice == 1)||(choice == 2)) return 0;/* call */ else { /* bluff fnctn */ int bet = bluff(rise,2); return bet; } } } else if((stage == 2)||(stage == 3)) { if (pla <=3) { int zz=dealer+1;

Page 43: AI For Texam Hold'em poker

int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /*check pattern */ } else { int choice = rand() % 4 + 1; if(choice == 1) { /* bluff fnctn */ int bet = bluff(rise,2); return bet; } else return 0; /* call */ } } else/*stage = 4 */ { if (pla <= 3) { int zz=dealer+1; int ppos=0; int still_in_play[3]={0}; do { if(zz==7) zz=1; if(players_folded[zz] == false) still_in_play[ppos++]=zz; zz++; }while(zz != dealer && !(zz==7 && dealer==1)); if(ppos==2) {still_in_play[ppos++]=0;} return getMove(stage,phand,still_in_play,teamno,dealer); /* check pattern */ } else

Page 44: AI For Texam Hold'em poker

{ int choice = rand()%5 + 1; if(choice == 1) { /* bluff fnctn */ int bet = bluff(rise,2); return bet; } else return 0; } } } } } int getMove(int stage,int phand,int *team,int teamno,int dealer) { int final; int rise; int *ptr,flag; int xx1res,xx2res; cout << "\nTeam 1 = " << *team << " Team 2 = " << *(team +1) << " Team 3 = " << *(team+2) ; if(*team == 0 || *(team + 1)==0||*(team + 2)==0) { //cout << "\nIs it HERE ? "; //getch(); flag = 0; for(int temp=1;temp <= 3 ;temp++) { ptr = team; if(*ptr == teamno) { continue; flag = 1; } else if(*ptr == 0) { continue; } else { if(flag == 0) { int pop = checkPattern_p(FIRST,players_data_move[*ptr][stage][round_number[stage]],stage,*ptr); if(phand == VERY_BAD||phand == BAD || phand == AVG) { int cal,rez,foiled; checkPattern_action(pop,RISE,stage,*ptr);

Page 45: AI For Texam Hold'em poker

/* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ cal = reaction_ratio[1]; foiled = reaction_ratio[2]; rez = reaction_ratio[3]; if(foiled<=cal || foiled<=rez) return -10;/* fold */ else { rise = get_minimum_raise(stage,teamno,dealer); cout << "\n Minimum Raise 1= " << rise; int bet = bluff(rise,2); cout << "\n Final Return 1= " << bet; //getch(); return bet; } } else/* phand is very gud */ { int calfold,rezfold; checkPattern_action(pop,CALL,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ calfold = reaction_ratio[2]; checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ rezfold = reaction_ratio[2]; if(calfold < rezfold) return 0;/* call */ else { rise = get_minimum_raise(stage,teamno,dealer); cout << "\n Minimum Raise2 = " << rise; int bet = bluff(rise,2); cout << "\n Final Return 2= " << bet; //getch(); return bet;/*raise bluff */ } } } else/* flag == 1 */ {

Page 46: AI For Texam Hold'em poker

if(phand == VERY_BAD || phand == BAD || phand == AVG) { if(stage == 1 || stage == 2) { int rez,cal,foiled; checkPattern_action(0,RISE,stage,temp,HIGH); cal = reaction_ratio[1]; foiled = reaction_ratio[2]; rez = reaction_ratio[3]; if(foiled<=cal || foiled<=rez) return -10;/* fold */ else { rise = get_minimum_raise(stage,teamno,dealer); cout << "\n Minimum Raise 3 = " << rise; int bet = bluff(rise,2); cout << "\n Final Return 3 = " << bet; //getch(); return bet;/* bluff */ } } else/* stage is 3 or 4 */ { int st; if(round_number[stage] == 1) st = stage - 1; else st = stage; int pop=1; int pratiototal[6]={0}; for(int counter = 1;counter <= round_number[st];counter++) { checkPattern_p(players_data_move[*(ptr-1)][st][counter],players_data_move[temp][st][counter],st,temp); for(int adder = 1;adder <= 6;adder++) pratiototal[adder] = pratiototal[adder] + p_ratio[adder]; } for(int counter = 2;counter <=5 ;counter++) { if(pratiototal[counter] >= pratiototal[pop]) pop = counter;/* predicted p hand of opponent */

Page 47: AI For Texam Hold'em poker

} /* calculated p yet */ checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ int cal = reaction_ratio[1]; int foiled = reaction_ratio[2]; int rez = reaction_ratio[3]; if(foiled<=cal || foiled<=rez) return -10;/* fold */ else { rise = get_minimum_raise(stage,teamno,dealer); cout << "\n Minimum Raise 3 = " << rise; int bet = bluff(rise,2); cout << "\n Final Return 3= " << bet; //getch(); return bet;/* bluff */ } } } else/* phand == GUD or very gud */ { if(stage==1 || stage==2) { int rezfold,calfold; checkPattern_action(0,RISE,stage,temp,HIGH); rezfold = reaction_ratio[2]; checkPattern_action(0,CALL,stage,temp,HIGH); calfold = reaction_ratio[2]; if(calfold > rezfold) { rise = get_minimum_raise(stage,teamno,dealer); cout << "\n Minimum Raise 4 = " << rise; int bet = bluff(rise,2); cout << "\n Final Return 4= " << bet; //getch(); return bet; } else return 0;/* call */

Page 48: AI For Texam Hold'em poker

} else/* stage = 3 or 4 */ { int st; if(round_number[stage] == 1) st = stage - 1; int pop=1; int calfold,rezfold; int pratiototal[6]={0}; for(int counter = 1;counter <= round_number[st];counter++) { checkPattern_p(players_data_move[*(ptr-1)][st][counter],players_data_move[temp][st][counter],st,temp); for(int adder = 1;adder <= 6;adder++) pratiototal[adder] = pratiototal[adder] + p_ratio[adder]; } for(int counter = 2;counter <=5 ;counter++) { if(pratiototal[counter] >= pratiototal[pop]) pop = counter;/* predicted p hand of opponent */ } /* calculated p yet */ checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ rezfold = reaction_ratio[2]; checkPattern_action(pop,CALL,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ calfold = reaction_ratio[2]; if(calfold < rezfold) return 0;/* call */ else { rise = get_minimum_raise(stage,teamno,dealer); cout << "\n Minimum Raise = " << rise; int bet = bluff(rise,2); cout << "\nFinal Bet = " << bet; //getch();

Page 49: AI For Texam Hold'em poker

return bet;/* bluff */ } } } } } } } else switch(phand) { case VERY_BAD: case BAD: case AVG: cout << "\nFirst Case"; //getch(); flag=0;/* when our phand bad or very bad */ ptr = team; for(int xx = 1;xx <= 3;xx++)/* loop for all the team entries */ { int temp = *ptr; if(temp == teamno) { flag = 1;/* the next entry will have our turn after us */ continue; } else if(flag == 0) { int pop=1; if(xx == 1) { checkPattern_p(FIRST,players_data_move[temp][stage][round_number[stage]],stage,temp); for(int temp1 = 2;temp1 <=5 ;temp1++) { if(p_ratio[temp1] >= p_ratio[pop]) pop = temp1;/* predicted p hand of oponent */ } int cal,rez,foiled; checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ cal = reaction_ratio[1]; foiled = reaction_ratio[2]; rez = reaction_ratio[3]; if(foiled<=cal || foiled<=rez) return -10;/* fold */

Page 50: AI For Texam Hold'em poker

} else if(xx == 2) { checkPattern_p(players_data_move[*(ptr-1)][stage][round_number[stage]],players_data_move[temp][stage][round_number[stage]],stage,temp); for(int temp1 = 2;temp1 <=5 ;temp1++) { if(p_ratio[temp1] >= p_ratio[pop]) pop = temp1;/* predicted p hand of opponent */ } int cal,rez,foiled; checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ cal = reaction_ratio[1]; foiled = reaction_ratio[2]; rez = reaction_ratio[3]; if(foiled<=cal || foiled<=rez) return -10/* fold */; } } else if(flag == 1) { if(stage==1 || stage==2) { int rez,cal,foiled; checkPattern_action(0,RISE,stage,temp,HIGH); cal = reaction_ratio[1]; foiled = reaction_ratio[2]; rez = reaction_ratio[3]; if(foiled<=cal || foiled<=rez) return -10;/* fold */ } else/* stage = 3 or 4 */ { int st; if(round_number[stage] == 1) st = stage - 1; else st = stage; int pop=1; int pratiototal[6]={0}; for(int counter = 1;counter <= round_number[st];counter++) { checkPattern_p(players_data_move[*(ptr-1)][st][counter],players_data_move[temp][st][counter],st,temp);

Page 51: AI For Texam Hold'em poker

for(int adder = 1;adder <= 6;adder++) pratiototal[adder] = pratiototal[adder] + p_ratio[adder]; } for(int counter = 2;counter <=5 ;counter++) { if(pratiototal[counter] >= pratiototal[pop]) pop = counter;/* predicted p hand of opponent */ } /* calculated p yet */ checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ int cal = reaction_ratio[1]; int foiled = reaction_ratio[2]; int rez = reaction_ratio[3]; if(foiled<=cal || foiled<=rez) return -10;/* fold */ } } ptr++; } rise = get_minimum_raise(stage,teamno,dealer); final = bluff(rise,2); cout << "\nFINAL BLUFF = " << final; //getch(); return final; break; case GOOD: case VERY_GOOD: //cout << "\n2nd Case"; //cout << //getch(); flag=0;/* when our phand bad or very bad */ ptr = team; for(int xx = 1;xx <= 3;xx++)/* loop for all the team entries */ { //cout << "\nChecking Player :: " << xx; int temp = *ptr; if(temp == teamno) { flag = 1;/* the next entry will have our turn after us */ continue; } else if(flag == 0) {

Page 52: AI For Texam Hold'em poker

int pop=1; if(xx == 1) { //cout << "\nCall Check Pattern1"; checkPattern_p(FIRST,players_data_move[temp][stage][round_number[stage]],stage,temp); for(int temp1 = 2;temp1 <=5 ;temp1++) { if(p_ratio[temp1] >= p_ratio[pop]) pop = temp1;/* 4predicted p hand of opponent */ } int calfold,rezfold; //cout << "\nCall Check Pattern2"; checkPattern_action(pop,CALL,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ calfold = reaction_ratio[2]; //cout << "\nCall Check Pattern3"; checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ rezfold = reaction_ratio[2]; if(calfold < rezfold) xx1res = CALL; else xx1res = RISE; } else if(xx == 2) { //cout << "\nCall Check Pattern4"; checkPattern_p(players_data_move[*(ptr-1)][stage][round_number[stage]],players_data_move[temp][stage][round_number[stage]],stage,temp); for(int temp1 = 2;temp1 <=5 ;temp1++) { if(p_ratio[temp1] >= p_ratio[pop]) pop = temp1;/* predicted p hand of opponent */ } int calfold,rezfold; //cout << "\nCall Check Pattern5"; checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */

Page 53: AI For Texam Hold'em poker

rezfold = reaction_ratio[2]; //cout << "\nCall Check Pattern6"; checkPattern_action(pop,CALL,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ calfold = reaction_ratio[2]; if(calfold < rezfold) xx2res = CALL; else xx2res = RISE; } } else if(flag == 1) { if(stage==1 || stage==2) { int rezfold,calfold; //cout << "\nCall Check Pattern7"; checkPattern_action(0,RISE,stage,temp,HIGH); rezfold = reaction_ratio[2]; //cout << "\nCall Check Pattern8"; checkPattern_action(0,CALL,stage,temp,HIGH); calfold = reaction_ratio[2]; if(calfold > rezfold) { if(xx = 2) xx1res = CALL; else xx2res = CALL; } else { if(xx = 2) xx1res = RISE; else xx2res = RISE; } } else/* stage = 3 or 4 */ { int st; if(round_number[stage] == 1) st = stage - 1; int pop=1; int calfold,rezfold; int pratiototal[6]={0}; for(int counter = 1;counter <= round_number[st];counter++)

Page 54: AI For Texam Hold'em poker

{ //cout << "\nCall Check Pattern9"; checkPattern_p(players_data_move[*(ptr-1)][st][counter],players_data_move[temp][st][counter],st,temp); for(int adder = 1;adder <= 6;adder++) pratiototal[adder] = pratiototal[adder] + p_ratio[adder]; } for(int counter = 2;counter <=5 ;counter++) { if(pratiototal[counter] >= pratiototal[pop]) pop = counter;/* predicted p hand of opponent */ } /* calculated p yet */ //cout << "\nCall Check Pattern 10"; checkPattern_action(pop,RISE,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ rezfold = reaction_ratio[2]; //cout << "\nCall Check Pattern 11"; checkPattern_action(pop,CALL,stage,temp); /* RISE :: 3 */ /* FOLD :: 2 */ /* CALL :: 1 */ /* BLUFF :: 4 */ calfold = reaction_ratio[2]; if(calfold < rezfold) { if(xx=2) xx1res = CALL; else xx2res = CALL; } else { if(xx=2) xx1res = RISE; else xx2res = RISE; } } } ptr++; } } if(xx1res == xx2res) { rise = get_minimum_raise(stage,teamno,dealer);

Page 55: AI For Texam Hold'em poker

if(xx1res == 1) xx1res = 0; else xx1res = bluff(rise,2); cout << "\nFinal Raise = " << xx1res; //getch(); return xx1res; } else return 0;/*call*/ } int bluff(int rise, int limit)/* highest raise */ { /* initialization for rand function */ srand ( time(NULL) ); FILE *pt; int bluffcounter = 0; pt = fopen("bluff.txt","rb"); if(pt == NULL) { int choice = rand() % 2 + 1; if (choice == 1)/* high raise bluff */ { pt = fopen("bluff.txt","wb"); bluffcounter = bluffcounter + 2; int bet = 2*rise; fprintf(pt,"%d",bluffcounter); fclose(pt); cout << "\nBluff Returning = " << bet; // getch(); if(bet > 200) return -10;/* fold */ return bet; } else/* consecutive low raise bluff */ { pt = fopen("bluff.txt","wb"); bluffcounter++; int bet = rise; fprintf(pt,"%d",bluffcounter); fclose(pt); cout << "\nBluff Returning = " << bet; //getch(); if(bet > 200) return -10;/* fold */ return bet; }

Page 56: AI For Texam Hold'em poker

} else/* already bluffed */ { fscanf(pt,"%d",&bluffcounter); if(bluffcounter == limit) { fclose(pt); return 0;/* already bluffed a lot */ } else/* bluffcounter is not max */ { if(bluffcounter %2 == 1)/* bluffcounter == 1 or odd */ { bluffcounter++; fclose(pt); pt = fopen("bluff.txt","wb"); fprintf(pt,"%d",bluffcounter); fclose(pt); int bet = rise; cout << "\nBluff Returning3 = " << bet; // getch(); if(bet > 200) return -10;/* fold */ return bet; } else/* bluffcounter is even and not full eg 2 if limit is 4 */ { int choice = rand()%2 + 1; if(choice == 1) { bluffcounter++; fclose(pt); pt = fopen("bluff.txt","wb"); fprintf(pt,"%d",bluffcounter); fclose(pt); int bet = rise; cout << "\nBluff Returning4 = " << bet; //getch(); if(bet > 200) return -10;/* fold */ return bet; } else { bluffcounter= bluffcounter+2; fclose(pt); pt = fopen("bluff.txt","wb"); fprintf(pt,"%d",bluffcounter); fclose(pt); int bet = 2*rise;

Page 57: AI For Texam Hold'em poker

cout << "\nBluff Returning5 = " << bet; //getch(); if(bet > 200) return -10;/* fold */ return bet; } } } } } int lowraise(int bet) { /* initialization for rand function */ srand ( time(NULL) ); float choice = rand()%5+1; choice = choice/10; choice = choice + 1; choice = choice * bet; int turn = choice; return turn; } /* compare function used by qsort for arrays */ int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } /* main function counting p */ int pcounter(int *cards,int hand,int total) { int probab,hole[5],*pt,allpairs[7],paircards,temp; pt = cards; /* noting hand cards */ for(temp = 0;temp < hand;temp++) { hole[temp] = *pt; pt++; } pt = cards; qsort(pt,total,sizeof(int), compare); qsort(hole,hand,sizeof(int), compare); probab = highcard(hole,hand);

Page 58: AI For Texam Hold'em poker

paircards = pairs(cards,allpairs,total); qsort(allpairs,paircards,sizeof(int), compare); //cout << "\nPair Cards = " << paircards; /* now calling different functions */ /* if a combonation is not formed then it will give value given by previous function */ temp = twoofakind(allpairs,paircards,hole,hand); //cout << "\nOne Pair = " << temp; if(temp!=0) probab = 13*1 + temp; //cout << "\nOne Pair = " << probab; temp = twopair(allpairs,paircards,hole,hand); //cout << "\nTwo Pair = " << temp; if(temp!=0) probab = 13*2 + temp; //cout << "\nTwo Pair = " << probab; temp = threeofakind(allpairs,paircards,cards,total,hole,hand); //cout << "\nThree of a Kind = " << temp; if(temp!=0) probab = 13*3 + temp; //cout << "\nThree of a Kind = " << probab; temp = straight(cards,total,hole,hand); //cout << "\nStraight = " << temp; if(temp!=0) probab = 13*4 + temp; //cout << "\nStraight = " << probab; temp = flush(cards,total,hole,hand); //cout << "\nFlush = " << temp; if(temp!=0) probab = 13*5 + temp; //cout << "\nFlush = " << probab; temp = fullhouse(allpairs,paircards,cards,total,hole,hand); //cout << "\nFullHouse = " << temp; if(temp!=0) probab = 13*6 + temp; //cout << "\nFullHouse = " << probab; temp = fourofakind(allpairs,paircards,hole,hand); //cout << "\nFour of a Kind = " << temp; if(temp!=0) probab = 13*7 + temp; //cout << "\nFour of a Kind = " << probab; temp = striaghtflush(cards,total,hole,hand); //cout << "\nStraight Flush = " << temp; if(temp!=0) probab = 13*8 + temp; //cout << "\nStraight Flush = " << probab; return probab; }

Page 59: AI For Texam Hold'em poker

int highcard(int *hole,int hand) { int hcard,*pt; vector<int> handcards (hole, hole + hand); for(int temp=0;temp < hand;temp++) handcards.at(temp) = handcards.at(temp)%13; sort(handcards.begin(),handcards.end()); hcard = handcards.at(hand-1); return hcard; } /* this funciton will find all pairs formed and store them in array */ int pairs(int *cards,int *allpairs,int total) { int counter = 0,*pt,allcount = total; pt = allpairs; vector<int> all (cards, cards + total); for(int temp=0;temp < total ;temp++) { all.at(temp) = all.at(temp)%13; } while(allcount > 1) { for(int temp = 1;temp < allcount;temp ++) { if(all.at(0) == all.at(temp)) { *pt = all.at(0); all.erase (all.begin() + temp); all.erase (all.begin()); allcount = allcount - 2; pt++; counter++; break; } else if(temp == allcount-1) { all.erase (all.begin()); allcount--; break; } } } return counter;

Page 60: AI For Texam Hold'em poker

} int twoofakind(int *allpairs,int paircards,int *hole,int hand) { int *ptp; ptp = allpairs + paircards - 1; vector<int> mycards (hole, hole + hand); for(int temp1=0;temp1<hand;temp1++) mycards.at(temp1) = mycards.at(temp1)%13; sort(mycards.begin(),mycards.end()); for(int temp1 = paircards-1;temp1 > -1 ; temp1--) { for(int temp2 = hand-1;temp2 > -1 ; temp2--) { if(*ptp == mycards.at(temp2)) { return mycards.at(temp2); } } ptp--; } return 0; } int twopair(int *allpairs,int paircards,int *hole,int hand) { if(paircards <2) return 0; int temp = twoofakind(allpairs,paircards,hole,hand); if (temp == 0) return 0; return *(allpairs+paircards-1); } int threeofakind(int *allpairs,int paircards,int *cards,int total,int *hole , int hand ) { int cpairsct = paircards , ccardsct = total , cholect = hand; vector<int> cpairs (allpairs , allpairs + paircards); vector<int> ccards (cards , cards + total); vector<int> chole (hole , hole + hand); for(int temp=0;temp < paircards ; temp++) cpairs.at(temp) = cpairs.at(temp)%13; for(int temp = 0;temp < total ;temp++) ccards.at(temp) = ccards.at(temp)%13; for(int temp = 0;temp < hand ;temp++) chole.at(temp) = chole.at(temp)%13; sort(cpairs.begin(),cpairs.end()); sort(ccards.begin(),ccards.end()); sort(chole.begin(),chole.end()); /*deleting pair cards from total cards */

Page 61: AI For Texam Hold'em poker

for(int temp1 = cpairsct-1;temp1 > -1;temp1--) { for(int count=1;count<=2;count++) { for(int temp2 = ccardsct - 1 ; temp2 > -1 ; temp2--) { if(ccards.at(temp2) == cpairs.at(temp1)) { ccards.erase (ccards.begin()+temp2); ccardsct--; break; } } } } /*deleting cards other than hole cards in pairs*/ for(int temp1=cpairsct-1;temp1>-1;temp1--) { for(int temp2=cholect-1;temp2 >-1;temp2--) { if(chole.at(temp2)==cpairs.at(temp1)) break; else if(temp2==0) { cpairs.erase(cpairs.begin()+temp1); cpairsct--; } } } for(int temp1=cpairsct-1;temp1 > -1;temp1--) { for(int temp2=ccardsct-1;temp2 > -1 ;temp2--) { if(cpairs.at(temp1)==ccards.at(temp2)) return cpairs.at(temp1); } } return 0; } int straight(int *cards,int total,int *hole,int hand) { if(total < 5) return 0; vector<int> all(cards,cards + total); vector<int> mycards(hole,hole + hand); for(int temp=0;temp<hand;temp++) mycards.at(temp) = mycards.at(temp)%13; sort(mycards.begin(),mycards.end()); for(int temp=0;temp<total;temp++) all.at(temp) = all.at(temp)%13;

Page 62: AI For Texam Hold'em poker

sort(all.begin(),all.end()); for(int temp=total-1;temp>3;temp--) { int count = 1,cons = temp-1; while(1) { if(all.at(cons) == all.at(cons+1)) cons--; else if(all.at(cons) == all.at(cons+1)-1) { cons--; count++; if(count==5) { for(int temp1=temp;temp1>temp-5;temp1--) { for(int temp2=hand-1;temp2>-1;temp2--) { if(mycards.at(temp2)==all.at(temp1)) return all.at(temp); } } break; } } else break; if(cons < 0) break; } } return 0; } int flush(int *cards,int total,int *hole,int hand) { if(total < 5) return 0; int a=0,b=0,c=0,d=0,suit; /*4 ints for 4 suits */ vector<int> all (cards,cards + total); vector<int> mycards (hole,hole + hand); for(int temp=0;temp<total;temp++) { if((all.at(temp)-1)/13==0) a++;

Page 63: AI For Texam Hold'em poker

if((all.at(temp)-1)/13==1) b++; if((all.at(temp)-1)/13==2) c++; if((all.at(temp)-1)/13==3) d++; } if(a==5) suit = 0; else if(b==5) suit = 1; else if(c==5) suit = 2; else if(d==5) suit = 3; else return 0; for(int temp=0;temp<hand;temp++) { if((mycards.at(temp)-1)/13 == suit) { int larger=0; for(int temp1=0;temp1<total;temp1++) { if(((all.at(temp1)-1)/13 == suit)&&(all.at(temp1)%13>larger)) larger = all.at(temp1)%13; if(temp1 == total-1) return larger; } } } return 0; } int fullhouse(int *allpairs,int paircards,int *cards,int total,int *hole , int hand ) { if(total < 5) return 0; int temp; temp = threeofakind(allpairs,paircards,cards,total,hole,hand); if (temp == 0) { if(total!=hand) { int riverpairs[5],rvrprcount,temptotal=total,temphand=hand; vector<int> river (cards,cards + total); vector<int> mycards (hole,hole + hand); for(int temp1=temptotal-1;temp1>=-1;temp1--) { for(int temp2=temphand-1;temp2>-1;temp2--) {

Page 64: AI For Texam Hold'em poker

if(river.at(temp1)==mycards.at(temp2)) { river.erase (river.begin() + temp1); mycards.erase (mycards.begin() + temp2); temptotal--; temphand--; break; } } } sort(river.begin(),river.end()); int riverarr[5]; for(int convert = 0;convert < total - hand ;convert++) riverarr[convert] = river.at(convert); rvrprcount = pairs(riverarr,riverpairs,total-hand); temp = threeofakind(riverpairs,rvrprcount,riverarr,total-hand,riverarr,total-hand); if((temp!=0)&&(paircards>1)) return temp; } return 0; } else if(paircards <2) return 0; else return temp; } int fourofakind(int *allpairs,int paircards,int *hole,int hand) { int probab; vector<int> pair (allpairs,allpairs + paircards); sort(pair.begin(),pair.end()); vector<int> mycards (hole,hole + hand); if(paircards>1) { for(int temp = paircards-1;temp>-1;temp--) { for(int temp1 = temp-1;temp1 > -1 ;temp1--) { if(pair.at(temp) == pair.at(temp1)) { for(int temp2 = 0;temp2 < hand ;temp2++) { if(mycards.at(temp2)%13 == pair.at(temp)) return pair.at(temp); } } } } }

Page 65: AI For Texam Hold'em poker

return 0; } int striaghtflush(int *cards,int total,int *hole,int hand) { if(total < 5) return 0; vector<int> all (cards,cards + total); vector<int> mycards (hole,hole + hand); int temp = straight(cards,total,hole,hand); int suit = (temp-1)/13; for(int temp1=1;temp1<5;temp1++) { for(int temp2=total-1;temp2>-1;temp2--) { if((all.at(temp2)%13==temp-temp1)&&((all.at(temp2)-1)/13==suit)) break; else if(temp2 == 0) return 0; } } for(int temp1=0;temp1<hand;temp1++) { if(((mycards.at(temp1)%13==temp)||(mycards.at(temp1)%13==temp-1)||(mycards.at(temp1)%13==temp-2)||(mycards.at(temp1)%13==temp-3)||(mycards.at(temp1)%13==temp-4))&&((mycards.at(temp1)-1)/13==suit)) return temp; } return 0; } /* no need to make separate function for royal flush */ /* the straight flush function will detect and give max value in case of a royal flush */ int priver(int*cards,int hand,int total) { /* if(total==2) return 0;*/ int *ptr,probab; ptr = cards; for(int temp=0;temp<hand;temp++) ptr++; total = total - hand; hand = total; probab = pcounter(ptr,hand,total); return probab; } /* gives 1 for pvbad and 5 for pvgud */ int pgrader(int probab , int stage)

Page 66: AI For Texam Hold'em poker

{ if(stage == 2) { if(probab<10) return 1; else if(probab < 15) return 2; else if(probab < 28) return 3; else if(probab < 85) return 4; else return 5; } else { if(probab<13) return 1; else if(probab < 21) return 2; else if(probab < 26) return 3; else if(probab < 85) return 4; else return 5; } } int ppreflop(int hand1,int hand2) { if((hand1%13==hand2%13)&&(hand1%13 > 9)) return 5; else if(hand1%13==hand2%13 && hand1%13 > 5 ) return 4; else if((((hand1-1)/13 == (hand2-1)/13)&&(hand1%13 > 5 && hand2%13 > 5))||((hand1%13 == hand2%13+1)&&(hand1%13 > 6 && hand2%13 > 6))||((hand2%13 == hand1%13+1)&&(hand1%13 > 6 && hand2%13 > 6))) return 3; else return 2; /* p vbad don;t exists ryt now */ } int pfuture(int *cards,int total) { /*if(total == 2) return 0;*/ int temp=0,probab=0; temp = futurestraight(cards,total); if(temp!=0)

Page 67: AI For Texam Hold'em poker

probab = 13*4 + temp; //cout<<probab<<"\n"; temp = futureflush(cards,total); if(temp!=0) probab = 13*5 + temp; //cout<<probab<<"\n"; return probab; } int futurestraight(int *cards,int total) { vector<int> all (cards,cards + total); for(int temp=0;temp < total;temp++) all.at(temp) = all.at(temp)%13; sort(all.begin(),all.end()); vector<int> mycards (cards,cards + 2); for(int temp=total-1;temp>-1;temp--) { int counter=2,skip=1,hand=0; if((mycards.at(0)%13==all.at(temp))||(mycards.at(1)%13==all.at(temp))) hand=1; for(int temp1 = temp-1;temp1>-1;temp1--) { if(all.at(temp1)==all.at(temp1+1)) { if((mycards.at(0)%13==all.at(temp1))||(mycards.at(1)%13==all.at(temp1))) hand=1; continue; } else if(all.at(temp1)==(all.at(temp1+1)-1)) { if((mycards.at(0)%13==all.at(temp1))||(mycards.at(1)%13==all.at(temp1))) hand=1; counter++; } else if(all.at(temp1)==(all.at(temp1+1)-2)) { if((mycards.at(0)%13==all.at(temp1))||(mycards.at(1)%13==all.at(temp1))) hand=1; counter++; skip++; } else break; if(skip>2) break;

Page 68: AI For Texam Hold'em poker

else if((counter==5)&&(hand==1)) return all.at(temp); } } return 0; } int futureflush(int *cards, int total) { int clas; vector<int> all (cards,cards + total); vector<int> mycards (cards , cards + 2); sort(all.begin(),all.end()); int suit[4]; for(int temp=0;temp<4;temp++) suit[temp] = 0; for(int temp=0;temp < total;temp++) { int suits = (all.at(temp)-1)/13; suit[suits]++; } if((mycards.at(0)-1)/13 == (mycards.at(1)-1)/13) suit[(mycards.at(0)-1)/13]++; else { suit[(mycards.at(0)-1)/13]++; suit[(mycards.at(1)-1)/13]++; } for(int temp=0;temp<3;temp++) { if(suit[temp] >4) { clas = temp; break; } } for(int temp=total-1;temp>-1;temp--) if((all.at(temp)-1)/13 == clas) return all.at(temp)%13; return 0; } int overallp(int*cards,int total,int stage) { cout << "\nCARDS = " << cards[0] << " " <<cards[1] << " " << cards[2] << " " << cards[3] << " " << cards[4] << " " << cards[5]; int probab; if(stage == 1) { probab = ppreflop(*cards,*(cards + 1));

Page 69: AI For Texam Hold'em poker

return probab; } else if(stage ==2 || stage == 3 || stage == 4 ) { int new_cards[7]; for(int xx=0;xx<7;xx++) new_cards[xx] = cards[xx]; int phand = pcounter(cards,2,total); //getch(); cout << "\nphand = " <<phand; int privr = priver(new_cards,2,total); cout << "\npriver = " << privr; phand = pgrader(phand,stage); cout << "\nphand Grade = " << phand; privr = pgrader(privr,stage); cout << "\nprivr = " << privr; if(phand > privr) probab = phand; else if(phand < privr && privr >3) {probab = privr;} else probab = phand; if((stage != 4)&&((probab == 1)||(probab == 2))) { int pwillbe = pfuture(cards,total); if (pwillbe != 0) probab = 3; } return probab; } else { } } int main() { ofstream del_count; del_count.open("Round_Counts.txt",ios::trunc); del_count.close(); char arr[5000]; int players_left,self_team_number; is_show_down = false; cout << "\n READ INPUT FILE START"; /* Acquire Data from input File */ read_file(INPUT_FILE,arr); cout << "\n READ INPUT FILE COMPLETE"; /* Get Our team number by decoding the input file */ self_team_number = decode_input_file(arr); printPlayerDetails(players[self_team_number]);

Page 70: AI For Texam Hold'em poker

//getch(); /* Acquiring data from Deals_money file */ cout << "\n READ DEALS MONEY FILE"; read_file(DEALS_MONEY,arr); cout << "\n COMLETED READing DEALS MONEY FILE"; /* Get the deal number and opponent pot by decoding this file */ deal_number = decode_dealsMoney(arr); cout << "\nPlayer Moneys :: "; for(int xx=1;xx<=6;xx++) cout << "\nPlayer " << xx << " = " << players[xx].money_remaining; //getch(); /* Call the main play function */ cout << "\n STARTING MAIN DEAL FUNCTION "; start_deal(arr,self_team_number,&players_left); /* Deleting The Round Count File */ /* get stage */ /*<play decide>*/ } void start_deal(char *data,int self_team_number,int *players_left) { int stage; int main_pot; int dealer; /* Setting Round Numbers as 1 */ for(int xx =0;xx<=500;xx++) round_number[xx]=1; read_file(DEAL_FILE,data); dealer = decode_dealFile(data,&stage,&main_pot); cout <<"\n Dealer = " << dealer << "\nStage = " << stage << "\n Main Pot = " << main_pot; //getch(); /* Check if a new stage has started or the previous stage is going on */ /* If new stage has started , then round count file is deleted */ // //stage_file=fopen("prevStage.txt","rb"); //if(stage_file == NULL) /* Condition of Stage 1 */ //{ // stage_file=fopen("prevStage.txt","wb"); // fprintf(stage_file,"%d",stage); // fclose(stage_file); // if(fopen("count_file.txt","rb") != NULL) // remove("count_file.txt"); //} //else

Page 71: AI For Texam Hold'em poker

//{ // /* Condition for Stage other than 1 */ // int prevStage; // fscanf(stage_file,"%d",&prevStage); // fclose(stage_file); // stage_file=fopen("prevStage.txt","wb"); // fprintf(stage_file,"%d",stage); // fclose(stage_file); // if(stage != prevStage) // if(fopen("count_file.txt","rb") != NULL) // { // int tempVar; // fscanf(fopen("count_file.txt","rb"),"%d",&tempVar); // ofstream fout; // fout.open("Round_Counts.txt",ios::app); // /* Add the round counts to the file */ // fout << tempVar << "\n"; // fout.close(); // remove("count_file.txt"); // } //} ///* Clear Help Files for Previous Stage and Round Counter on show down */ //if(stage == 5) //{ // cout << "\n SHOW DOWN IT IS!!"; // /* In Case of Showdown */ // if(fopen("count_file.txt","rb") != NULL) // { // int tempVar; // // /* Read the count for Number of Rounds in the prev stage */ // fscanf(fopen("count_file.txt","rb"),"%d",&tempVar); // ofstream fout; // fout.open("Round_Counts.txt",ios::app); // /* Add the round counts to the file */ // fout << tempVar << "\n"; // fout.close(); // // remove("count_file.txt"); // } // if(fopen("prevStage.txt","rb") != NULL) // remove("prevStage.txt"); // /* Read the Show_Down Stage Data */ // get_current_state_players(data,stage,self_teamNumber,dealer); // /* Get all the Data from the Deal.txt Files to the Arrays that have been Globally Declared */

Page 72: AI For Texam Hold'em poker

// analyze_data(self_teamNumber,dealer); // /* Remove Count File,Team Help Files and other files . TO BE WRITTEN */ // //} //else //{ // /* For cases other than show down */ // int xyz; // Temporary variable // // xyz = // get_current_state_players(data,stage,self_team_number,dealer); cout << "\n Returned From Reading Data"; analyze_data(self_team_number,dealer); cout << "\n Returned from Analyzing Data"; //getch(); if(stage == 5) {remove("Round_Counts.txt");return ;} int all_cards[7]; all_cards[0] = players_hand_card_1[self_team_number]; all_cards[1] = players_hand_card_2[self_team_number]; cout << "\nWTF HAND CARDS = " << all_cards[0] << " " << all_cards[1]; for(int xx = 2;xx < 2+r_count;xx++) all_cards[xx] = river_cards[xx-2]; /* deciding the mode of play */ set_play_mode(self_team_number); int decide; switch(play_mode) { case OFFENSIVE: decide = offensive(stage,all_cards,r_count+2,self_team_number,dealer); break; case DEFENSIVE: decide = defensive_play(stage,all_cards,r_count+2,self_team_number,dealer); break; case ULTRA_DEFENSIVE: decide = -10;/* fold */ break; } ofstream fout; fout.open("outputf.txt",ios::out); fout<<decide; fout.close(); } int anyoppraisehigh(int stage,int round,int firstplayer,int myteamno,int dealer) {

Page 73: AI For Texam Hold'em poker

cout << "\nChecking Any opp Raise"; int self_team_number = myteamno; int result=0; int s_pos = dealer+1; cout <<"\nData Received by Any Opp Raise ::\nstage="<<stage<<"\nround = " << round <<"\nFirst Player = " << firstplayer <<"\nTeam Number = "<< myteamno << "\nDealer = " << dealer; while(s_pos != self_team_number && !(s_pos == 7 && self_team_number == 1)) { //cout << "\nIn loop 0"; if(s_pos == 7) s_pos = 1; if(players_data_move[s_pos][stage][round] == RISE) { //cout << "\nIn loop 1"; int rise = players_data_bet[s_pos][stage][round] - pot_money[s_pos][stage][round]; //cout << "\nIn loop 2"; if(rise > 50) { result = 1; return result; break; } } s_pos++; } cout << "\nAny Opp Raise Ends. Result = " << result; return result; }