-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWord.java
More file actions
154 lines (137 loc) · 4.97 KB
/
Word.java
File metadata and controls
154 lines (137 loc) · 4.97 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
import java.util.*;
import java.io.*;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
import javax.script.ScriptEngineFactory;
import javax.swing.text.Position;
import java.net.UnknownHostException;
import java.net.http.*;
public class Word {
private String word;
private String scrambledWord;
public Word(int length) {
word = getWordFromFile(length);
scrambledWord = listToString(scramble(word));
while (scrambledWord.equals(word)){
scrambledWord = listToString(scramble(word));
}
}
public static void main(String[] args) {
spaceOut("hello");
System.out.println(checkForWord("hello"));
System.out.println(checkCharacters("Hello", "helloo"));
}
public String getWord(){
return word;
}
public String getScrambledWord(){
return scrambledWord;
}
public void update(int length) {
word = getWordFromFile(length);
scrambledWord = listToString(scramble(word));
while (scrambledWord.equals(word)){
scrambledWord = listToString(scramble(word));
}
}
public static ArrayList<String> getAllWords() {
File file = new File("./words.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
ArrayList<String> words = new ArrayList<>();
String st;
while ((st = br.readLine()) != null) {
words.add(st);
}
br.close();
return words;
} catch(Exception e){
System.out.print(e);
return null;
}
}
public static String getWordFromFile(int wordLength) {
ArrayList<String> words = getAllWords();
ArrayList<String> possibleWords = new ArrayList<>();
for (int i = 0; i < words.size(); i++) {
if (words.get(i).length() == wordLength) {
possibleWords.add(words.get(i));
}
}
int randomIndex = (int) (Math.random() * possibleWords.size());
return possibleWords.get(randomIndex);
}
public static ArrayList<String> scramble(String input){
ArrayList<String> scrambled = new ArrayList<String>();
for (int i = 0; i < input.length(); i++){
scrambled.add(input.substring(i, i + 1));
}
for (int k = 0; k < scrambled.size(); k++){
int location = (int)(Math.random() * scrambled.size());
String temp = scrambled.get(location);
int destination = (int)(Math.random() * scrambled.size());
scrambled.set(location, scrambled.get(destination));
scrambled.set(destination, temp);
}
return scrambled;
}
public static String listToString(ArrayList<String> scrambled){
String result = "";
for (int j = 0; j < scrambled.size(); j++){
result += scrambled.get(j);
}
return result;
}
public static String spaceOut(String scrambledWord) {
String spacedOutWord = "";
for (int i = 0; i < scrambledWord.length(); i++) {
spacedOutWord = spacedOutWord + scrambledWord.substring(i, i+1) + " ";
}
return spacedOutWord;
}
public static boolean checkForWord(String word) {
String apiUrl = "https://api.dictionaryapi.dev/api/v2/entries/en/";
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl+word))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
if (response.body().indexOf("No Definitions Found") != -1){
return false;
}
}
catch (MalformedURLException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
} catch (InterruptedException e) {
System.out.println(e);
}
return true;
}
public static boolean checkCharacters(String first, String second){
first = first.toLowerCase();
second = second.toLowerCase();
ArrayList<String> firstWord = new ArrayList<String>();
for (int i = 0; i < first.length(); i++){
firstWord.add(first.substring(i, i + 1));
}
ArrayList<String> secondWord = new ArrayList<String>();
for (int j = 0; j < second.length(); j++){
secondWord.add(second.substring(j, j + 1));
}
for (int f = 0; f < firstWord.size(); f++){
for (int s = 0; s < secondWord.size(); s++){
if (firstWord.get(f).equals(secondWord.get(s))){
secondWord.remove(s);
firstWord.set(f, "");
}
}
}
if(secondWord.size() == 0){
return true;
}
return false;
}
}