World Project

🧩 Syntax:
/**
 * CS 152 Lab 5 - Wordle
 * <p>
 * Implement the methods needed to play a Wordle-like game.
 * <p>
 * Student name: Yahir Lozoya
 */

import java.util.Scanner;
import java.util.Random;

/**
 * Contains methods for a wordle clone
 */
public class Wordle {

    /**
     * Represents a letter in the guess in the correct position.
     */
    public static final char CORRECT = 'X';

    /**
     * Represents a letter in the guess that is present, but in wrong place.
     */
    public static final char PRESENT = 'o';

    /**
     * Represents a letter in the guess does not occur in the word at all.
     */
    public static final char MISSING = '.';

    /**
     * How many guesses do we get to solve the puzzle?
     */
    public static final int NUMBER_OF_GUESSES = 6;

    /**
     * Picks a random word from the dictionary.
     *
     * @param dictionary An array of words.
     * @return Randomly chosen word from dictionary.
     */
    public static String pickRandomWord(String[] dictionary) {
        Random rand = new Random();
        int num = rand.nextInt(dictionary.length);
        return dictionary[num];
    }

    /**
     * Is the guess a recognized word?
     *
     * @param guess      The guess word.
     * @param dictionary Array of known words.
     * @return True if guess is in dictionary, false if not.
     */
    public static boolean isValidWord(String guess, String[] dictionary) {
        guess = guess.toLowerCase();
        // Goes through the array of dictionary
        for (String s : dictionary) {
            // Compares the guess to every string in the dictionary array
            // returning true if it is the same as one.
            if (guess.equals(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * How close is the guess to the secret word?
     *
     * @param guess Guessed word
     * @param word  The hidden word
     * @return Array of characters corresponding to the letters of guess,
     * where the char at a given index is:
     * CORRECT if the guess letter appears at that position in word
     * PRESENT if the guess letter is present in word elsewhere
     * MISSING if the guess letter does not occur in word
     */
    public static char[] getResultForGuess(String guess, String word) {
        char[] arr = new char[guess.length()];
        // Goes through every index of the String guess
        for (int i = 0; i < guess.length(); i++) {
            char indexGuess = guess.charAt(i);
            int count = 0;
            //Counting number of times of guess at i
            if (arr[i] == 0) {
                //Checks if the word doesn't contain the letter at index i of
                //guess
                if (!word.contains("" + indexGuess + "")) {
                    arr[i] = MISSING;
                } else {
                    //Counts the amount of time that letter appears in the word
                    for (int x = 0; x < guess.length(); x++) {
                        if (indexGuess == word.charAt(x)) {
                            count++;
                        }
                    }
                    //Checks if any of the letter of type index i in guess are
                    //in the correct spot within the guess and replaces them
                    //with correct if they do.
                    for (int y = 0; y < guess.length(); y++) {
                        //Checks if the letter at char i is equal to any other
                        //location within the array
                        if (guess.charAt(i) == guess.charAt(y)) {
                            if (guess.charAt(y) == word.charAt(y)) {
                                arr[y] = CORRECT;
                                count--;
                            }
                        }
                    }
                    for (int z = 0; z < guess.length(); z++) {
                        if (arr[z] == 0 && guess.charAt(i) == guess.charAt(z)) {
                            if (count > 0) {
                                arr[z] = PRESENT;
                                count--;
                            } else {
                                arr[z] = MISSING;
                            }
                        }
                    }


                }

            }
        }
        return arr;
    }

    /**
     * Is this a winning result?
     *
     * @param guessResult Array as returned by getResultForGuess
     * @return True if all places are CORRECT, false if not
     */
    public static boolean isWinningResult(char[] guessResult) {
        // Enhanced for loop goes through the array guessResult
        for (char c : guessResult) {
            // Checks if all the values are equal to the value of CORRECT, if
            // not it returns false, if so, it exits the for loop and returns
            // true.
            if (c != CORRECT) return false;
        }
        return true;
    }

    /**
     * Plays a console based Wordle game
     *
     * @param args Ignored
     */
    public static void main(String[] args) {
        System.out.println("Let's play Wordle!");
        System.out.println();
        // The big array of words is in a separate file
        String[] words = WordleDictionary.FIVE_LETTER_WORDS;

        Scanner in = new Scanner(System.in);

        String secret = "glass";
        int guesses = NUMBER_OF_GUESSES;

        boolean winning;

        do {
            System.out.println("What is your guess?");

            String guess = in.nextLine().trim().toLowerCase();  

            while (!isValidWord(guess, words)) {
                System.out.println("Not a recognized word! Try again");
                guess = in.nextLine().trim().toLowerCase();
            }

            char[] guessResult = getResultForGuess(guess, secret);
            System.out.println(new String(guessResult));

            winning = isWinningResult(guessResult);
            guesses--;

        } while (guesses > 0 && !winning);

        System.out.println("The word was " + secret);
    }
}