-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
100 lines (79 loc) · 2.55 KB
/
Game.java
File metadata and controls
100 lines (79 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.Scanner;
import java.util.Random;
public class Game {
Scanner sc;
boolean running_round, running_game;
int round, rangeLimit, solution, maxRound;
public Game(int upperLimit, int maxRound) {
this.running_round = false;
this.running_game = true;
this.round = 0;
this.maxRound = maxRound;
this.rangeLimit = upperLimit;
this.sc = new Scanner(System.in);
}
private void roll() {
Random rand = new Random(); // instance of random class
solution = rand.nextInt(rangeLimit);
}
public void reaskNumber(int data) {
System.out.println("" + data);
}
public void showSolution() {
System.out.println("\nRozwiazaniem zagadki jest liczba: " + this.solution + "\n");
}
private void checkUserInput(int input) {
if (input < solution) {
System.out.println("Podałeś za małą liczbę");
} else if (input > solution) {
System.out.println("Podałeś za dużą liczbę");
} else {
System.out.println("\nGratuluję! Zgadłeś liczbę w " + this.round + " próbie\n");
this.running_round = false;
}
}
private void increaseRoundCounter() {
this.round++;
if (round > maxRound) {
System.out.println("Przekroczyles maksymalna ilosc prob.");
this.round = 0;
running_round = false;
}
}
private void clearRoundCounter() {
round = 0;
}
private void askToContinue() {
do {
System.out.println("Czy chesz kontynuowac gre : yes or no");
String decision = sc.nextLine();
switch (decision) {
case "yes":
this.running_game = true;
return;
case "no":
this.running_game = false;
return;
default:
System.out.println("Nieprawidlowy user input");
break;
}
} while (true);
}
public void play() {
do {
running_round = true;
roll();
showSolution();
System.out.println("Sprobuj szczęscia i podaj liczbe: ");
do {
int userInput = sc.nextInt();
sc.nextLine();
increaseRoundCounter();
checkUserInput(userInput);
} while (running_round);
askToContinue();
clearRoundCounter();
} while (running_game);
}
}