Gambler Ruin ASCII Plot in Java

| * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | |* | 

I need to print something like the above ASCII graph for a problem with Gambler Ruin. Where bid and goal are taken as arguments. The most left | represents 0 dollars, right most | represents the goal, and * represents cash. My program is below:

  public class RuinPath { public static void main(String[] args) { // TODO - Your solution int stake = Integer.parseInt(args[0]); // gambler stating bankroll int goal = Integer.parseInt(args[1]); // gambler desired bankroll { int i = 0; if (i == 0) { System.out.print("|"); i++; while (i < stake) { System.out.print(" "); i++; if (i == stake) { System.out.print("*"); i++; while (i > stake && i < goal) { System.out.print(" "); i++; if (i == goal) { System.out.print("|"); i = 0; System.out.println(); { if (Math.random() < 0.5) { stake++; // win $1 } else { stake--; // lose $1 } if (stake == 1 || stake == goal - 1); break; } } } } } } } } } 

what this program prints is:

 | * | * | * | * | * | * | * | * | * | * | * | * | * | * | * 

Why is my program not working so that I can get the leftmost part | seem to represent 0 dollars all the way? I have it so I = 0; at the end of the cycle, so when it comes back, it should loop again until the bet is 1 or less than the target. Instead, he repeats the loops from the middle of the program.

+6
source share
2 answers

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) { //If you are at less than 50% of your goal then just put it all on the line //and let it ride :) onTheLine = stake; } if (Math.random() < 0.5) { return stake+onTheLine; // win $ } else { return stake-onTheLine; // lose $ } } 

What can be called as follows:

 //Gamble up to 80% of your current stake stake = gambleWithReclessAbandon(stake, goal, 0.80); 

With the same two meanings, this is what I saw on my first pass (I was close to oooh):

 | * | | * | | *| | * | | *| You Lost 
+1
source

Your logic is a bit complicated. In addition, your indentation will make debugging very difficult.

Just take it step by step:

 public class RuinPath { public static void main(String[] args) { int stake = Integer.parseInt(args[0]); // Starting bankroll int goal = Integer.parseInt(args[1]); // Desired bankroll while (stake > 0 && stake < goal) { System.out.print("|"); // Print out the spaces. Using a for loop and // printing a "*" if the counter variable // is equal to stake is good way to do it. // Flip a coin and increment/decrement the stake System.out.print("|\n"); } } } 
+1
source

Source: https://habr.com/ru/post/957002/


All Articles