Skip to content

Commit b50b669

Browse files
вынос вывода в движок
1 parent 20236d9 commit b50b669

File tree

6 files changed

+91
-146
lines changed

6 files changed

+91
-146
lines changed
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package hexlet.code;
22

3+
import java.util.Scanner;
4+
35
public class Engine {
46

57
private static final int ROUNDS = 3;
68
//границы случайных чисел не были обозначены сделал от 1 до 100. требуется уточнение у аналитика
79
private static final int MAX_NUMBER = 100;
8-
private static final String QUESTION_TEXT = "Question: ";
9-
private static final String ANSWER_TEXT = "Your answer: ";
10-
private static final String CORRECT_ANSWER = "Correct!";
11-
private static final String WRONG_ANSWER = "'%s' is wrong answer ;(. Correct answer was '%s'.";
12-
private static final String RETRY = "Let's try again, %s!";
13-
private static final String CONGRATULATIONS = "Congratulations, %s!";
1410

1511
public static int getRounds() {
1612
return ROUNDS;
@@ -20,27 +16,29 @@ public static int getMaxNumber() {
2016
return MAX_NUMBER;
2117
}
2218

23-
public static String getQuestionText() {
24-
return QUESTION_TEXT;
25-
}
19+
public static void startGame(String optionDescription, String[][] questionsAndCorrectAnswers) {
20+
String userName = Cli.greet();
21+
System.out.println(optionDescription);
22+
Scanner scanner = new Scanner(System.in);
2623

27-
public static String getAnswerText() {
28-
return ANSWER_TEXT;
29-
}
24+
for (int i = 0; i < ROUNDS; i++) {
25+
String question = questionsAndCorrectAnswers[i][0];
26+
String correctAnswer = questionsAndCorrectAnswers[i][1];
3027

31-
public static String getCorrectAnswer() {
32-
return CORRECT_ANSWER;
33-
}
28+
System.out.println("Question: " + question);
29+
System.out.print("Your answer: ");
30+
String userAnswer = scanner.nextLine();
3431

35-
public static String getWrongAnswer() {
36-
return WRONG_ANSWER;
37-
}
38-
39-
public static String getRetry() {
40-
return RETRY;
41-
}
42-
43-
public static String getCongratulations() {
44-
return CONGRATULATIONS;
32+
if (userAnswer.equals(correctAnswer)) {
33+
System.out.println("Correct!");
34+
} else {
35+
System.out.println("'" + userAnswer + "' is wrong answer ;(. Correct answer was '"
36+
+ correctAnswer + "'.");
37+
System.out.println("Let's try again, " + userName + "!");
38+
return;
39+
}
40+
}
41+
System.out.println("Congratulations, " + userName + "!");
4542
}
4643
}
44+
Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,39 @@
11
package hexlet.code.games;
22

3-
import hexlet.code.Cli;
43
import hexlet.code.Engine;
54

6-
import java.util.Scanner;
7-
85
public class Calc {
96
public static void play() {
10-
String userName = Cli.greet();
11-
System.out.println("What is the result of the expression?");
12-
Scanner scanner = new Scanner(System.in);
7+
String optionDescription = "What is the result of the expression?";
8+
String[][] questionsAndCorrectAnswers = new String[Engine.getRounds()][2];
139

14-
for (int i = 1; i <= Engine.getRounds(); i++) {
10+
for (int i = 0; i < Engine.getRounds(); i++) {
1511
int firstValue = (int) (Math.random() * Engine.getMaxNumber()) + 1;
1612
int secondValue = (int) (Math.random() * Engine.getMaxNumber()) + 1;
1713
char[] signs = {'+', '-', '*'};
1814
char randomSign = signs[(int) (Math.random() * signs.length)];
19-
int rightAnswer = 0;
15+
16+
String question = firstValue + " " + randomSign + " " + secondValue;
17+
String correctAnswer = "";
2018

2119
switch (randomSign) {
2220
case '+':
23-
rightAnswer = firstValue + secondValue;
21+
correctAnswer = String.valueOf(firstValue + secondValue);
2422
break;
2523
case '-':
26-
rightAnswer = firstValue - secondValue;
24+
correctAnswer = String.valueOf(firstValue - secondValue);
2725
break;
2826
case '*':
29-
rightAnswer = firstValue * secondValue;
27+
correctAnswer = String.valueOf(firstValue * secondValue);
3028
break;
3129
default:
32-
System.out.println("Неизвестный оператор: " + randomSign);
33-
break;
30+
correctAnswer = "Неизвестный оператор: " + randomSign;
3431
}
3532

36-
System.out.println(Engine.getQuestionText() + firstValue + " " + randomSign + " " + secondValue);
37-
System.out.print(Engine.getAnswerText());
38-
int answer = scanner.nextInt();
33+
questionsAndCorrectAnswers[i][0] = question;
34+
questionsAndCorrectAnswers[i][1] = correctAnswer;
3935

40-
if (answer == rightAnswer) {
41-
System.out.println(Engine.getCorrectAnswer());
42-
} else {
43-
System.out.printf(Engine.getWrongAnswer(), answer, rightAnswer);
44-
System.out.printf(Engine.getRetry(), userName);
45-
return;
46-
}
4736
}
48-
System.out.printf(Engine.getCongratulations(), userName);
37+
Engine.startGame(optionDescription, questionsAndCorrectAnswers);
4938
}
5039
}
Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
package hexlet.code.games;
22

3-
import hexlet.code.Cli;
43
import hexlet.code.Engine;
54

6-
import java.util.Scanner;
7-
85
public class Even {
96
public static void play() {
10-
String userName = Cli.greet();
11-
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
12-
Scanner scanner = new Scanner(System.in);
7+
String optionDescription = "Answer 'yes' if the number is even, otherwise answer 'no'.";
8+
String[][] questionsAndCorrectAnswers = new String[Engine.getRounds()][2];
139

14-
for (int i = 1; i <= Engine.getRounds(); i++) {
10+
for (int i = 0; i < Engine.getRounds(); i++) {
1511
int random = (int) (Math.random() * Engine.getMaxNumber()) + 1;
16-
System.out.println(Engine.getQuestionText() + random);
17-
System.out.print(Engine.getAnswerText());
18-
String answer = scanner.nextLine();
19-
12+
String question = String.valueOf(random);
2013
boolean isEven = random % 2 == 0;
21-
String rightAnswer = isEven ? "yes" : "no";
14+
String correctAnswer = isEven ? "yes" : "no";
2215

23-
24-
if (answer.equals(rightAnswer)) {
25-
System.out.println(Engine.getCorrectAnswer());
26-
} else {
27-
System.out.printf(Engine.getWrongAnswer(), answer, rightAnswer);
28-
System.out.printf(Engine.getRetry(), userName);
29-
return;
30-
}
16+
questionsAndCorrectAnswers[i][0] = question;
17+
questionsAndCorrectAnswers[i][1] = correctAnswer;
3118
}
32-
System.out.printf(Engine.getCongratulations(), userName);
33-
19+
Engine.startGame(optionDescription, questionsAndCorrectAnswers);
3420
}
3521
}
Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
11
package hexlet.code.games;
22

3-
import hexlet.code.Cli;
43
import hexlet.code.Engine;
54

6-
import java.util.Scanner;
7-
85
public class Gcd {
96
public static void play() {
10-
String userName = Cli.greet();
11-
System.out.println("Find the greatest common divisor of given numbers.");
12-
Scanner scanner = new Scanner(System.in);
7+
String optionDescription = "Find the greatest common divisor of given numbers.";
8+
String[][] questionsAndCorrectAnswers = new String[Engine.getRounds()][2];
139

14-
for (int i = 1; i <= Engine.getRounds(); i++) {
10+
for (int i = 0; i < Engine.getRounds(); i++) {
1511
int firstValue = (int) (Math.random() * Engine.getMaxNumber()) + 1;
1612
int secondValue = (int) (Math.random() * Engine.getMaxNumber()) + 1;
17-
int rightAnswer = 0;
1813

1914
int tempFirstValue = firstValue;
2015
int tempSecondValue = secondValue;
16+
int tempAnswer = 0;
2117

2218
while (tempSecondValue != 0) {
23-
rightAnswer = tempSecondValue;
19+
tempAnswer = tempSecondValue;
2420
tempSecondValue = tempFirstValue % tempSecondValue;
25-
tempFirstValue = rightAnswer;
21+
tempFirstValue = tempAnswer;
2622
}
2723

28-
System.out.println(Engine.getQuestionText() + firstValue + " " + secondValue);
29-
System.out.print(Engine.getAnswerText());
30-
int answer = scanner.nextInt();
24+
String question = firstValue + " " + secondValue;
25+
String correctAnswer = String.valueOf(tempAnswer);
3126

32-
if (answer == rightAnswer) {
33-
System.out.println(Engine.getCorrectAnswer());
34-
} else {
35-
System.out.printf(Engine.getWrongAnswer(), answer, rightAnswer);
36-
System.out.printf(Engine.getRetry(), userName);
37-
return;
38-
}
27+
questionsAndCorrectAnswers[i][0] = question;
28+
questionsAndCorrectAnswers[i][1] = correctAnswer;
3929
}
40-
System.out.printf(Engine.getCongratulations(), userName);
30+
Engine.startGame(optionDescription, questionsAndCorrectAnswers);
4131
}
4232
}
33+

app/src/main/java/hexlet/code/games/Prime.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
11
package hexlet.code.games;
22

3-
import hexlet.code.Cli;
43
import hexlet.code.Engine;
54

6-
import java.util.Scanner;
7-
85
public class Prime {
96
public static void play() {
10-
String userName = Cli.greet();
11-
System.out.println("Answer 'yes' if given number is prime. Otherwise answer 'no'.");
12-
Scanner scanner = new Scanner(System.in);
7+
String optionDescription = "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
8+
String[][] questionsAndCorrectAnswers = new String[Engine.getRounds()][2];
139

14-
for (int i = 1; i <= Engine.getRounds(); i++) {
15-
int num = (int) (Math.random() * Engine.getMaxNumber()) + 1;
16-
String rightAnswer = logicPrime(num);
10+
for (int i = 0; i < Engine.getRounds(); i++) {
11+
int random = (int) (Math.random() * Engine.getMaxNumber()) + 1;
1712

18-
System.out.println(Engine.getQuestionText() + num);
19-
System.out.print(Engine.getAnswerText());
20-
String answer = scanner.next();
13+
String question = String.valueOf(random);
14+
String correctAnswer = logicPrime(random);
15+
16+
questionsAndCorrectAnswers[i][0] = question;
17+
questionsAndCorrectAnswers[i][1] = correctAnswer;
2118

22-
if (answer.equals(rightAnswer)) {
23-
System.out.println(Engine.getCorrectAnswer());
24-
} else {
25-
System.out.printf(Engine.getWrongAnswer(), answer, rightAnswer);
26-
System.out.printf(Engine.getRetry(), userName);
27-
return;
28-
}
2919
}
30-
System.out.printf(Engine.getCongratulations(), userName);
20+
Engine.startGame(optionDescription, questionsAndCorrectAnswers);
3121
}
3222

33-
34-
public static String logicPrime(int num) {
35-
if (num < 2) {
23+
public static String logicPrime(int random) {
24+
if (random < 2) {
3625
return "no";
3726
}
38-
for (int i = 2; i <= Math.sqrt(num); i++) {
39-
if (num % i == 0) {
27+
for (int i = 2; i <= Math.sqrt(random); i++) {
28+
if (random % i == 0) {
4029
return "no";
4130
}
4231
}
Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,28 @@
11
package hexlet.code.games;
22

3-
import hexlet.code.Cli;
43
import hexlet.code.Engine;
54

6-
import java.util.Scanner;
7-
85
public class Progression {
96
private static final int STEP_COUNT = 10;
107
private static final int VALUE_COUNT = 10;
118

129
public static void play() {
13-
String userName = Cli.greet();
14-
System.out.println("What number is missing in the progression?");
15-
Scanner scanner = new Scanner(System.in);
16-
10+
String optionDescription = "What number is missing in the progression?";
11+
String[][] questionsAndCorrectAnswers = new String[Engine.getRounds()][2];
1712

18-
for (int i = 1; i <= Engine.getRounds(); i++) {
13+
for (int i = 0; i < Engine.getRounds(); i++) {
1914
int start = (int) (Math.random() * Engine.getMaxNumber()) + 1;
2015
int step = (int) (Math.random() * STEP_COUNT) + 1;
16+
int hideIndex = (int) (Math.random() * VALUE_COUNT);
2117
int[] sequence = addSequence(start, step, VALUE_COUNT);
2218

23-
int hideIndex = (int) (Math.random() * VALUE_COUNT);
24-
int rightAnswer = sequence[hideIndex];
25-
26-
System.out.print(Engine.getQuestionText());
27-
for (int j = 0; j < VALUE_COUNT; j++) {
28-
if (j == hideIndex) {
29-
System.out.print(".. ");
30-
} else {
31-
System.out.print(sequence[j] + " ");
32-
}
33-
}
34-
System.out.print(Engine.getAnswerText());
35-
int answer = scanner.nextInt();
19+
String question = createQuestion(sequence, hideIndex);
20+
String correctAnswer = String.valueOf(sequence[hideIndex]);
3621

37-
if (answer == rightAnswer) {
38-
System.out.println(Engine.getCorrectAnswer());
39-
} else {
40-
System.out.printf(Engine.getWrongAnswer(), answer, rightAnswer);
41-
System.out.printf(Engine.getRetry(), userName);
42-
return;
43-
}
22+
questionsAndCorrectAnswers[i][0] = question;
23+
questionsAndCorrectAnswers[i][1] = correctAnswer;
4424
}
45-
System.out.printf(Engine.getCongratulations(), userName);
25+
Engine.startGame(optionDescription, questionsAndCorrectAnswers);
4626
}
4727

4828
public static int[] addSequence(int start, int step, int length) {
@@ -53,4 +33,16 @@ public static int[] addSequence(int start, int step, int length) {
5333
}
5434
return sequence;
5535
}
36+
37+
public static String createQuestion(int[] sequence, int hideIndex) {
38+
String question = "";
39+
for (int i = 0; i < VALUE_COUNT; i++) {
40+
if (i == hideIndex) {
41+
question += ".. ";
42+
} else {
43+
question += sequence[i] + " ";
44+
}
45+
}
46+
return question;
47+
}
5648
}

0 commit comments

Comments
 (0)