Skip to content

Commit a0f6288

Browse files
committed
add game Calc, add class Engine
1 parent 3de4fb5 commit a0f6288

File tree

12 files changed

+134
-46
lines changed

12 files changed

+134
-46
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
![Java CI](https://github.com/Prototype206/java-project-61/workflows/Quality%20Check/badge.svg)
77
![Java](https://img.shields.io/badge/Java-22-blue)
88

9-
![Greetings](screenshots/choice-1.png)
10-
![Even win](screenshots/choice-2-win.png)
11-
![Even lose](screenshots/choice-2-lose.png)
12-
![Exit](screenshots/choice-3-exit.png)
9+
### User's Greeting
10+
![Greetings](screenshots/Greetings.png)
11+
### Victory in the app Even
12+
![Even win](screenshots/EvenGame-win.png)
13+
### Defeat in the app Even
14+
![Even lose](screenshots/EvenGame-lose.png)
15+
### Victory in the app Calc
16+
![Calc win](screenshots/CalcGame-win.png)
17+
### Defeat in the app Calc
18+
![Calc lose](screenshots/CalcGame-lose.png)
19+
### Exiting the app
20+
![Exit](screenshots/Exiting-app.png)

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package hexlet.code;
22
import java.util.Scanner;
33

4+
import hexlet.code.games.Calc;
5+
import hexlet.code.games.Even;
6+
47
public class App {
58
public static void main(String[] args) {
69
Scanner scanner = new Scanner(System.in);
710
try {
811
System.out.println("Please enter the game number and press Enter.");
9-
System.out.print("1 - Greet\n2 - Even\n0 - Exit\nYour choice: ");
12+
System.out.print("1 - Greet\n2 - Even\n3 - Cacl\n0 - Exit\nYour choice: ");
1013
int usersChoise = 0;
1114

1215
try {
@@ -22,7 +25,13 @@ public static void main(String[] args) {
2225
break;
2326
case 2:
2427
Cli.greetings(scanner);
25-
Even.startGameEven(scanner);
28+
Even.startGameEven();
29+
Engine.runGame(Engine.EVEN_GAME_NUMBER, scanner);
30+
break;
31+
case 3:
32+
Cli.greetings(scanner);
33+
Calc.startGameCalc();
34+
Engine.runGame(Engine.CALC_GAME_NUMBER, scanner);
2635
break;
2736
case 0:
2837
break;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package hexlet.code;
2+
3+
import java.util.Scanner;
4+
5+
import hexlet.code.games.Calc;
6+
import hexlet.code.games.Even;
7+
8+
public class Engine {
9+
private static int scoreCounter = 0;
10+
private static int scoreToWin = 3;
11+
private static StringBuilder correctAnswer = new StringBuilder();
12+
private static StringBuilder usersAnswer = new StringBuilder();
13+
static final int EVEN_GAME_NUMBER = 2;
14+
static final int CALC_GAME_NUMBER = 3;
15+
16+
public static void runGame(int gameNumber, Scanner scanner) {
17+
for(int i = 0; i<scoreToWin; i++) {
18+
correctAnswer.setLength(0);
19+
usersAnswer.setLength(0);
20+
switch(gameNumber) {
21+
case EVEN_GAME_NUMBER:
22+
correctAnswer.append(Even.generateCorrectAnswer());
23+
break;
24+
case CALC_GAME_NUMBER:
25+
correctAnswer.append(Calc.generateCorrectAnswer());
26+
break;
27+
}
28+
System.out.print("Your answer: ");
29+
usersAnswer.append(scanner.nextLine());
30+
if(usersAnswer.toString().equals(correctAnswer.toString())) {
31+
System.out.println("Correct!");
32+
scoreCounter++;
33+
}
34+
else{
35+
System.out.println(usersAnswer + " is wrong answer ;(. Correct answer was " + correctAnswer.toString() + ".\nLet\'s try again, " + Cli.userName + "!");
36+
break;
37+
}
38+
if(scoreCounter == scoreToWin) {
39+
System.out.println("Congratulations, " + Cli.userName);
40+
}
41+
}
42+
}
43+
44+
}

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

Lines changed: 0 additions & 40 deletions
This file was deleted.
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 class Calc {
4+
private static int minValue = 0;
5+
private static int maxValue = 2;
6+
private static int operationNumber;
7+
private static int correctAnswer = 0;
8+
private static int numberOne;
9+
private static int numberTwo;
10+
11+
12+
public static void startGameCalc() {
13+
System.out.println("What is the result of the expression?");
14+
}
15+
16+
public static String generateCorrectAnswer() {
17+
numberOne = (int) (Math.random()*100);
18+
numberTwo = (int) (Math.random()*100);
19+
operationNumber = (int) (Math.random()*(maxValue-minValue+1)+minValue);
20+
21+
System.out.print("Question: " + numberOne);
22+
23+
switch(operationNumber) {
24+
case(0):
25+
System.out.println(" + " + numberTwo);
26+
correctAnswer = numberOne + numberTwo;
27+
break;
28+
case(1):
29+
System.out.println(" - " + numberTwo);
30+
correctAnswer = numberOne - numberTwo;
31+
break;
32+
case(2):
33+
System.out.println(" * " + numberTwo);
34+
correctAnswer = numberOne * numberTwo;
35+
break;
36+
}
37+
return String.valueOf(correctAnswer);
38+
}
39+
}
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 Even {
4+
private static int evenDivisor = 2;
5+
private static int oddRemainder = 1;
6+
private static StringBuilder correctAnswer = new StringBuilder();
7+
8+
public static void startGameEven() {
9+
System.out.println("Answer \'yes\' if the number is even, otherwise answer \'no\'.");
10+
}
11+
12+
13+
public static String generateCorrectAnswer() {
14+
correctAnswer.setLength(0);
15+
int randomNumber = (int) (Math.random()*100);
16+
if(randomNumber % evenDivisor == oddRemainder) {
17+
correctAnswer.append("no");
18+
}
19+
else {
20+
correctAnswer.append("yes");
21+
}
22+
23+
System.out.println("Question: " + randomNumber);
24+
return correctAnswer.toString();
25+
}
26+
27+
28+
}

screenshots/CalcGame - lose.png

22.8 KB
Loading

screenshots/CalcGame - win.png

18.5 KB
Loading
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)