五子棋人机对战java代码

  • 格式:doc
  • 大小:67.50 KB
  • 文档页数:15

1 附件二 源程序代码 package Panels; 文件一: import javax.swing.JPanel; import javax.swing.JFrame; import Panels.Bjpanel; public class Wuziqi { private JFrame jFrame = null; private JPanel jContentPane = null; public static void main(String[] args) { { Wuziqi application = new Wuziqi(); application.getJFrame().setVisible(true); } } private JFrame getJFrame() { if (jFrame == null) { jFrame = new JFrame(); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setSize(500,500); jFrame.setContentPane(getJContentPane()); jFrame.setTitle("简易五子棋"); //窗体位置设置大小不可变 jFrame.setLocation(400,150); //设置关闭方式:窗口关闭后程序结束 jFrame.setResizable(false); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } return jFrame; } private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); this.jContentPane.add(new Bjpanel()); } return jContentPane; 2

} }

文件二: package Panels; import java.awt.Font; import java.awt.Graphics; import javax.imageio.ImageIO; import javax.swing.JOptionPane; import javax.swing.JPanel; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; public class Bjpanel extends JPanel implements MouseListener { private static final long serialVersionUID = 1L; //素材 BufferedImage backgroundImage=null; BufferedImage whiteImage1=null; BufferedImage blackImage2=null; BufferedImage chessboardImage=null; //棋子坐标 int x=0; int y=0; //保存下过棋子的全部坐标 //数据内容含义:0:这个点没有棋子 1:这个点是黑子 2:这个点是白子 int[][] allqz=new int[15][15]; //标志下一步该黑走还是白走 boolean flag=true; //能否进行游戏的标志 boolean canplay=true; //记录每个可放白棋的点的优先大小,数字越大,越优先在这个点放白棋 int[][] youxian=new int[15][15]; //最优先 int[][] zyouxian=new int[15][15]; //保存游戏信息 String message="黑方下子"; public Bjpanel() { super(); initialize(); } 3

private void initialize() { this.setSize(500, 500); try { URL imgURL = Bjpanel.class.getResource("background.jpg"); URL imgURL1 = Bjpanel.class.getResource("white.png"); URL imgURL2 = Bjpanel.class.getResource("black.png"); URL imgURL3 = Bjpanel.class.getResource("chessboard.jpg"); backgroundImage=ImageIO.read(imgURL); whiteImage1=ImageIO.read(imgURL1); blackImage2=ImageIO.read(imgURL2); chessboardImage=ImageIO.read(imgURL3); } catch (IOException e) { e.printStackTrace(); } this.addMouseListener(this); this.setLayout(null); //启动线程 } public void paint(Graphics g) { g.drawImage(backgroundImage, 0, 0,this); g.drawImage(chessboardImage, 10, 50, 363, 364, this); g.setFont(new Font("黑体",Font.BOLD,20)); g.drawString("游戏信息:"+message, 150, 40); g.setFont(new Font("宋体",0,12)); g.drawString("黑方时间:无限制", 30, 450); g.drawString("白方时间:无限制", 250, 450); //绘制棋子 for(int i=0;i<15;i++) { for(int j=0;j<15;j++) { if(allqz[i][j]==1) { int tempx=i*24+18; int tempy=j*24+58; g.drawImage(blackImage2, tempx, tempy, 15, 15,this ); } if(allqz[i][j]==2) { int tempx=i*24+18; 4

int tempy=j*24+58; g.drawImage(whiteImage1, tempx, tempy, 15, 15, this); } } } } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { if(canplay) { x=e.getX(); y=e.getY(); if(x>=18&&x<=379&&y>=58&&y<=580) { x=(x-18)/24; y=(y-58)/24; //判断该坐标是否有棋子,没有则放棋子,有则显示已经有棋子对话框 if(allqz[x][y]==0) { //玩家鼠标点击的坐标设置为放黑子标志 allqz[x][y]=1; message="白方走棋"; this.repaint(); boolean Winflag=this.Win(); if(Winflag) { JOptionPane .showMessageDialog(this, "游戏结束:"+(allqz[x][y]==1?"黑方胜":"白方胜")); canplay=false; } if(canplay) { //电脑通过最优算法获得最能够阻止玩家的黑棋成为5连得坐标x,y 并把这个坐标设置为放白子标记 //allqz[x][y]=2; 5

this.zuiyouBaiqi(); message="黑方走棋"; this.repaint(); //判断落棋后是否连成5个,即判断谁赢并游戏结束 Winflag=this.Win(); if(Winflag) { JOptionPane .showMessageDialog(this, "游戏结束:"+(allqz[x][y]==1?"黑方胜":"白方胜")); canplay=false; } } } else { JOptionPane .showMessageDialog(this, "已经有棋子,请重新落子!"); } } } //点击开始游戏按钮 if(e.getX()>=401&&e.getX()<=469&&e.getY()>=51&&e.getY()<=79) { int result=JOptionPane.showConfirmDialog(this, "是否重新开始游戏?"); canplay=true; if(result==0) { //把棋盘清空---将allqz[][]全部数据清零 allqz=new int[15][15]; //将游戏信息提示变回出示位置 message="黑方先行"; this.repaint(); } } if(e.getX()>=401&&e.getX()<=469&&e.getY()>=151&&e.getY()<=179) { JOptionPane.showMessageDialog(this, "这是一个简易五子棋游戏,玩家持黑棋,电脑持白棋,"); JOptionPane.showMessageDialog(this, "任一方先在棋盘上形成横向、竖向、斜向的连续的相同颜色的五个(含五个以上)棋子的一方为胜!"); } if(e.getX()>=401&&e.getX()<=469&&e.getY()>=252&&e.getY()<=280) { int b=JOptionPane.showConfirmDialog(this, "确定认输");