Java實(shí)現(xiàn)英文猜詞游戲的示例代碼
前言
《英文猜詞游戲》代碼行數(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中。可在代碼中修改)
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)類(lèi)
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è)單詞:(輸入回車(chē)則默認(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這門(mén)語(yǔ)言也有了比以前更深刻的認(rèn)識(shí)。
java的一些基本語(yǔ)法,比如數(shù)據(jù)類(lèi)型、運(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整合阿里巴巴SMS的實(shí)現(xiàn)示例
本文主要介紹了Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Java中Base64和File之間互轉(zhuǎn)代碼示例
這篇文章主要給大家介紹了關(guān)于Java中Base64和File之間互轉(zhuǎn)的相關(guān)資料,Base64是網(wǎng)絡(luò)上最常見(jiàn)的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個(gè)可打印字符來(lái)表示二進(jìn)制數(shù)據(jù)的方法,需要的朋友可以參考下2023-08-08
詳解Javascript判斷Crontab表達(dá)式是否合法
這篇文章主要介紹了詳解Javascript判斷Crontab表達(dá)式是否合法的相關(guān)資料,需要的朋友可以參考下2017-03-03
詳解springboot設(shè)置cors跨域請(qǐng)求的兩種方式
這篇文章主要介紹了詳解springboot設(shè)置cors跨域請(qǐng)求的兩種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Springboot的maven間接依賴的實(shí)現(xiàn)
這篇文章主要介紹了Springboot的maven間接依賴的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

