-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwordle.java
88 lines (66 loc) · 2.04 KB
/
wordle.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
import java.util.*;
class wordle {
public static List<String> Wordle(String guess, String solution) {
final int LENGTH = solution.length();
List<Character> splitSolution = new ArrayList<>(LENGTH);
List<Character> splitGuess = new ArrayList<>(LENGTH);
List<Boolean> solutionCharsTaken = new ArrayList<>(LENGTH);
List<String> statuses = new ArrayList<String>(LENGTH);
for(char ch : solution.toCharArray()) {
splitSolution.add(ch);
}
for(char ch : guess.toCharArray()) {
splitGuess.add(ch);
solutionCharsTaken.add(false);
statuses.add(null);
}
/*
Correct Cases
**/
int i = 0;
for(char ch: splitGuess) {
if(ch == splitSolution.get(i)) {
statuses.set(i, "correct");
solutionCharsTaken.set(i, true);
i++;
continue;
}
}
/**
* Absent Cases
*/
int j = 0;
for(char ch: splitGuess) {
if(statuses.get(j) != null) {
j++;
continue;
}
if(!splitSolution.contains(ch)) {
statuses.set(j, "absent");
j++;
continue;
}
/**
* Present Cases
*/
int indexOfPresentChar = !solutionCharsTaken.get(splitSolution.indexOf(ch)) ? splitSolution.indexOf(ch) : -1;
if(indexOfPresentChar > -1) {
statuses.set(j, "present");
solutionCharsTaken.add(j, true);
j++;
continue;
} else {
statuses.set(j, "absent");
j++;
continue;
}
}
return statuses;
}
public static void main(String args[]) {
List<String> statuses = wordle.Wordle("night","nilgt");
for(String st: statuses) {
System.out.println(st);
}
}
}