Skip to content

Commit

Permalink
Edits made
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSorb committed Feb 28, 2024
1 parent 6567878 commit 3dd169b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 91 deletions.
56 changes: 27 additions & 29 deletions app/src/main/java/hexlet/code/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,23 @@
public class Engine {
private static final int NUMBER_OF_QUESTION = 0;
private static final int NUMBER_OF_ANSWER = 1;
public static final int COUNT_LEVELS = 3;

public static String greeting() {
System.out.println("Welcome to the Brain Games!");
System.out.print("May I have your name ? ");
String userName = Engine.getUserInput();
System.out.println("Hello, " + userName + "!");
return userName;
}

private static String getUserInput() {
public static void startLevels(String gameRule, String[][]levels) {
Scanner inputStr = new Scanner(System.in);
return inputStr.next().trim();
}
public static void wrongAnswer(String userAnswer, String rightAnswer, String userName) {
String wrongMassage = "'"
+ userAnswer
+ "' is wrong answer ;(. Correct answer was '"
+ rightAnswer
+ "'.";

String restartMassage = "Let's try again, "
+ userName
+ "!";

System.out.println(wrongMassage);
System.out.println(restartMassage);
}

public static void startLevels(String gameRule, String[][]date) {
String userName = Engine.greeting();
System.out.println("Welcome to the Brain Games!");
String userName = getUserName(inputStr);
System.out.println(gameRule);

for (String[] level : date) {
for (String[] level : levels) {
String question = level[NUMBER_OF_QUESTION];
String rightAnswer = level[NUMBER_OF_ANSWER];

System.out.println("Question: " + question);

System.out.print("Your answer: ");
String userAnswer = Engine.getUserInput();
String userAnswer = inputStr.next().trim();
if (!userAnswer.equalsIgnoreCase(rightAnswer)) {
Engine.wrongAnswer(userAnswer, rightAnswer, userName);
return;
Expand All @@ -54,4 +31,25 @@ public static void startLevels(String gameRule, String[][]date) {
}
System.out.println("Congratulations, " + userName + "!");
}
public static void wrongAnswer(String userAnswer, String rightAnswer, String userName) {
String wrongMassage = "'"
+ userAnswer
+ "' is wrong answer ;(. Correct answer was '"
+ rightAnswer
+ "'.";

String restartMassage = "Let's try again, "
+ userName
+ "!";

System.out.println(wrongMassage);
System.out.println(restartMassage);
}
private static String getUserName(Scanner inputStream) {
System.out.print("May I have your name ? ");
String userName = inputStream.next().trim();
System.out.println("Hello, " + userName + "!");
return userName;
}

}
22 changes: 10 additions & 12 deletions app/src/main/java/hexlet/code/games/Calc.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ public class Calc {
private static final String[] SYMBOLS_OPERATION = {"+", "-", "*"};
private static final int MAX_NUMBER = 10;
private static final int MIN_NUMBER = 1;
private static final int COUNT_LEVELS = 3;


public static void startGame() {

String[][] result = new String[COUNT_LEVELS][2];
String[][] result = new String[Engine.COUNT_LEVELS][2];
for (int level = 0; level < result.length; level++) {
for (int i = 0; i < result[level].length; i++) {
int leftArgument = Utils.randomIntValue(MIN_NUMBER, MAX_NUMBER);
int rightArgument = Utils.randomIntValue(MIN_NUMBER, MAX_NUMBER);
int numberOfOperation = Utils.randomIntValue(0, SYMBOLS_OPERATION.length);

String question = leftArgument + " " + SYMBOLS_OPERATION[numberOfOperation] + " " + rightArgument;
int rightAnswer = Calc.getRightAnswer(leftArgument, rightArgument, numberOfOperation);
result[level][0] = question;
result[level][1] = Integer.toString(rightAnswer);
}
int leftArgument = Utils.randomIntValue(MIN_NUMBER, MAX_NUMBER);
int rightArgument = Utils.randomIntValue(MIN_NUMBER, MAX_NUMBER);
int numberOfOperation = Utils.randomIntValue(0, SYMBOLS_OPERATION.length);

String question = leftArgument + " " + SYMBOLS_OPERATION[numberOfOperation] + " " + rightArgument;
int rightAnswer = Calc.getRightAnswer(leftArgument, rightArgument, numberOfOperation);
result[level][0] = question;
result[level][1] = Integer.toString(rightAnswer);
}

Engine.startLevels(GAME_RULE, result);
Expand Down
14 changes: 6 additions & 8 deletions app/src/main/java/hexlet/code/games/Even.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@

public class Even {
private static final String GAME_RULE = "Answer 'yes' if the number is even, otherwise answer 'no'.";
private static final int COUNT_LEVELS = 3;


public static void startGame() {
String[][] levels = new String[COUNT_LEVELS][2];
String[][] levels = new String[Engine.COUNT_LEVELS][2];
for (int level = 0; level < levels.length; level++) {
for (int i = 0; i < levels[level].length; i++) {
int question = Utils.randomIntValue();

int question = Utils.randomIntValue();
String rightAnswer = Even.isEven(question) ? "yes" : "no";
levels[level][0] = Integer.toString(question);
levels[level][1] = rightAnswer;
}
String rightAnswer = Even.isEven(question) ? "yes" : "no";
levels[level][0] = Integer.toString(question);
levels[level][1] = rightAnswer;
}
Engine.startLevels(GAME_RULE, levels);
}
Expand Down
22 changes: 11 additions & 11 deletions app/src/main/java/hexlet/code/games/GCD.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

public class GCD {
private static final String GAME_RULE = "Find the greatest common divisor of given numbers.";
private static final int COUNT_LEVELS = 3;
private static final int MAX_NUMBER = 30;
private static final int MIN_NUMBER = 1;


public static void startGame() {

String[][] levels = new String[COUNT_LEVELS][2];
String[][] levels = new String[Engine.COUNT_LEVELS][2];
for (int level = 0; level < levels.length; level++) {
for (int i = 0; i < levels[level].length; i++) {
int leftArgument = Utils.randomIntValue();
int rightArgument = Utils.randomIntValue();

String question = leftArgument + " " + rightArgument;
int rightAnswer = GCD.getGCD(leftArgument, rightArgument);
levels[level][0] = question;
levels[level][1] = Integer.toString(rightAnswer);
}
int leftArgument = Utils.randomIntValue(MIN_NUMBER, MAX_NUMBER);
int rightArgument = Utils.randomIntValue(MIN_NUMBER, MAX_NUMBER);

String question = leftArgument + " " + rightArgument;
int rightAnswer = GCD.getGCD(leftArgument, rightArgument);
levels[level][0] = question;
levels[level][1] = Integer.toString(rightAnswer);

}

Engine.startLevels(GAME_RULE, levels);
Expand Down
18 changes: 6 additions & 12 deletions app/src/main/java/hexlet/code/games/Prime.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@
public class Prime {

private static final String GAME_RULE = "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
private static final int COUNT_LEVELS = 3;


public static void startGame() {

String[][] levels = new String[COUNT_LEVELS][2];
String[][] levels = new String[Engine.COUNT_LEVELS][2];
for (int level = 0; level < levels.length; level++) {
for (int i = 0; i < levels[level].length; i++) {

int question = Utils.randomIntValue();
String rightAnswer = Prime.isPrimeNumber(question) ? "yes" : "no";
int question = Utils.randomIntValue();
String rightAnswer = Prime.isPrimeNumber(question) ? "yes" : "no";

levels[level][0] = Integer.toString(question);
levels[level][1] = rightAnswer;
}
levels[level][0] = Integer.toString(question);
levels[level][1] = rightAnswer;
}

Engine.startLevels(GAME_RULE, levels);
}
private static boolean isPrimeNumber(int num) {
if (num == 1) {
if (num == 1 || num == 0) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
Expand Down
34 changes: 16 additions & 18 deletions app/src/main/java/hexlet/code/games/Progression.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ public class Progression {
private static final int RIGHT_BORDER_OF_SIZE_PROGRESSION = 10;
private static final int LEFT_BORDER_OF_STEP_PROGRESSION = 5;
private static final int RIGHT_BORDER_OF_STEP_PROGRESSION = 10;
private static final int COUNT_LEVELS = 3;

public static void startGame() {

String[][] levels = new String[COUNT_LEVELS][2];
String[][] levels = new String[Engine.COUNT_LEVELS][2];
for (int level = 0; level < levels.length; level++) {
for (int i = 0; i < levels[level].length; i++) {
int sizeProgression = Utils.randomIntValue(LEFT_BORDER_OF_SIZE_PROGRESSION,
RIGHT_BORDER_OF_SIZE_PROGRESSION);
int stepOfProgression = Utils.randomIntValue(LEFT_BORDER_OF_STEP_PROGRESSION,
RIGHT_BORDER_OF_STEP_PROGRESSION);
int[] progression = Progression.generateProgression(sizeProgression, stepOfProgression);
int positionOfWishingElement = Utils.randomIntValue(0, progression.length);


String question = getQuestion(progression, positionOfWishingElement);
int rightAnswer = progression[positionOfWishingElement];
levels[level][0] = question;
levels[level][1] = Integer.toString(rightAnswer);
}
int sizeProgression = Utils.randomIntValue(LEFT_BORDER_OF_SIZE_PROGRESSION,
RIGHT_BORDER_OF_SIZE_PROGRESSION);
int stepOfProgression = Utils.randomIntValue(LEFT_BORDER_OF_STEP_PROGRESSION,
RIGHT_BORDER_OF_STEP_PROGRESSION);
int start = Utils.randomIntValue();
int[] progression = Progression.generateProgression(start, sizeProgression, stepOfProgression);
int positionOfWishingElement = Utils.randomIntValue(0, progression.length);


String question = getQuestion(progression, positionOfWishingElement);
int rightAnswer = progression[positionOfWishingElement];
levels[level][0] = question;
levels[level][1] = Integer.toString(rightAnswer);
}

Engine.startLevels(GAME_RULE, levels);
Expand All @@ -50,10 +48,10 @@ private static String getQuestion(int[] progression, int positionOfWishingElemen
return result.toString().trim();
}

private static int[] generateProgression(int sizeProgression, int stepOfProgression) {
private static int[] generateProgression(int start, int sizeProgression, int stepOfProgression) {
int[] progression = new int[sizeProgression];

progression[0] = Utils.randomIntValue();
progression[0] = start;
for (int i = 1; i < progression.length; i++) {
progression[i] = progression[i - 1] + stepOfProgression;
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/hexlet/code/games/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static int randomIntValue(int leftBorder, int rightBorder) {
}

public static int randomIntValue() {
final int left = 1;
final int left = 0;
final int right = 26;
return randomIntValue(left, right);
}
Expand Down

0 comments on commit 3dd169b

Please sign in to comment.