Skip to content

Commit 80f372b

Browse files
committed
refactor: logic change
1 parent 01032c1 commit 80f372b

File tree

1 file changed

+21
-24
lines changed
  • app/src/main/java/hexlet/code/games

1 file changed

+21
-24
lines changed
Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
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 GCD {
8-
public static void gameLogic() {
8+
public static void startGame() {
99
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++) {
1615
int num1 = randNum.nextInt(maxValue);
1716
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;
3430
}
31+
return a;
3532
}
3633
}

0 commit comments

Comments
 (0)