Skip to content

Commit 94a42fe

Browse files
committed
Added PrimeGame class
1 parent 18bb3c2 commit 94a42fe

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

app/Prime.cast

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{"version": 2, "width": 134, "height": 24, "timestamp": 1759591849, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2+
[0.019361, "o", "\u001b[?2004h\u001b]0;aseccxz@aseccxz-Lenovo-Legion-5-15ARH05H: ~/firstProject/java-project-61/app\u0007\u001b[01;32maseccxz@aseccxz-Lenovo-Legion-5-15ARH05H\u001b[00m:\u001b[01;34m~/firstProject/java-project-61/app\u001b[00m$ "]
3+
[1.637944, "o", "m"]
4+
[1.766101, "o", "a"]
5+
[1.925712, "o", "k"]
6+
[2.137737, "o", "e"]
7+
[2.669547, "o", " "]
8+
[2.850019, "o", "run-dist "]
9+
[3.558804, "o", "\r\n\u001b[?2004l\r"]
10+
[3.563449, "o", "./build/install/app/bin/app"]
11+
[3.56433, "o", "\r\n"]
12+
[3.620206, "o", "Please enter the game number and press Enter.\r\n1 - Greet\r\n2 - Even\r\n3 - Calc\r\n4 - GCD\r\n5 - Progression\r\n6 - Prime\r\n0 - Exit\r\n"]
13+
[5.041887, "o", "6"]
14+
[5.737916, "o", "\r\n"]
15+
[5.744713, "o", "Your choice: 6\r\n"]
16+
[5.745326, "o", "Welcome to the Brain Games!\r\nMay I have your name? "]
17+
[7.402201, "o", "A"]
18+
[7.690431, "o", "l"]
19+
[7.814064, "o", "e"]
20+
[8.090372, "o", "x"]
21+
[8.25407, "o", "\r\n"]
22+
[8.273789, "o", "Hello, Alex!\r\n"]
23+
[8.274993, "o", "Answer 'yes' if given number is prime. Otherwise answer 'no'.\r\n"]
24+
[8.27587, "o", "Question: 80\r\n"]
25+
[9.797744, "o", "n"]
26+
[9.909507, "o", "o"]
27+
[10.245345, "o", "\r\n"]
28+
[10.246011, "o", "Correct!\r\n"]
29+
[10.246326, "o", "Question: 19\r\n"]
30+
[13.109381, "o", "y"]
31+
[13.202215, "o", "e"]
32+
[13.485748, "o", "s"]
33+
[13.693814, "o", "\r\n"]
34+
[13.694199, "o", "Correct!\r\n"]
35+
[13.694454, "o", "Question: 15\r\n"]
36+
[18.282984, "o", "n"]
37+
[18.374196, "o", "o"]
38+
[19.126435, "o", "\r\n"]
39+
[19.126858, "o", "Correct!\r\n"]
40+
[19.127611, "o", "Congratulations, Alex!\r\n"]
41+
[19.144513, "o", "\u001b[?2004h\u001b]0;aseccxz@aseccxz-Lenovo-Legion-5-15ARH05H: ~/firstProject/java-project-61/app\u0007\u001b[01;32maseccxz@aseccxz-Lenovo-Legion-5-15ARH05H\u001b[00m:\u001b[01;34m~/firstProject/java-project-61/app\u001b[00m$ "]
42+
[21.618408, "o", "make run-dist "]
43+
[22.258252, "o", "\r\n\u001b[?2004l\r"]
44+
[22.262232, "o", "./build/install/app/bin/app\r\n"]
45+
[22.308379, "o", "Please enter the game number and press Enter.\r\n1 - Greet\r\n2 - Even\r\n3 - Calc\r\n4 - GCD\r\n5 - Progression\r\n6 - Prime\r\n0 - Exit\r\n"]
46+
[23.520516, "o", "6"]
47+
[24.182453, "o", "\r\n"]
48+
[24.192005, "o", "Your choice: 6\r\n"]
49+
[24.192761, "o", "Welcome to the Brain Games!\r\nMay I have your name? "]
50+
[24.897701, "o", "A"]
51+
[25.169957, "o", "l"]
52+
[25.338711, "o", "e"]
53+
[25.581763, "o", "x"]
54+
[25.770621, "o", "\r\n"]
55+
[25.788784, "o", "Hello, Alex!\r\n"]
56+
[25.790052, "o", "Answer 'yes' if given number is prime. Otherwise answer 'no'.\r\n"]
57+
[25.791498, "o", "Question: 29\r\n"]
58+
[28.749961, "o", "y"]
59+
[28.830114, "o", "e"]
60+
[29.378257, "o", "s"]
61+
[29.534331, "o", "\r\n"]
62+
[29.53483, "o", "Correct!\r\n"]
63+
[29.535, "o", "Question: 48\r\n"]
64+
[31.230339, "o", "y"]
65+
[31.345821, "o", "e"]
66+
[31.602144, "o", "s"]
67+
[31.773209, "o", "\r\n"]
68+
[31.786547, "o", "'yes' is wrong answer ;(. Correct answer was 'no'.\r\n"]
69+
[31.786845, "o", "Let's try again, Alex!\r\n"]
70+
[31.80281, "o", "\u001b[?2004h\u001b]0;aseccxz@aseccxz-Lenovo-Legion-5-15ARH05H: ~/firstProject/java-project-61/app\u0007\u001b[01;32maseccxz@aseccxz-Lenovo-Legion-5-15ARH05H\u001b[00m:\u001b[01;34m~/firstProject/java-project-61/app\u001b[00m$ "]
71+
[33.842286, "o", "\u001b[?2004l\r\r\nexit\r\n"]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static void main(String[] args) {
1111
3 - Calc
1212
4 - GCD
1313
5 - Progression
14+
6 - Prime
1415
0 - Exit""");
1516
int gameChoice;
1617
Scanner input = new Scanner(System.in);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package hexlet.code;
22

3-
import hexlet.code.games.CalcGame;
4-
import hexlet.code.games.EvenGame;
5-
import hexlet.code.games.GCDGame;
6-
import hexlet.code.games.ProgressionGame;
3+
import hexlet.code.games.*;
74

85
import java.util.Scanner;
96

@@ -32,6 +29,9 @@ public static void gameRun(int gameNum) {
3229
ProgressionGame.showGameRules(gameCount);
3330
solution = ProgressionGame.getSolution();
3431
break;
32+
case 6:
33+
PrimeGame.showGameRules(gameCount);
34+
solution = PrimeGame.getSolution();
3535
}
3636
playerAnswer = input.nextLine();
3737
if (solution.equals(playerAnswer)) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package hexlet.code.games;
2+
3+
public class PrimeGame {
4+
public static void showGameRules(int gameCount) {
5+
if (gameCount == 0) {
6+
System.out.println("Answer 'yes' if given number is prime. Otherwise answer 'no'.");
7+
}
8+
}
9+
public static String getSolution() {
10+
int guessedNumber = (int) (Math.random() * 100.0);
11+
System.out.println("Question: " + guessedNumber);
12+
if (guessedNumber < 2) {
13+
return "no";
14+
}
15+
if (guessedNumber == 2) {
16+
return "yes";
17+
}
18+
if (guessedNumber % 2 == 0) {
19+
return "no";
20+
}
21+
for (int i = 3; i * i <= guessedNumber ; i += 2) {
22+
if (guessedNumber % i == 0) {
23+
return "no";
24+
}
25+
}
26+
return "yes";
27+
}
28+
}

0 commit comments

Comments
 (0)