0

I am trying to make the user go back to the position he was on if he gets higher than 100.

So if he was on 97 and rolls a 5 on Dice1 and becomes 102 I want the variable position1 to go back to 97.

I am still new so it might just be a stupid logic problem also my variable names are not industry standard so im still working on this.

Scanner Scan = new Scanner(System.in);
Random random = new Random();
int Position1 = 0;
// int position2 = 0;

System.out.println("Type Start");
String Input = Scan.next();

if (Input.equals("start")) {

    while (Position1 != 100) {

        int Dice1 = random.nextInt(1, 7);

        if (Dice1 == 6) {
            Position1 = Position1 + Dice1;
            System.out.println(Position1);
            System.out.println("player 1 turn ");
            Input = Scan.next();

        } else if (Dice1 == 5) {
            Position1 = Position1 + Dice1;
            System.out.println(Position1);
            System.out.println("player l turn");
            Input = Scan.next();

        } else if (Dice1 == 4) {
            Position1 = Position1 + Dice1;
            System.out.println(Position1);
            System.out.println("player 1 turn ");
            Input = Scan.next();
        } else if (Dice1 == 5) {
            Position1 = Position1 + Dice1;
            System.out.println(Position1);
            System.out.println("player l turn");
            Input = Scan.next();

        } else if (Dice1 == 4) {
            Position1 = Position1 + Dice1;
            System.out.println(Position1);
            System.out.println("player 1 turn ");
            Input = Scan.next();

        } else if (Dice1 == 3) {
            Position1 = Position1 + Dice1;
            System.out.println(Position1);
            System.out.println("Player 1 turn");
            Input = Scan.next();

        } else if (Dice1 == 2) {
            Position1 = Position1 + Dice1;
            System.out.println(Position1);
            System.out.println("player 1 turn");
            Input = Scan.next();

        } else if (Dice1 == 1) {
            Position1 = Position1 + Dice1;
            System.out.println(Position1);
            System.out.println("player 1 turn ");
            Input = Scan.next();
        }
        if (Position1 > 100) {
            Position1 = Position1 - Dice1;
        } else if (Position1 == 100) {
            System.out.println("U Win");

        }
    }
}
Diego Borba
  • 1,282
  • 8
  • 22
  • 1
    Hello, welcome to Stack Overflow! Please, [read this](https://stackoverflow.com/help/minimal-reproducible-example) and then edit your question. – Diego Borba Aug 02 '23 at 18:44
  • One easy way is to keep a "stack" of all the moves. Push each position on the stack. Then, when you need to back up, you just pop the last position. That's the way "undo" is implemented. – Tim Roberts Aug 02 '23 at 18:50
  • Sorry, can you further explain what a "Stack" is. I'm still a beginner – Kareem Nabil Aug 02 '23 at 18:53
  • 1
    Why are you always scanning input in the loop if it was not even used? And why are you checking the dice if you always do the same thing? – Diego Borba Aug 02 '23 at 19:08
  • @DiegoBorba I am scanning the input so that the dice wouldn't roll again unless the user inputs something. And Thanks for pointing out that checking the dice is basically useless – Kareem Nabil Aug 02 '23 at 19:22
  • So the dice must roll until the position is exactly 100? – Diego Borba Aug 02 '23 at 19:25
  • YES and if it goes above 100 it it should go back to the position before the last dice throw (e.g. If I'm on 98 and I roll a 4 and go to 102 I must return to 98 and keep rolling till I reach 100 exactly) – Kareem Nabil Aug 02 '23 at 19:29

2 Answers2

1

You said:

I am still new...

So I'll take advantage of the answer to show you some good programming practices:

  1. Variables always begins with lowercase.
  2. Always close Scanner! Will previne a memory leak in your program.
  3. You can use Switch Statement instead of many if else cases.

Considering what we said:

Why are you always scanning input in the loop if it was not even used? And why are you checking the dice if you always do the same thing? - Diego Borba

@DiegoBorba I am scanning the input so that the dice wouldn't roll again unless the user inputs something. And Thanks for pointing out that checking the dice is basically useless - Kareem Nabil

You don't need to check the dice, so both the if/else and the switch are expendable.

So, you can do this way:

// Variables begins with lowercase
Scanner scan = new Scanner(System.in);
Random random = new Random();
int position1 = 0;

// Start input
System.out.println("Type Start");
String input = scan.nextLine();
if (input.equalsIgnoreCase("start")) {
    //Position validation
    while (position1 != 100) {
        // Display position
        System.out.println(position1);
        
        // Print player turn
        System.out.println("player 1 turn ");
        
        // It is not necessary to assign the value to the variable
        scan.nextLine();
        
        // Roll the dice
        int dice1 = random.nextInt(6) + 1;
        
        // Verify dice value
        if(dice1 <= 100 - position1) {
            // You can use "+=" operator
            position1+= dice1;
        }
    }
    
    // When the loop ends, it means that position is equals 100
    System.out.println("U Win");
}

// Always close Scanner!
scan.close();

Notice that:

Diego Borba
  • 1,282
  • 8
  • 22
0

It looks as if you are planning to do this for multiple players, so probably you should refactor the whole code into something reusable, where you provide the result of the dice-throw from outside.:

/**
 * Handles a players score for one game.
 * Create a new instance per game for every player.
 */
class PlayerScore {
  private int score = 100;

  /** 
   * Returns the players current score.
   * @return the players current score
   */
  public int getScore() {
    return score;
  }

  /**
   * Handles the score after player rolled the dice.
   * @param dice the value of the thrown dice
   * @return {@code true} if the player can continue to play, {@code false} otherwise.
   */
  public boolean rollDice(int dice) {
    if ((dice < 1) || (dice > 6)) {
      throw new IllegalArgumentException("invalid dice value: " + dice);
    } 
    if (score + dice > 100) {
      return false;
    }
    score += dice;
    return (score < 100); // don't let the player continue if he reached the goal
  }

  /**
   * Checks if player has won. 
   * A {@code false} can also mean that the game is still going on.
   * @return {@code true} if the player has won, {@code false} otherwise.
   */
  public boolean isWinner() {
    return score == 100;
  }
}

With this you can create a new PlayerScore instance for each player and game(!), perform the dice rolls with the call and check if the player has won or may continue otherwise. I leave this as exercise to you ;-)

cyberbrain
  • 3,433
  • 1
  • 12
  • 22