From b15c9415682dace12d852613c3941d80805979f1 Mon Sep 17 00:00:00 2001 From: Neon Date: Mon, 2 Dec 2024 13:30:59 -0500 Subject: [PATCH] fiusebf --- .../src/main/java/com/saahild/Main.java | 2 +- .../src/main/java/com/saahild/Wordle.java | 77 ++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/projects/wordle/src/main/java/com/saahild/Main.java b/projects/wordle/src/main/java/com/saahild/Main.java index a84ce37..6ce767b 100644 --- a/projects/wordle/src/main/java/com/saahild/Main.java +++ b/projects/wordle/src/main/java/com/saahild/Main.java @@ -2,6 +2,6 @@ public class Main { public static void main(String[] args) { - System.out.println("\\x1b[45mHello world!"); + new Wordle("Tasco").playWordle(); } } \ No newline at end of file diff --git a/projects/wordle/src/main/java/com/saahild/Wordle.java b/projects/wordle/src/main/java/com/saahild/Wordle.java index 3cb241e..23c8205 100644 --- a/projects/wordle/src/main/java/com/saahild/Wordle.java +++ b/projects/wordle/src/main/java/com/saahild/Wordle.java @@ -1,7 +1,82 @@ +package com.saahild; + +import java.util.Arrays; +import java.util.Scanner; + public class Wordle { private String word; public Wordle(String word) { this.word = word; } - public + private String[] getWordList() { + return new String[] { "rossa","jetty","wizzo","cuppa","cohoe","gurks","squad","beisa","shrug","fossa","fluyt","camus","speed","mamil","array","polio","barns","panes","souts","limas","fetch","queck","twink"}; + } + private String pickRandomWord(String[] list) { + return list[Math.round((float) (Math.random() * list.length))]; + } + private String colorLetter(String letter, String color) { + return color + letter + Reset; + } + private String[] evaluateGuess(String guess, String secretWord) { + String[] words = new String[guess.length()]; + for (int i = 0; i < guess.length(); i++) { + char letter = guess.charAt(i); + int lIndex = secretWord.indexOf(letter); + if (lIndex >= 0) { + if (lIndex == guess.indexOf(letter)) { + words[i] = colorLetter(String.valueOf(letter), FgGreen); + } else { + words[i] = colorLetter(String.valueOf(letter), FgYellow); + } + } else { + words[i] = colorLetter(String.valueOf(letter), FgGray); + } + } + return words; + } + public void playWordle() { + System.out.println("Starting wordle..."); + String word = pickRandomWord(getWordList()); + if (System.getenv("REVEAL_WORD") != null) { + System.out.println("Cheater: " + word); + } + String[][] board = new String[5][5]; + run(board, word); + } + private void run(String[][] board, String word) { + System.out.println("========================================"); + if (board.length > 0) { + for (String[] row : board) { + System.out.println(String.join(" ", row)); + } + } else { + System.out.println(""); + } + System.out.println("========================================"); + String input = ""; // Initialize the input variable + System.out.print("Enter your guess: "); + Scanner scanner = new Scanner(System.in); + input = scanner.nextLine().substring(0,5); + // if (input.length() < 5) { + // System.out.println("Invalid input. Please enter a 5 letter word."); + // run(board, word); + // }else { + String[] guess = evaluateGuess(input, word); + if (Arrays.toString(guess).equals(word)) { + System.out.println("You win!"); + } else { + board[board.length] = guess; + run(board, word); + } + // } + } + private static final String FgGreen = "\u001B[32m"; + private static final String FgYellow = "\u001B[33m"; + private static final String FgGray = "\u001B[37m"; + private static final String Reset = "\u001B[0m"; + // public static void main(String[] args) { + // new Wordle("Tasco").playWordle(); + // } + + }