Java Swing猜英文单词游戏(java+面向对象编程+swing+awt+io流)

简介: Java Swing猜英文单词游戏(java+面向对象编程+swing+awt+io流)

一、直接上运行效果

猜测成功的界面:

猜测失败的界面:

二、技术栈

java+面向对象编程+swing+awt+io流

三、项目结构

四、部分代码

主程序入口类 WordleApp.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package wordlegame;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class WordleApp {
    public static void main(String[] args) {
        try{
            ArrayList<String> CommonWords = new ArrayList<>();  // Array list to store words.
            int i = 0;
            File myObj = new File("common1.txt");           //Read words from test file
            Scanner myReader = new Scanner(myObj);        // Scanner object to scan the input file.
            while (myReader.hasNextLine()) {              // While loop till the end of text file.
              String data = myReader.nextLine();          // Read each line from text file one by one.
              CommonWords.add(data.toLowerCase().trim());       // Add words one-by-one in the array list.
            }
            myReader.close();
            ArrayList<String> Words = new ArrayList<>();
            myObj = new File("words1.txt");           //Read words from test file
            myReader = new Scanner(myObj);        // Scanner object to scan the input file.
            while (myReader.hasNextLine()) {              // While loop till the end of text file.
              String data = myReader.nextLine();          // Read each line from text file one by one.
              Words.add(data.toLowerCase().trim());       // Add words one-by-one in the array list.
            }
            myReader.close();
            Random rand = new Random();                   //Create instance of random to get random number form the list.    
            int upperbound = CommonWords.size()-1;              // set the upperlimit for the random number to be generated.
            int int_random = rand.nextInt(upperbound);    // Generate random number.
            String actualWord =CommonWords.get(int_random);     // Get word form the list.
            System.out.println("Your have total 6 attempts to predict the word"); 
                System.err.println(actualWord);   // actual word uncomment the line if want to keep track.
            WordleModel m = new WordleModel(false,0,0,actualWord,"",Words,false,false);  //Initialize model.
            WordleView v = new WordleView(); //Create instance of View.
            WordleController c = new WordleController(m, v); //Create instance of controller.
            c.initController(); //Initialize controller.
            System.out.println(actualWord);
            System.out.println();
        }
        catch(Exception ex){
            System.out.println(ex);
        }
       }
}

Indexes.java类

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package wordlegame;
import java.awt.Color;
public class Indexes{
        public int index;
        public Color color;
        public Indexes(int index, Color color){
            this.index = index;
            this.color = color;
        }
    }

五、源码地址

https://download.csdn.net/download/weixin_43860634/87784529


相关文章
|
1天前
|
Java
Java Swing模拟水波纹扩散效果动画
Java Swing模拟水波纹扩散效果动画
15 6
|
1天前
|
Java
【Gloomyfish】Java Swing 实现实际大小到全屏切换
【Gloomyfish】Java Swing 实现实际大小到全屏切换
9 1
|
2天前
|
算法 Java Go
【经典算法】LeetCode 58.最后一个单词的长度(Java/C/Python3/Go实现含注释说明,Easy)
【经典算法】LeetCode 58.最后一个单词的长度(Java/C/Python3/Go实现含注释说明,Easy)
4 0
|
2天前
|
存储 算法 Java
【经典算法】LeetCode 151. 反转字符串中的单词(Java/C/Python3实现含注释说明,中等)
【经典算法】LeetCode 151. 反转字符串中的单词(Java/C/Python3实现含注释说明,中等)
5 0
|
2天前
|
算法 JavaScript 前端开发
【经典算法】LCR187:破冰游戏(约瑟夫问题,Java/C/Python3/JavaScript实现含注释说明,Easy)
【经典算法】LCR187:破冰游戏(约瑟夫问题,Java/C/Python3/JavaScript实现含注释说明,Easy)
5 1
|
3天前
|
Java 应用服务中间件
已解决:An error occurred at line: 1 in the generated java file The type java.io.ObjectInputStream canno
已解决:An error occurred at line: 1 in the generated java file The type java.io.ObjectInputStream canno
|
3天前
|
Java
io读两个文件,生成list 排重后写本地文件(Java)
io读两个文件,生成list 排重后写本地文件(Java)
|
3天前
|
Java
电脑玩诺基亚手机jar游戏java-涂鸦地带举例
电脑玩诺基亚手机jar游戏java-涂鸦地带举例
|
3天前
|
Java Windows
文件操作和IO(2):Java中操作文件
文件操作和IO(2):Java中操作文件
6 0
|
5天前
|
Java 安全 索引
滚雪球学Java(48):面向对象编程中的StringBuffer类详解
【6月更文挑战第2天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
28 5
滚雪球学Java(48):面向对象编程中的StringBuffer类详解