Skip to content

Commit

Permalink
replace magic numbers with variables
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeloie committed Jul 21, 2023
1 parent 0bdbc33 commit e49753e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/hexlet/code/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Engine {
public static Scanner scan = new Scanner(System.in);
public static Random rnd = new Random();
public static String userName = "";
public static int numberOfRounds = 3;
public static int supremum = 100;

public static void greetingUser() {
System.out.println("Welcome to the Brain Games!");
Expand Down
19 changes: 9 additions & 10 deletions app/src/main/java/hexlet/code/games/Calc.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Calc {
public static void playCalc() {
Engine.greetingUser();
Engine.printGameRules("Calc");
for (int i = 0; i < 3; i++) {
for (int i = 0; i < Engine.numberOfRounds; i++) {
String correctAnswer = askCalcQuestion();
String userAnswer = scan.nextLine();
checkAnswer(userAnswer, correctAnswer);
Expand All @@ -24,17 +24,16 @@ public static void playCalc() {
public static String askCalcQuestion() {
var signsArray = Arrays.asList("+", "-", "*");
var operationalSign = signsArray.get(rnd.nextInt(signsArray.size()));
int firstNumber = rnd.nextInt(100);
int secondNumber = rnd.nextInt(100);
int firstNumber = rnd.nextInt(Engine.supremum);
int secondNumber = rnd.nextInt(Engine.supremum);
int calcAnswer = 0;

switch (operationalSign) {
case ("+") -> calcAnswer = firstNumber + secondNumber;
case ("-") -> calcAnswer = firstNumber - secondNumber;
case ("*") -> calcAnswer = firstNumber * secondNumber;
default -> {
}
}
calcAnswer = switch (operationalSign) {
case ("+") -> firstNumber + secondNumber;
case ("-") -> firstNumber - secondNumber;
case ("*") -> firstNumber * secondNumber;
default -> throw new IllegalStateException("Unexpected value: " + operationalSign);
};
System.out.printf("Question: %d %s %d%n", firstNumber, operationalSign, secondNumber);
System.out.print("Your answer: ");
return String.valueOf(calcAnswer);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/hexlet/code/games/Even.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void playEven() {

Engine.greetingUser();
Engine.printGameRules("Even");
for (int i = 0; i < 3; i++) {
for (int i = 0; i < Engine.numberOfRounds; i++) {
String correctAnswer = askEvenQuestion();
String userAnswer = scan.nextLine();
checkAnswer(userAnswer, correctAnswer);
Expand All @@ -24,7 +24,7 @@ public static void playEven() {

public static String askEvenQuestion() {

int x = rnd.nextInt(100);
int x = rnd.nextInt(Engine.supremum);
boolean evenCheck = (x % 2 == 0);
System.out.println("Question: " + x);
System.out.print("Your answer: ");
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/hexlet/code/games/GCD.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void playGCD() {

Engine.greetingUser();
Engine.printGameRules("GCD");
for (int i = 0; i < 3; i++) {
for (int i = 0; i < Engine.numberOfRounds; i++) {
String correctAnswer = askGCDQuestion();
String userAnswer = scan.nextLine();
checkAnswer(userAnswer, correctAnswer);
Expand All @@ -22,8 +22,8 @@ public static void playGCD() {
}

public static String askGCDQuestion() {
int a = rnd.nextInt(1, 100);
int b = rnd.nextInt(1, 100);
int a = rnd.nextInt(1, Engine.supremum);
int b = rnd.nextInt(1, Engine.supremum);
System.out.printf("Question: %d %d%nYour answer: ", a, b);
return computeGCD(a, b);
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/hexlet/code/games/Prime.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Prime {
public static void playPrime() {
Engine.greetingUser();
Engine.printGameRules("Prime");
for (int i = 0; i < 3; i++) {
for (int i = 0; i < Engine.numberOfRounds; i++) {
String correctAnswer = askPrimeQuestion();
String userAnswer = scan.nextLine();
checkAnswer(userAnswer, correctAnswer);
Expand All @@ -21,7 +21,7 @@ public static void playPrime() {
}

public static String askPrimeQuestion() {
int hiddenNumber = rnd.nextInt(100);
int hiddenNumber = rnd.nextInt(Engine.supremum);
System.out.printf("Question: %d%nYour answer: ", hiddenNumber);
return isPrime(hiddenNumber) ? "yes" : "no";
}
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/hexlet/code/games/Progression.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Progression {
public static void playProgression() {
Engine.greetingUser();
Engine.printGameRules("Progression");
for (int i = 0; i < 3; i++) {
for (int i = 0; i < Engine.numberOfRounds; i++) {
String correctAnswer = askProgQuestion();
String userAnswer = scan.nextLine();
checkAnswer(userAnswer, correctAnswer);
Expand All @@ -23,9 +23,13 @@ public static void playProgression() {

public static String askProgQuestion() {
int sizeSequence = 10;
int incMin = 1;
int incMax = 5;
int beginMin = 10;
int beginMax = 30;
int gap = rnd.nextInt(0, sizeSequence - 1);
int increment = rnd.nextInt(1, 5);
int begin = rnd.nextInt(10, 30);
int increment = rnd.nextInt(incMin, incMax);
int begin = rnd.nextInt(beginMin, beginMax);
int[] progArray = new int[sizeSequence];
System.out.print("Question:");
for (int i = 0; i < sizeSequence; i++) {
Expand Down

0 comments on commit e49753e

Please sign in to comment.