-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
183 lines (159 loc) · 5.96 KB
/
Copy pathMain.java
File metadata and controls
183 lines (159 loc) · 5.96 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package bullscows;
import java.lang.Math;
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder hiddenNum = new StringBuilder();
StringBuilder num = new StringBuilder();
StringBuilder hiddenNumProto = new StringBuilder();
int turns = 1;
int numberLength = 0;
int symbol = 0;
while(true) {
//System.out.println("loop a");
boolean isBreak = false;
while (true) {
System.out.println("Please, enter the secret code's length:");
String str = scanner.next();
try{
numberLength = Integer.parseInt(str);
}catch(NumberFormatException e){
System.out.println("Error: " + str + " isn't a valid number.");
isBreak = true;
break;
}
if (numberLength > 36 || numberLength <= 0) {
System.out.println("Error");
isBreak = true;
break;
} else {
break;
}
}
if(isBreak){System.exit(0);}
while (true) {
System.out.println("Input the number of possible symbols in the code:");
String str = scanner.next();
try{
symbol = Integer.parseInt(str);
}catch(NumberFormatException e){
System.out.println("Error: " + str + " isn't a valid number.");
isBreak = true;
break;
}
if (symbol <= 0) {
System.out.println("Error");
isBreak = true;
break;
} else if(symbol > 36){
System.out.println("Error: maximum number of possible symbols in the code is 36 (0-9, a-z)");
isBreak = true;
break;
}else {
break;
}
}
if(isBreak){System.exit(0);}
if(numberLength > symbol){
System.out.println("Error: it's not possible to generate a code with a length of "+ numberLength + " with "+ symbol + " unique symbols.");
System.exit(0);
}else {
break;
}
}
hiddenNumProto.append(generateRndomNumber_v4(symbol,numberLength));
hiddenNum.append(generateCharachkters(hiddenNumProto,symbol,numberLength));
if(symbol <= 10){
System.out.print("The secret is prepared: " + "*".repeat(numberLength) + " ");
System.out.print(" (0-");
System.out.print(symbol - 1);
System.out.println(")");
}else{
System.out.print("The secret is prepared: " + "*".repeat(numberLength) + " ");
System.out.print(" (0-9,");
System.out.print("a-");
int plusChar = symbol - 11;
char p = 'a';
p+= plusChar;
System.out.print(p);
System.out.println(")");
}
//System.out.println(hiddenNum);
System.out.println("Okay, let's start a game!");
while(true) {
int bulls = 0,cows = 0;
// System.out.println("Loop 2");
System.out.println("Turn" + turns + ":");
turns += 1;
num.append(scanner.next());
for (int i = 0; i < num.length(); i++) {
for (int j = 0; j < hiddenNum.length(); j++) {
if ((num.charAt(i) == hiddenNum.charAt(j)) && i == j) {
bulls++;
} else if (num.charAt(i) == hiddenNum.charAt(j)) {
cows++;
}
}
}
if (bulls == 0 && cows != 0) {
System.out.println("Grade: " + cows + " cow(s)");
}else if (cows == 0 && bulls != 0) {
System.out.println("Grade: " + bulls + " bull(s)");
}else if (bulls == 0 && cows == 0) {
System.out.println("Grade: None.");
}else {
System.out.println("Grade:" + bulls + " bull(s) and " + cows + " cow(s)");
}
if(bulls == num.length()){
System.out.println("Congratulations! You guessed the secret code.");
break;
}
num.setLength(0);
}
}
public static StringBuilder generateCharachkters (StringBuilder s,int l,int n){
if(l <= 10){
return s;
}
int counter = n/2;
Random random = new Random(54034);
ArrayList arrayList = new ArrayList<Character>();
int numberOfChars = random.nextInt(counter);
while (counter !=0) {
char protoChar = 'a';
int randomChar = random.nextInt(l - 11);
protoChar += randomChar;
int charPos = random.nextInt(n);
if(!arrayList.contains(protoChar)){
s.delete(charPos, charPos + 1).insert(charPos, protoChar);
arrayList.add(protoChar);
counter--;
}
}
return s;
}
public static StringBuilder generateRndomNumber_v4(int l,int n){
ArrayList<Integer> arrayList = new ArrayList<>();
StringBuilder s =new StringBuilder();
Random random = new Random();
int border = 9;
if(l < 10){
border = l;
}
while(n != 0){
//System.out.println("Loop 5");
int randomInt = random.nextInt(border);
if(arrayList.contains(randomInt)){
continue;
}else{
s.append(randomInt);
arrayList.add(randomInt);
n--;
}
}
return s;
}
}