JAVA 实现《英文猜词游戏》游戏

前言

《英文猜词游戏》代码行数没有超过200行,是之前为了背英语单词,特意研发的小游戏。

主要设计

  1. 事先准备单词文本。

  2. 为了让玩家能与程序互动,使用下面这个命令可达效果

    Scanner sc = new Scanner(System.in);
    
  3. 运行WordleMaster里的main方法

  4. 在Wordle中输入第一个单词(默认第一个单词是abort,会显示在console中。可在代码中修改)

  5. 将Wordle中的判定结果输入到console中。

    1. 0表示不包含该字母,即灰色。
    2. 1表示包含该字母,但是位置不正确,即黄色。
    3. 2表示包含该字母且在正确的位置,即绿色。
  6. 在console输出的结果中选择一个单词输入Wordle中,并在console中输入该词的序号。

  7. 重复5-6步,直至找到正确答案。

功能截图

游戏开始:

image-202202219047717

输入单词(这个单词可以自己设定)

image-20220221909373

image-202202219550940

代码实现

游戏启动类



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();
            }
        }
    }

}

处理


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;
    }
}

单词判断

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;
    }
}

总结

通过此次的《英文猜词游戏》实现,让我对JAVA的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。

java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。

源码获取

源码下载地址:传送门------->

点赞,关注博主后,私聊博主免费获取
需要技术指导,写项目程序,等更多服务请联系博主

今天是持续写作的第 19 / 100 天。
可以关注我,点赞我、评论我、收藏我啦。

  • 21
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值