JAVA implements the game "English Word Guessing Game"

foreword

The number of lines of code in "English Word Guessing Game" does not exceed 200 lines. It is a small game specially developed to memorize English words before.

main design

  1. Prepare word text in advance.

  2. In order to allow the player to interact with the program, use the following command to achieve the effect

    Scanner sc = new Scanner(System.in);
    
  3. Run the main method in WordleMaster

  4. Enter the first word in Wordle (the default first word is abort, it will be displayed in the console. It can be modified in the code)

  5. Input the judgment result in Wordle into the console.

    1. 0 means that the letter is not included, i.e. gray.
    2. 1 means the letter is included, but in the wrong position, i.e. yellow.
    3. 2 means the letter is included and in the correct position, i.e. green.
  6. Select a word from the console output and enter it into Wordle, and enter the serial number of the word into the console.

  7. Repeat steps 5-6 until you find the correct answer.

Functional screenshot

Games start:

image-202202219047717

Enter a word (this word can be set by yourself)

image-20220221909373

image-202202219550940

Code

Game startup class



public class WordleMaster {
    
    
    public static void main(String[] args) throws IOException {
    
    
        final String DEFAULT_FIRST_WORD = "abort";
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎使用 wordle-master !请在Wordle游戏中输入第一个单词:(输入回车则默认使用abort作为初始词)");
        System.out.println("提示:英文单词长度要为5!");
        Word lastWord = new Word(sc.nextLine());
        while (!lastWord.isValid()) {
    
    
            if (lastWord.getWord().equals("")) {
    
    
                lastWord = new Word(DEFAULT_FIRST_WORD);
                break;
            }
            System.out.println("请输入一个有效的单词!");
            lastWord = new Word(sc.nextLine());
        }
        System.out.println("初始词为:" + lastWord.getWord());
        Pattern pattern = new Pattern();
        // 输入Wordle结果
        int[] res = pattern.result();

        // 读取所有的单词
        List<Word> allWords = new ArrayList<>();

        File file = new File("wordle_words.txt");
        InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
        BufferedReader bufferedReader = new BufferedReader(reader);
        String word = bufferedReader.readLine();
        while (word != null){
    
    
            Word w = new Word(word);
            allWords.add(w);
            word = bufferedReader.readLine();
        }
        bufferedReader.close();
        reader.close();

        // 符合条件的单词
        List<Word> hope = allWords;
        while (hope.size() > 1){
    
    
            for (int i = 0; i < res.length; i++) {
    
    
                int finalI = i;
                Word finalLastWord = lastWord;
                // 如果出现单词中有两个相同字母的情况(如cheer)
                for (int j = 0; j < finalLastWord.getWord().length(); j++) {
    
    
                    for (int k = j + 1; k < finalLastWord.getWord().length(); k++) {
    
    
                        if (finalLastWord.getWord().charAt(j) == finalLastWord.getWord().charAt(k)){
    
    
                            if (res[j] == 0 && res[k] != 0){
    
    
                                res[j] = 3;
                                hope.remove(lastWord);
                            }else if(res[j] != 0 && res[k] == 0){
    
    
                                res[k] = 3;
                                hope.remove(lastWord);
                            }
                        }
                    }
                }
                switch (res[i]) {
    
    
                    case 0:
                        hope = hope.stream().filter(w -> w.notInclude(finalLastWord.getWord().charAt(finalI))).collect(Collectors.toList());
                        break;
                    case 2:
                        hope = hope.stream().filter(w -> w.getWord().charAt(finalI) == finalLastWord.getWord().charAt(finalI)).collect(Collectors.toList());
                        break;
                    case 1:
                        hope = hope.stream().filter(w -> w.notLocate(finalLastWord.getWord().charAt(finalI), finalI)).collect(Collectors.toList());
                        break;
                    default:
                }
            }
            System.out.println("size: " + hope.size());
            for (int i = 0; i < Math.min(10, hope.size()); i++) {
    
    
                System.out.print(i + ". " + hope.get(i).getWord() + "  ");
            }
            System.out.println();
            if (hope.size() > 1) {
    
    
                Scanner scanner = new Scanner(System.in);
                int chose = Integer.MAX_VALUE;
                while (chose > 9 || chose < 0) {
    
    
                    System.out.println("请选择一个:");
                    String s = scanner.nextLine();
                    chose = s.length() == 1 ? Integer.parseInt(s) : Integer.MAX_VALUE;
                }
                lastWord = hope.get(chose);
                System.out.println(lastWord.getWord());
                res = pattern.result();
            }
        }
    }

}

deal with


public class Pattern {
    
    

    private int[] pattern;


    /**
     * 输入wordle结果,五位数字组成。
     * 0:The letter is not in the word in any spot.
     * 1:The letter is in the word and in the correct spot.
     * 2:The letter is in the word but in the wrong spot.
     * @return  int数组
     */
    public int[] result(){
    
    
        String s = "";
        while (input(s) == null){
    
    
            System.out.println("输入单词判定结果:0为灰色,1为黄色,2为绿色。例如10120。");
            Scanner scanner = new Scanner(System.in);
            s = scanner.nextLine();
        }
        pattern = input(s);
        return pattern;
    }

    public int[] input(String s){
    
    
        if (s.length() != 5) return null;
        int[] res = new int[5];
        for (int i = 0; i < s.length(); i++) {
    
    
            if (s.charAt(i) < '0' || s.charAt(i) > '2') {
    
    
                return null;
            }
            res[i] = s.charAt(i) - '0';
        }
        return res;
    }

    public int[] getPattern() {
    
    
        return pattern;
    }
}

word judgment

public class Word {
    
    
    private final String word;

    Word(String word){
    
    
        this.word = word;
    }

    public boolean notInclude(char c){
    
    
        return !word.contains(String.valueOf(c));
    }

    public boolean notLocate(char c, int i){
    
    
        return word.contains(String.valueOf(c)) && word.charAt(i) != c;
    }

    public String getWord(){
    
    
        return this.word;
    }
    public boolean isValid() {
    
    
        if (word.length() != 5) {
    
    
            return false;
        }
        for (int i = 0; i < word.length(); i++) {
    
    
            if (word.charAt(i) < 'a' || word.charAt(i) > 'z') {
    
    
                return false;
            }
        }
        return true;
    }
}

Summarize

Through the realization of the "English Word Guessing Game", I have a further understanding of the relevant knowledge of JAVA, and have a deeper understanding of the language of java than before.

Some basic grammars of java, such as data types, operators, program flow control and arrays, are more thoroughly understood. The core of java is the object-oriented idea, and finally realized something about this concept.

Source code acquisition

Source code download address: Portal------->

Like, follow the blogger, chat with the blogger for free

Today is the 19th /100th day of continuous writing .
You can follow me, like me, comment me, bookmark me.

Guess you like

Origin blog.csdn.net/qq_40869977/article/details/123612015