Apparently there was some game I came up with and I wanted to make a program that could play it really well.
read more
import java.util.Random;
public class smartLearningPlayer {
public static void main(String[] args) {
// This is essentially using the E value to teach the program when to roll again and when not to
//If the total number of times successfully rolled greater monetary profits times the total amount of
// money gained from rolling again divided by the number of times rolled (with that number already rolled)
// is greater than the amount of money that would be made by staying, roll again.
//There may be an error here. To be considered an auspicious roll, the roll must be greater than or equal to the previous roll. I believe one error is that I'm only applying the E value calculation to determining
//whether a third roll should be taken. I need to apply this to determining a whether a second roll should be taken. FIXED. I had a total monetary increase of 20!
//I'm now going to add an array that will keep track of the rolls the computer stops at (thus determining the monetary gain) as the computer continues to learn.
//I hope to see that the number of times the computer ends on low rolls (such as 1s) will decrease as it will hopefully try to roll again when presented with a 1.
Random generator = new Random();
int val = 0;
int totalgained = 0;
int transientEvalue;
int roll=0;
int rolltemp;
int numberofrolls=0;
int numberofattempts = 1000000;
int[][] roll_timesrolled_timesgained_monetaryprofits = new int [4][20];
int[][] rollsvals = new int [2][3];
int [] endingvals = new int [20];
int [] endingvals2 = new int [20];
int [] endingvalsdelta = new int [20];
boolean rolledonce=false;
boolean canIRollAgain;
boolean shouldIRollAgain;
int a=0;
while (a<20){
roll_timesrolled_timesgained_monetaryprofits[0][a]=a+1;
a++;
}
for (int i = numberofattempts; i > 0; i--) {
canIRollAgain = true;
shouldIRollAgain = true;
rolltemp = 0;
numberofrolls=0;
rolledonce=false;
while (shouldIRollAgain && canIRollAgain) {
roll = generator.nextInt(20) + 1;
roll_timesrolled_timesgained_monetaryprofits[1][roll-1]++;
rollsvals[0][numberofrolls]=roll;
if (1 <= roll && roll <= 4) {
val = 10;
}
if (5 <= roll && roll <= 8) {
val = 20;
}
if (9 <= roll && roll <= 12) {
val = 50;
}
if (13 <= roll && roll <= 16) {
val = 100;
}
if (17 <= roll && roll <= 20) {
val = 500;
}
rollsvals[1][numberofrolls]=val;
if(numberofrolls>=2){
canIRollAgain=false;
}
if (rolltemp > roll) {
canIRollAgain = false;
val = 0;
}
if(rolledonce){
if(roll>rolltemp){ //if the new roll is greater than the old roll
roll_timesrolled_timesgained_monetaryprofits[2][rolltemp-1]++; //add to the number of times gained
roll_timesrolled_timesgained_monetaryprofits[3][rolltemp-1]+=(rollsvals[1][numberofrolls]-rollsvals[1][numberofrolls-1]);//add the difference in monetary gain between the new roll and the old one
}
}
transientEvalue=(roll_timesrolled_timesgained_monetaryprofits[2][roll-1]/roll_timesrolled_timesgained_monetaryprofits[1][roll-1])*roll_timesrolled_timesgained_monetaryprofits[3][roll-1];
if(transientEvalue<val){
shouldIRollAgain=false;
}
rolltemp = roll;
numberofrolls++;
rolledonce=true;
}
endingvals[roll-1]++;
totalgained += val;
if(i%100==0){
System.out.print(((double)endingvalsdelta[0]/5)+" "+((double)endingvalsdelta[16]/5));
System.out.println();
for(int x=0; x<20;x++){
endingvalsdelta[x]=0;
}
}
if(i%20==0){
for(int y=0;y<20;y++){
endingvalsdelta[y]+=(endingvals[y]-endingvals2[y]);
}
}
if(i%10==0){
for(int x=0;x<20;x++){
endingvals2[x]=endingvals[x];
}
}
}
System.out.println(totalgained/ numberofattempts);
System.out.println(roll_timesrolled_timesgained_monetaryprofits[1][19]);
}}