|
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 Prime { |
8 | | - public static void gameLogic() { |
| 8 | + public static void startGame() { |
9 | 9 | final int rndMax = 1000; |
10 | | - Scanner sc = new Scanner(System.in); |
11 | 10 | 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++) { |
15 | 15 | int num = randNum.nextInt(2, rndMax); |
16 | 16 | 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) { |
20 | 20 | isPrime = false; |
21 | 21 | break; |
22 | 22 | } |
23 | 23 | } |
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; |
39 | 28 | } |
| 29 | + Engine.run(description, roundsData); |
40 | 30 | } |
41 | 31 | } |
0 commit comments