Skip to content

Commit 5b1b2d6

Browse files
committed
add game Prime
1 parent 50348ca commit 5b1b2d6

File tree

6 files changed

+70
-16
lines changed

6 files changed

+70
-16
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@
3232
![Progression win](screenshots/ProgressionGame-win.png)
3333
### Defeat in the app Progression
3434
![Progression lose](screenshots/ProgressionGame-lose.png)
35+
### Victory in the app Prime
36+
![Prime win](screenshots/PrimeGame-win.png)
37+
### Defeat in the app Prime
38+
![Prime lose](screenshots/PrimeGame-lose.png)
3539
### Exiting the app
3640
![Exit](screenshots/Exiting-app.png)

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import hexlet.code.games.Calc;
55
import hexlet.code.games.Even;
66
import hexlet.code.games.GCD;
7+
import hexlet.code.games.Prime;
78
import hexlet.code.games.Progression;
89

910
public class App {
@@ -12,7 +13,7 @@ public static void main(String[] args) {
1213
try(Scanner scanner = new Scanner(System.in)) {
1314

1415
System.out.println("Please enter the game number and press Enter.");
15-
System.out.print("1 - Greet\n2 - Even\n3 - Cacl\n4 - GCD\n5 - Progression\n0 - Exit\nYour choice: ");
16+
System.out.print("1 - Greet\n2 - Even\n3 - Cacl\n4 - GCD\n5 - Progression\n6 - Prime\n0 - Exit\nYour choice: ");
1617
int usersChoise = 0;
1718

1819
try {
@@ -46,6 +47,11 @@ public static void main(String[] args) {
4647
Progression.startGameProgression();
4748
Engine.runGame(Engine.PROGRESSION_GAME_NUMBER, scanner);
4849
break;
50+
case 6:
51+
Cli.greetings(scanner);
52+
Prime.startGamePrime();
53+
Engine.runGame(Engine.PRIME_GAME_NUMBER, scanner);
54+
break;
4955
case 0:
5056
break;
5157
default:

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hexlet.code.games.Calc;
66
import hexlet.code.games.Even;
77
import hexlet.code.games.GCD;
8+
import hexlet.code.games.Prime;
89
import hexlet.code.games.Progression;
910

1011
public final class Engine {
@@ -16,6 +17,7 @@ public final class Engine {
1617
static final int CALC_GAME_NUMBER = 3;
1718
static final int GCD_GAME_NUMBER = 4;
1819
static final int PROGRESSION_GAME_NUMBER = 5;
20+
static final int PRIME_GAME_NUMBER = 6;
1921

2022
private Engine() {
2123
throw new AssertionError("Utility class instantiation prohibited");
@@ -27,21 +29,24 @@ public static void runGame(int gameNumber, Scanner scanner) {
2729
usersAnswer.setLength(0);
2830

2931
switch(gameNumber) {
30-
case EVEN_GAME_NUMBER:
31-
correctAnswer.append(Even.generateCorrectAnswer());
32-
break;
33-
case CALC_GAME_NUMBER:
34-
correctAnswer.append(Calc.generateCorrectAnswer());
35-
break;
36-
case GCD_GAME_NUMBER:
37-
correctAnswer.append(GCD.generateCorrectAnswer());
38-
break;
39-
case PROGRESSION_GAME_NUMBER:
40-
correctAnswer.append(Progression.generateCorrectAnswer());
41-
Progression.generateProgression();
42-
break;
43-
default:
44-
throw new IllegalArgumentException("Unknown number of game " + gameNumber);
32+
case EVEN_GAME_NUMBER:
33+
correctAnswer.append(Even.generateCorrectAnswer());
34+
break;
35+
case CALC_GAME_NUMBER:
36+
correctAnswer.append(Calc.generateCorrectAnswer());
37+
break;
38+
case GCD_GAME_NUMBER:
39+
correctAnswer.append(GCD.generateCorrectAnswer());
40+
break;
41+
case PROGRESSION_GAME_NUMBER:
42+
correctAnswer.append(Progression.generateCorrectAnswer());
43+
Progression.generateProgression();
44+
break;
45+
case PRIME_GAME_NUMBER:
46+
correctAnswer.append(Prime.generateCorrectAnswer());
47+
break;
48+
default:
49+
throw new IllegalArgumentException("Unknown number of game " + gameNumber);
4550
}
4651

4752
System.out.print("Your answer: ");
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+
public final class Prime {
4+
private static int number;
5+
private static int maxValue;
6+
private final static int MIN_VALUE = 3;
7+
private final static int EVEN_DIVISOR = 2;
8+
9+
10+
11+
private Prime() {
12+
throw new AssertionError("Utility class instantiation prohibited");
13+
}
14+
15+
public static void startGamePrime() {
16+
System.out.println("Answer \'yes\' if given number is prime. Otherwise answer \'no\'.");
17+
}
18+
19+
public static String generateCorrectAnswer() {
20+
number = (int) (Math.random()*100);
21+
maxValue = (int) (Math.ceil(Math.sqrt(number)));
22+
System.out.println("Question: " + number);
23+
if(number == EVEN_DIVISOR) {
24+
return "yes";
25+
}
26+
else if(number < EVEN_DIVISOR || number % EVEN_DIVISOR == 0) {
27+
return "no";
28+
}
29+
else {
30+
for(int i = MIN_VALUE; i < maxValue; i++) {
31+
if(number % i == 0) {
32+
return "no";
33+
}
34+
}
35+
return "yes";
36+
}
37+
}
38+
39+
}

screenshots/PrimeGame-lose.png

20.7 KB
Loading

screenshots/PrimeGame-win.png

19.6 KB
Loading

0 commit comments

Comments
 (0)