Skip to content

Instantly share code, notes, and snippets.

@shai-almog
Created January 16, 2023 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shai-almog/778708aeaf7dcecdcd2b9d166ba77e03 to your computer and use it in GitHub Desktop.
Save shai-almog/778708aeaf7dcecdcd2b9d166ba77e03 to your computer and use it in GitHub Desktop.
A trivial command line wordle game with no real dictionary
import java.util.Scanner;
public class Main {
private static final String WORD = "LYMPH";
private static final String[] DICTIONARY = {"CRATE", "USING", "LYMPH", "LUMPS", "LOOMS"};
public static void main(String[] args) {
System.out.println("Write a guess:");
Scanner scanner = new Scanner(System.in);
int attempts = 0;
for(String line = scanner.nextLine() ; line != null ; line = scanner.nextLine()) {
if(line.length() != 5) {
System.out.println("5 characters only... Please try again:");
continue;
}
line = line.toUpperCase();
if(line.equals(WORD)) {
System.out.println("Success!!!");
return;
}
if(!isInDictionary(line)) {
System.out.println("Word not in dictionary... Try again:");
} else {
attempts++;
printWordResult(line);
if(attempts > 7) {
System.out.println("Game over!");
return;
}
}
}
}
private static void printWordResult(String word) {
for(int iter = 0 ; iter < word.length() ; iter++) {
char currentChar = word.charAt(iter);
if(currentChar == WORD.charAt(iter)) {
System.out.print("G"); // Green
continue;
}
if(WORD.indexOf(currentChar) > -1) {
System.out.print("Y"); // Yellow
continue;
}
System.out.print("B"); // Black
}
System.out.println();
}
private static boolean isInDictionary(String word) {
for(String current : DICTIONARY) {
if(current.equals(word)) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment