Skip to content

Commit 941c096

Browse files
committed
add Engine.java, rewrote Even and Calc games, deleted old asciinema
1 parent 0f23391 commit 941c096

File tree

5 files changed

+154
-76
lines changed

5 files changed

+154
-76
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,3 @@
44
### Hexlet tests and linter status:
55
[![Actions Status](https://github.com/DashProsh/java-project-61/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/DashProsh/java-project-61/actions)
66

7-
**Game №2Even**
8-
9-
View the recording at:
10-
[![asciicast](https://asciinema.org/a/7psX8BrLJUo8XcUTUS0IynlEH.svg)](https://asciinema.org/a/7psX8BrLJUo8XcUTUS0IynlEH)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ public static void main(String[] args) {
1010
Please enter the game number and press Enter.
1111
1 - Greet
1212
2 - Even
13+
3 - Calc
1314
0 - Exit
1415
Your choice:""");
1516

1617
String choice = scanner.nextLine();
1718

1819
switch (choice) {
19-
case "1" ->
20-
Cli.sayHello();
20+
case "1" -> Cli.sayHello();
2121

22-
case "0" ->
23-
Cli.sayGoodbye();
22+
case "0" -> Cli.sayGoodbye();
2423

25-
case "2" ->
26-
Games.even();
24+
case "2" -> Games.even();
25+
26+
case "3" -> Games.calc();
2727

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

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,3 @@ public static void sayGoodbye() {
2121
System.out.println("Bye!");
2222
}
2323
}
24-
25-
26-
27-
28-
29-
30-
31-
//Cli.sayHello();
32-
//Scanner scanner = new Scanner(System.in);
33-
//Random random = new Random();
34-
//int randomNumber = random.nextInt(100);
35-
// System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
36-
// System.out.println("Question: " + randomNumber);
37-
// System.out.println("Your answer: ");
38-
//String answer = scanner.nextLine();
39-
//int count = 0;
40-
// while (count < 4) {
41-
// if (randomNumber % 2 == 0) {
42-
//var rightAnswer1 = "yes";
43-
// if (answer.equals(rightAnswer1)) {
44-
// System.out.println("Correct!");
45-
// return count = count + 1;
46-
// }
47-
// System.out.println("'no' is wrong answer ;(. Correct answer was 'yes'.\n" +
48-
// "Let's try again, " + name + "!");
49-
//
50-
// } else if (randomNumber % 2 != 0) {
51-
//var rightAnswer2 = "no";
52-
// if (answer.equals(rightAnswer2)) {
53-
// System.out.println("Correct!");
54-
// return count = count + 1;
55-
// }
56-
// System.out.println("'yes' is wrong answer ;(. Correct answer was 'no'.\n" +
57-
// "Let's try again, " + name + "!");
58-
// }
59-
//
60-
// }
61-
// return randomNumber;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package hexlet.code;
2+
3+
import java.util.Scanner;
4+
5+
public class Engine {
6+
public static final int rounds = 3;
7+
8+
public static void skeletonOfGames(String mainGameQuestion, String[] question, String[] correctAnswer) {
9+
Scanner scanner = new Scanner(System.in);
10+
System.out.println("""
11+
Welcome to the Brain Games!
12+
May I have your name?""");
13+
String name = scanner.nextLine();
14+
System.out.println("Hello, " + name + "!");
15+
16+
System.out.println(mainGameQuestion);
17+
18+
for (var i = 0; i < rounds; i++) {
19+
System.out.println("Question: " + question[i]);
20+
System.out.println("Your answer: ");
21+
String userAnswer = scanner.nextLine();
22+
23+
if (userAnswer.equals(correctAnswer[i])) {
24+
System.out.println("Correct!");
25+
26+
} else {
27+
System.out.println("'" + userAnswer + "'" + "is wrong answer ;(. "
28+
+ "Correct answer was " + "'" + correctAnswer[i] + "'" + "."
29+
+ "Let's try again, "
30+
+ name
31+
+ "!");
32+
return;
33+
}
34+
}
35+
System.out.println("Congratulations, " + name + "!");
36+
}
37+
}
38+
39+
40+
41+
42+
//import java.util.Arrays;
43+
//import java.util.List;
44+
//import java.util.Scanner;
45+
//import java.util.Random;
46+
//import hexlet.code.Engine;
Lines changed: 102 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,122 @@
11
package hexlet.code;
22

3-
import java.util.Scanner;
3+
import java.util.Arrays;
4+
import java.util.List;
45
import java.util.Random;
56

67
public class Games {
78

89
// Game №2 Even
910
public static int even() {
10-
String name = Cli.sayHello();
1111

12-
Scanner scanner = new Scanner(System.in);
1312
Random random = new Random();
13+
String mainGameQuestion = "Answer 'yes' if the number is even, otherwise answer 'no'.";
1414

15-
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
15+
String[] question = new String[Engine.rounds];
16+
String[] correctAnswer = new String[Engine.rounds];
1617

17-
int count = 0;
18-
19-
while (count < 3) {
18+
for (var i = 0; i < Engine.rounds; i++) {
2019
int randomNumber = random.nextInt(99) + 1;
21-
System.out.println("Question: " + randomNumber);
22-
System.out.println("Your answer: ");
23-
String answer = scanner.nextLine();
20+
question[i] = Integer.toString(randomNumber);
21+
correctAnswer[i] = (randomNumber % 2 == 0) ? "yes" : "no";
22+
}
23+
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
24+
25+
return 0;
26+
}
27+
28+
29+
30+
// Game №3 Calc
31+
public static int calc() {
32+
33+
Random random = new Random();
34+
String mainGameQuestion = "What is the result of the expression?";
35+
36+
String[] question = new String[Engine.rounds];
37+
String[] correctAnswer = new String[Engine.rounds];
38+
39+
for (var i = 0; i < Engine.rounds; i++) {
40+
int randomNumber1 = random.nextInt(19) + 1;
41+
int randomNumber2 = random.nextInt(9) + 1;
42+
43+
String plus = randomNumber1 + "+" + randomNumber2;
44+
String minus = randomNumber1 + "-" + randomNumber2;
45+
String mult = randomNumber1 + "*" + randomNumber2;
2446

25-
if ((randomNumber % 2 == 0 && answer.equals("yes"))
26-
|| (randomNumber % 2 != 0 && answer.equals("no"))) {
27-
System.out.println("Correct!");
28-
count++;
47+
List<String> randomExpression = Arrays.asList(plus, minus, mult);
48+
Random rand = new Random();
49+
String randomQuestion = randomExpression.get(rand.nextInt(randomExpression.size()));
2950

51+
question[i] = randomQuestion;
52+
53+
if (randomQuestion.equals(plus)) {
54+
correctAnswer[i] = String.valueOf(randomNumber1 + randomNumber2);
55+
} else if (randomQuestion.equals(minus)) {
56+
correctAnswer[i] = String.valueOf(randomNumber1 - randomNumber2);
3057
} else {
31-
if (randomNumber % 2 == 0) {
32-
System.out.println("'no' is wrong answer ;(. Correct answer was 'yes'.\n"
33-
+ "Let's try again, "
34-
+ name
35-
+ "!");
36-
} else {
37-
System.out.println("'yes' is wrong answer ;(. Correct answer was 'no'.\n"
38-
+ "Let's try again, "
39-
+ name
40-
+ "!");
41-
}
42-
count = 0;
58+
correctAnswer[i] = String.valueOf(randomNumber1 * randomNumber2);
4359
}
4460
}
45-
System.out.println("Congratulations, " + name + "!");
46-
return count;
61+
Engine.skeletonOfGames(mainGameQuestion, question, correctAnswer);
62+
return 0;
4763
}
4864
}
65+
66+
67+
68+
// while (count < rounds) {
69+
// int randomNumber1 = random.nextInt(19) + 1;
70+
// int randomNumber2 = random.nextInt(9) + 1;
71+
//
72+
// String plus = randomNumber1 + "+" + randomNumber2;
73+
// String minus = randomNumber1 + "-" + randomNumber2;
74+
// String mult = randomNumber1 + "*" + randomNumber2;
75+
//
76+
// int resultPlus = randomNumber1 + randomNumber2;
77+
// int resultMinus = randomNumber1 - randomNumber2;
78+
// int resultMult = randomNumber1 * randomNumber2;
79+
//
80+
// List<String> givenList = Arrays.asList(plus, minus, mult);
81+
// Random rand = new Random();
82+
// String randomQuestion = givenList.get(rand.nextInt(givenList.size()));
83+
//
84+
// System.out.println("Question: " + randomQuestion);
85+
// System.out.println("Your answer: ");
86+
// String answer = scanner.nextLine();
87+
//
88+
// if (randomQuestion.equals(plus) && Integer.parseInt(answer) == resultPlus
89+
// || randomQuestion.equals(minus) && Integer.parseInt(answer) == resultMinus
90+
// || randomQuestion.equals(mult) && Integer.parseInt(answer) == resultMult) {
91+
// System.out.println("Correct!");
92+
// count++;
93+
//
94+
// } else {
95+
// if (randomQuestion.equals(plus)) {
96+
// System.out.println("'" + answer + "'" + " is wrong answer ;(. " +
97+
// "Correct answer was " + "'" + resultPlus + "'" + ".\n"
98+
// + "Let's try again, "
99+
// + name
100+
// + "!");
101+
// } else if (randomQuestion.equals(minus)){
102+
// System.out.println("'" + answer + "'" + " is wrong answer ;(. " +
103+
// "Correct answer was " + "'" + resultMinus + "'" + ".\n"
104+
// + "Let's try again, "
105+
// + name
106+
// + "!");
107+
// } else {
108+
// System.out.println("'" + answer + "'" + " is wrong answer ;(. " +
109+
// "Correct answer was " + "'" + resultMult + "'" + ".\n"
110+
// + "Let's try again, "
111+
// + name
112+
// + "!");
113+
// }
114+
// count = 0;
115+
// return count;
116+
// }
117+
// }
118+
// System.out.println("Congratulations, " + name + "!");
119+
// return count;
120+
// }
121+
//}
122+
//

0 commit comments

Comments
 (0)