Java實現(xiàn)英文猜詞游戲的示例代碼
前言
《英文猜詞游戲》代碼行數(shù)沒有超過200行,是之前為了背英語單詞,特意研發(fā)的小游戲。
主要設(shè)計
1.事先準備單詞文本。
2.為了讓玩家能與程序互動,使用下面這個命令可達效果
Scanner sc = new Scanner(System.in);
3.運行WordleMaster里的main方法
4.在Wordle中輸入第一個單詞(默認第一個單詞是abort,會顯示在console中。可在代碼中修改)
5.將Wordle中的判定結(jié)果輸入到console中。
- 0表示不包含該字母,即灰色。
- 1表示包含該字母,但是位置不正確,即黃色。
- 2表示包含該字母且在正確的位置,即綠色。
6.在console輸出的結(jié)果中選擇一個單詞輸入Wordle中,并在console中輸入該詞的序號。
7.重復(fù)5-6步,直至找到正確答案。
功能截圖
游戲開始:

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


代碼實現(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的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
java Split 實現(xiàn)去除一個空格和多個空格
這篇文章主要介紹了java Split 實現(xiàn)去除一個空格和多個空格,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
SpringBoot前后端傳輸加密設(shè)計實現(xiàn)方案
這篇文章主要給大家介紹了關(guān)于SpringBoot前后端傳輸加密設(shè)計實現(xiàn)方案的相關(guān)資料,包括數(shù)據(jù)加密方案、解密傳輸數(shù)據(jù)實現(xiàn)方案和響應(yīng)數(shù)據(jù)加密實現(xiàn)方案,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-11-11
Java 實戰(zhàn)項目錘煉之網(wǎng)上花店商城的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(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),IOC是Spring的核心思想之一,它通過將對象的創(chuàng)建、依賴注入和生命周期管理交給容器來實現(xiàn)解耦,使開發(fā)者能夠更專注于業(yè)務(wù)邏輯的實現(xiàn),需要的朋友可以參考下2025-02-02

