|
1 | 1 | package hexlet.code.games; |
2 | 2 |
|
3 | 3 | import hexlet.code.Engine; |
4 | | - |
5 | | -import java.util.Random; |
| 4 | +import hexlet.code.Utils; |
6 | 5 |
|
7 | 6 | public class Progression { |
8 | 7 | public static void startGame() { |
9 | | - final int arrLength = 10; |
10 | | - final int stepMax = 10; |
11 | | - final int hideElementMax = 9; |
12 | | - final int firstNumMax = 100; |
13 | 8 | String[][] roundsData = new String[Engine.ROUNDS][2]; |
14 | | - Random randNum = new Random(); |
15 | 9 | String description = "What number is missing in the progression?"; |
16 | 10 |
|
17 | 11 | for (int i = 0; i < Engine.ROUNDS; i++) { |
18 | | - int progressionStep = randNum.nextInt(1, stepMax); |
19 | | - int randHideElement = randNum.nextInt(0, hideElementMax); |
20 | | - int firstNum = randNum.nextInt(1, firstNumMax); |
21 | | - |
22 | | - int[] progression = new int[arrLength]; |
23 | | - progression[0] = firstNum; |
24 | | - for (int j = 1; j < progression.length; j++) { |
25 | | - progression[j] = progression[j - 1] + progressionStep; |
26 | | - } |
27 | | - |
28 | | - String[] progressionWithDots = new String[progression.length]; |
| 12 | + int randHideElement = Utils.generateNumber(0, 9); |
| 13 | + String[] progressionWithDots = new String[getProgression().length]; |
29 | 14 |
|
30 | | - for (int k = 0; k < progression.length; k++) { |
| 15 | + for (int k = 0; k < getProgression().length; k++) { |
31 | 16 | if (k == randHideElement) { |
32 | 17 | progressionWithDots[k] = ".."; |
33 | 18 | } else { |
34 | | - progressionWithDots[k] = String.valueOf(progression[k]); |
| 19 | + progressionWithDots[k] = String.valueOf(getProgression()[k]); |
35 | 20 | } |
36 | 21 | } |
37 | 22 | String question = String.join(" ", progressionWithDots); |
38 | 23 | roundsData[i][0] = question; |
39 | | - roundsData[i][1] = String.valueOf(progression[randHideElement]); |
| 24 | + roundsData[i][1] = String.valueOf(getProgression()[randHideElement]); |
40 | 25 | } |
41 | 26 | Engine.run(description, roundsData); |
42 | 27 | } |
| 28 | + |
| 29 | + private static int[] getProgression() { |
| 30 | + final int arrLength = 10; |
| 31 | + int progressionStep = Utils.generateNumber(0, 10); |
| 32 | + int firstNum = Utils.generateNumber(0, 100); |
| 33 | + |
| 34 | + int[] progression = new int[arrLength]; |
| 35 | + progression[0] = firstNum; |
| 36 | + for (int j = 1; j < progression.length; j++) { |
| 37 | + progression[j] = progression[j - 1] + progressionStep; |
| 38 | + } |
| 39 | + return progression; |
| 40 | + } |
43 | 41 | } |
0 commit comments