Reputation: 1235
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
Reputation: 2066
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:
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);
}
}
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();
}
}
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);
}
}
public class Roller {
private Random random = new Random();
public Dice roll()
{
return new Dice(random.nextInt(6) + 1);
}
}
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