淘先锋技术网

首页 1 2 3 4 5 6 7
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author Green.Gee
 * @date 2023/3/13 12:52
 * @email [email protected]
 */
public class GuessGAME {

    // guess the work and position
    public static void main(String[] args) {
        System.err.println(new Wordle("aaaaa").guess("bbbbb"));// .....
        System.err.println(new Wordle("aaaaa").guess("abbbb"));// A....

    }
}
/**
 * @See the picture in the end.
 * Because you will have to pay for it
 * or someone will have to pay for it one day or another.
 * Clearly never do that.
 * <p>
 * About record keyword
 * And this is the only way you can use to add some state to a record.
 */
record Wordle(String hiddenAsString) {
    record Guess (String guess){

        public int length () {
            return this.guess.length();
        }

        public GuessWithIndex hasALetterAtPosition ( int index){
            return new GuessWithIndex(this, index);
        }

        public int codePointAt ( int index){
            return this.guess.codePointAt(index);
        }

        public GuessWithIndex checkCharacterAtPosition ( int index){
            return new GuessWithIndex(this, index);
        }

        public LetterStream checkAgainst(Hidden hidden) {
            return new LetterStream(
                    IntStream.range(0, length())
                            .mapToObj(index -> checkCharacterAtPosition(index).with(hidden))
            );
        }
    }
    record LetterStream(Stream<Letter> stream) {
        String replaceEachLetterWith(Function<Letter, String> mapper) {
            return stream.map(mapper).collect(Collectors.joining());

        }


    }

    record GuessWithIndex (Guess guess,int index){
        public boolean isWellPlacedIn (Hidden hidden){
            return hidden.match(this, index, index);
        }

        public boolean isNotWellPlacedIn (Hidden hidden){
            return IntStream.range(0, hidden.length())
                    .filter(i -> index != i)
                    .filter(i -> guess.codePointAt(i) != hidden.codePointAt(i))
                    .anyMatch(i -> hidden.match(this, index, i));

        }

        public Letter with (Hidden hidden){
            if (isWellPlacedIn(hidden)) {
                return new WELL_PLACED(this.guess.codePointAt(this.index));

            } else if (isNotWellPlacedIn(hidden)) {
                return new NOT_WELL_PLACED(this.guess.codePointAt(this.index));

            } else {
                return new ABSENT();

            }
        }
    }


    record Hidden (String hidden,boolean[] used){

        public Hidden(String hidden) {
            this(hidden, new boolean[hidden.length()]);
        }

        public int codePointAt ( int index){
            return this.hidden.codePointAt(index);
        }

        public int length () {
            return this.hidden.length();
        }

        boolean match (GuessWithIndex guessWithIndex,int indexGuess, int indexHidden){
            if (used[indexHidden]) {
                return false;
            }
            boolean match = guessWithIndex.guess.codePointAt(indexGuess) ==
                    codePointAt(indexHidden);
            if (match) {
                used[indexHidden] = true;
            }
            return match;
        }

    }

    /**
     * @deprecated by ChatGPT
     * The keyword 'sealed' in JDK 17t is a feature of
     * the Java language that allows a class or interface to be declared as sealed.
     * Sealed classes and interfaces can only be extended or implemented by classes and interfaces in the same module.
     * This feature provides a way to control inheritance in Java,
     * allowing developers to restrict the types that can extend or implement a given class or interface.
     */
    sealed interface Letter permits WELL_PLACED, NOT_WELL_PLACED, ABSENT {
    }
    record WELL_PLACED (int codePoint) implements Letter {

    }
    record NOT_WELL_PLACED (int codePoint) implements Letter {
    }
    record ABSENT () implements Letter {
    }

    public String guess (String guessAsString){
        Guess guess = new Guess(guessAsString);
        Hidden hidden = new Hidden(hiddenAsString);

        return guess.checkAgainst(hidden)
                .replaceEachLetterWith(
                        l -> switch (l) {
                            case WELL_PLACED letter -> Character.toString(letter.codePoint()).toUpperCase();
                            case NOT_WELL_PLACED letter -> Character.toString(letter.codePoint());
                            case ABSENT letter -> ".";
                        });
           }
}

Don’t do that?

请添加图片描述