Skip to content

Commit 0437c8f

Browse files
изменения на основании 3 ревью
1 parent 31ffead commit 0437c8f

File tree

7 files changed

+42
-28
lines changed

7 files changed

+42
-28
lines changed

app/src/main/java/hexlet/code/Engine.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import java.util.Scanner;
44

55
public class Engine {
6-
76
public static final int ROUNDS = 3;
8-
//границы случайных чисел не были обозначены сделал от 1 до 100. требуется уточнение у аналитика
97

108
public static void startGame(String optionDescription, String[][] questionsAndCorrectAnswers) {
119
String userName = Cli.greet();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package hexlet.code;
2+
3+
public class Utils {
4+
//границы случайных чисел не были обозначены сделал от 1 до 100. требуется уточнение у аналитика
5+
public static final int MIN_NUMBER = 1;
6+
public static final int MAX_NUMBER = 100;
7+
8+
public static int generateNumber(int min, int max) {
9+
return (int) Math.floor(Math.random() * (max - min + 1)) + min;
10+
}
11+
12+
public static int generateNumber() {
13+
return generateNumber(MIN_NUMBER, MAX_NUMBER);
14+
}
15+
}

app/src/main/java/hexlet/code/games/Calc.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package hexlet.code.games;
22

33
import hexlet.code.Engine;
4+
import hexlet.code.Utils;
45

56
public class Calc {
6-
public static final int MAX_NUMBER = 100;
7-
87
public static void play() {
98
String optionDescription = "What is the result of the expression?";
109
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1110

1211
for (int i = 0; i < Engine.ROUNDS; i++) {
13-
int firstValue = (int) (Math.random() * MAX_NUMBER) + 1;
14-
int secondValue = (int) (Math.random() * MAX_NUMBER) + 1;
12+
int firstValue = Utils.generateNumber();
13+
int secondValue = Utils.generateNumber();
1514
char[] signs = {'+', '-', '*'};
16-
char randomSign = signs[(int) (Math.random() * signs.length)];
15+
char randomSign = signs[Utils.generateNumber(0, signs.length - 1)];
1716

1817
String question = firstValue + " " + randomSign + " " + secondValue;
1918
String correctAnswer = calculateForCorrectAnswer(firstValue, secondValue, randomSign);

app/src/main/java/hexlet/code/games/Even.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package hexlet.code.games;
22

33
import hexlet.code.Engine;
4+
import hexlet.code.Utils;
45

56
public class Even {
6-
public static final int MAX_NUMBER = 100;
7-
87
public static void play() {
98
String optionDescription = "Answer 'yes' if the number is even, otherwise answer 'no'.";
109
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1110

1211
for (int i = 0; i < Engine.ROUNDS; i++) {
13-
int random = (int) (Math.random() * MAX_NUMBER) + 1;
12+
int random = Utils.generateNumber();
1413
String question = String.valueOf(random);
1514
boolean isEven = random % 2 == 0;
1615
String correctAnswer = isEven ? "yes" : "no";

app/src/main/java/hexlet/code/games/Gcd.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
package hexlet.code.games;
22

33
import hexlet.code.Engine;
4+
import hexlet.code.Utils;
45

56
public class Gcd {
6-
public static final int MAX_NUMBER = 100;
7-
87
public static void play() {
98
String optionDescription = "Find the greatest common divisor of given numbers.";
109
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1110

1211
for (int i = 0; i < Engine.ROUNDS; i++) {
13-
int firstValue = (int) (Math.random() * MAX_NUMBER) + 1;
14-
int secondValue = (int) (Math.random() * MAX_NUMBER) + 1;
12+
int firstValue = Utils.generateNumber();
13+
int secondValue = Utils.generateNumber();
1514

1615
String question = firstValue + " " + secondValue;
17-
String correctAnswer = String.valueOf(calculateForCorrectAnswer(firstValue, secondValue));
16+
String correctAnswer = String.valueOf(gcd(firstValue, secondValue));
1817

1918
questionsAndCorrectAnswers[i][0] = question;
2019
questionsAndCorrectAnswers[i][1] = correctAnswer;
2120
}
2221
Engine.startGame(optionDescription, questionsAndCorrectAnswers);
2322
}
2423

25-
public static int calculateForCorrectAnswer(int firstValue, int secondValue) {
24+
public static int gcd(int firstValue, int secondValue) {
2625
while (secondValue != 0) {
2726
int tempAnswer = secondValue;
2827
secondValue = firstValue % secondValue;
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package hexlet.code.games;
22

33
import hexlet.code.Engine;
4+
import hexlet.code.Utils;
45

56
public class Prime {
6-
public static final int MAX_NUMBER = 100;
7-
87
public static void play() {
98
String optionDescription = "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
109
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1110

1211
for (int i = 0; i < Engine.ROUNDS; i++) {
13-
int random = (int) (Math.random() * MAX_NUMBER) + 1;
12+
int random = Utils.generateNumber();
1413

1514
String question = String.valueOf(random);
16-
String correctAnswer = logicPrime(random);
15+
String correctAnswer;
16+
if (isPrime(random)) {
17+
correctAnswer = "yes";
18+
} else {
19+
correctAnswer = "no";
20+
}
1721

1822
questionsAndCorrectAnswers[i][0] = question;
1923
questionsAndCorrectAnswers[i][1] = correctAnswer;
@@ -22,15 +26,15 @@ public static void play() {
2226
Engine.startGame(optionDescription, questionsAndCorrectAnswers);
2327
}
2428

25-
public static String logicPrime(int random) {
29+
public static boolean isPrime(int random) {
2630
if (random < 2) {
27-
return "no";
31+
return false;
2832
}
2933
for (int i = 2; i <= Math.sqrt(random); i++) {
3034
if (random % i == 0) {
31-
return "no";
35+
return false;
3236
}
3337
}
34-
return "yes";
38+
return true;
3539
}
3640
}

app/src/main/java/hexlet/code/games/Progression.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package hexlet.code.games;
22

33
import hexlet.code.Engine;
4+
import hexlet.code.Utils;
45

56
public class Progression {
6-
public static final int MAX_NUMBER = 100;
77
private static final int STEP_COUNT = 10;
88
private static final int VALUE_COUNT = 10;
99

@@ -12,9 +12,9 @@ public static void play() {
1212
String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2];
1313

1414
for (int i = 0; i < Engine.ROUNDS; i++) {
15-
int start = (int) (Math.random() * MAX_NUMBER) + 1;
16-
int step = (int) (Math.random() * STEP_COUNT) + 1;
17-
int hideIndex = (int) (Math.random() * VALUE_COUNT);
15+
int start = Utils.generateNumber();
16+
int step = Utils.generateNumber(Utils.MIN_NUMBER, STEP_COUNT);
17+
int hideIndex = Utils.generateNumber(Utils.MIN_NUMBER, VALUE_COUNT);
1818

1919
String[] sequence = addSequence(start, step, VALUE_COUNT);
2020
String correctAnswer = sequence[hideIndex];

0 commit comments

Comments
 (0)