Skip to content

Commit 1713e42

Browse files
committed
Fixed checkstyle errors
1 parent 94a42fe commit 1713e42

File tree

8 files changed

+58
-33
lines changed

8 files changed

+58
-33
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ public static void main(String[] args) {
1313
5 - Progression
1414
6 - Prime
1515
0 - Exit""");
16-
int gameChoice;
16+
String gameChoice;
1717
Scanner input = new Scanner(System.in);
18-
gameChoice = input.nextInt();
19-
System.out.println("Your choice: " + gameChoice);
20-
if (gameChoice == 1) {
21-
Cli.greeting();
22-
} else {
18+
gameChoice = input.nextLine();
19+
if (!gameChoice.equals("0")) {
20+
System.out.println("Your choice: " + gameChoice);
2321
Engine.gameRun(gameChoice);
2422
}
2523
}

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

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

55
public class Cli {
6-
public static String name;
6+
private static String name;
77

88
public static void greeting() {
99
System.out.println("Welcome to the Brain Games!");
@@ -13,6 +13,8 @@ public static void greeting() {
1313
name = input.nextLine();
1414
}
1515
System.out.println("Hello, " + name + "!");
16-
16+
}
17+
public static String getName() {
18+
return name;
1719
}
1820
}
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,61 @@
11
package hexlet.code;
22

3-
import hexlet.code.games.*;
4-
3+
import hexlet.code.games.CalcGame;
4+
import hexlet.code.games.EvenGame;
5+
import hexlet.code.games.GCDGame;
6+
import hexlet.code.games.PrimeGame;
7+
import hexlet.code.games.ProgressionGame;
58
import java.util.Scanner;
69

710
public class Engine {
8-
public static void gameRun(int gameNum) {
11+
static final int NUMBER_OF_GAMES = 3;
12+
13+
public static void gameRun(String gameNum) {
914
Cli.greeting();
1015
int gameCount = 0;
1116
String solution = "";
1217
String playerAnswer;
1318
Scanner input = new Scanner(System.in);
14-
while (gameCount < 3) {
19+
while (gameCount < NUMBER_OF_GAMES) {
20+
if (gameNum.equals("1")) {
21+
break;
22+
}
1523
switch (gameNum) {
16-
case 2:
24+
case "2":
1725
EvenGame.showGameRules(gameCount);
1826
solution = EvenGame.getSolution();
1927
break;
20-
case 3:
28+
case "3":
2129
CalcGame.showGameRules(gameCount);
2230
solution = CalcGame.getSolution();
2331
break;
24-
case 4:
32+
case "4":
2533
GCDGame.showGameRules(gameCount);
2634
solution = GCDGame.getSolution();
2735
break;
28-
case 5:
36+
case "5":
2937
ProgressionGame.showGameRules(gameCount);
3038
solution = ProgressionGame.getSolution();
3139
break;
32-
case 6:
40+
case "6":
3341
PrimeGame.showGameRules(gameCount);
3442
solution = PrimeGame.getSolution();
43+
break;
44+
default:
45+
System.out.println("Unexpected input");
3546
}
3647
playerAnswer = input.nextLine();
3748
if (solution.equals(playerAnswer)) {
3849
System.out.println("Correct!");
3950
gameCount++;
4051
} else {
4152
System.out.println("'" + playerAnswer + "' is wrong answer ;(. Correct answer was '" + solution + "'.");
42-
System.out.println("Let's try again, " + Cli.name + "!");
53+
System.out.println("Let's try again, " + Cli.getName() + "!");
4354
break;
4455
}
4556
}
46-
if (gameCount == 3) {
47-
System.out.println("Congratulations, " + Cli.name + "!");
57+
if (gameCount == NUMBER_OF_GAMES) {
58+
System.out.println("Congratulations, " + Cli.getName() + "!");
4859
}
4960
}
5061
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package hexlet.code.games;
22

33
public class CalcGame {
4+
static final int MAX_NUMBER_VALUE = 100;
5+
static final int MAX_INDEX_VALUE = 100;
46
public static void showGameRules(int gameCount) {
57
if (gameCount == 0) {
68
System.out.println("What is the result of the expression?");
79
}
810
}
911
public static String getSolution() {
10-
int firstNumber = (int) (Math.random() * 20.0);
11-
int secondNumber = (int) (Math.random() * 20.0);
12-
int operatorIndex = (int) (Math.random() * 3.0);
12+
int firstNumber = (int) (Math.random() * MAX_NUMBER_VALUE);
13+
int secondNumber = (int) (Math.random() * MAX_NUMBER_VALUE);
14+
int operatorIndex = (int) (Math.random() * MAX_INDEX_VALUE);
1315
char[] operator = {'+', '-', '*'};
1416
int solution = 0;
1517
System.out.println("Question: " + firstNumber + " " + operator[operatorIndex] + " " + secondNumber);
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package hexlet.code.games;
22

33
public class EvenGame {
4+
static final int MAX_NUMBER_VALUE = 100;
5+
46
public static void showGameRules(int gameCount) {
57
if (gameCount == 0) {
68
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
79
}
810
}
911
public static String getSolution() {
10-
int guessedNumber = (int) (Math.random() * 100.0);
12+
int guessedNumber = (int) (Math.random() * MAX_NUMBER_VALUE);
1113
System.out.println("Question: " + guessedNumber);
1214
return guessedNumber % 2 == 0 ? "yes" : "no";
1315
}
14-
}
16+
}

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

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

33
public class GCDGame {
4+
static final int MAX_NUMBER_VALUE = 100;
5+
46
public static void showGameRules(int gameCount) {
57
if (gameCount == 0) {
68
System.out.println("Find the greatest common divisor of given numbers.");
79
}
810
}
911
public static String getSolution() {
10-
int firstNumber = (int) (Math.random() * 100.0);
11-
int secondNumber = (int) (Math.random() * 100.0);
12+
int firstNumber = (int) (Math.random() * MAX_NUMBER_VALUE);
13+
int secondNumber = (int) (Math.random() * MAX_NUMBER_VALUE);
1214
int solution;
1315
System.out.println("Question: " + firstNumber + " " + secondNumber);
1416
while (secondNumber != 0) {
@@ -20,4 +22,4 @@ public static String getSolution() {
2022
solution = firstNumber;
2123
return Integer.toString(solution);
2224
}
23-
}
25+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package hexlet.code.games;
22

33
public class PrimeGame {
4+
static final int MAX_NUMBER_VALUE = 100;
5+
static final int MIN_INDEX_TO_START = 100;
6+
47
public static void showGameRules(int gameCount) {
58
if (gameCount == 0) {
69
System.out.println("Answer 'yes' if given number is prime. Otherwise answer 'no'.");
710
}
811
}
912
public static String getSolution() {
10-
int guessedNumber = (int) (Math.random() * 100.0);
13+
int guessedNumber = (int) (Math.random() * MAX_NUMBER_VALUE);
1114
System.out.println("Question: " + guessedNumber);
1215
if (guessedNumber < 2) {
1316
return "no";
@@ -18,7 +21,7 @@ public static String getSolution() {
1821
if (guessedNumber % 2 == 0) {
1922
return "no";
2023
}
21-
for (int i = 3; i * i <= guessedNumber ; i += 2) {
24+
for (int i = MIN_INDEX_TO_START; i * i <= guessedNumber; i += 2) {
2225
if (guessedNumber % i == 0) {
2326
return "no";
2427
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package hexlet.code.games;
22

33
public class ProgressionGame {
4+
static final int MAX_NUMBERS_LENGTH = 9;
5+
static final int MAX_NUMBER_VALUE = 100;
6+
static final int MAX_STEP_VALUE = 10;
7+
static final int MIN_STEP_VALUE = 1;
8+
49
public static void showGameRules(int gameCount) {
510
if (gameCount == 0) {
611
System.out.println("What number is missing in the progression?");
712
}
813
}
914
public static String getSolution() {
10-
int[] numbers = new int[9];
11-
int startNumber = (int) (Math.random() * 100.0);
12-
int step = (int) (Math.random() * 10.0 + 1.0);
15+
int[] numbers = new int[MAX_NUMBERS_LENGTH];
16+
int startNumber = (int) (Math.random() * MAX_NUMBER_VALUE);
17+
int step = (int) (Math.random() * MAX_STEP_VALUE + MIN_STEP_VALUE);
1318
int hiddenNumberIndex = (int) (Math.random() * numbers.length);
1419
StringBuilder printNumbers = new StringBuilder("Question:");
1520
for (int i = 0; i < numbers.length; i++) {

0 commit comments

Comments
 (0)