Java how to start a game turn class for stored turn data

Question

I'm making a dice game and need to know how I might set up a class that stores the dice roll numbers from the previous turn such that an if statement can display something. I.E. if computerDie == 1 && userDie == 4 occurs twice in a row something happens. Any suggestions?

public static void main( String [] args ){
    Random random = new Random();
    int totalNumberOfTurns = 1;
    int computerDie;
    int userDie;
    int computerScore = 0;
    int userScore = 0;


    for( int turn = 1; turn <= totalNumberOfTurns; turn++ ) {
        computerDie = random.nextInt( 6 ) + 1;
        userDie = random.nextInt( 6 ) + 1;

        if () {
}
}
}

Show source
| java   | class   | dice   2017-01-06 00:01 2 Answers

Answers ( 2 )

  1. 2017-01-06 00:01

    You could use an ArrayList and save each roll for each player if you need to use them later.

    Or if you don't care to use them later, you can have 2 auxiliar variables like lastUserDie and lastComputerDie which are updated after you check if they're the same.

    In this example I used a random between 1-3 to make it more probable to get the same roll, just change it to 6 again

    import java.util.Random;
    
    public class SaveLastDiceRoll {
        public static void main(String[] args) {
            Random random = new Random();
    
            int totalNumberOfTurns = 10;
    
            int computerDie = 0;
            int userDie = 0;
            int computerScore = 0;
            int userScore = 0;
            int lastUserDie = 0;
            int lastComputerDie = 0;
    
            for (int i = 0; i < totalNumberOfTurns; i++) {
                computerDie = random.nextInt(3) + 1;
                userDie = random.nextInt(3) + 1;
                System.out.println("UserRoll: " + userDie);
                System.out.println("ComputerRoll: " + computerDie);
                System.out.println("------------");
                if (sameRoll(lastUserDie, userDie, lastComputerDie, computerDie)) {
                    System.out.println("GAME OVER, SAME ROLL TWICE");
                    break;
                }
                lastComputerDie = computerDie;
                lastUserDie = userDie;
            }
        }
    
        public static boolean sameRoll(int lastUserDie, int currentUserDie, int lastComputerDie, int currentComputerDie) {
            if (lastUserDie == currentUserDie && lastComputerDie == currentComputerDie) {
                return true;
            }
            return false;
        }
    }
    

    Some sample outputs:

    UserRoll: 1
    ComputerRoll: 1
    ------------
    UserRoll: 1
    ComputerRoll: 1
    ------------
    GAME OVER, SAME ROLL TWICE
    

    And:

    UserRoll: 3
    ComputerRoll: 1
    ------------
    UserRoll: 2
    ComputerRoll: 3
    ------------
    UserRoll: 2
    ComputerRoll: 2
    ------------
    UserRoll: 2
    ComputerRoll: 1
    ------------
    UserRoll: 2
    ComputerRoll: 1
    ------------
    GAME OVER, SAME ROLL TWICE
    
  2. 2017-01-06 00:01

    Looks like you need an OOP overhaul.

    For object oriented programming, you want to encapsulate information and related functionality in a reusable fashion. For your scenario, you have the user and the computer, but both of those sets of information can be thought of as a player (and associated information). That suggests creating a Player class to store both the score and the rolls that have been made for that player:

    public class Player {
        private int score;
        private List<Integer> diceRolls;
    }
    

    From there, we add methods to class Player that allow for the mutation and retrieval of the stored information.

    public class Player {
        private int score;
        // Rolls are stored in chronological order. Last element is most recent.
        private List<Integer> diceRolls;
    
        public Player() {
            score = 0;
            diceRolls = new ArrayList<Integer>();
        }
    
        public int getScore() {
            return score;
        }
    
        public int addRoll(int roll) {
            diceRolls.add(roll);
        }
    
        public int getMostRecentRoll() {
            return diceRolls.get(diceRolls.size() - 1);
        }
    
        public boolean mostRecentTwoRollsMatch() {
            return diceRolls.size() >= 2 && 
                diceRolls.get(diceRolls.size() - 1).equals(diceRolls.get(diceRolls.size() - 2));
    
        public List<Integer> getAllRolls() {
            return new ArrayList<>(diceRolls); // copied for safety
        }
    }
    

    From there, coding your logic in the main method becomes much simpler and easier to understand.

    public static void main( String [] args ){
        Random random = new Random();
        int totalNumberOfTurns = 10;
        Player user = new Player();
        Player computer = new Player();
    
        for(int turn = 0; turn < totalNumberOfTurns; turn++ ) {
           user.addRoll(random.nextInt(6) + 1);
           computer.addRoll(random.nextInt(6) + 1);
    
           if (user.mostRecentTwoRollsMatch()) {
               System.out.println("User rolled " + user.getMostRecentRoll() + " twice!");
           }
    
           // etc etc...
        }
    }
    
◀ Go back