Java实现英⽂猜词游戏的⽰例代码
⽬录
前⾔
主要设计
功能截图
代码实现
游戏启动类
处理
单词判断
总结
前⾔
《英⽂猜词游戏》代码⾏数没有超过200⾏,是之前为了背英语单词,特意研发的⼩游戏。
主要设计
1.事先准备单词⽂本。
2.为了让玩家能与程序互动,使⽤下⾯这个命令可达效果
Scanner sc = new Scanner(System.in);
3.运⾏WordleMaster⾥的main⽅法
4.在Wordle中输⼊第⼀个单词(默认第⼀个单词是abort,会显⽰在console中。可在代码中修改)
5.将Wordle中的判定结果输⼊到console中。
0表⽰不包含该字母,即灰⾊。
1表⽰包含该字母,但是位置不正确,即黄⾊。
2表⽰包含该字母且在正确的位置,即绿⾊。
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 Line());
while (!lastWord.isValid()) {
if (Word().equals("")) {
lastWord = new Word(DEFAULT_FIRST_WORD);
break;
}
System.out.println("请输⼊⼀个有效的单词!");
lastWord = new Line());
}
System.out.println("初始词为:" + Word());
Pattern pattern = new Pattern();
// 输⼊Wordle结果
int[] res = sult();
// 读取所有的单词
List<Word> allWords = new ArrayList<>();
File file = new File("");
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(reader);
String word = adLine();
while (word != null){
Word w = new Word(word);
allWords.add(w);
word = adLine();
}
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 < Word().length(); j++) {
for (int k = j + 1; k < Word().length(); k++) {
if (Word().charAt(j) == Word().charAt(k)){
if (res[j] == 0 && res[k] != 0){
res[j] = 3;
}else if(res[j] != 0 && res[k] == 0){
res[k] = 3;
}
}
}
}
switch (res[i]) {
case 0:
hope = hope.stream().filter(w -> w.Word().charAt(finalI))).List());
break;
case 2:
hope = hope.stream().filter(w -> w.getWord().charAt(finalI) == Word().charAt(finalI)).List());                        break;
case 1:
hope = hope.stream().filter(w -> w.Word().charAt(finalI), finalI)).List());
break;
default:
}
}
System.out.println("size: " + hope.size());
for (int i = 0; i < Math.min(10, hope.size()); i++) {
System.out.print(i + ". " + (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 = Line();
chose = s.length() == 1 ? Integer.parseInt(s) : Integer.MAX_VALUE;
}
lastWord = (chose);
System.out.Word());
res = sult();
}
}
}
处理
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 = Line();
英文游戏名称
}
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 !ains(String.valueOf(c));
}
public boolean notLocate(char c, int i){
ains(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最核⼼的核⼼就是⾯向对象思想,对于这⼀个概念,终于悟到了⼀些。
以上就是Java实现英⽂猜词游戏的⽰例代码的详细内容,更多关于Java猜词游戏的资料请关注其它相关⽂章!