时间:2022-12-15 10:31:41 | 栏目:JAVA代码 | 点击:次
《英文猜词游戏》代码行数没有超过200行,是之前为了背英语单词,特意研发的小游戏。
1.事先准备单词文本。
2.为了让玩家能与程序互动,使用下面这个命令可达效果
Scanner sc = new Scanner(System.in);
3.运行WordleMaster里的main方法
4.在Wordle中输入第一个单词(默认第一个单词是abort
,会显示在console中。可在代码中修改)
5.将Wordle中的判定结果输入到console中。
6.在console输出的结果中选择一个单词输入Wordle中,并在console中输入该词的序号。
7.重复5-6步,直至找到正确答案。
游戏开始:
输入单词(这个单词可以自己设定)
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最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。