-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
73 lines (71 loc) · 2.1 KB
/
Main.java
File metadata and controls
73 lines (71 loc) · 2.1 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
import java.util.*;
import java.io.*;
class Main
{
public static void main(String[] args) throws FileNotFoundException
{
File wordsFile = new File("words.txt");
Scanner wordRead = new Scanner(wordsFile);
PrintStream output = new PrintStream(new File("userOutput.txt"));
Random gen = new Random();
Scanner userInput = new Scanner(System.in);
String userGuess = "";
String[] wordList = new String[5757];
for (int i = 0; i < wordList.length; i++)
{
wordList[i] = wordRead.nextLine();
}
System.out.println("There are " + wordList.length + " words in this list, selecting a random one for you to guess...");
String secretWord = "";
int secretIndex = 0;
while(true)
{
secretIndex = gen.nextInt(5757);
secretWord = wordList[secretIndex];
while (!userGuess.strip().equals(secretWord.strip()))
{
do
{
System.out.print("What guess do you have? (5 letters words): ");
userGuess = userInput.next();
}while (userGuess.length() != 5);
doWordle(userGuess, secretWord, 0, output);
System.out.println();
output.println();
}
}
}
public static void doWordle(String guess, String word, int index, PrintStream output)
{
if (index == guess.length())
{
if(guess.equals(word))
{
System.out.println();
System.out.println("Wow, you got it! Let's do another!");
}
return;
}
if(guess.charAt(index) == word.charAt(index))
{
System.out.print("[" + guess.charAt(index) + "]");
output.print("[" + guess.charAt(index) + "]");
doWordle(guess, word, index + 1, output);
return;
}
for (int j = 0; j < 5; j++)
{
if (guess.charAt(index) == word.charAt(j))
{
System.out.print("*" + guess.charAt(index) + "*");
output.print("*" + guess.charAt(index) + "*");
doWordle(guess, word, index + 1, output);
return;
}
}
System.out.print(guess.charAt(index));
output.print(guess.charAt(index));
doWordle(guess, word, index + 1, output);
return;
}
}