五子棋程序
知识点 1、Swing 编程 2、ImageIO 类的使用 3、图片的绘制
1、 Swing的概念 Swing是在java的基础图形化用户界面AWT的基础上扩展的API集
Swing的功能:通过java的代码编写窗口程序,创建图形化界面(GUI)
JFrame是Swing的基础类之一
JOptionPane类 showMessageDialog():显示一个消息对话框,主要用来提示信息 showConfirmDialog():显示一个对话框,用户选择后根据返回的结果进行判断 showMessageDialog():显示一个信息输入对话框,用于保存用户输入的信息
MouseListener类 MouseListener的使用方法,调用JFremain的addMouseListener方法加入监听。 mouseClicked():监听鼠标点击事件 mousePressed():监听鼠标进去事件的操作(窗体内部) mouseReleased():监听鼠标的离开事件的操作(窗体外部) mouseEntered()::监听鼠标按下的事件 mouseExited():监听鼠标抬起的事件
鼠标点击时的执行顺序时:mousePressed——>mouseReleased——>mouseClicked(判断按下与抬起是否在同一位置)
MouseEvent类 getX():得到鼠标的横向位置的坐标 getY():得到鼠标的纵向位置的坐标
Graphics类 drawString():绘制字符串 drawOval():绘制一个空心的圆形 fillOval():绘制一个实心的圆形 drawLine():绘制一条线 drawRect():绘制一个空心矩形 fillRect():绘制一个实心矩形 drawImage():绘制一个已经存在的图片,将一个图片直接显示到窗体中 setColor():设置画笔的颜色 setFont():设置文字的字体
功能
(1)、在点击鼠标时在相应的位置显示旗子 (2)、可以自动判断游戏是否结束,黑方还是白方胜利 (3)、游戏时间进行设置,判断是否超时
1、开发界面
计算棋盘中每一条线的间距:这里是19*19的围棋盘 总宽度为467-17=450,总高度533-89=450 分18份,每一份:x:25 y:25 此代码获取棋盘四个端点的坐标
@Override
public void mousePressed(MouseEvent e) {
System.out.println("X"+e.getX());
System.out.println("Y"+e.getY());
}
2、在棋盘上的鼠标点击位置,显示一个旗子
黑子:实心黑圆 白子:实心白圆
repaint():表示重新执行repaint
3、保存之前下过的旗子
通过一个二维数组来保存之前下过的棋子
4、判断游戏胜负
五子棋的基本规则,是否有同一颜色的棋子连成五个(核心算法)
public boolean checKwin() {
boolean flag=false;
int color=allChess[x][y];
int count=1;
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;
}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(x+xChange>=0 &&x+xChange<=18 && y+yChange>=0 &&y+yChange<=18 && color==allChess[x+xChange][y+yChange]) {
count++;
if(xChange!=0)
xChange++;
if(yChange!=0) {
if(yChange>0)
yChange++;
else {
yChange--;
}
}
}
xChange=tempY;
yChange=tempX;
while(color==allChess[x-xChange][y-yChange]) {
count++;
if(xChange!=0)
xChange++;
if(yChange!=0) {
if(yChange>0)
yChange++;
else {
yChange--;
}
}
}
return count;
}
5、实现各个按钮的功能
开始游戏:重新开始
游戏设置:设置倒计时
线程实现
if(e.getX()>=508 && e.getX()<=595 &&e.getY()>=154 &&e.getY()<=190) {
String input=JOptionPane.showInputDialog("请输入游戏的最大时间(分钟),输入0表示没有时间限制");
try {
maxTime=Integer.parseInt(input)*60;
if(maxTime<0) {
JOptionPane.showMessageDialog(this, "不允许输入负数");
}
if(maxTime>0){
int r1=JOptionPane.showConfirmDialog(this,"是否重新开始游戏?");
if(r1==0) {
allChess=new int[19][19];
message="黑方先行";
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);
isBlack=true;
t.resume();
this.repaint();
}
}
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(this, "请正确输入信息");
}
}
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) {
e.printStackTrace();
}
}
}
}
游戏说明:用来说明游戏规则和操作
认输: 是某一方放弃游戏 关于 : 作者,版本 退出: 退出游戏
总代码
public class test {
public static void main(String[] args) {
FiveChessFream jj=new FiveChessFream();
}
}
package FiveChessDome;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FiveChessFream extends JFrame implements MouseListener,Runnable{
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int higth=Toolkit.getDefaultToolkit().getScreenSize().height;
BufferedImage bgImage=null;
int x=0;
int y=0;
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="无限制";
public FiveChessFream() {
this.setTitle("五子棋");
this.setSize(626, 625);
this.setLocation((width-500)/2,(higth-500)/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);
this.setVisible(true);
t.start();
t.suspend();
this.repaint();
try {
bgImage=ImageIO.read(new File("d:/桌面/五子棋2.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
BufferedImage bi=new BufferedImage(626, 625, BufferedImage.TYPE_INT_ARGB);
Graphics g2=bi.createGraphics();
g.drawImage(bgImage, 3, 20, this);
g.setFont(new Font("黑体",Font.BOLD,20));
g.drawString("游戏信息:"+message,100, 60);
g.setFont(new Font("宋体",10,14));
g.drawString("黑色的时间:"+blackMessage, 40, 585);
g.drawString("白色的时间:"+whiteMessage, 330, 585);
for(int i=0;i<19;i++) {
g.drawLine(17,89+25*i,467,89+25*i);
g.drawLine(17+25*i, 89, 17+25*i, 538);
}
g.fillOval(90, 162,4,4);
g.fillOval(90, 462,4,4);
g.fillOval(390, 162,4,4);
g.fillOval(390,462,4,4);
for(int i=0;i<19;i++) {
for(int j=0;j<19;j++) {
if(allChess[i][j]==1) {
int tempx=i*25+17;
int tempy=j*25+89;
g.fillOval(tempx-7,tempy-7,14,14);
}
if(allChess[i][j]==2) {
int tempx=i*25+17;
int tempy=j*25+89;
g.setColor(Color.WHITE);
g.fillOval(tempx-7,tempy-7,14,14);
g.setColor(Color.BLACK);
g.drawOval(tempx-7,tempy-7,14,14);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if(canPlay==true) {
x=e.getX();
y=e.getY();
if(x>17&&x<=467 && y>89&&y<=538) {
x=(x-17)/25;
y=(y-89)/25;
if(allChess[x][y]==0) {
if(isBlack==true) {
allChess[x][y]=1;
isBlack=false;
message="轮到白方";
}else {
allChess[x][y]=2;
isBlack=true;
message="轮到黑方";
}
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()>=508 && e.getX()<=595 &&e.getY()>=90 &&e.getY()<=125) {
int r1=JOptionPane.showConfirmDialog(this,"是否重新开始游戏?");
if(r1==0) {
allChess=new int[19][19];
message="黑方先行";
isBlack=true;
this.repaint();
}
}
if(e.getX()>=508 && e.getX()<=595 &&e.getY()>=154 &&e.getY()<=190) {
String input=JOptionPane.showInputDialog("请输入游戏的最大时间(分钟),输入0表示没有时间限制");
try {
maxTime=Integer.parseInt(input)*60;
if(maxTime<0) {
JOptionPane.showMessageDialog(this, "不允许输入负数");
}
if(maxTime>0){
int r1=JOptionPane.showConfirmDialog(this,"是否重新开始游戏?");
if(r1==0) {
allChess=new int[19][19];
message="黑方先行";
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);
isBlack=true;
t.resume();
this.repaint();
}
}
}catch(NumberFormatException e1){
JOptionPane.showMessageDialog(this, "请正确输入信息");
}
}
if(e.getX()>=508 && e.getX()<=595 &&e.getY()>=214 &&e.getY()<=248) {
JOptionPane.showMessageDialog(this," 这是一个五子棋游戏,黑白双方轮流下");
}
if(e.getX()>=508 && e.getX()<=595 &&e.getY()>=344 &&e.getY()<=378) {
int result=JOptionPane.showConfirmDialog(this,"是否确认认输?");
if(result==0) {
if(isBlack) {
JOptionPane.showMessageDialog(this,"黑方认输");
}else {
JOptionPane.showMessageDialog(this,"白方认输");
}
}
}
if(e.getX()>=508 && e.getX()<=595 &&e.getY()>=406 &&e.getY()<=438) {
JOptionPane.showMessageDialog(this,"本游戏由@JiaHao制作,视频地址https://www.bilibili."
+ "com/video/BV1kJ411s7zf?p=10&share_source=copy_web");
}
if(e.getX()>=508 && e.getX()<=595 &&e.getY()>=466 &&e.getY()<=501) {
JOptionPane.showMessageDialog(this,"退出");
System.exit(0);
}
}
public boolean checKwin() {
boolean flag=false;
int color=allChess[x][y];
int count=1;
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;
}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(x+xChange>=0 &&x+xChange<=18 && y+yChange>=0 &&y+yChange<=18 && color==allChess[x+xChange][y+yChange]) {
count++;
if(xChange!=0)
xChange++;
if(yChange!=0) {
if(yChange>0)
yChange++;
else {
yChange--;
}
}
}
xChange=tempY;
yChange=tempX;
while(color==allChess[x-xChange][y-yChange]) {
count++;
if(xChange!=0)
xChange++;
if(yChange!=0) {
if(yChange>0)
yChange++;
else {
yChange--;
}
}
}
return count;
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@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) {
e.printStackTrace();
}
}
}
}
}
|