Ollieboarder
Ollieboarder

Reputation: 31

Going back to previous if statement when fail if statement

I only started programming in Java 1 week ago so sorry for all the bad code. I have been recreating a "refinement system" from a game I used to play just because I thought it would be a good idea for a beginner project. This system improves gear based on a percentage chance of success. As you get a higher refinement, the percentage chance goes down.

One method of refining in my project is where every failed refinement doesn't reset your refinement level to 0, but instead decreases the refinement level by 1 every time you fail.

I have already successfully created a method that resets to 0 upon fail, but can't seem to figure out how to decrease the level by 1 upon fail.

So my question is, how can I get my "refinement level" to decrease by 1 upon fail, rather than reset to 0. Also, the console needs to return to the previous IF statement upon fail otherwise it will not work.

This was my attempt:

import java.util.Random;

public class Tisha {

    public static void main(String[] args) throws InterruptedException {

        if (new Random().nextDouble() <= 0.535) {
            System.out.println("Refine successful  -  Refine level 1");
        } else {
            System.out.println("Refine failed  -  Refine reset");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 2");
        } else {
            System.out.println("Refine failed  -  Refine level 1");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 3");
        } else {
            System.out.println("Refine failed  -  Refine level 2");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 4");
        } else {
            System.out.println("Refine failed  -  Refine level 3");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 5");
        } else {
            System.out.println("Refine failed  -  Refine level 4");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 6");
        } else {
            System.out.println("Refine failed  -  Refine level 5");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 7");
        } else {
            System.out.println("Refine failed  -  Refine level 6");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.335) {
            System.out.println("Refine successful  -  Refine level 8");
        } else {
            System.out.println("Refine failed  -  Refine level 7");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.285) {
            System.out.println("Refine successful  -  Refine level 9");
        } else {
            System.out.println("Refine failed  -  Refine level 8");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.235) {
            System.out.println("Refine successful  -  Refine level 10");
        } else {
            System.out.println("Refine failed  -  Refine level 9");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.155) {
            System.out.println("Refine successful  -  Refine level 11");
        } else {
            System.out.println("Refine failed  -  Refine level 10");
        }

        Thread.sleep(250);

        if (new Random().nextDouble() <= 0.085) {
            System.out.println("Refine successful  -  Refine level 12");
        } else {
            System.out.println("Refine failed  -  Refine level 11");
        }
    }

}

Output:

Refine successful  -  Refine level 1
Refine failed  -  Refine level 1
Refine failed  -  Refine level 2
Refine failed  -  Refine level 3
Refine failed  -  Refine level 4
Refine successful  -  Refine level 6
Refine failed  -  Refine level 6
Refine failed  -  Refine level 7
Refine successful  -  Refine level 9
Refine failed  -  Refine level 9
Refine successful  -  Refine level 11
Refine successful  -  Refine level 12

Process finished with exit code 0

Sorry for the messy code, I warned you.

Upvotes: 3

Views: 182

Answers (4)

Oneiros
Oneiros

Reputation: 4378

My try on this problem, feel free to ask if anything is not clear. I used a do-while loop to solve your "go-back" problem and some variables to make the program dynamic

class Tisha {

    // An integer field to store the current level of refinement.
    int level = 0;

    // An array of probabilities for each level of refinement.
    double[] probs = new double[]{
        0.535, 0.335, 0.335, 0.335, 0.335,
        0.335, 0.335, 0.335, 0.285, 0.235,
        0.155, 0.085
    };

    // The random number generator
    Random random = new Random();

    // This function tries to perform a refinement
    public void tryRefinement() {
        if (random.nextDouble() <= probs[level]) {
            level++;
            System.out.println("Refine successful  -  Refine level " + level);
        } else {
            level--;
            if (level >= 0)
                System.out.println("Refine failed  -  Refine level " + level);
            else
                System.out.println("Refine failed  -  Refine reset");
        }
    }

    // The main method
    public static void main(String[] args) throws InterruptedException {

        // Create a 'Tisha' object (are you familiar with OOP?)
        Tisha tisha = new Tisha();

        // Keep refininig until the level reaches 0
        do {
            tisha.tryRefinement();
            Thread.sleep(250);
        } while (tisha.level > 0);
    }
}

Upvotes: 0

laune
laune

Reputation: 31300

You might want to put this into a class so that the object contains the current level and "knows" against which threshold to check the next random to determine success or failure. Note the checks to ensure that the array grade isn't accessed beyond its limits.

public class Refinement {
    private static final double[] grade =
        { 0.535, 0.335, 0.335, 0.335,
        0.335, 0.335, 0.335, 0.335,
        0.285, 0.235, 0.155, 0.085 };
    private int level = 0;
    private Random rand = new Random();

    public int getLevel(){
        return level;
    }

    public void step(){
        int testlevel = level == grade.length ? grade.length - 1 : level;
        if( rand.nextDouble() <= grade[level] ){
            if( ++level >= grade.length ) level = grade.length;
        } else {
            if( --level < 0 ) level = 0;
        }
    }

    public static void main( String[] args ){
        Refinement refinement = new Refinement();
        for( int i = 1; i <= 20; ++i ){
            refinement.step();
            System.out.println( "level: " + refinement.getLevel() );
        }
    }
}

Note that the probabilities are such that it is rather difficult to reach higher levels.

Upvotes: 0

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

I'm not a very experienced developer so my suggestion might not be the best of all and nor the most efficient one.

I thought of creating an infinite loop. Inside of that loop I would put a switch statement which checks the value of int x that is firstly initialized with 0.

You could create a refinement variable outside of the switch statement and put all the next steps of your program in next cases of swich statement. Inside of each case you could increase or decrease the value of refinement and also increment or decrement the value that is checked in switch statement (value of x) . When refinement is successful you change the value of refinement and increment the value of x so in next loop the next case is being processed. If refinement was failed, the value of x is decremented and in next while loop the previous case is being processed.

I want to repeat that as I am novice, this can be not the best solutions. If it's not maybe it can help you find some new ideas.

Upvotes: 0

rememberthetitans
rememberthetitans

Reputation: 96

Set your refinement level as a variable, and increase or decrease accordingly.

int refinementLevel = 0;

if (new Random().nextDouble() <= 0.155) {
  refinementLevel++;
  System.out.println("Refine successful  -  Refine level "+refinementLevel);
} else {
  refinementLevel--;
  System.out.println("Refine failed  -  Refine level "+refinementLevel);
}

Upvotes: 4

Related Questions