|
1 | 1 | package hexlet.code.games; |
2 | 2 |
|
3 | 3 | import hexlet.code.Engine; |
| 4 | + |
4 | 5 | import java.util.Random; |
5 | | -import java.util.Scanner; |
6 | 6 |
|
7 | 7 | public class Progression { |
8 | | - public static void gameLogic() { |
| 8 | + public static void startGame() { |
9 | 9 | final int arrLength = 10; |
10 | 10 | final int stepMax = 10; |
11 | 11 | final int hideElementMax = 9; |
12 | 12 | 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]; |
15 | 14 | Random randNum = new Random(); |
| 15 | + String description = "What number is missing in the progression?"; |
16 | 16 |
|
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++) { |
20 | 18 | int progressionStep = randNum.nextInt(1, stepMax); |
21 | 19 | int randHideElement = randNum.nextInt(0, hideElementMax); |
22 | 20 | int firstNum = randNum.nextInt(1, firstNumMax); |
| 21 | + |
| 22 | + int[] progression = new int[arrLength]; |
23 | 23 | 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; |
26 | 26 | } |
27 | | - int hideElement = progression[randHideElement]; |
| 27 | + |
28 | 28 | 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] = ".."; |
32 | 33 | } else { |
33 | | - progressionWithDots[i] = String.valueOf(progression[i]); |
| 34 | + progressionWithDots[k] = String.valueOf(progression[k]); |
34 | 35 | } |
35 | 36 | } |
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]); |
52 | 40 | } |
53 | | - |
| 41 | + Engine.run(description, roundsData); |
54 | 42 | } |
55 | 43 | } |
0 commit comments