Java五子棋全代码(共12页).doc
精选优质文档-倾情为你奉上作者:商洛学院-杨小军子类代码package org.wuziqi;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 FiveChessFrame extends JFrame implements MouseListener,Runnable int width=Toolkit.getDefaultToolkit().getScreenSize().width; /取得屏幕的宽度 int height=Toolkit.getDefaultToolkit().getScreenSize().height; /取得屏幕的高度 BufferedImage bgimage=null; /保存棋子坐标 int x = 0; int y = 0; /保存之前下过的全部棋子的坐标 /其中的数据内容为0:此点为无子;1为黑子,2为白子 int allChess=new int1920; /标识当前应该是黑棋还是白棋下棋 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 FiveChessFrame() this.setTitle("我的五子棋游戏作者:杨小军"); this.setSize(500,500); this.setLocation(width-500)/2,(height-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:/wuziqi.jpg"); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); public void paint(Graphics g) /双缓冲技术防止屏幕闪烁 BufferedImage bi= new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB); Graphics g2=bi.createGraphics(); g2.drawImage(bgimage, 0, 20,this); g2.setFont(new Font("宋体",Font.BOLD,20); g2.setColor(Color.RED); g2.drawString("木易小舟", 10, 60); g2.setColor(Color.BLACK); g2.drawString("游戏信息:"+message, 100, 60); g2.setFont(new Font("黑体",0,14); g2.drawString("黑方时间:"+blackMessage, 30, 475); g2.drawString("白方时间:"+whiteMessage, 260, 475); /绘制棋盘 for(int i=0;i<19;i+) g2.drawLine(10,70+20*i,370,70+20*i); g2.drawLine(10+20*i,70,10+20*i,430); g2.fillOval(69,128,4,4); g2.fillOval(308,128,4,4); g2.fillOval(68,388,4,4); g2.fillOval(308,388,4,4); g2.fillOval(189,248,4,4); g2.fillOval(189,128,4,4); g2.fillOval(189,388,4,4); g2.fillOval(308,248,4,4); g2.fillOval(68,248,4,4); /g.fillOval(x,y,10,10); for(int i=0;i<19;i+) for(int j=0;j<19;j+) if(allChessij=1) /黑子 int tempX=i*20+10; int tempY=j*20+70; g2.setColor(Color.BLACK); g2.fillOval(tempX-7,tempY-7,14,14); if(allChessij=2) /白子 int tempX=i*20+10; int tempY=j*20+70; g2.setColor(Color.WHITE); g2.fillOval(tempX-7, tempY-7,14,14); g2.setColor(Color.BLACK); g2.drawOval(tempX-7,tempY-7, 14, 14); g.drawImage(bi,0,0,this); Override public void mouseClicked(MouseEvent e) / TODO Auto-generated method stub Override public void mouseEntered(MouseEvent e) / TODO Auto-generated method stub Override public void mouseExited(MouseEvent e) / TODO Auto-generated method stub Override public void mousePressed(MouseEvent e) / TODO Auto-generated method stub /System.out.println("X:"+e.getX(); /System.out.println("Y:"+e.getY(); if(canPlay=true) x=e.getX(); y=e.getY(); if(x>=10&&x<=370&&y>=75&&y<=455) x=(x-10)/20; y=(y-70)/20; if(allChessxy=0) /判断该谁下棋; if(isBlack=true) allChessxy=1; message="轮到白方" isBlack=false; else allChessxy=2; isBlack=true; message="轮到黑方" /判断胜负,是否有子连到五个; boolean winFlag=this.checkWin(); if(winFlag=true) JOptionPane.showMessageDialog(this, "游戏结束,"+(allChessxy=1?"黑方胜":"白方胜"); canPlay=false; else JOptionPane.showMessageDialog(this, "当前此位置已经有棋子了"); this.repaint(); /System.out.println(e.getX()+"-"+e.getY(); if(e.getX()>=408&&e.getX()<=474&&e.getY()>=70&&e.getY()<=97) /点击开始游戏按钮 int result=JOptionPane.showConfirmDialog(this, "是否重新开始游戏?"); if(result=0) /现在从新开始游戏 /1,将棋盘清空/allChessij全部数据归0 /2,将游戏信息显示到开始位置 /3,将下一步下棋的人改为黑方 for(int i=0;i<19;i+) for(int j=0;j<19;j+) allChessij=0; / 令一种方式将allchess=new int1919 message="黑方先行" isBlack=true; 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="无限制" this.canPlay=true; this.repaint();/重新刷新一下页面 if(e.getX()>=406&&e.getX()<=475&&e.getY()>=122&&e.getY()<=149) /点击游戏设置按钮 String input= JOptionPane.showInputDialog("请输入游戏的最大时间(单位:分钟),如果输入0表示没有时间限制"); try maxTime=Integer.parseInt(input)*60; if(maxTime<0) JOptionPane.showMessageDialog(this, "请输入正确信息,不充许输入负数"); if(maxTime=0) int reslut=JOptionPane.showConfirmDialog(this, "设置完成,是否开始新的游戏"); for(int i=0;i<19;i+) for(int j=0;j<19;j+) allChessij=0; / 令一种方式将allchess=new int1919 message="黑方先行" isBlack=true; blackTime=maxTime; whiteTime=maxTime; blackMessage="无限制" whiteMessage="无限制" this.repaint();/重新刷新一下页面 this.canPlay=true; if(maxTime>0) int reslut=JOptionPane.showConfirmDialog(this, "设置完成,是否开始新的游戏"); for(int i=0;i<19;i+) for(int j=0;j<19;j+) allChessij=0; / 令一种方式将allchess=new int1919 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.repaint();/重新刷新一下页面 this.canPlay=true; catch(NumberFormatException e1) JOptionPane.showMessageDialog(this, "请正确输入信息"); if(e.getX()>=408&&e.getX()<=474&&e.getY()>=174&&e.getY()<=198) /点击游戏说明按钮 JOptionPane.showMessageDialog(this, "这是一个五子棋游戏,黑白双方" + "轮流下棋,当某一方的棋子连到了五子是算获胜,游戏结束"); if(e.getX()>=408&&e.getX()<=474&&e.getY()>=275&&e.getY()<=300) /点击认输按钮 int result= JOptionPane.showConfirmDialog(this, "是否确认认输?"); if(result=0) if(isBlack) JOptionPane.showMessageDialog(this, "黑方已经认输,白方获胜"); else JOptionPane.showMessageDialog(this,"白方已经认输,黑方获胜"); canPlay=false; if(e.getX()>=408&&e.getX()<=474&&e.getY()>=327&&e.getY()<=353) /点击关于按钮 JOptionPane.showMessageDialog(this, "本游戏有Acanoe制作,有任何问提请直接咨询09电本,杨小军同学"); if(e.getX()>=408&&e.getX()<=474&&e.getY()>=379&&e.getY()<=406) /点击退出游戏按钮 JOptionPane.showMessageDialog(this, "退出游戏?"); System.exit(0); Override public void mouseReleased(MouseEvent e) / TODO Auto-generated method stub private boolean checkWin() boolean flag=false; /保存共有多少相同的颜色的棋子相连 int count=1; /判断横向的是否有五个相同的棋子相连 /纵坐标相同 int color=allChessxy; /通过循环来进行判断 /*int i=1; while(color=allChessx+iy) count+; i+; i=1; while(color=allChessx-iy) count+; i+; if(count>=5) flag=true; / int i2=1; int count2=1; while(color=allChessxy+i2) count2+; i2+; i2=1; while(color=allChessxy-i2) count2+; i2+; if(count2>=5) flag=true; /斜方向判断(右上方+左下方) int i3=1; int count3=1; while(color=allChessx+i3y-i3) count3+; i3+; i3=1; while(color=allChessx-i3y+i3) count3+; i3+; if(count3>=5) flag=true; /左上方+右下方判断 int i4=1; int count4=1; while(color=allChessx+i4y+i4) count4+; i4+; i4=1; while(color=allChessx-i4y-i4) count4+; i4+; if(count4>=5) flag=true; */ 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(x+xChange>=0&&x+xChange<=18&&y+yChange>=0 &&y+yChange<=18 &&color=allChessx+xChangey+yChange) count+; if(xChange!=0) xChange+; if(yChange!=0) if(yChange>0) yChange+; else yChange-; xChange=tempX; yChange=tempY; while(x-xChange>=0&&x-xChange<=18&&y-yChange>=0 &&y-yChange<=18 &&color=allChessx-xChangey-yChange) count+; if(xChange!=0) xChange+; if(yChange!=0) if(yChange>0) yChange+; else yChange-; return count; Override public void run() / TODO Auto-generated method stub 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+":"+