|
1 | 1 | package hexlet.code.games; |
2 | 2 |
|
3 | 3 | import hexlet.code.Engine; |
| 4 | +import hexlet.code.Utils; |
4 | 5 |
|
5 | 6 | public class Gcd { |
6 | | - public static final int MAX_NUMBER = 100; |
7 | | - |
8 | 7 | public static void play() { |
9 | 8 | String optionDescription = "Find the greatest common divisor of given numbers."; |
10 | 9 | String[][] questionsAndCorrectAnswers = new String[Engine.ROUNDS][2]; |
11 | 10 |
|
12 | 11 | 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(); |
15 | 14 |
|
16 | 15 | String question = firstValue + " " + secondValue; |
17 | | - String correctAnswer = String.valueOf(calculateForCorrectAnswer(firstValue, secondValue)); |
| 16 | + String correctAnswer = String.valueOf(gcd(firstValue, secondValue)); |
18 | 17 |
|
19 | 18 | questionsAndCorrectAnswers[i][0] = question; |
20 | 19 | questionsAndCorrectAnswers[i][1] = correctAnswer; |
21 | 20 | } |
22 | 21 | Engine.startGame(optionDescription, questionsAndCorrectAnswers); |
23 | 22 | } |
24 | 23 |
|
25 | | - public static int calculateForCorrectAnswer(int firstValue, int secondValue) { |
| 24 | + public static int gcd(int firstValue, int secondValue) { |
26 | 25 | while (secondValue != 0) { |
27 | 26 | int tempAnswer = secondValue; |
28 | 27 | secondValue = firstValue % secondValue; |
|
0 commit comments