#include #include #include #include const int N = 150; bool PRINT_ON = 0; int CURRENT_TO_BEAT; double BLUFF[N][N][N]; double CALL[N][N]; int NET_STANDING[1000]; int ROUNDS_PLAYED = 0; int MAX_SCORE; // return player 1's utility double compute_local_equilibrium(double* p, double* v_1, double* v_2, int n){ double bluffer_strategy[N][N]; double caller_strategy[N]; for (int i=0;i remaining_prob){ stop=1; } else { remaining_prob -= needed_prob; remaining_prob -= p[i]; while (remaining_j_prob < needed_prob){ bluffer_strategy[j][i] = remaining_j_prob/p[j]; needed_prob -=remaining_j_prob; j--; remaining_j_prob = p[j]; } remaining_j_prob -= needed_prob; bluffer_strategy[j][i] = needed_prob/p[j]; bluffer_strategy[i][i] = 1; i++; } } //stopped bluffer_strategy[j][i] = remaining_j_prob/p[j]; j--; while (j>= i){ bluffer_strategy[j][i] = 1; j--; } // ok, figured out bluffer strategy. know that i is uncontested if (PRINT_ON) cout << MAX_SCORE-i << " is uncontested\n"; for (int j = 1; j<=i; j++) if (v_1[j]==0) caller_strategy[j]=0; // this makes sense, but is it right? else caller_strategy[j] = 1-(v_1[i]/v_1[j]); for (int j = i+1; j0) cout << "Given that the roller rolls " << MAX_SCORE-i << ", the probability of claiming " << MAX_SCORE-j << " is " << bluffer_strategy[i][j] << "\n"; for (int i=0;i0) cout << "The probability that the caller will call when the roller claims " << MAX_SCORE-i << " is " << caller_strategy[i] << "\n"; cout << "The utility for the player is " << result << ".\n"; } for (int i=0;i= rn) return i; else{ rn -= p[i]; i++; } } } int bluff_play(int to_beat, int rolled){ double rn = random_double(); int i=0; while (1){ if (BLUFF[to_beat][rolled][i] >= rn) return i; else{ rn -= BLUFF[to_beat][rolled][i]; i++; } } } bool call_play(int to_beat, int said){ double rn = random_double(); if (CALL[to_beat][said] >= rn) return 1; else return 0; } int play_step(int n, double* p, int to_beat, int player){ if (player==0) { //computer int rolled = roll(n,p); if (rolled == 0){ cout << "The computer rolled "<= to_beat){ // sometimes the computer has no hope of winning and reports something illegal. hack to fix this: //cout << "(hopeless computer)\n"; chosen = to_beat-1; } cout << "The computer claims " << MAX_SCORE-chosen; bool called; if (chosen==0){ cout << ". You must call because this is the highest possible claim.\n"; called=1; } else{ cout << ". Type 1 to call, 0 to accept: "; cin >> called; } cout << "(The computer rolled " << MAX_SCORE-rolled <<".)\n"; if (called) { if (chosen!=rolled){ cout << "You win this round.\n"; return 1; } else{ cout << "The computer wins this round.\n"; return 0; } } else return play_step(n, p, chosen, 1); } else { int rolled = roll(n,p); if (rolled == 0){ cout << "You rolled " << MAX_SCORE << ". You win this round!\n"; return 1; } cout << "You rolled " << MAX_SCORE-rolled << ". What would you like to claim? "; int chosen; cin >> chosen; chosen= MAX_SCORE-chosen; while (chosen >= to_beat || chosen <0){ cout << "That is not a legal move. Please try again: "; cin >> chosen; chosen=MAX_SCORE-chosen; } bool called = call_play(to_beat, chosen); if (called) { cout << "The computer calls.\n"; if (chosen==rolled){ cout << "You win this round.\n"; return 1; } else{ cout << "The computer wins this round.\n"; return 0; } } else{ cout << "The computer did not call.\n"; return play_step(n, p, chosen, 0); } } } double compute_prob(int num_dice, int dice_sides, int target_total){ int num_ways_current[N]; int num_ways_next[N]; for (int i=0; i= 0) num_ways_next[i]+=num_ways_current[k]; } for (int i=0; i> ans; int n; double p[N]; if (!ans){ int dice_sides; cout << "How many sides does a die have? (E.g. a 2-sided die is a coin.) "; cin >> dice_sides; int num_dice; cout << "How many dice are there? "; cin >> num_dice; MAX_SCORE = num_dice * dice_sides; n=MAX_SCORE; for (int i=0; i> n; MAX_SCORE=n; for (int j=0;j> p[j]; } } // double v_1[N]; // double v_2[N]; // for (int j=0;j> v_1[j]; // cout << "give v_2(" << j+1 << "): "; // cin >> v_2[j]; // } cout << "Would you like to see the optimal strategy (1) or turn this off (0)? "; cin >> PRINT_ON; compute_overall_equilibrium(p,n); cout << "\nOptimal strategy computed, ready to play...\n\n"; int computer_wins=0; int human_wins=0; while(1){ NET_STANDING[ROUNDS_PLAYED++]=human_wins-computer_wins; cout << "History of total winnings: "; for (int i=0; i