Skip to content

Commit 4491335

Browse files
committed
add game GCD
1 parent 4e06e35 commit 4491335

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@
1616
![Calc win](screenshots/CalcGame-win.png)
1717
### Defeat in the app Calc
1818
![Calc lose](screenshots/CalcGame-lose.png)
19+
### Victory in the app GCD
20+
![GCD win](screenshots/GCDGame-win.png)
21+
### Defeat in the app GCD
22+
![GCD lose](screenshots/GCDGame-lose.png)
1923
### Exiting the app
2024
![Exit](screenshots/Exiting-app.png)

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
import hexlet.code.games.Calc;
55
import hexlet.code.games.Even;
6+
import hexlet.code.games.GCD;
67

78
public class App {
89
public static void main(String[] args) {
910
Scanner scanner = new Scanner(System.in);
1011
try {
1112
System.out.println("Please enter the game number and press Enter.");
12-
System.out.print("1 - Greet\n2 - Even\n3 - Cacl\n0 - Exit\nYour choice: ");
13+
System.out.print("1 - Greet\n2 - Even\n3 - Cacl\n4 - GCD\n0 - Exit\nYour choice: ");
1314
int usersChoise = 0;
1415

1516
try {
@@ -33,6 +34,11 @@ public static void main(String[] args) {
3334
Calc.startGameCalc();
3435
Engine.runGame(Engine.CALC_GAME_NUMBER, scanner);
3536
break;
37+
case 4:
38+
Cli.greetings(scanner);
39+
GCD.startGameGCD();
40+
Engine.runGame(Engine.GCD_GAME_NUMBER, scanner);
41+
break;
3642
case 0:
3743
break;
3844
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import hexlet.code.games.Calc;
66
import hexlet.code.games.Even;
7+
import hexlet.code.games.GCD;
78

89
public class Engine {
910
private static int scoreCounter = 0;
@@ -12,19 +13,24 @@ public class Engine {
1213
private static StringBuilder usersAnswer = new StringBuilder();
1314
static final int EVEN_GAME_NUMBER = 2;
1415
static final int CALC_GAME_NUMBER = 3;
16+
static final int GCD_GAME_NUMBER = 4;
1517

1618
public static void runGame(int gameNumber, Scanner scanner) {
1719
for(int i = 0; i<scoreToWin; i++) {
1820
correctAnswer.setLength(0);
1921
usersAnswer.setLength(0);
22+
2023
switch(gameNumber) {
2124
case EVEN_GAME_NUMBER:
2225
correctAnswer.append(Even.generateCorrectAnswer());
2326
break;
2427
case CALC_GAME_NUMBER:
2528
correctAnswer.append(Calc.generateCorrectAnswer());
2629
break;
30+
case GCD_GAME_NUMBER:
31+
correctAnswer.append(GCD.generateCorrectAnswer());
2732
}
33+
2834
System.out.print("Your answer: ");
2935
usersAnswer.append(scanner.nextLine());
3036
if(usersAnswer.toString().equals(correctAnswer.toString())) {
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 GCD {
4+
private static int numberA;
5+
private static int numberB;
6+
private static int numberC;
7+
8+
public static void startGameGCD() {
9+
System.out.println("Find the greatest common divisor of given numbers.");
10+
}
11+
12+
public static String generateCorrectAnswer() {
13+
numberA = (int) (Math.random()*100);
14+
numberB = (int) (Math.random()*100);
15+
System.out.println("Question: " + numberA + " " + numberB);
16+
while(true) {
17+
if(numberB > 0) {
18+
numberC = numberA;
19+
numberA = numberB;
20+
numberB = numberC % numberB;
21+
}
22+
else {
23+
break;
24+
}
25+
}
26+
return String.valueOf(numberA);
27+
}
28+
}

screenshots/GCDGame-lose.png

19.9 KB
Loading

screenshots/GCDGame-win.png

19.1 KB
Loading

0 commit comments

Comments
 (0)