Skip to content

Commit dad2012

Browse files
committed
Simplificationing Calculator, Even, Gcd; editioning App, Engine, README; adding Progression;
1 parent 2fe0194 commit dad2012

File tree

7 files changed

+61
-23
lines changed

7 files changed

+61
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
"Calculator" https://asciinema.org/a/QIkWy2KinHzcdj0LULvdMOfM0
1212

1313
"GCD" https://asciinema.org/a/anWD6GPnBTcWTBLCfvbMGZeV6
14+
15+
"Progression" https://asciinema.org/a/4cwJUcnV4TcEFSpdqMrIa3xjf

app/src/main/java/hexlet/code/App.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import hexlet.code.games.Calculator;
44
import hexlet.code.games.Even;
55
import hexlet.code.games.Gcd;
6+
import hexlet.code.games.Progression;
67
import java.util.Scanner;
78

89
class App {
@@ -12,6 +13,7 @@ public static void main(String[] args) {
1213
System.out.println("2 - Even");
1314
System.out.println("3 - Calculator");
1415
System.out.println("4 - GCD");
16+
System.out.println("5 - Progression");
1517
System.out.println("0 - Exit");
1618

1719
Scanner scanner = new Scanner(System.in);
@@ -31,6 +33,9 @@ public static void main(String[] args) {
3133
case "4":
3234
Gcd.startGame();
3335
break;
36+
case "5":
37+
Progression.startGame();
38+
break;
3439
case "0":
3540
System.out.println("Goodbye!");
3641
break;

app/src/main/java/hexlet/code/Engine.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import java.util.Scanner;
44

55
public class Engine {
6-
private static final int ROUNDS = 3;
7-
private static final Scanner SCANNER = new Scanner(System.in);
6+
public static final int ROUNDS = 3;
87

98
public static int getRoundsCount() {
109
return ROUNDS;
@@ -14,14 +13,15 @@ public static void run(String description, String[][] data) {
1413
System.out.println("Welcome to the Brain Games!");
1514
String userName = Cli.greetUser();
1615
System.out.println(description);
16+
Scanner scanner = new Scanner(System.in);
1717

1818
for (int i = 0; i < ROUNDS; i++) {
1919
String question = data[i][0];
2020
String correctAnswer = data[i][1];
2121

2222
System.out.println("Question: " + question);
2323
System.out.print("Your answer: ");
24-
String answer = SCANNER.next();
24+
String answer = scanner.next();
2525

2626
if (answer.equalsIgnoreCase(correctAnswer)) {
2727
System.out.println("Correct!");

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.security.SecureRandom;
55

66
public class Calculator {
7-
private static final String DESCRIPTION = "What is the result of the expression?";
8-
private static final char[] OPERATORS = {'+', '-', '*'};
97

108
public static int calculate(int operand1, int operand2, char operator) {
119
switch (operator) {
@@ -21,13 +19,16 @@ public static int calculate(int operand1, int operand2, char operator) {
2119
}
2220

2321
public static void startGame() {
22+
String description = "What is the result of the expression?";
23+
char[] operators = {'+', '-', '*'};
2424
String[][] data = new String[Engine.getRoundsCount()][2];
2525
SecureRandom random = new SecureRandom();
2626

27+
2728
for (int i = 0; i < Engine.getRoundsCount(); i++) {
2829
int number1 = random.nextInt(100) + 1;
2930
int number2 = random.nextInt(100) + 1;
30-
char operator = OPERATORS[random.nextInt(OPERATORS.length)];
31+
char operator = operators[random.nextInt(operators.length)];
3132

3233
String question = number1 + " " + operator + " " + number2;
3334
String correctAnswer = String.valueOf(calculate(number1, number2, operator));
@@ -36,8 +37,6 @@ public static void startGame() {
3637
data[i][1] = correctAnswer;
3738
}
3839

39-
Engine.run(DESCRIPTION, data);
40+
Engine.run(description, data);
4041
}
4142
}
42-
43-

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@
44
import java.security.SecureRandom;
55

66
public class Even {
7-
private static final String DESCRIPTION = "Answer 'yes' if the number is even, otherwise answer 'no'.";
8-
9-
10-
private static boolean isEven(int number) {
11-
return number % 2 == 0;
12-
}
137
public static void startGame() {
8+
String description = "Answer 'yes' if the number is even, otherwise answer 'no'.";
149
String[][] data = new String[Engine.getRoundsCount()][2];
1510
SecureRandom random = new SecureRandom();
1611

12+
1713
for (int i = 0; i < Engine.getRoundsCount(); i++) {
1814
int number = random.nextInt(100) + 1;
1915
String question = String.valueOf(number);
20-
String correctAnswer = isEven(number) ? "yes" : "no";
16+
String correctAnswer = number % 2 == 0 ? "yes" : "no";
2117

2218
data[i][0] = question;
2319
data[i][1] = correctAnswer;
2420
}
25-
26-
Engine.run(DESCRIPTION, data);
21+
Engine.run(description, data);
2722
}
28-
29-
3023
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.security.SecureRandom;
55

66
public class Gcd {
7-
private static final String DESCRIPTION = "Find the greatest common divisor of given numbers.";
7+
88

99
private static int isGCD(int a, int b) {
1010
while (b != 0) {
@@ -16,6 +16,7 @@ private static int isGCD(int a, int b) {
1616
}
1717

1818
public static void startGame() {
19+
String description = "Find the greatest common divisor of given numbers.";
1920
String[][] data = new String[Engine.getRoundsCount()][2];
2021
SecureRandom random = new SecureRandom();
2122

@@ -28,7 +29,6 @@ public static void startGame() {
2829
data[i][0] = question;
2930
data[i][1] = correctAnswer;
3031
}
31-
Engine.run(DESCRIPTION, data);
32+
Engine.run(description, data);
3233
}
33-
3434
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package hexlet.code.games;
2+
3+
import hexlet.code.Engine;
4+
import java.security.SecureRandom;
5+
6+
public class Progression {
7+
public static String[] generateProgression(int start, int step, int length) {
8+
String[] progression = new String[length];
9+
for (int index = 0; index < length; index++) {
10+
progression[index] = String.valueOf(start + index * step);
11+
}
12+
return progression;
13+
}
14+
15+
public static void startGame() {
16+
String description = "What number is missing in the progression?";
17+
String[][] data = new String[Engine.getRoundsCount()][2];
18+
SecureRandom random = new SecureRandom();
19+
20+
21+
for (int i = 0; i < Engine.getRoundsCount(); i++) {
22+
int start = random.nextInt(50) + 1;
23+
int step = random.nextInt(10) + 1;
24+
int hiddenIndex = random.nextInt(10);
25+
26+
String[] progression = generateProgression(start, step, 10);
27+
String correctAnswer = progression[hiddenIndex];
28+
progression[hiddenIndex] = "..";
29+
30+
String question = String.join(" ", progression);
31+
32+
33+
data[i][0] = question;
34+
data[i][1] = correctAnswer;
35+
}
36+
Engine.run(description, data);
37+
}
38+
}
39+

0 commit comments

Comments
 (0)