Josh
Josh

Reputation: 1235

Java: Trying to take keyboard input and evaluate it in an if/else condition to set a boolean value to true or false

I'm working on building out a sample dice rolling game that rolls the dice 10 times unless interrupted by user input.

I have a while statement that runs based on the state of two boolean values, one of which is set based on the user input.

My current progress is below. Any suggestions would be a huge help!

import java.util.Scanner;
import java.swing.JOptionPane;
import java.util.Random;


public class DiceGame
{
   public static void main(String[] args)
   {


     Random rand = new Random();


      int player1Wins = 0;
      int player2Wins = 0;
      int ties = 0;
      int rollCount = 0;
      boolean rollAgain = true;

      while (rollCount < 10 && rollAgain)
      {
         int player1Dice = rand.nextInt(6) + 1;
         int player2Dice = rand.nextInt(6) + 1;

         if (player1Dice > player2Dice)
         {
            player1Wins++;
            System.out.println("Player 1 wins!!");
         } 

         else if (player2Dice > player1Dice)
         {
            player2Wins++;
            System.out.println("Player 2 wins!!");
         }

         else 
         {
            ties++;
            System.out.println("It's a tie...");
         }

         rollCount++;
         String answer;
         Scanner keyboard = new Scanner(System.in);
         System.out.println("Would you like to roll again? Press y for yes");
         answer = keyboard.nextLine();

         if (answer == "y") 
         {
            rollAgain = true;
         }

         else
         {
            rollAgain = false;
         }

      }
      System.out.println();
      System.out.println("Player 1 Total wins: " + player1Wins);
      System.out.println("Player 2 Total wins: " + player2Wins);
      System.out.println("Total Ties: " + ties);
      System.out.close();
   }
}

Upvotes: 0

Views: 214

Answers (1)

Try to break down the code based on the single responsability principle. Clean code, easy to test, and much mmore granular.

I did one quick implementation, far to be the best solution, but it is just to exemplify tsuch principle:

Dice object, but could be named as Player I think

public class Dice implements Comparable<Dice> {

    private int number;

    public Dice(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    @Override
    public int compareTo(Dice o) {
        return Integer.compare(number, o.number);
    }
}

Counter - check the rolls, show the winner and keep results

public class Counter {

    private int playerOneWins;
    private int playerTwoWins;
    private int ties;

    public void check(Dice player1, Dice player2) {

        if (player1.compareTo(player2) > 0) {
            System.out.println("Player 1 wins!!");
            playerOneWins++;
        } else if (player2.compareTo(player1) > 0) {
            System.out.println("Player 2 wins!!");
            playerTwoWins++;
        } else {
            System.out.println("It's a tie...");
            ties++;
        }
    }

    public void showResults() {
        System.out.println();
        System.out.println("Player 1 Total wins: " + playerOneWins);
        System.out.println("Player 2 Total wins: " + playerTwoWins);
        System.out.println("Total Ties: " + ties);
        System.out.close();
    }
}

Input Reader - know how to get inputs from user

public class InputReader {

    private static final String YES = "y";

    private Scanner keyboard = new Scanner(System.in);

    public boolean askUser() {

        System.out.println("Would you like to roll again? Press y for yes");

        String answer = keyboard.nextLine();

        return YES.equalsIgnoreCase(answer);
    }
}

Roller - create number / throw the dices

public class Roller {

    private Random random = new Random();

    public Dice roll()
    {
        return new Dice(random.nextInt(6) + 1);
    }
}

And, finnaly, this is the main class:

public class Game {

    private static final int MAX_ROLLS = 10;

    private Roller roller = new Roller();
    private Counter counter = new Counter();
    private InputReader inputReader = new InputReader();

    public void start() {

        int rolls = 0;
        boolean continueRolling = true;

        while (rolls <= MAX_ROLLS && continueRolling) {

            Dice player1 = roller.roll();
            Dice player2 = roller.roll();

            counter.check(player1, player2);

            rolls++;

            continueRolling = inputReader.askUser();
        }

        counter.showResults();
    }

    public static void main(String[] args) {
        new Game().start();
    }
}

I hope it helps you to understand such principle.

Upvotes: 1

Related Questions