Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovsenka committed Jun 2, 2024
1 parent c5c08af commit fe804df
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 30 deletions.
19 changes: 13 additions & 6 deletions app/src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

public class App {

private static final int SELECT_GREETING = 1;
private static final int SELECT_EVEN = 2;
private static final int SELECT_CALC = 3;
private static final int SELECT_GCD = 4;
private static final int SELECT_PROGRESSION = 5;
private static final int SELECT_PRIME = 6;

private static final String MENU = """
1 - Greet
2 - Even
Expand All @@ -25,30 +32,30 @@ public static void main(String[] args) {
System.out.print("Your choice: ");
int selected = new Scanner(System.in).nextInt();
switch (selected) {
case 1:
case SELECT_GREETING:
Cli.greeting();
break;
case 2:
case SELECT_EVEN:
Cli.greeting();
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
EvenGame.runGame();
break;
case 3:
case SELECT_CALC:
Cli.greeting();
System.out.println("What is the result of the expression?");
CalcGame.runGame();
break;
case 4:
case SELECT_GCD:
Cli.greeting();
System.out.println("Find the greatest common divisor of given numbers.");
GcdGame.runGame();
break;
case 5:
case SELECT_PROGRESSION:
Cli.greeting();
System.out.println("What number is missing in the progression?");
ProgressionGame.runGame();
break;
case 6:
case SELECT_PRIME:
Cli.greeting();
System.out.println("Answer 'yes' if given number is prime. Otherwise answer 'no'.");
PrimeGame.runGame();
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/hexlet/code/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Scanner;

public class Cli {
public static String playerName = "";
private static String playerName = "";
private static void askPlayerName() {
Scanner scanner = new Scanner(System.in);
System.out.print("May I have your name? ");
Expand All @@ -15,4 +15,12 @@ public static void greeting() {
Cli.askPlayerName();
System.out.println("Hello, " + playerName + "!");
}

public static String getPlayerName() {
return playerName;
}

public static void setPlayerName(String playerName) {
Cli.playerName = playerName;
}
}
14 changes: 11 additions & 3 deletions app/src/main/java/hexlet/code/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class Engine {
public static final int COUNT_ROUNDS = 3;

public static int score = 0;
private static int score = 0;

public static boolean startRound(String question, String correctAnswer) {
askQuestion(question);
Expand All @@ -14,12 +14,12 @@ public static boolean startRound(String question, String correctAnswer) {
System.out.println("Correct!");
Engine.score++;
if (Engine.score == Engine.COUNT_ROUNDS) {
System.out.println("You win! Congratulations, " + Cli.playerName + "!");
System.out.println("You win! Congratulations, " + Cli.getPlayerName() + "!");
}
return true;
}
System.out.println("'" + answer + "' is wrong answer ;(. Correct answer was '" + correctAnswer + "'.");
System.out.println("Let's try again, " + Cli.playerName + "!");
System.out.println("Let's try again, " + Cli.getPlayerName() + "!");
return false;
}

Expand All @@ -31,4 +31,12 @@ private static String getAnswer() {
System.out.print("Your answer: ");
return new Scanner(System.in).next();
}

public static int getScore() {
return score;
}

public static void setScore(int score) {
Engine.score = score;
}
}
14 changes: 9 additions & 5 deletions app/src/main/java/hexlet/code/games/CalcGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ public class CalcGame {

private static final String[] OPERATORS = {" + ", " - ", " * "};
private static final Random RANDOM = new Random();

private static final int MAX_NUMBER = 100;

private static final int MAX_COUNT_OPERATORS = 3;
public static void runGame() {
Engine.score = 0;
while (Engine.score != Engine.COUNT_ROUNDS) {
int num1 = RANDOM.nextInt(100);
int num2 = RANDOM.nextInt(100);
int operator = RANDOM.nextInt(3);
Engine.setScore(0);
while (Engine.getScore() != Engine.COUNT_ROUNDS) {
int num1 = RANDOM.nextInt(MAX_NUMBER);
int num2 = RANDOM.nextInt(MAX_NUMBER);
int operator = RANDOM.nextInt(MAX_COUNT_OPERATORS);
int correctAnswer = -1;
String question = num1 + OPERATORS[operator] + num2;
switch (operator) {
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/hexlet/code/games/EvenGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

public class EvenGame {
private static final Random RANDOM = new Random();

private static final int MAX_NUMBER = 10000;
public static void runGame() {
Engine.score = 0;
while (Engine.score != Engine.COUNT_ROUNDS) {
int number = RANDOM.nextInt(10000);
Engine.setScore(0);
while (Engine.getScore() != Engine.COUNT_ROUNDS) {
int number = RANDOM.nextInt(MAX_NUMBER);
String correctAnswer;
if (number % 2 == 0) {
correctAnswer = "yes";
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/hexlet/code/games/GcdGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

public class GcdGame {
private static final Random RANDOM = new Random();

private static final int MAX_RAND_NUMBER = 200;
public static void runGame() {
Engine.score = 0;
while (Engine.score != Engine.COUNT_ROUNDS) {
int num1 = RANDOM.nextInt(200);
int num2 = RANDOM.nextInt(200);
Engine.setScore(0);
while (Engine.getScore() != Engine.COUNT_ROUNDS) {
int num1 = RANDOM.nextInt(MAX_RAND_NUMBER);
int num2 = RANDOM.nextInt(MAX_RAND_NUMBER);
int correctAnswer;
String question = num1 + " " + num2;
correctAnswer = Utils.getGcd(num1, num2);
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/hexlet/code/games/PrimeGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
public class PrimeGame {
private static final Random RANDOM = new Random();

private static final int MAX_RANDOM_NUMBER = 100;

public static void runGame() {
Engine.score = 0;
while (Engine.score != Engine.COUNT_ROUNDS) {
int number = RANDOM.nextInt(100) + 1;
Engine.setScore(9);
while (Engine.getScore() != Engine.COUNT_ROUNDS) {
int number = RANDOM.nextInt(MAX_RANDOM_NUMBER) + 1;
String correctAnswer;
if (Utils.isPrime(number)) {
correctAnswer = "yes";
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/hexlet/code/games/ProgressionGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

public class ProgressionGame {
private static final Random RANDOM = new Random();
private static final int MAX_LEN_PROGRESSION = 10;
private static final int MAX_INIT_VALUE = 25;
private static final int MAX_D_VALUE = 15;
public static void runGame() {
Engine.score = 0;
while (Engine.score != Engine.COUNT_ROUNDS) {
Engine.setScore(0);
while (Engine.getScore() != Engine.COUNT_ROUNDS) {
StringBuilder progression = new StringBuilder();
int d = RANDOM.nextInt(10) + 1;
int value = RANDOM.nextInt(25) + 1;
int lengthProgression = RANDOM.nextInt(8) + 5;
int d = RANDOM.nextInt(MAX_D_VALUE) + 1;
int value = RANDOM.nextInt(MAX_INIT_VALUE) + 1;
int lengthProgression = RANDOM.nextInt(MAX_LEN_PROGRESSION) + 1;
int position = RANDOM.nextInt(lengthProgression);
String correctAnswer = "";
for (int i = 0; i < lengthProgression; i++) {
Expand Down

0 comments on commit fe804df

Please sign in to comment.