猜数字小游戏(Java GUI)
又到了学期末,又到了交各种各样作业的时间。悉听闻面向对象的小作业,诶,课程设计四选一,还没写,得嘞,得写了。
找一天,开始写,放正文
游戏规则
游戏规则,顾名思义,游戏的规则, 该项课程设计,要求我们自行设计游戏规则,那么,规则如下:
开始游戏后,产生一个没有重复数字的4位随机数,用户每猜一个数字,显示出“完全猜中的数字个数”和“猜中数字但位置错误的数字个数”,用户根据游戏提示的信息继续猜,直到猜中为止。一次猜对得100分,多一次扣十分,多于五次得50分。
核心代码
在本游戏中我们采用面向对象的思维,将游戏的各个组件模块化,我们先来看看游戏的核心代码逻辑:
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class Game {
int num = 0;
private Set<Integer> numOfSet = new HashSet<Integer>();
private int a = 0;
private int b = 0;
public Game(){
Set<Integer> set = new HashSet<Integer>();
set = makeNumber();
numOfSet = set;
Iterator<Integer> it = set.iterator();
int t = 0;
while (it.hasNext()){
t = t*10+it.next();
}
num = t;
System.out.println(num);
}
public void clear(){
a=0;
b=0;
}
public boolean mark(int num){
int numSet1[] = new int[4];
for (int i=0;i<4;i++) {
int x = num%10;
num = num/10;
numSet1[3-i] = x;
}
Iterator<Integer> iterator = numOfSet.iterator();
int numSet2[] = new int[4];
for(int i=0;i<4&iterator.hasNext();i++){
numSet2[i] = iterator.next();
}
for(int i=0;i<4;i++){
if(numSet2[i]==numSet1[i]){
a++;
}
}
for (int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(i != j&numSet1[i]==numSet2[j]){
b++;
}
}
}
if(a==4){
return true;
}else {
return false;
}
}
public Set<Integer> makeNumber(){
//生成随机数
Set<Integer> set = new HashSet<Integer>();
Random random = new Random();
while (set.size()<4){
int num = random.nextInt(10);
if(num == 0){
continue;
}else {
set.add(num);
}
}
return set;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}
makeNumber()方法用来生成需要猜的数字,mark(int num) 方法用来进行对本局游戏的得分分析,clear() 方法用于重开游戏时对分数数据进行初始化。
封装性
作为面向对象的设计(虽然学艺不精),对用户类进行了封装,如下:
public class User {
private String User_id;
private String name;
private String password;
private int score;
public int getScore() {
return score;
}
public String getUser_id() {
return User_id;
}
public void setUser_id(String user_id) {
User_id = user_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setScore(int score) {
this.score = score;
}
}
除此之外,也有上一个小标里对游戏类的封装。
对JDBC操作的封装:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQL {
static Connection conn = null;
static {
conn = SQLConnector.getConnection();//操作前获取连接
}
//定义更新操作的方法
public static int executeUpdate(String sql) {
int rows = 0;
try {
Statement stmt = conn.createStatement();//获取操作数据库对象
rows = stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("更新数据库操作发生异常");
}
return rows;
}
//使用预编译语句更新方法
public static int executeUpdate(String sql,Object[]params) {
int rows = 0;
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
for(int i = 0;i<params.length;i++) {
pstmt.setObject(i+1, params[i]);
}
rows = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("使用预编译语句更新数据库操作发生异常");
}
return rows;
}
//查询数据方法
public static ResultSet executeQuery(String sql) {
ResultSet rs = null;
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("查询数据操作发生异常");
}
return rs;
}
public static ResultSet executeQuery(String sql,Object[]params) {
ResultSet rs = null;
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
for(int i = 0;i<params.length;i++) {
pstmt.setObject(i+1, params[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("使用预编译语句查询数据操作发生异常");
}
return rs;
}
}
数据库设计
由于需要对用户成绩进行排序等,我们需要用到数据库这一技术,对数据库进行设计如下:
细枝末节
其次还有一些细枝末节的功能等,例如登录,注册,排序,游戏说明等一些无关紧要的功能,再次也就不赘述了,诶,如果是山东大学软件学院的兄弟姐妹的话,请参考大一下学期Java课设的相关功能,毕竟,咱也别重复造轮子(我单纯是因为懒)
UI设计
好了,既然说到这了,那么来看看上边那堆代码套上UI是啥子样子吧, 上大图:
开始界面
排行榜
登录界面
注册界面
游戏界面
总结
好了,由于课业比较烦人(选课一时爽,期末火葬场),课设论文一大堆,这个到这里就不多赘述了,这里,把该项目的源代码放到了Github中, 请各位大佬多多指教(如果大佬们不满意,轻点喷)
Github链接:https://github.com/Firstpeople00/Record
斗胆请各位大佬点赞,关注