|
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 GCD { |
8 | | - public static void gameLogic() { |
| 8 | + public static void startGame() { |
9 | 9 | final int maxValue = 100; |
10 | | - Engine.userGreetings(); |
11 | | - System.out.println("Find the greatest common divisor of given numbers."); |
12 | | - Scanner sc = new Scanner(System.in); |
13 | | - int result; |
14 | | - while (Engine.getQuestionCounter() != Engine.getMaxQuestions()) { |
15 | | - Random randNum = new Random(); |
| 10 | + String[][] roundsData = new String[Engine.ROUNDS][2]; |
| 11 | + String description = "Find the greatest common divisor of given numbers."; |
| 12 | + Random randNum = new Random(); |
| 13 | + |
| 14 | + for (int i = 0; i < Engine.ROUNDS; i++) { |
16 | 15 | int num1 = randNum.nextInt(maxValue); |
17 | 16 | int num2 = randNum.nextInt(maxValue); |
18 | | - System.out.println("Question: " + num1 + " " + num2); |
19 | | - int answer = sc.nextInt(); |
20 | | - result = Engine.gcdGenerate(num1, num2); |
21 | | - if (answer == result) { |
22 | | - System.out.println("Correct!\n"); |
23 | | - Engine.incrementQuestionsCounter(); |
24 | | - } else { |
25 | | - System.out.println("Your answer: " + answer); |
26 | | - System.out.println("'" + answer + "'" + "is wrong answer ;(. " |
27 | | - + "Correct answer was" + "'" + result + "'"); |
28 | | - System.out.println("Let's try again, " + Engine.getUserName() + "!"); |
29 | | - break; |
30 | | - } |
31 | | - if (Engine.getQuestionCounter() == Engine.getMaxQuestions()) { |
32 | | - System.out.println("Congratulations, " + Engine.getUserName() + "!"); |
33 | | - } |
| 17 | + String question = num1 + " " + num2; |
| 18 | + int result = gcdCalculate(num1, num2); |
| 19 | + roundsData[i][0] = question; |
| 20 | + roundsData[i][1] = String.valueOf(result); |
| 21 | + } |
| 22 | + Engine.run(description, roundsData); |
| 23 | + } |
| 24 | + |
| 25 | + private static int gcdCalculate(int a, int b) { |
| 26 | + while (b != 0) { |
| 27 | + int temp = a % b; |
| 28 | + a = b; |
| 29 | + b = temp; |
34 | 30 | } |
| 31 | + return a; |
35 | 32 | } |
36 | 33 | } |
0 commit comments