java - How to find indicies of longest run of consecutive values in ordered list? -
i'm building ai player yahtzee game , i'm trying @ moment have evaluate 5 dice given (in sorted arraylist), decide probability of attaining low/high straight current position (e.g. 1 or 2 more rolls); , dice hold result. @ moment acheive probability finding longest streak of consecutive numbers , calculating probabilities there. i'm having issues telling computer dice hold. can't find start , end of streak.
my method getting longest streak this:
public class checkstraight{ private hashmap<integer, integer> diefreq = new hashmap<integer, integer>(); private arraylist<die> dice = new arraylist<die>(); //die has method "getvalue()" // returns face value , method roll() assigns random value. public checkstraight(){ for(die d : dice){ d.roll(); } for(int = 1; i<7; i++){ diefreq.put(i, 0); } buildmap(); } public void buildmap(){ for(int = 0; i<5; i++){ diefreq.put(dice.get(i).getvalue(), diefreq.get(dice.get(i).getvalue()) + 1); } } public int longeststreak(){ int count = 1; int highcount = 1; for(int = 1; i<6; i++){ if(diefreq.get(i) != 0 && diefreq.get(i+1) != 0){ count++; } else{ if(count>highcount){ highcount = count; } count = 1; } return highcount; } } obviously if you're going straight should hold 1 of each consecutive die , reroll others, can't see way find dice comprise longest streak in arraylist of die due potentially having more 1 die of same value. want pass integers new arraylist determine dice hold (1 hold 0 reroll). can of think of way can that, either within method calculating longest streak or other way?
thanks
you have these bugs in longeststreak() method
1) counts should start @ (and reset to) zero, not one.
2) loop not include 6
for(int i=1; i<6; i++) { should
for(int i=1; i<=6; i++) { strategically, correct longest streak method not problem trying solve, because 1, 2, 4, 5 yields 33% chance per reroll of getting straight, while longest streak two.
Comments
Post a Comment