最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)英文猜詞游戲的示例代碼

 更新時間:2022年02月22日 08:30:32   作者:小虛竹and掘金  
這篇文章主要介紹了如何用Java編寫一個英文猜詞游戲,可以用來背英語單詞。文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下

前言

《英文猜詞游戲》代碼行數(shù)沒有超過200行,是之前為了背英語單詞,特意研發(fā)的小游戲。

主要設計

1.事先準備單詞文本。

2.為了讓玩家能與程序互動,使用下面這個命令可達效果

Scanner sc = new Scanner(System.in);

3.運行WordleMaster里的main方法

4.在Wordle中輸入第一個單詞(默認第一個單詞是abort,會顯示在console中??稍诖a中修改)

5.將Wordle中的判定結(jié)果輸入到console中。

  • 0表示不包含該字母,即灰色。
  • 1表示包含該字母,但是位置不正確,即黃色。
  • 2表示包含該字母且在正確的位置,即綠色。

6.在console輸出的結(jié)果中選擇一個單詞輸入Wordle中,并在console中輸入該詞的序號。

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

功能截圖

游戲開始:

輸入單詞(這個單詞可以自己設定)

代碼實現(xiàn)

游戲啟動類

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結(jié)果
        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;
                // 如果出現(xiàn)單詞中有兩個相同字母的情況(如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結(jié)果,五位數(shù)字組成。
     * 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數(shù)組
     */
    public int[] result(){
        String s = "";
        while (input(s) == null){
            System.out.println("輸入單詞判定結(jié)果: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;
    }
}

總結(jié)

通過此次的《英文猜詞游戲》實現(xiàn),讓我對JAVA的相關(guān)知識有了進一步的了解,對java這門語言也有了比以前更深刻的認識。

java的一些基本語法,比如數(shù)據(jù)類型、運算符、程序流程控制和數(shù)組等,理解更加透徹。java最核心的核心就是面向?qū)ο笏枷耄瑢τ谶@一個概念,終于悟到了一些。

以上就是Java實現(xiàn)英文猜詞游戲的示例代碼的詳細內(nèi)容,更多關(guān)于Java猜詞游戲的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot項目實現(xiàn)關(guān)閉數(shù)據(jù)庫配置和springSecurity

    SpringBoot項目實現(xiàn)關(guān)閉數(shù)據(jù)庫配置和springSecurity

    這篇文章主要介紹了SpringBoot項目實現(xiàn)關(guān)閉數(shù)據(jù)庫配置和springSecurity的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • mybatis 事務回滾配置操作

    mybatis 事務回滾配置操作

    這篇文章主要介紹了mybatis 事務回滾配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • java Split 實現(xiàn)去除一個空格和多個空格

    java Split 實現(xiàn)去除一個空格和多個空格

    這篇文章主要介紹了java Split 實現(xiàn)去除一個空格和多個空格,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • SpringBoot集成QQ第三方登陸的實現(xiàn)

    SpringBoot集成QQ第三方登陸的實現(xiàn)

    這篇文章主要介紹了SpringBoot集成QQ第三方登陸的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • SpringBoot前后端傳輸加密設計實現(xiàn)方案

    SpringBoot前后端傳輸加密設計實現(xiàn)方案

    這篇文章主要給大家介紹了關(guān)于SpringBoot前后端傳輸加密設計實現(xiàn)方案的相關(guān)資料,包括數(shù)據(jù)加密方案、解密傳輸數(shù)據(jù)實現(xiàn)方案和響應數(shù)據(jù)加密實現(xiàn)方案,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-11-11
  • 快速入門介紹Java中強大的String.format()

    快速入門介紹Java中強大的String.format()

    這篇文章主要給大家介紹了如何快速入門介紹Java中強大的String.format()的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-03-03
  • Java 實戰(zhàn)項目錘煉之網(wǎng)上花店商城的實現(xiàn)流程

    Java 實戰(zhàn)項目錘煉之網(wǎng)上花店商城的實現(xiàn)流程

    讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+servlet+mysql+ajax實現(xiàn)一個網(wǎng)上花店商城系統(tǒng),大家可以在過程中查缺補漏,提升水平
    2021-11-11
  • Spring IOC控制反轉(zhuǎn)的實現(xiàn)解析

    Spring IOC控制反轉(zhuǎn)的實現(xiàn)解析

    這篇文章主要介紹了Spring IOC控制反轉(zhuǎn)的實現(xiàn),IOC是Spring的核心思想之一,它通過將對象的創(chuàng)建、依賴注入和生命周期管理交給容器來實現(xiàn)解耦,使開發(fā)者能夠更專注于業(yè)務邏輯的實現(xiàn),需要的朋友可以參考下
    2025-02-02
  • Java實現(xiàn)驗證碼具體代碼(圖片、漢字)

    Java實現(xiàn)驗證碼具體代碼(圖片、漢字)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)驗證碼具體代碼,包括圖片驗證碼、漢字驗證碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Java中讀取文件轉(zhuǎn)換為字符串的方法

    Java中讀取文件轉(zhuǎn)換為字符串的方法

    今天小編就為大家分享一篇Java中讀取文件轉(zhuǎn)換為字符串的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評論

湾仔区| 吉安县| 新和县| 无极县| 四子王旗| 大荔县| 确山县| 犍为县| 斗六市| 衡山县| 蕲春县| 泸州市| 米易县| 巴东县| 金沙县| 福贡县| 西乌| 错那县| 沙坪坝区| 高陵县| 大冶市| 浙江省| 宕昌县| 陇南市| 宣城市| 大宁县| 南华县| 浦县| 德惠市| 亚东县| 舒城县| 屏边| 车致| 中山市| 旅游| 南乐县| 尼木县| 柳河县| 封开县| 红桥区| 乳山市|