Here is a broken solution that is easier to talk about:
import java.io.PrintStream; public class SO { private static int gambleWithCaution(int stake) { if (Math.random() < 0.5) { return stake+1; // win $1 } else { return stake-1; // lose $1 } } private static void renderStanding(int stake, int goal) { System.out.print('|'); for(int dollar = 1; dollar< goal; dollar++) { if(dollar == stake) { System.out.print('*'); } else { System.out.print(' '); } } System.out.println('|'); } public static void main(String ... args) { int stake = Integer.parseInt(args[0]); // gambler stating bankroll int goal = Integer.parseInt(args[1]); // gambler desired bankroll while(stake > 0 && stake < goal) { renderStanding(stake, goal); stake = gambleWithCaution(stake); } System.out.println((stake > goal) ? "You Won!" : "You Lost"); } }
With values โโof 3 and 5 you will get this result:
| * | | * | |* | | * | | * | | *| | * | | *| You Won!
Now that it's shared, you can have some fun with it, like creating such a function:
private static int gambleWithReclessAbandon(int stake, int goal, double abandon) { int onTheLine = (int)(Math.random() * (int)(stake * abandon)); if(stake < (0.5)*goal) {
What can be called as follows:
With the same two meanings, this is what I saw on my first pass (I was close to oooh):
| * | | * | | *| | * | | *| You Lost
source share