java实现五子棋小游戏
作者:hebedich 发布时间:2021-12-25 06:58:56
标签:java,五子棋,小游戏
java实现五子棋小游戏
package Gomoku;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class GomokuFrame extends JFrame
{
//定义一个操作面板
OperatorPane op=null;
public GomokuFrame()
{
//设置名称
this.setTitle("五子棋");
//设置窗口大小
this.setSize(510,510);
//设置窗口位置
//取得电脑屏幕大小
int computer_width=Toolkit.getDefaultToolkit().getScreenSize().width;
int computer_height=Toolkit.getDefaultToolkit().getScreenSize().height;
System.out.println("电脑屏幕的宽度:\n"+computer_width+"\n电脑屏幕的高度:\n"+computer_height);
//居中
this.setLocation((computer_width-510)/2, (computer_height-510)/2);
//实例化幕布
op=new OperatorPane();
//导入幕布
this.add(op);
//添加鼠标监听
this.addMouseListener(op);
//设置窗口的显示
this.setVisible(true);
//设置窗口的正常关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//执行测试
public static void main(String[] args)
{
new GomokuFrame();
}
}
package Gomoku;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageFilter;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class OperatorPane extends JPanel implements MouseListener,Runnable
{
//定义背景图片对象
Image imageBackground = null;
//定义棋盘的格子的行数
int boardrows=18;
//定义棋盘的格子的列数
int boardcols=18;
//定义棋盘的格子的大小
int boardsize=20;
//保存棋子坐标
int x=0,y=0;
//保存之前下过的全部棋子坐标
// 其中数据内容 0: 表示这个点并没有棋子, 1: 表示这个点是黑子, 2:表示这个点是白子
int allchess[][]=new int [19][19];
//标记下一步下黑棋还是白棋
boolean isBlack=true;
//判断游戏是否能够继续
boolean canPlay=true;
//保存显示信息
String message="黑方先行";
// 保存最多拥有多少时间(秒)
int maxTime = 0;
// 做倒计时的线程类
Thread t = new Thread(this);
// 保存黑方与白方的剩余时间
int blackTime = 0;
int whiteTime = 0;
// 保存双方剩余时间的显示信息
String blackMessage = "无限制";
String whiteMessage = "无限制";
@SuppressWarnings("deprecation")
public OperatorPane()
{
t.start();
t.suspend();
imageBackground=new ImageIcon("image/background.jpg").getImage();
}
public void paint(Graphics g)
{
//双缓冲技术
BufferedImage b1=new BufferedImage(495,475,BufferedImage.TYPE_INT_ARGB);
Graphics g2=b1.createGraphics();
// 画出背景图片
g2.drawImage(imageBackground, 0, 0,495,475,null);
//画出棋盘线
Color c=g2.getColor();
g2.setColor(Color.BLACK);
for(int i=0;i<=boardrows;i++)
{
g2.drawLine(10,50+boardsize*i,10+boardsize*boardrows,50+boardsize*i);
}
for(int i=0;i<=boardcols;i++)
{
g2.drawLine(10+boardsize*i,50,10+boardsize*i,50+boardsize*boardcols);
}
//画出三三位置
g2.fillOval(67, 107, 6, 6);
g2.fillOval(67, 347, 6, 6);
g2.fillOval(307, 107, 6, 6);
g2.fillOval(307, 347, 6, 6);
//画出附点
g2.fillOval(67, 227, 6, 6);
g2.fillOval(307, 227, 6, 6);
g2.fillOval(187, 107, 6, 6);
g2.fillOval(187, 347, 6, 6);
//画出天元
g2.fillOval(187, 227, 6, 6);
//画出文字提示
/*Font f=new Font("黑体", Font.BOLD, 24);
g.setFont(f);*/
g2.setFont(new Font("黑体", Font.BOLD, 20));
g2.setColor(Color.BLACK);
g2.drawString("游戏信息:"+message, 130, 40);
g2.setFont(new Font("宋体", Font.ITALIC, 15));
g2.drawString("黑方时间:"+blackMessage,25, 445);
g2.drawString("白方时间:"+whiteMessage,245, 445);
//绘制全部棋子
for(int i=0;i<=boardrows;i++)
{
for(int j=0;j<=boardcols;j++)
{
//存储黑棋
if(allchess[i][j]==1)
{
int tempX=i*20-10;
int tempY=j*20+30;
//画出黑棋
g2.setColor(Color.BLACK);
g2.fillOval(tempX+12, tempY+13, 15, 15);
}
//存储白棋
if(allchess[i][j]==2)
{
int tempX=i*20-10;
int tempY=j*20+30;
//画出白棋
g2.setColor(Color.BLACK);
g2.drawOval(tempX+12, tempY+13, 15, 15);
g2.setColor(Color.WHITE);
g2.fillOval(tempX+12, tempY+13, 15, 15);
}
}
}
g2.setColor(c);
g.drawImage(b1,0,0,this);
}
private boolean checkWin()
{
boolean flag=false;
int color = allchess[x][y];
/*// 保存共有相同颜色多少棋子相连
int count1=1;
int count2=1;
int count3=1;
int count4=1;
// 判断横向是否有5个棋子相连,特点 纵坐标 是相同, 即allChess[x][y]中y值是相同
// 通过循环来做棋子相连的判断
int i = 1;
while (color == allchess[x+i][y])
{
count1++;
i++;
}
//将i值复位
i = 1;
while (color == allchess[x-i][y])
{
count1++;
i++;
}
if(count1 >= 5)
{
flag = true;
}
//判断纵向 ,即allChess[x][y]中x值是相同
int j = 1;
while (color == allchess[x][y+j])
{
count2++;
j++;
}
//将j值复位
j = 1;
while (color == allchess[x][y-j])
{
count2++;
j++;
}
if(count2>= 5)
{
flag = true;
}
//判断斜向"\"
int m1=1;
int n1=1;
while (color == allchess[x+m1][y+n1])
{
count3++;
m1++;
n1++;
}
m1=1;
n1=1;
while (color == allchess[x-m1][y-n1])
{
count3++;
m1++;
n1++;
}
if(count3>= 5)
{
flag = true;
}
//判断斜向"/"
int m2=1;
int n2=1;
while (color == allchess[x+m2][y-n2])
{
count4++;
m2++;
n2++;
}
m2=1;
n2=1;
while (color == allchess[x-m2][y+n2])
{
count4++;
m2++;
n2++;
}
if(count4>= 5)
{
flag = true;
}
*/
int count;
//横向判断
count=this.checkCount(1, 0, color);
if(count>=5)
{
flag = true;
}
else
{
//纵向判断
count=this.checkCount(0, 1, color);
if(count>=5)
{
flag = true;
}
else
{
//斜向“/”
count=this.checkCount(1, 1, color);
if(count>=5)
{
flag = true;
}
else
{
//斜向“\”
count=this.checkCount(1, -1, color);
if(count>=5)
{
flag = true;
}
}
}
}
return flag;
}
private int checkCount(int xChange,int yChange,int color)
{
int count=1;
int tempX=xChange;
int tempY=yChange;
while (color==allchess[x+xChange][y+yChange])
{
count++;
if(xChange!=0)
{
xChange++;
}
if(yChange!=0)
{
if(yChange<0)
{
yChange--;
}
else
{
yChange++;
}
}
}
//复位
xChange=tempX;
yChange=tempY;
while (color==allchess[x-xChange][y-yChange])
{
count++;
if(xChange!=0)
{
xChange++;
}
if(yChange!=0)
{
if(yChange<0)
{
yChange--;
}
else
{
yChange++;
}
}
}
return count;
}
public void mouseClicked(MouseEvent e)
{
System.out.println("x:"+e.getX()+"y:"+e.getY());
x=e.getX();
y=e.getY();
if(x>=10&&x<=(10+boardsize*boardrows+20)&&y>=50&&y<=(50+boardsize*boardcols+40))
{
//System.out.println("点在棋盘内。");
x=(x-10)/20;
y=(y-50-20)/20;
if(canPlay==true)
{
//判断当前要下什么颜色
if(allchess[x][y]==0)
{
if(isBlack==true)
{
allchess[x][y]=1;
isBlack=false;
message="轮到白方";
}
else
{
allchess[x][y]=2;
isBlack=true;
message="轮到黑方";
}
// 判断这个棋子是否和其他的棋子连成5连,即判断游戏是否结束
boolean winFlag=this.checkWin();
if(winFlag==true)
{
JOptionPane.showMessageDialog(this,"游戏结束!"+
(allchess[x][y]==1?"黑方":"白方")+"获胜。");
canPlay=false;
}
}
else
{
JOptionPane.showMessageDialog(this,"当前位置已经有棋子,请重新落子!");
}
}
this.repaint();
}
//点击,游戏开始按钮
//重新开始新的游戏
if(e.getX()>=400&&e.getX()<=470&&e.getY()>=80&&e.getY()<=110)
{
int result=JOptionPane.showConfirmDialog(this, "设置完成,是否重新开始游戏?");
if(result==0)
{
//重新开始的操作,allchess[][]数组中的信息全部为0
//清空棋盘
for (int i = 0; i < 19; i++)
{
for (int j = 0; j < 19; j++)
{
allchess[i][j] = 0;
}
}
//另一种方式 allChess = new int[19][19];
blackTime = maxTime;
whiteTime = maxTime;
if (maxTime > 0)
{
blackMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
whiteMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
t.resume();
}
else
{
blackMessage = "无限制";
whiteMessage = "无限制";
}
message = "黑方先行";
isBlack = true;
this.canPlay = true;
this.repaint();
}
}
//点击 游戏设置按钮
if(e.getX()>=400&&e.getX()<=470&&e.getY()>=130&&e.getY()<=160)
{
String input = JOptionPane
.showInputDialog("请输入游戏的最大时间(单位:分钟),如果输入0,表示没有时间限制:");
try {
maxTime = Integer.parseInt(input) * 60;
if (maxTime < 0)
{
JOptionPane.showMessageDialog(this, "请输入正确信息,不允许输入负数!");
}
if (maxTime == 0)
{
int result = JOptionPane.showConfirmDialog(this,
"设置完成,是否重新开始游戏?");
if (result == 0)
{
for (int i = 0; i < 19; i++)
{
for (int j = 0; j < 19; j++)
{
allchess[i][j] = 0;
}
}
// 另一种方式 allChess = new int[19][19];
message = "黑方先行";
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
blackMessage = "无限制";
whiteMessage = "无限制";
this.canPlay = true;
this.repaint();
}
}
if (maxTime > 0)
{
int result = JOptionPane.showConfirmDialog(this,
"设置完成,是否重新开始游戏?");
if (result == 0)
{
for (int i = 0; i < 19; i++)
{
for (int j = 0; j < 19; j++)
{
allchess[i][j] = 0;
}
}
// 另一种方式 allChess = new int[19][19];
message = "黑方先行";
isBlack = true;
blackTime = maxTime;
whiteTime = maxTime;
blackMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
whiteMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
t.resume();
this.canPlay = true;
this.repaint();
}
}
}
catch (NumberFormatException e1)
{
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "请正确输入信息!");
}
}
//点击 游戏说明按钮
if(e.getX()>=400&&e.getX()<=470&&e.getY()>=180&&e.getY()<=210)
{
JOptionPane.showMessageDialog(this,"这个一个五子棋游戏程序,黑白双方轮流下棋,当某一方连到五子时,游戏结束。");
}
//点击 认输按钮
if(e.getX()>=400&&e.getX()<=470&&e.getY()>=280&&e.getY()<=310)
{
int result=JOptionPane.showConfirmDialog(this,"是否确定认输?");
if (result == 0)
{
if (isBlack)
{
JOptionPane.showMessageDialog(this, "黑方已经认输,游戏结束!");
}
else
{
JOptionPane.showMessageDialog(this, "白方已经认输,游戏结束!");
}
canPlay = false;
}
}
//点击 关于按钮
if(e.getX()>=400&&e.getX()<=470&&e.getY()>=330&&e.getY()<=360)
{
JOptionPane.showMessageDialog(this,"本游戏由南木工作室制作,有相关问题可以访问www.yiyiinformation.com");
}
//点击 退出按钮
if(e.getX()>=400&&e.getX()<=470&&e.getY()>=380&&e.getY()<=410)
{
JOptionPane.showMessageDialog(this, "游戏结束");
System.exit(0);
}
}
//************************//
@Override
public void mouseEntered(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run()
{
if (maxTime > 0)
{
while (true)
{
if (isBlack)
{
blackTime--;
if (blackTime == 0)
{
JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!");
}
}
else
{
whiteTime--;
if (whiteTime == 0)
{
JOptionPane.showMessageDialog(this, "白方超时,游戏结束!");
}
}
blackMessage = blackTime / 3600 + ":"
+ (blackTime / 60 - blackTime / 3600 * 60) + ":"
+ (blackTime - blackTime / 60 * 60);
whiteMessage = whiteTime / 3600 + ":"
+ (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
+ (whiteTime - whiteTime / 60 * 60);
this.repaint();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(blackTime + " -- " + whiteTime);
}
}
}
}
演示图:
以上所述就是本文的全部内容了,希望能够对大家熟练掌握java有所帮助。
0
投稿
猜你喜欢
- 本Demo使用三个类一个Test类一个自定义的Stack类一个自定义的Queue类可以实现的功能:1.对于一个写在文本文件中的迷宫,能够将其
- 前言有人说Gradle使用groovy语言编写,简单,明了没像maven使用xml臃肿,其实我倒不觉得,我觉得maven挺好的,管理jar依
- @RequestMapping注解注意点类上加没加@RequestMappin注解区别1.如果类上加了 @RequestMappin注解,那
- JenkinsJenkins是一个开源的、可扩展的持续集成、交付、部署的基于web界面的平台。允许持续集成和持续交付项目,无论用的是什么平台
- 几个月前写过一篇博客《xUtils3.0框架学习笔记》 ,上面也有记录通过xUtils实现文件上传的使用方法,代码如下:private vo
- 上一节,简单讲述了 Mybatis-Plus 搭建与使用入门,这一节,简单讲一下如何使用 MP 实现多表分页。分析使用的工程,依旧是 spr
- 消费逻辑上文 流式图表框架搭建框架搭建好之后着手开发下kafka的核心消费逻辑,流式图表的核心消费逻辑就是实现一个消费链接池维护消
- 命令模式命令模式很好理解,举个例子,司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,传到了士兵耳
- 背景众所周知,所有被打开的系统资源,比如流、文件或者Socket连接等,都需要被开发者手动关闭,否则随着程序的不断运行,资源泄露将会累积成重
- 上一次接触到编码的知识,还是上大学的时候,那时候学的是通信工程专业,有关编码的内容,不记得是在通信原理还是信息论与编码里面学到的了。却依然记
- ❤️大家好,我是贾斯汀,今天主要聊一聊关于线程的瓜!❤️先来看一下线程这张图线程的几种运行状态之间运行流程:看不懂没关系,慢慢来学习,往
- 在类中自定义的“函数”称为“方法”,由于C#是完全面向对象的
- 一、方法这里我们用两种方法来实现跑马灯效果,虽然实质上是一种实质就是:1、TextView调出跑马灯效果2、TextView获取焦点&nbs
- Java——获取字符串编码格式判断一个字符串的编码格式: public static St
- 这篇文章主要介绍了Spring JDK * 实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- springboot项目启动,访问报404错误今天在做一个springboot项目的时候,是接着别人的项目写的,写完之后想做一下测试,于是就
- 一、栈(Stack)1、什么是栈?栈其实就是一种数据结构 - 先进后出(先入栈的数据后出来,最先入栈的数据会被压入栈底)什么是java虚拟机
- Java List转换成String数组实现代码:List<String> list = new ArrayList<St
- yield()介绍yield()的作用是让步。它能让当前线程由“运行状态”进入到“就绪状态”,从而让其它具有相同优先级的等待线程获取执行权;
- 1. dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们