Skip to content

Commit 9c22212

Browse files
committed
added game №6 Prime, refreshed structure of the project
1 parent ea6c74c commit 9c22212

File tree

8 files changed

+190
-137
lines changed

8 files changed

+190
-137
lines changed

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

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

3+
import hexlet.code.Games.*;
4+
35
import java.util.Scanner;
46

57
public class App {
@@ -13,6 +15,7 @@ public static void main(String[] args) {
1315
3 - Calc
1416
4 - GCD
1517
5 - Progression
18+
6 - Prime
1619
0 - Exit
1720
Your choice:""");
1821

@@ -23,13 +26,15 @@ public static void main(String[] args) {
2326

2427
case "0" -> Cli.sayGoodbye();
2528

26-
case "2" -> Games.even();
29+
case "2" -> Even.even();
30+
31+
case "3" -> Calc.calc();
2732

28-
case "3" -> Games.calc();
33+
case "4" -> Gcd.gcd();
2934

30-
case "4" -> Games.gcd();
35+
case "5" -> Progression.progression();
3136

32-
case "5" -> Games.progression();
37+
case "6" -> Prime.prime();
3338

3439
default -> System.out.println("Invalid choice, please try again.");
3540

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package hexlet.code;
22

3-
43
import java.util.Scanner;
54

65
public class Engine {

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

Lines changed: 0 additions & 132 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package hexlet.code.Games;
2+
3+
import hexlet.code.Engine;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.Random;
8+
9+
10+
public class Calc {
11+
public static int calc() {
12+
13+
Random random = new Random();
14+
String mainGameQuestion = "What is the result of the expression?";
15+
16+
String[] question = new String[Engine.rounds];
17+
String[] correctAnswer = new String[Engine.rounds];
18+
19+
for (var i = 0; i < Engine.rounds; i++) {
20+
int randomNumber1 = random.nextInt(19) + 1;
21+
int randomNumber2 = random.nextInt(9) + 1;
22+
23+
String plus = randomNumber1 + "+" + randomNumber2;
24+
String minus = randomNumber1 + "-" + randomNumber2;
25+
String mult = randomNumber1 + "*" + randomNumber2;
26+
27+
List<String> randomExpression = Arrays.asList(plus, minus, mult);
28+
Random rand = new Random();
29+
String randomQuestion = randomExpression.get(rand.nextInt(randomExpression.size()));
30+
31+
question[i] = randomQuestion;
32+
33+
if (randomQuestion.equals(plus)) {
34+
correctAnswer[i] = String.valueOf(randomNumber1 + randomNumber2);
35+
} else if (randomQuestion.equals(minus)) {
36+
correctAnswer[i] = String.valueOf(randomNumber1 - randomNumber2);
37+
} else {
38+
correctAnswer[i] = String.valueOf(randomNumber1 * randomNumber2);
39+
}
40+
}
41+
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
42+
return 0;
43+
}
44+
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package hexlet.code.Games;
2+
3+
import hexlet.code.Engine;
4+
5+
import java.util.Random;
6+
7+
public class Even {
8+
public static int even() {
9+
10+
Random random = new Random();
11+
String mainGameQuestion = "Answer 'yes' if the number is even, otherwise answer 'no'.";
12+
13+
String[] question = new String[Engine.rounds];
14+
String[] correctAnswer = new String[Engine.rounds];
15+
16+
for (var i = 0; i < Engine.rounds; i++) {
17+
int randomNumber = random.nextInt(99) + 1;
18+
question[i] = Integer.toString(randomNumber);
19+
correctAnswer[i] = (randomNumber % 2 == 0) ? "yes" : "no";
20+
}
21+
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
22+
return 0;
23+
}
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package hexlet.code.Games;
2+
3+
import hexlet.code.Engine;
4+
5+
import java.math.BigInteger;
6+
import java.util.Random;
7+
8+
9+
public class Gcd {
10+
public static int gcd() {
11+
Random random = new Random();
12+
String mainGameQuestion = "Find the greatest common divisor of given numbers.";
13+
14+
String[] question = new String[Engine.rounds];
15+
String[] correctAnswer = new String[Engine.rounds];
16+
17+
for (var i = 0; i < Engine.rounds; i++) {
18+
19+
int randomCommon = random.nextInt(10) + 2;
20+
int randomNumber1 = randomCommon * (random.nextInt(10) + 1);
21+
int randomNumber2 = randomCommon * (random.nextInt(10) + 1);
22+
23+
BigInteger bigA = BigInteger.valueOf(randomNumber1);
24+
BigInteger bigB = BigInteger.valueOf(randomNumber2);
25+
26+
String gcd = String.valueOf((bigA).gcd(bigB));
27+
28+
question[i] = randomNumber1 + " " + randomNumber2;
29+
correctAnswer[i] = gcd;
30+
}
31+
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
32+
return 0;
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package hexlet.code.Games;
2+
3+
import hexlet.code.Engine;
4+
5+
import java.util.Random;
6+
7+
public class Prime {
8+
public static int prime() {
9+
10+
Random random = new Random();
11+
String mainGameQuestion = "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
12+
13+
String[] question = new String[Engine.rounds];
14+
String[] correctAnswer = new String[Engine.rounds];
15+
16+
for (var i = 0; i < Engine.rounds; i++) {
17+
int randomNumber = random.nextInt(20) + 2;
18+
question[i] = Integer.toString(randomNumber);
19+
20+
boolean isPrime = true;
21+
22+
for (int j = 2; j <= (randomNumber - 1); j++) {
23+
if (randomNumber % j == 0) {
24+
isPrime = false;
25+
break;
26+
} isPrime = true;
27+
28+
} correctAnswer[i] = isPrime ? "yes" : "no";
29+
}
30+
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
31+
return 0;
32+
}
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package hexlet.code.Games;
2+
3+
import hexlet.code.Engine;
4+
5+
import java.util.Arrays;
6+
import java.util.Random;
7+
8+
public class Progression {
9+
public static int progression() {
10+
Random random = new Random();
11+
String mainGameQuestion = "What number is missing in the progression?";
12+
13+
String[] question = new String[Engine.rounds];
14+
String[] correctAnswer = new String[Engine.rounds];
15+
16+
for (var i = 0; i < Engine.rounds; i++) {
17+
18+
int arrayLength = random.nextInt(5) + 5;
19+
int[] array = new int[arrayLength];
20+
21+
int step = random.nextInt(9) + 1;
22+
int start = random.nextInt(14) + 1;
23+
24+
for (int j = 0; j < arrayLength; j++) {
25+
array[j] = start + j * step;
26+
}
27+
28+
int indexFromArray = random.nextInt(arrayLength - 1);
29+
30+
String arrayToString = Arrays.toString(array);
31+
String elementToHide = String.valueOf(array[indexFromArray]);
32+
String oneElement = "\\b" + elementToHide + "\\b";
33+
String arrayHideNumber = arrayToString.replaceFirst(oneElement, "..");
34+
arrayHideNumber = arrayHideNumber.replace("[", "").replace("]", "")
35+
.replace(",", "");
36+
37+
question[i] = arrayHideNumber;
38+
correctAnswer[i] = String.valueOf(array[indexFromArray]);
39+
40+
}
41+
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
42+
return 0;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)