Skip to content

Commit 9390f23

Browse files
committed
refactor: logic change
1 parent 2e6de43 commit 9390f23

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed
Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,43 @@
11
package hexlet.code.games;
22

33
import hexlet.code.Engine;
4+
45
import java.util.Random;
5-
import java.util.Scanner;
66

77
public class Progression {
8-
public static void gameLogic() {
8+
public static void startGame() {
99
final int arrLength = 10;
1010
final int stepMax = 10;
1111
final int hideElementMax = 9;
1212
final int firstNumMax = 100;
13-
Scanner sc = new Scanner(System.in);
14-
int[] progression = new int[arrLength];
13+
String[][] roundsData = new String[Engine.ROUNDS][2];
1514
Random randNum = new Random();
15+
String description = "What number is missing in the progression?";
1616

17-
Engine.userGreetings();
18-
System.out.println("What number is missing in the progression?");
19-
while (Engine.getQuestionCounter() != Engine.getMaxQuestions()) {
17+
for (int i = 0; i < Engine.ROUNDS; i++) {
2018
int progressionStep = randNum.nextInt(1, stepMax);
2119
int randHideElement = randNum.nextInt(0, hideElementMax);
2220
int firstNum = randNum.nextInt(1, firstNumMax);
21+
22+
int[] progression = new int[arrLength];
2323
progression[0] = firstNum;
24-
for (int i = 1; i < progression.length; i++) {
25-
progression[i] = progression[i - 1] + progressionStep;
24+
for (int j = 1; j < progression.length; j++) {
25+
progression[j] = progression[j - 1] + progressionStep;
2626
}
27-
int hideElement = progression[randHideElement];
27+
2828
String[] progressionWithDots = new String[progression.length];
29-
for (int i = 0; i < progression.length; i++) {
30-
if (i == randHideElement) {
31-
progressionWithDots[i] = "..";
29+
30+
for (int k = 0; k < progression.length; k++) {
31+
if (k == randHideElement) {
32+
progressionWithDots[k] = "..";
3233
} else {
33-
progressionWithDots[i] = String.valueOf(progression[i]);
34+
progressionWithDots[k] = String.valueOf(progression[k]);
3435
}
3536
}
36-
System.out.println("Question: " + String.join(" ", progressionWithDots));
37-
int answer = sc.nextInt();
38-
39-
if (answer == hideElement) {
40-
System.out.println("Correct!\n");
41-
Engine.incrementQuestionsCounter();
42-
} else {
43-
System.out.println("Your answer: " + answer);
44-
System.out.println("'" + answer + "'" + "is wrong answer ;(. "
45-
+ "Correct answer was" + "'" + hideElement + "'");
46-
System.out.println("Let's try again, " + Engine.getUserName() + "!");
47-
break;
48-
}
49-
if (Engine.getQuestionCounter() == Engine.getMaxQuestions()) {
50-
System.out.println("Congratulations, " + Engine.getUserName() + "!");
51-
}
37+
String question = String.join(" ", progressionWithDots);
38+
roundsData[i][0] = question;
39+
roundsData[i][1] = String.valueOf(progression[randHideElement]);
5240
}
53-
41+
Engine.run(description, roundsData);
5442
}
5543
}

0 commit comments

Comments
 (0)