Skip to content

Commit 2e6de43

Browse files
committed
refactor: logic change
1 parent 80f372b commit 2e6de43

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed
Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
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 Prime {
8-
public static void gameLogic() {
8+
public static void startGame() {
99
final int rndMax = 1000;
10-
Scanner sc = new Scanner(System.in);
1110
Random randNum = new Random();
12-
Engine.userGreetings();
13-
System.out.println("Answer 'yes' if given number is prime. Otherwise answer 'no'.");
14-
while (Engine.getQuestionCounter() != Engine.getMaxQuestions()) {
11+
String[][] roundsData = new String[Engine.ROUNDS][2];
12+
String description = "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
13+
14+
for (int i = 0; i < Engine.ROUNDS; i++) {
1515
int num = randNum.nextInt(2, rndMax);
1616
boolean isPrime = true;
17-
System.out.println("Question: " + num);
18-
for (int i = 2; i <= Math.sqrt(num); i++) {
19-
if (num % i == 0) {
17+
18+
for (int j = 2; j <= Math.sqrt(num); j++) {
19+
if (num % j == 0) {
2020
isPrime = false;
2121
break;
2222
}
2323
}
24-
System.out.print("Your answer: ");
25-
String answer = sc.next();
26-
if (answer.equals("yes") && isPrime) {
27-
System.out.println("Correct!");
28-
Engine.incrementQuestionsCounter();
29-
} else if (answer.equals("no") && !isPrime) {
30-
System.out.println("Correct!");
31-
Engine.incrementQuestionsCounter();
32-
} else {
33-
System.out.println("Let's try again, " + Engine.getUserName() + "!");
34-
break;
35-
}
36-
if (Engine.getQuestionCounter() == Engine.getMaxQuestions()) {
37-
System.out.println("Congratulations, " + Engine.getUserName() + "!");
38-
}
24+
String answer = isPrime ? "yes" : "no";
25+
26+
roundsData[i][0] = String.valueOf(num);
27+
roundsData[i][1] = answer;
3928
}
29+
Engine.run(description, roundsData);
4030
}
4131
}

0 commit comments

Comments
 (0)