Skip to content

Commit 85329c1

Browse files
committed
changes App.java, Calc.java, Engine.java, Even.java, Gcd.java, Prime.java, Progression.java
1 parent 85422da commit 85329c1

File tree

7 files changed

+38
-45
lines changed

7 files changed

+38
-45
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ public static void playerChoice(String choice) {
2828
Cli.greeting();
2929
break;
3030
case "2":
31-
Even.gameEven();
31+
Even.playEven();
3232
break;
3333
case "3":
34-
Calc.gameCalc();
34+
Calc.playCalc();
3535
break;
3636
case "4":
37-
Gcd.gameGcd();
37+
Gcd.playGcd();
3838
break;
3939
case "5":
40-
Progression.gameProgression();
40+
Progression.playProgression();
4141
break;
4242
case "6":
43-
Prime.gamePrime();
43+
Prime.playPrime();
4444
break;
4545
case "0":
4646
break;

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

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

33
import java.util.Scanner;
4+
import java.util.function.Supplier;
45

56
public class Engine {
67
private static final int COUNT_OF_ROUNDS = 3;
78

8-
public static int getCountOfRounds() {
9-
return COUNT_OF_ROUNDS;
10-
}
11-
129
public static void gameEngine(String exercise, String[][] questionAndAnswer) {
1310
Scanner sc = new Scanner(System.in);
1411
System.out.println("Welcome to the Brain Games!");
@@ -36,4 +33,11 @@ public static void gameEngine(String exercise, String[][] questionAndAnswer) {
3633
System.out.println("Congratulations, " + name + "!");
3734
}
3835
}
36+
public static void runGame(String exercise, Supplier<String[]> questionAndAnswerSupplier) {
37+
String[][] questionAndAnswer = new String[COUNT_OF_ROUNDS][2];
38+
for (int i = 0; i < COUNT_OF_ROUNDS; i++) {
39+
questionAndAnswer[i] = questionAndAnswerSupplier.get();
40+
}
41+
gameEngine(exercise, questionAndAnswer);
42+
}
3943
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ public static int calculate(int a, int b, char operator) {
2626
};
2727
}
2828

29-
public static void gameCalc() {
29+
public static void playCalc() {
3030
String exercise = "What is the result of the expression?";
31-
String[][] questionAndAnswer = new String[Engine.getCountOfRounds()][2];
32-
for (int i = 0; i < Engine.getCountOfRounds(); i++) {
33-
questionAndAnswer[i] = getQuestionAndAnswer();
34-
}
35-
Engine.gameEngine(exercise, questionAndAnswer);
31+
Engine.runGame(exercise, Calc::getQuestionAndAnswer);
3632
}
3733
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ private static String[] getQuestionAndAnswer() {
1616
return new String[]{question, correctAnswer};
1717
}
1818

19-
public static void gameEven() {
19+
public static void playEven() {
2020
String exercise = "Answer 'yes' if the number is even, otherwise answer 'no'.";
21-
String[][] questionAndAnswer = new String[Engine.getCountOfRounds()][2];
22-
for (int i = 0; i < Engine.getCountOfRounds(); i++) {
23-
questionAndAnswer[i] = getQuestionAndAnswer();
24-
}
25-
Engine.gameEngine(exercise, questionAndAnswer);
21+
Engine.runGame(exercise, Even::getQuestionAndAnswer);
2622
}
2723
}
2824

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ private static String[] getQuestionAndAnswer() {
1717
return new String[]{question, correctAnswer};
1818
}
1919

20-
public static void gameGcd() {
20+
public static void playGcd() {
2121
String exercise = "Find the greatest common divisor of given numbers.";
22-
String[][] questionAndAnswer = new String[Engine.getCountOfRounds()][2];
23-
for (int i = 0; i < Engine.getCountOfRounds(); i++) {
24-
questionAndAnswer[i] = getQuestionAndAnswer();
25-
}
26-
Engine.gameEngine(exercise, questionAndAnswer);
22+
Engine.runGame(exercise, Gcd::getQuestionAndAnswer);
2723
}
2824
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ private static boolean isPrime(int number) {
3636
return true;
3737
}
3838

39-
public static void gamePrime() {
39+
public static void playPrime() {
4040
String exercise = "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
41-
String[][] questionAndAnswer = new String[Engine.getCountOfRounds()][2];
42-
for (int i = 0; i < Engine.getCountOfRounds(); i++) {
43-
questionAndAnswer[i] = getQuestionAndAnswer();
44-
}
45-
Engine.gameEngine(exercise, questionAndAnswer);
41+
Engine.runGame(exercise, Prime::getQuestionAndAnswer);
4642
}
43+
4744
}

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ public class Progression {
77
private static final int MIN_LENGTH = 5;
88
private static final int MAX_LENGTH = 10;
99

10+
private static int[] generateProgression(int length, int start, int step) {
11+
int[] numbers = new int[length];
12+
numbers[0] = start;
13+
for (int i = 1; i < length; i++) {
14+
numbers[i] = numbers[i - 1] + step;
15+
}
16+
return numbers;
17+
}
18+
1019
private static String[] getQuestionAndAnswer() {
1120
int randomLength = Utils.getRandomNumber(MIN_LENGTH, MAX_LENGTH);
12-
int[] numbers = new int[randomLength];
13-
int diff = Utils.getRandomNumber();
14-
numbers[0] = Utils.getRandomNumber();
15-
for (int i = 1; i < randomLength; i++) {
16-
numbers[i] = numbers[i - 1] + diff;
17-
}
21+
int step = Utils.getRandomNumber();
22+
int startNumber = Utils.getRandomNumber();
23+
24+
int[] numbers = generateProgression(randomLength, startNumber, step);
25+
1826
int randomIndex = Utils.getRandomNumber(0, randomLength - 1);
1927
String correctAnswer = String.valueOf(numbers[randomIndex]);
2028
numbers[randomIndex] = -1;
@@ -25,12 +33,8 @@ private static String[] getQuestionAndAnswer() {
2533
return new String[] {question.trim(), correctAnswer};
2634
}
2735

28-
public static void gameProgression() {
36+
public static void playProgression() {
2937
String exercise = "What number is missing in the progression?";
30-
String[][] questionAndAnswer = new String[Engine.getCountOfRounds()][2];
31-
for (int i = 0; i < Engine.getCountOfRounds(); i++) {
32-
questionAndAnswer[i] = getQuestionAndAnswer();
33-
}
34-
Engine.gameEngine(exercise, questionAndAnswer);
38+
Engine.runGame(exercise, Progression::getQuestionAndAnswer);
3539
}
3640
}

0 commit comments

Comments
 (0)