手把手教你用Java做出一个五子棋小游戏(下)
【摘要】 🌊 作者主页:海拥🌊 简介:🏆CSDN全栈领域优质创作者、🥇HDZ核心组成员、🥈蝉联C站周榜前十🌊 粉丝福利:粉丝群 每周送6~9本书,不定期送各种小礼品,往期获奖公布 解决第一次不能显示输赢标志的问题g2.drawImage(win, 0, 0, 1, 1, null);g2.drawImage(lose, 0, 0, 1, 1, null);if(winFLAG == 0 &...
🌊 作者主页:海拥
🌊 简介:🏆CSDN全栈领域优质创作者、🥇HDZ核心组成员、🥈蝉联C站周榜前十
🌊 粉丝福利:粉丝群 每周送6~9本书,不定期送各种小礼品,往期获奖公布
解决第一次不能显示输赢标志的问题
g2.drawImage(win, 0, 0, 1, 1, null);
g2.drawImage(lose, 0, 0, 1, 1, null);
if(winFLAG == 0 && player == 0) {//表示还未分出胜负并且玩家下了,调用电脑下棋
tuntoComputer();
}
设置下拉框和单选按钮在棋盘不为空的是否不能进行选择
设置棋子颜色单选框可用状态
if(chessboardEmpty == 1) {//棋盘不为空
difficulityClass.setEnabled(false);//设置下拉框不可用
if(whiteChessjr.isSelected()) {//白棋子单选框被选中
blackChessjr.setEnabled(false);
whiteChessjr.setEnabled(true);
}else {//黑棋子单选框被选中
blackChessjr.setEnabled(true);
whiteChessjr.setEnabled(false);
}
}else {//棋盘为空
blackChessjr.setEnabled(true);//释放两个单选框
whiteChessjr.setEnabled(true);
difficulityClass.setEnabled(true);//释放下拉框
}
重绘积分,等级,胜率
classNum = (int)((int)(pointsNum /100 * 0.4 + gamewinNum * 0.4 + gameNum * 0.2) * 0.8);
sunNum = (int) (classNum / 100);
moonNum = (int)(classNum - sunNum * 100) / 50;
starNum = (int)(classNum - sunNum * 100 - moonNum * 50) / 10;
for(t = 0; t < sunNum; t++) {//绘画太阳
g2.drawImage(sun, 75 + t * 30, 538, 30, 30, null);
}
for(t = sunNum ; t < moonNum + sunNum; t++) {//绘画月亮
g2.drawImage(moon, 75 + t * 30, 540, 25, 25, null);
}
if(moonNum > 0 || sunNum > 0) {//绘画星星
for(t = moonNum + sunNum ; t < starNum + moonNum + sunNum; t ++) {
g2.drawImage(star, 75 + t * 30, 540, 25, 25, null);
}
}else {
for(t = moonNum ; t < starNum + 1; t ++) {
g2.drawImage(star, 75 + t * 30, 538, 30, 30, null);
}
}
判断棋子是否连成五个
public void judge() {
for(t = newchessX,s = newchessY,count = 0; t >=0 && s >= 0 && count <= 4; t--,s--,count++) {
comeX = t;
comeY = s;
}
for(t = newchessX, s = newchessY, count = 0; t <=14 && s >= 0 && count <= 4; t++, s--, count++) {
toX = t;
toY = s;
}
if(winFLAG == 0) {
for(int ch = 1; ch <=2; ch++) {
CHESSCOLOR = ch;
//判断横向棋子
for(s = (newchessX - 4) >=0 ? (newchessX - 4) : 0 ; s <= newchessX; s++) {//表示玩家获胜
t = newchessY;
if(map[s][t] == CHESSCOLOR && s < 11) {//行棋子数量计算
if(map[s + 1][t] == CHESSCOLOR) {
if(map[s + 2][t] == CHESSCOLOR) {
if(map[s + 3][t] == CHESSCOLOR) {
if(map[s + 4][t] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 1;
if(CHESSCOLOR == 1) {//白棋
winFLAG = 1;
}else {//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {//如果某一方赢了就直接退出
break;
}
//判断列项棋子
for(t = (newchessY - 4) >=0 ? (newchessY - 4) : 0 ; t <= newchessY; t ++) {
s = newchessX;
if(map[s][t] == CHESSCOLOR && t < 11) {//列棋子数量计算
if(map[s][t + 1] == CHESSCOLOR) {
if(map[s][t + 2] == CHESSCOLOR) {
if(map[s][t + 3] == CHESSCOLOR) {
if(map[s][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 2;
if(CHESSCOLOR == 1) {//白棋
winFLAG = 1;
}else {//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {//如果某一方赢了就直接退出
break;
}
//判断左上到右下棋子
for(s = comeX, t = comeY; s <= newchessX && t <= newchessY; s ++, t++) {
if(map[s][t] == CHESSCOLOR && s < 11 && t < 11) {//斜下棋子数量计算
if(map[s + 1][t + 1] == CHESSCOLOR) {
if(map[s + 2][t + 2] == CHESSCOLOR) {
if(map[s + 3][t + 3] == CHESSCOLOR) {
if(map[s + 4][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 3;
if(CHESSCOLOR == 1) {//白棋
winFLAG = 1;
}else {//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {//如果某一方赢了就直接退出
break;
}
//判断右上到左下棋子
for(s = toX, t = toY; s >= newchessX && t <= newchessY; s --, t++) {
if(map[s][t] == CHESSCOLOR && s >= 4 && t < 11) {//斜上棋子数量计算
if(map[s - 1][t + 1] == CHESSCOLOR) {
if(map[s - 2][t + 2] == CHESSCOLOR) {
if(map[s - 3][t + 3] == CHESSCOLOR) {
if(map[s - 4][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 4;
if(CHESSCOLOR == 1) {//白棋
winFLAG = 1;
}else {//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {//如果某一方赢了就直接退出
break;
}
}
}
}
💌四、主函数 Main.java
基本布局设置
setTitle("开心五子棋");
setBounds(200, 200, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
SpringLayout springLayout = new SpringLayout();//使用弹簧布局管理器
Container c = getContentPane();//创建容器
c.setBackground(new Color(255, 218, 185));
c.setLayout(springLayout);
userjt.setFont(new Font("微软雅黑", 0, 18 ));
userjt.setText(Register.userName);
passwordjt.setFont(new Font("微软雅黑", 0, 18));
passwordjt.setText(Register.password);
logoLabel.setFont(new Font("微软雅黑", 1, 48));
logoLabel.setForeground(Color.pink);
ImageIcon logoimage = new ImageIcon(Main.class.getResource("/image/logo5.jpg"));
logoimage.setImage(logoimage.getImage().getScaledInstance(260, 130, Image.SCALE_DEFAULT));
logo.setIcon(logoimage);
userLabel.setFont(new Font("微软雅黑", 1, 20));
passwordLabel.setFont(new Font("微软雅黑", 1, 20));
rememberPasswordjl.setFont(new Font("微软雅黑", 0, 14));
rememberPasswordjl.setForeground(Color.gray);
automaticLoginjl.setFont(new Font("微软雅黑", 0, 14));
automaticLoginjl.setForeground(Color.gray);
loginButton.setFont(new Font("微软雅黑", 1, 16));
registerLabel.setFont(new Font("微软雅黑", 1, 13));
registerLabel.setForeground(Color.gray);
promptPasswordFalse.setFont(new Font("微软雅黑", 0, 13));
promptPasswordFalse.setForeground(Color.red);
promptUserNameEmpty.setFont(new Font("微软雅黑", 0, 13));
promptUserNameEmpty.setForeground(Color.red);
prompPasswordEmpty.setFont(new Font("微软雅黑", 0, 13));
prompPasswordEmpty.setForeground(Color.red);
promptRegister.setFont(new Font("微软雅黑", 0, 13));
promptRegister.setForeground(Color.red);
rememberPasswordjcb.setBackground(new Color(255, 218, 185));
automaticLoginjcb.setBackground(new Color(255, 218, 185));
首页图标
c.add(logo);//首页图标
springLayout.putConstraint(springLayout.NORTH, logo, 40, springLayout.NORTH, c);
springLayout.putConstraint(springLayout.WEST, logo, 115, springLayout.WEST, c);
c.add(logoLabel);//标题“开心五子棋”
springLayout.putConstraint(springLayout.NORTH, logoLabel, 100, springLayout.NORTH, c);
springLayout.putConstraint(springLayout.WEST, logoLabel, 120, springLayout.WEST, c);
logoLabel.setVisible(false);
用户名
c.add(userLabel);//用户名
springLayout.putConstraint(springLayout.NORTH, userLabel, 35, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, userLabel, 110, springLayout.WEST, c);
c.add(userjt);
springLayout.putConstraint(springLayout.NORTH, userjt, 35, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, userjt, 10, springLayout.EAST, userLabel);
密码
c.add(passwordLabel);//密码
springLayout.putConstraint(springLayout.NORTH, passwordLabel, 10, springLayout.SOUTH, userLabel);
springLayout.putConstraint(springLayout.WEST, passwordLabel, 110, springLayout.WEST, c);
c.add(passwordjt);
springLayout.putConstraint(springLayout.NORTH, passwordjt, 10, springLayout.SOUTH, userjt);
springLayout.putConstraint(springLayout.WEST, passwordjt, 10, springLayout.EAST, passwordLabel);
复选框
c.add(rememberPasswordjcb);//复选框
springLayout.putConstraint(springLayout.NORTH, rememberPasswordjcb, 10, springLayout.SOUTH, passwordLabel);
springLayout.putConstraint(springLayout.WEST, rememberPasswordjcb, 175, springLayout.WEST, c);
c.add(rememberPasswordjl);
springLayout.putConstraint(springLayout.NORTH, rememberPasswordjl, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, rememberPasswordjl, 5, springLayout.EAST, rememberPasswordjcb);
c.add(automaticLoginjcb);
springLayout.putConstraint(springLayout.NORTH, automaticLoginjcb, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, automaticLoginjcb, 30, springLayout.EAST, rememberPasswordjl);
c.add(automaticLoginjl);
springLayout.putConstraint(springLayout.NORTH, automaticLoginjl, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, automaticLoginjl, 5, springLayout.EAST, automaticLoginjcb);
登陆和注册按钮
c.add(loginButton);//登陆按钮
springLayout.putConstraint(springLayout.NORTH, loginButton, 20, springLayout.SOUTH, rememberPasswordjl);
springLayout.putConstraint(springLayout.WEST, loginButton, 110, springLayout.WEST, c);
c.add(registerLabel);//注册按钮
springLayout.putConstraint(springLayout.NORTH, registerLabel, 5, springLayout.SOUTH, loginButton);
springLayout.putConstraint(springLayout.WEST, registerLabel, 320, springLayout.WEST, c);
账号未注册提示
c.add(promptRegister);//账号未注册提示
promptRegister.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptRegister, 41, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, promptRegister, 5, springLayout.EAST, userjt);
c.add(promptUserNameEmpty);//请输入账号
promptUserNameEmpty.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptUserNameEmpty, 41, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, promptUserNameEmpty, 5, springLayout.EAST, userjt);
密码错误提示
c.add(promptPasswordFalse);
promptPasswordFalse.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptPasswordFalse, 20, springLayout.SOUTH, promptRegister);
springLayout.putConstraint(springLayout.WEST, promptPasswordFalse, 5, springLayout.EAST, passwordjt);
密码为空提示
c.add(prompPasswordEmpty);
prompPasswordEmpty.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, prompPasswordEmpty, 20, springLayout.SOUTH, promptRegister);
springLayout.putConstraint(springLayout.WEST, prompPasswordEmpty, 5, springLayout.EAST, passwordjt);
设置文本框鼠标点击事件
userjt.addMouseListener(new MouseAdapter() {//文本框
public void mouseClicked(MouseEvent e) {
userjt.setText("");
}
});
passwordjt.addMouseListener(new MouseAdapter() {//密码框
public void mouseClicked(MouseEvent e) {
passwordjt.setText("");
}
});
设置登陆按钮单击事件
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName = userjt.getText().trim();//获取用户输入的账号和密码
String Password = new String(passwordjt.getPassword()).trim();
//判断账号和密码
if(userName.length() != 0) {//用户名不为空
promptUserNameEmpty.setVisible(false);//关闭账号为空显示
if(Password.length() != 0) {//密码不为空
if(f.readData("user.xls", userName) && Password.equals(f.backData("user.xls", userName, "password"))) {//用户输入的账号和密码正确
promptRegister.setVisible(false);//隐藏提示信息
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
loginButton.setText(" 登 陆 中... ");
new Chessboard();//跳转到五子棋棋盘页面
dispose();//销毁当前页面
}
else if( f.readData("user.xls", userName) && !Password.equals(f.backData("user.xls", userName, "password"))) {//用户输入密码错误
promptPasswordFalse.setVisible(true);//显示密码错误提示
promptRegister.setVisible(false);
prompPasswordEmpty.setVisible(false);
passwordjt.setText("");//密码框清空
passwordjt.requestFocus();//光标定位到密码框
}else {//账号还未注册
promptRegister.setVisible(true);
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
}
}
else {//密码为空
if(userName.equals("admin")) {//用户名已经注册, 提示输入密码
prompPasswordEmpty.setVisible(true);
promptUserNameEmpty.setVisible(false);
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
}else {//用户名未注册
prompPasswordEmpty.setVisible(false);
promptUserNameEmpty.setVisible(false);
promptRegister.setVisible(true);
promptPasswordFalse.setVisible(false);
}
}
}else {//用户名为空
promptUserNameEmpty.setVisible(true);//提示输入账号
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
passwordjt.setText("");//将密码框置为空
if(Password.length() == 0) {//密码为空
prompPasswordEmpty.setVisible(true);
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
}
}
}
});
注册标签监听器
registerLabel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
dispose();
new Register();
}
public void mouseEntered(MouseEvent e) {
registerLabel.setForeground(Color.red);;
}
public void mouseExited(MouseEvent e) {
registerLabel.setForeground(Color.black);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
🎖五、算法类 Algorithm.java
计算左边棋子数量
for(int i = X - 1; i >=0; i--) {
if(map[i][Y] == color) {
upcount[0]++;
}else if(map[i][Y] != 0 && map[i][Y] != color) {//表示有对方棋子
upflag[0] = -1;
break;
}else {//表示为空
upflag[0] = 1;
if(i - 1 >= 0 && map[i][Y] == 0) {
upflag[0] = 2;//表示两个空格
}else {
break;
}
if(i - 2 >= 0 && map[i][Y] == 0) {
upflag[0] = 3;//表示有三个空格
}else {
break;
}
break;
}
}
计算右边棋子数量
for(int j = X + 1; j <= 14; j++) {
if(map[j][Y] == color) {
downcount[0]++;
}else if(map[j][Y] != 0 && map[j][Y] != color) {
downflag[0] = -1;
break;
}else {//表示为空
downflag[0] = 1;
if(j + 1 <= 14 && map[j][Y] == 0) {
downflag[0] = 2;
}else {
break;
}
if(j + 2 <= 14 && map[j][Y] == 0) {
downflag[0] = 3;
}else {
break;
}
break;
}
}
计算方向向上
for(int i = Y - 1; i >= 0; i--) {
if(map[X][i] == color) {
upcount[1]++;
}else if(map[X][i] != 0 && map[X][i] != color) {//表示该点是对方棋子
upflag[1] = -1;
break;
}else {//表示为空
upflag[1] = 1;
if(i - 1 >= 0 && map[X][i] == 0) {
upflag[1] = 2;
}else {
break;
}
if(i - 2 >= 0 && map[X][i] == 0) {
upflag[1] = 3;
}else {
break;
}
break;
}
}
计算方向向下
for(int j = Y + 1; j <= 14; j++) {
if(map[X][j] == color) {
downcount[1]++;
}else if(map[X][j] != 0 && map[X][j] != color) {//表示该点是对方棋子
downflag[1] = -1;
break;
}else {//表示为空
downflag[1] = 1;
if(j + 1 >= 0 && map[X][j] == 0) {
downflag[1] = 2;
}else {
break;
}
if(j + 2 >= 0 && map[X][j] == 0) {
downflag[1] = 3;
}else {
break;
}
break;
}
}
计算斜向上
int i = 0;
int j = 0;
for(i = X - 1, j = Y - 1; i >= 0 && j >= 0; i--, j--) {
if(map[i][j] == color) {
upcount[2]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
upflag[2] = -1;
break;
}else {//为空
upflag[2] = 1;
if(i - 1 >= 0 && j - 1 >= 0 && map[i][j] == 0) {
upflag[2] = 2;
}else {
break;
}
if(i - 2 >= 0 && j - 2 >= 0 && map[i][j] == 0) {
upflag[2] = 3;
}else {
break;
}
break;
}
}
计算斜向下
for(i = X + 1, j = Y + 1; i <= 14 && j <= 14; i++, j++) {
if(map[i][j] == color) {
downcount[2]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
downflag[2] = -1;
break;
}else {//为空
downflag[2] = 1;
if(i + 1 <= 14 && j + 1 <= 14 && map[i][j] == 0) {
downflag[2] = 2;
}else {
break;
}
if(i + 2 <= 14 && j + 2 <= 14 && map[i][j] == 0) {
downflag[2] = 3;
}else {
break;
}
break;
}
}
估值算法,返回一个数组,用于记录坐标
public static int[] evalute(int map[][], int depth, int computerColor) {
int maxscore = 0;
Random r = new Random();
int pos[][] = new int[10][2];{
for(int i = 0; i < pos.length; i++) {
for(int j = 0; j < pos[i].length; j++) {
pos[i][j] = 0;
}
}
}
int FLAG = 0;
int score[][] = new int[15][15];{//初始化计分数组
for(int i = 0; i < 15; i++) {
for(int j = 0; j < 15; j++) {
score[i][j] = 0;
}
}
}
int position[] = new int[]{0, 0};//初始化位置坐标数组
for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {//搜索横坐标
for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {//搜索纵坐标
if(map[i][j] == 0) {//表示该点在棋盘上面为空
score[i][j] = countScore(map, i, j, computerColor);
if(maxscore < score[i][j]) {
maxscore = score[i][j];//记录当前棋盘分数的最大值
}
}
}
}
for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {
for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {
if(score[i][j] == maxscore) {
pos[FLAG][0] = i;
pos[FLAG++][1] = j;
}
}
}
int m = r.nextInt(FLAG);
position[0] = pos[m][0];
position[1] = pos[m][1];
return position;
}
项目目录
Java文件
其他所需文件
图片:
jar包:jxl.jar
代码下载:
关注作者同名公众号【海拥】回复【java五子棋小游戏】
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)