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

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

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

前言

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

主要設(shè)計(jì)

1.事先準(zhǔn)備單詞文本。

2.為了讓玩家能與程序互動(dòng),使用下面這個(gè)命令可達(dá)效果

Scanner sc = new Scanner(System.in);

3.運(yùn)行WordleMaster里的main方法

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

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

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

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

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

功能截圖

游戲開(kāi)始:

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

代碼實(shí)現(xiàn)

游戲啟動(dòng)類

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 !請(qǐng)?jiān)赪ordle游戲中輸入第一個(gè)單詞:(輸入回車則默認(rèn)使用abort作為初始詞)");
        System.out.println("提示:英文單詞長(zhǎng)度要為5!");
        Word lastWord = new Word(sc.nextLine());
        while (!lastWord.isValid()) {
            if (lastWord.getWord().equals("")) {
                lastWord = new Word(DEFAULT_FIRST_WORD);
                break;
            }
            System.out.println("請(qǐng)輸入一個(gè)有效的單詞!");
            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)單詞中有兩個(gè)相同字母的情況(如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("請(qǐng)選擇一個(gè):");
                    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é)

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

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

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

相關(guān)文章

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

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

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

    mybatis 事務(wù)回滾配置操作

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

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

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

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

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

    SpringBoot前后端傳輸加密設(shè)計(jì)實(shí)現(xiàn)方案

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論

繁峙县| 庐江县| 柘城县| 临沧市| 辽阳市| 仲巴县| 威远县| 延边| 胶南市| 隆子县| 淮安市| 新竹县| 如东县| 青龙| 桐乡市| 鲁山县| 万盛区| 巨野县| 马公市| 景洪市| 嘉鱼县| 双城市| 舞阳县| 亳州市| 阆中市| 平潭县| 临高县| 长武县| 铜鼓县| 定州市| 韶关市| 宁化县| 普格县| 麦盖提县| 门源| 交口县| 宁乡县| 大渡口区| 马关县| 蕉岭县| 西城区|