-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLingo.java
More file actions
77 lines (53 loc) · 2.06 KB
/
Lingo.java
File metadata and controls
77 lines (53 loc) · 2.06 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
import java.io.*;
import java.util.*;
public class Lingo {
public static void main(String[] args){
try{
File file = new File("5LetterWords.txt");
FileReader fileReader = new FileReader(file);
BufferedReader input = new BufferedReader(fileReader);
ArrayList<String> words = new ArrayList<String>();
String line;
while ((line = input.readLine()) != null) {
words.add(line);
}
Random rn = new Random();
int index = rn.nextInt(words.size());
String trueWord = words.get(index).toUpperCase();
//Now for the Lingo Logic
Scanner in = new Scanner(System.in);
String[] hint = trueWord.split("");
String firstLetter = hint[0].toUpperCase();
hint[0] = firstLetter;
for(int i = 1; i < hint.length; i++){
hint[i] = ".";
}
while(true){
for(int i = 0; i < hint.length; i++){
System.out.print(hint[i]);
}
//Reset hint now - looks better
for(int i = 1; i < hint.length; i++){
hint[i] = ".";
}
System.out.println();
String guess = in.nextLine().toUpperCase();
if(guess.equals(trueWord)){
System.out.println("You win!");
break;
}
for(int i = 0; i < guess.length(); i++){//Assumes length 5 of guess
if(guess.charAt(i) == trueWord.charAt(i)){
char letter = trueWord.charAt(i);
hint[i] = Character.toString(letter).toUpperCase();
} else if(trueWord.contains(Character.toString(guess.charAt(i)).toUpperCase())){
char letter = guess.charAt(i);
hint[i] = Character.toString(letter).toLowerCase();
}
}
}
} catch (IOException error) { //Need this for using buffered readers
error.printStackTrace();
}
}
}