Skip to content

Commit

Permalink
add Engine.printGameRules method
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeloie committed Jul 21, 2023
1 parent f68e5d1 commit 6280f73
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/hexlet/code/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public static void checkAnswer(String userAnswer, String correctAnswer) {
}
}

public static void printGameRules(String gameName) {
String gameRule = switch (gameName) {
case ("Even") -> "Answer 'yes' if the number is even, otherwise answer 'no'.";
case ("Calc") -> "What is the result of the expression?";
case ("GCD") -> "Find the greatest common divisor of given numbers.";
case ("Progression") -> "What number is missing in the progression?";
case ("Prime") -> "Answer 'yes' if given number is prime. Otherwise answer 'no'.";
default -> "";
};
System.out.println(gameRule);
}




Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/hexlet/code/games/Calc.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Calc {

public static void playCalc() {
Engine.greetingUser();
System.out.println("What is the result of the expression?");
Engine.printGameRules("Calc");
for (int i = 0; i < 3; i++) {
String correctAnswer = askCalcQuestion();
String userAnswer = scan.nextLine();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/hexlet/code/games/Even.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Even {
public static void playEven() {

Engine.greetingUser();
System.out.println("Answer 'yes' if the number is even, otherwise answer 'no'.");
Engine.printGameRules("Even");
for (int i = 0; i < 3; i++) {
String correctAnswer = askEvenQuestion();
String userAnswer = scan.nextLine();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/hexlet/code/games/GCD.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class GCD {
public static void playGCD() {

Engine.greetingUser();
System.out.println("Find the greatest common divisor of given numbers.");
Engine.printGameRules("GCD");
for (int i = 0; i < 3; i++) {
String correctAnswer = askGCDQuestion();
String userAnswer = scan.nextLine();
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/hexlet/code/games/Prime.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import hexlet.code.Engine;

import java.util.ArrayList;
import java.util.Arrays;

import static hexlet.code.Engine.checkAnswer;
import static hexlet.code.Engine.rnd;
import static hexlet.code.Engine.scan;
import static hexlet.code.Engine.userName;
import static java.util.Arrays.binarySearch;

public class Prime {

public static void playPrime() {
Engine.greetingUser();
System.out.println("Answer 'yes' if given number is prime. Otherwise answer 'no'.");
Engine.printGameRules("GCD");
for (int i = 0; i < 3; i++) {
String correctAnswer = askPrimeQuestion();
String userAnswer = scan.nextLine();
Expand All @@ -24,9 +22,9 @@ public static void playPrime() {
}

public static String askPrimeQuestion() {
ArrayList<Integer> primeList = new ArrayList<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97));
int[] primeArray = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int hiddenNumber = rnd.nextInt(100);
System.out.printf("Question: %d%nYour answer: ", hiddenNumber);
return primeList.contains(hiddenNumber) ? "yes" : "no";
return binarySearch(primeArray, hiddenNumber) >= 0 ? "yes" : "no";
}
}
8 changes: 6 additions & 2 deletions app/src/main/java/hexlet/code/games/Progression.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@


import hexlet.code.Engine;
import static hexlet.code.Engine.*;

import static hexlet.code.Engine.checkAnswer;
import static hexlet.code.Engine.rnd;
import static hexlet.code.Engine.scan;
import static hexlet.code.Engine.userName;

public class Progression {

public static void playProgression() {
Engine.greetingUser();
System.out.println("What number is missing in the progression?");
Engine.printGameRules("Progression");
for (int i = 0; i < 3; i++) {
String correctAnswer = askProgQuestion();
String userAnswer = scan.nextLine();
Expand Down

0 comments on commit 6280f73

Please sign in to comment.