This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeBreaker.java
135 lines (106 loc) · 3.68 KB
/
CodeBreaker.java
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import static java.util.Collections.shuffle;
import static java.util.stream.Collectors.toList;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;
public class CodeBreaker {
private final Scanner scanner;
private final int MAX_NUMBER; // must be less than 10
private final int CODE_LENGTH; // must be less than MAX_NUMBER
private int tryCount;
private int playCount = 0;
public CodeBreaker() {
this(7, 3);
}
public CodeBreaker(int maxNumber, int codeLength) {
scanner = new Scanner(System.in);
MAX_NUMBER = maxNumber;
CODE_LENGTH = codeLength;
}
void newGame() {
tryCount = 0;
playCount++;
List<Integer> correctAnswer = newCorrectAnswer();
guessAnswer(correctAnswer);
if (playerWannaContinue()) {
newGame();
}
System.out.println("See you next time.");
}
private List<Integer> newCorrectAnswer() {
IntStream integerStream = IntStream.range(0, MAX_NUMBER + 1);
List<Integer> integerList = streamToIntegerList(integerStream);
shuffle(integerList);
return integerList.subList(0, CODE_LENGTH);
}
private List<Integer> streamToIntegerList(IntStream stream) {
return stream.boxed().collect(toList());
}
private void guessAnswer(List<Integer> correctAnswer) {
tryCount++;
System.out.print("What's your guess? ");
List<Integer> playerGuess = getPlayerGuess();
if (correctAnswer.equals(playerGuess)) {
showWinResults();
return;
}
showHints(correctAnswer, playerGuess);
guessAnswer(correctAnswer);
}
private List<Integer> getPlayerGuess() {
String playerInput = scanner.next();
if (isValidInput(playerInput)) {
return inputToIntegerList(playerInput);
}
return getPlayerGuess();
}
private boolean isValidInput(String input) {
if (!input.matches("[0-9]+")) {
System.out.print("Not a numeric number. ");
return false;
}
if (CODE_LENGTH != input.length()) {
System.out.print("Wrong number of digits. ");
return false;
}
return true;
}
private List<Integer> inputToIntegerList(String input) {
List<Integer> integerList = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
integerList.add(Integer.parseInt(String.valueOf(input.charAt(i))));
}
return integerList;
}
private void showWinResults() {
System.out.println("You Win!");
System.out.println("You tried " + tryCount + " time(s).");
System.out.println("And you played the game " + playCount + " time(s).");
}
private void showHints(List<Integer> correctAnswer, List<Integer> playerGuess) {
int hit = 0, blow = 0;
for (int i = 0; i < CODE_LENGTH; i++) {
for (int j = 0; j < CODE_LENGTH; j++) {
if (!correctAnswer.get(i).equals(playerGuess.get(j))) {
continue;
}
if (i == j) {
hit++;
} else {
blow++;
}
}
}
System.out.println(hit + " HIT, " + blow + " BLOW");
}
private boolean playerWannaContinue() {
System.out.print("Do you wanna continue? (y / n): ");
String input = scanner.next();
if (input.equals("y") || input.equals("n")) {
return input.equals("y");
}
System.out.print("Please enter y or n: ");
return playerWannaContinue();
}
}