|
1 | 1 | package hexlet.code; |
2 | 2 |
|
3 | | -import hexlet.code.games.CalcGame; |
4 | | -import hexlet.code.games.EvenGame; |
5 | | -import hexlet.code.games.GCDGame; |
6 | | -import hexlet.code.games.PrimeGame; |
7 | | -import hexlet.code.games.ProgressionGame; |
8 | 3 | import java.util.Scanner; |
9 | 4 |
|
10 | 5 | public class Engine { |
11 | 6 | static final int NUMBER_OF_GAMES = 3; |
12 | 7 |
|
13 | | - public static void gameRun(String gameNum) { |
14 | | - Cli.greeting(); |
15 | | - int gameCount = 0; |
16 | | - String solution = ""; |
17 | | - String playerAnswer; |
| 8 | + public static void gameRun(String gameRules, String[][] questionsAndAnswers) { |
18 | 9 | Scanner input = new Scanner(System.in); |
19 | | - while (gameCount < NUMBER_OF_GAMES) { |
20 | | - if (gameNum.equals("1")) { |
21 | | - break; |
22 | | - } |
23 | | - switch (gameNum) { |
24 | | - case "2": |
25 | | - EvenGame.showGameRules(gameCount); |
26 | | - solution = EvenGame.getSolution(); |
27 | | - break; |
28 | | - case "3": |
29 | | - CalcGame.showGameRules(gameCount); |
30 | | - solution = CalcGame.getSolution(); |
31 | | - break; |
32 | | - case "4": |
33 | | - GCDGame.showGameRules(gameCount); |
34 | | - solution = GCDGame.getSolution(); |
35 | | - break; |
36 | | - case "5": |
37 | | - ProgressionGame.showGameRules(gameCount); |
38 | | - solution = ProgressionGame.getSolution(); |
39 | | - break; |
40 | | - case "6": |
41 | | - PrimeGame.showGameRules(gameCount); |
42 | | - solution = PrimeGame.getSolution(); |
43 | | - break; |
44 | | - default: |
45 | | - System.out.println("Unexpected input"); |
46 | | - } |
47 | | - playerAnswer = input.nextLine(); |
48 | | - if (solution.equals(playerAnswer)) { |
| 10 | + String playerAnswer; |
| 11 | + int gameCount = 0; |
| 12 | + System.out.println("Welcome to the Brain Games!"); |
| 13 | + System.out.print("May I have your name? "); |
| 14 | + String userName = input.next(); |
| 15 | + System.out.println("Hello, " + userName + "!"); |
| 16 | + System.out.println(gameRules); |
| 17 | + for (int i = 0; i < NUMBER_OF_GAMES; i++) { |
| 18 | + String question = questionsAndAnswers[i][0]; |
| 19 | + String answer = questionsAndAnswers[i][1]; |
| 20 | + System.out.println(question); |
| 21 | + playerAnswer = input.next(); |
| 22 | + if (answer.equals(playerAnswer)) { |
49 | 23 | System.out.println("Correct!"); |
50 | 24 | gameCount++; |
51 | 25 | } else { |
52 | | - System.out.println("'" + playerAnswer + "' is wrong answer ;(. Correct answer was '" + solution + "'."); |
53 | | - System.out.println("Let's try again, " + Cli.getName() + "!"); |
| 26 | + System.out.println("'" + playerAnswer + "' is wrong answer ;(. Correct answer was '" + answer + "'."); |
| 27 | + System.out.println("Let's try again, " + userName + "!"); |
54 | 28 | break; |
55 | 29 | } |
56 | | - } |
57 | | - if (gameCount == NUMBER_OF_GAMES) { |
58 | | - System.out.println("Congratulations, " + Cli.getName() + "!"); |
| 30 | + |
| 31 | + if (gameCount == NUMBER_OF_GAMES) { |
| 32 | + System.out.println("Congratulations, " + userName + "!"); |
| 33 | + } |
59 | 34 | } |
60 | 35 | } |
61 | 36 | } |
0 commit comments