patternjavaMinor
Craps game: breaking a single method to more than one
Viewed 0 times
crapsmethodthanmorebreakingonegamesingle
Problem
Below is a long messy one method code for a craps game. The user can play the game multiple times and has the option to quit when ever they want.
```
import java.util.Random;
import java.util.Scanner;
public class craps {
public static void main(String args[]) {
System.out.print("How much money do you have total? ");
Scanner keyboard4 = new Scanner(System.in);
double total = keyboard4.nextDouble();
boolean stop = false;
while (stop == false) {
int n = 0;
System.out.print("Place your bets! $");
Scanner keyboard = new Scanner(System.in);
double bet = keyboard.nextDouble();
double bet1 = 0;
Random generator = new Random();
int dies = generator.nextInt(6) + 1;
int dies2 = generator.nextInt(6) + 1;
int dietotal = dies + dies2;
System.out.println("Press 'Enter' to roll");
Scanner keyboard3 = new Scanner(System.in);
String q2 = keyboard3.nextLine();
System.out.println("Your first roll was " + dietotal);
if (dietotal == 7 || dietotal == 11) {
System.out.println("You win!");
System.out.println("Amount won: $" + bet);
total = total + bet;
System.out.println("Your new total is: " + total);
System.out.print("\nDo you want to play again?: ");
Scanner keyboard2 = new Scanner(System.in);
String cont = keyboard2.nextLine();
if (cont.equals("") || cont.equals("Y") || cont.equals("y") || cont.equals("yes") || cont.equals("YES") || cont.equals("Yes")) {
stop = false;
} else {
stop = true;
}
- How could you break this code down into different types of methods?
- How many methods do you think the code can be broken down to?
- What types of methods should be used?
```
import java.util.Random;
import java.util.Scanner;
public class craps {
public static void main(String args[]) {
System.out.print("How much money do you have total? ");
Scanner keyboard4 = new Scanner(System.in);
double total = keyboard4.nextDouble();
boolean stop = false;
while (stop == false) {
int n = 0;
System.out.print("Place your bets! $");
Scanner keyboard = new Scanner(System.in);
double bet = keyboard.nextDouble();
double bet1 = 0;
Random generator = new Random();
int dies = generator.nextInt(6) + 1;
int dies2 = generator.nextInt(6) + 1;
int dietotal = dies + dies2;
System.out.println("Press 'Enter' to roll");
Scanner keyboard3 = new Scanner(System.in);
String q2 = keyboard3.nextLine();
System.out.println("Your first roll was " + dietotal);
if (dietotal == 7 || dietotal == 11) {
System.out.println("You win!");
System.out.println("Amount won: $" + bet);
total = total + bet;
System.out.println("Your new total is: " + total);
System.out.print("\nDo you want to play again?: ");
Scanner keyboard2 = new Scanner(System.in);
String cont = keyboard2.nextLine();
if (cont.equals("") || cont.equals("Y") || cont.equals("y") || cont.equals("yes") || cont.equals("YES") || cont.equals("Yes")) {
stop = false;
} else {
stop = true;
}
Solution
How could you break this code down into different types of methods?
Never use Copy&Paste for code. (You can use Cut&Paste.) If you want to Copy&Paste a chunk of code, instead put that chunk in a method and call it from multiple locations. Do this to already Copy&Pasted code also.
chunk is an obvious target for converting to a method. It could be:
Another such candidate is
Also note
So these chunks become:
All repeated chunks MUST be extracted to methods.
Any chunk of code that does something SHOULD be extracted to method, even if it is not repeated.
Consider:
This should be extracted to:
How many methods do you think the code can be broken down to?
The ideal is Extract till you Drop
Never use Copy&Paste for code. (You can use Cut&Paste.) If you want to Copy&Paste a chunk of code, instead put that chunk in a method and call it from multiple locations. Do this to already Copy&Pasted code also.
System.out.print("\nDo you want to play again?: ");
Scanner keyboard2 = new Scanner(System.in);
String cont = keyboard2.nextLine();
if (cont.equals("") || cont.equals("Y") || cont.equals("y") || cont.equals("yes") || cont.equals("YES") || cont.equals("Yes")) {
stop = false;
} else {
stop = true;
}chunk is an obvious target for converting to a method. It could be:
private static final Set affirmativeAnswers = new HashSet(
Arrays.asList("Y", "y", "yes", "YES", "Yes"));
private static boolean userWantsToPlayAgain(Scanner scanner) {
System.out.print("Do you want to play again? [y/n]: ");
return affirmativeAnswers.contains(scanner.nextLine());
}Another such candidate is
int dies = generator.nextInt(6) + 1;
int dies2 = generator.nextInt(6) + 1;
int dietotal = dies + dies2;Also note
generator.nextInt(6) + 1 chunks are repeated within the repeated chunk.So these chunks become:
private static int getDieTotal(Random random) {
return rollDie(random) + rollDie(random);
}
private static int rollDie(Random random) {
return random.nextInt(6) + 1;
}All repeated chunks MUST be extracted to methods.
Any chunk of code that does something SHOULD be extracted to method, even if it is not repeated.
Consider:
if (dietotal == 4 || dietotal == 10) {
bet1 = bet * 2;
}
if (dietotal == 5 || dietotal == 9) {
bet1 = bet / 2 * 3;
}
if (dietotal == 6 || dietotal == 8) {
bet1 = bet / 5 * 6;
}This should be extracted to:
private static double getBet1(double bet, int dietotal) {
return bet * getScale(dietotal);
}
private static double getScale(int dietotal) {
if (dietotal == 4 || dietotal == 10) return 2.0;
if (dietotal == 5 || dietotal == 9) return 1.5;
if (dietotal == 6 || dietotal == 8) return 1.2;
return 0;
}How many methods do you think the code can be broken down to?
The ideal is Extract till you Drop
Code Snippets
System.out.print("\nDo you want to play again?: ");
Scanner keyboard2 = new Scanner(System.in);
String cont = keyboard2.nextLine();
if (cont.equals("") || cont.equals("Y") || cont.equals("y") || cont.equals("yes") || cont.equals("YES") || cont.equals("Yes")) {
stop = false;
} else {
stop = true;
}private static final Set<String> affirmativeAnswers = new HashSet<String>(
Arrays.asList("Y", "y", "yes", "YES", "Yes"));
private static boolean userWantsToPlayAgain(Scanner scanner) {
System.out.print("Do you want to play again? [y/n]: ");
return affirmativeAnswers.contains(scanner.nextLine());
}int dies = generator.nextInt(6) + 1;
int dies2 = generator.nextInt(6) + 1;
int dietotal = dies + dies2;private static int getDieTotal(Random random) {
return rollDie(random) + rollDie(random);
}
private static int rollDie(Random random) {
return random.nextInt(6) + 1;
}if (dietotal == 4 || dietotal == 10) {
bet1 = bet * 2;
}
if (dietotal == 5 || dietotal == 9) {
bet1 = bet / 2 * 3;
}
if (dietotal == 6 || dietotal == 8) {
bet1 = bet / 5 * 6;
}Context
StackExchange Code Review Q#69649, answer score: 4
Revisions (0)
No revisions yet.