本文实例为大家分享了Java实现五子棋游戏的具体代码,供大家参考,具体内容如下
学习目的:熟悉java中swing类与java基础知识的巩固.(文末有源代码文件和打包的jar文件)
效果图:
思路:**1.首先构建一个Frame框架,来设置菜单选项与按钮点击事件。
MyFrame.java文件代码如下
package StartGame; import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JRadioButtonMenuItem; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; //窗体框架类 public class MyFrame extends JFrame { // static boolean canPlay1 = false;判断是否可以开始游戏 final MyPanel panel = new MyPanel(); public MyFrame() { // 设置窗体的大小并居中 this.setSize(500, 600); // 设置窗体大小 this.setTitle("五子棋游戏"); // 设置窗体标题 int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 获取屏幕的宽度 int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 获取屏幕的高度 this.setLocation((width - 500) / 2, (height - 500) / 2); // 设置窗体的位置(居中) this.setResizable(false); // 设置窗体不可以放大 // this.setLocationRelativeTo(null);//这句话也可以设置窗体居中 /* * 菜单栏的目录设置 */ // 设置菜单栏 JMenuBar bar = new JMenuBar(); this.setJMenuBar(bar); // 添加菜单栏目录 JMenu menu1 = new JMenu("游戏菜单"); // 实例化菜单栏目录 JMenu menu2 = new JMenu("设置"); JMenu menu3 = new JMenu("帮助"); bar.add(menu1); // 将目录添加到菜单栏 bar.add(menu2); bar.add(menu3); JMenu menu4 = new JMenu("博弈模式"); // 将“模式”菜单添加到“设置”里面 menu2.add(menu4); // JMenuItem item1=new JMenuItem("人人博弈"); // JMenuItem item2=new JMenuItem("人机博弈"); // 设置“”目录下面的子目录 JRadioButtonMenuItem item1 = new JRadioButtonMenuItem("人人博弈"); JRadioButtonMenuItem item2 = new JRadioButtonMenuItem("人机博弈"); // item1按钮添加时间并且为匿名类 item1.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新开始游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否保存设置并重新开始", "人机博弈设置", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { panel.setIsManAgainst(true); panel.Start(); item1.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 设置item2按钮的事件监听事件,也就是设置人机博弈 item2.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新开始游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否保存设置并重新开始", "人机博弈设置", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { panel.setIsManAgainst(false); panel.Start(); item2.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 设置按钮组并把人机博弈与人人博弈添加到一个按钮组里面 ButtonGroup bg = new ButtonGroup(); bg.add(item1); bg.add(item2); // 将按钮组添加到菜单里面 menu4.add(item1); menu4.add(item2); item2.setSelected(true); // 先行设置 JMenu menu5 = new JMenu("先行设置"); // 将“先行设置”菜单添加到“设置”里面 menu2.add(menu5); // 设置黑子先行还是白字先行的按钮 JRadioButtonMenuItem item3 = new JRadioButtonMenuItem("黑方先行"); JRadioButtonMenuItem item4 = new JRadioButtonMenuItem("白字先行"); // 设置item3的鼠标点击事件,设置黑方先行 item3.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新开始游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否保存设置并重新开始", "人机博弈设置", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { panel.setIsBlack(true); panel.Start(); item3.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 设置item4的鼠标点击事件 item4.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新开始游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否保存设置并重新开始", "人机博弈设置", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { panel.setIsBlack(false); panel.Start(); item4.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 设置按钮组并把人机博弈与人人博弈添加到一个按钮组里面 ButtonGroup bg1 = new ButtonGroup(); bg1.add(item3); bg1.add(item4); // 将按钮组添加到菜单里面 menu5.add(item3); menu5.add(item4); item3.setSelected(true); // 设置“帮助”下面的子目录 JMenuItem menu6 = new JMenuItem("帮助"); menu3.add(menu6); /* * 菜单栏的目录设置完毕 */ // 开始游戏菜单设置 JMenuItem menu7 = new JMenuItem("开始游戏"); menu1.add(menu7); JMenuItem menu8 = new JMenuItem("重新开始"); menu1.add(menu8); menu7.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub panel.Start(); // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); menu8.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "重新开始游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否重新开始", "消息", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { panel.Start(); } // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); // 退出游戏选项设置 JMenuItem menu9 = new JMenuItem("退出游戏"); menu1.add(menu9); menu9.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "退出游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否退出游戏", "消息", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { System.exit(0);// 退出程序 } // panel.repaint(); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); // 游戏难度设置 JRadioButtonMenuItem item5 = new JRadioButtonMenuItem("傻瓜");// 添加按钮 JRadioButtonMenuItem item6 = new JRadioButtonMenuItem("简单"); JRadioButtonMenuItem item7 = new JRadioButtonMenuItem("普通"); // JRadioButtonMenuItem item8= new JRadioButtonMenuItem("困难"); ButtonGroup bg3 = new ButtonGroup();// 设置按钮组 bg3.add(item5); bg3.add(item6); bg3.add(item7); // bg3.add(item8); JMenu menu10 = new JMenu("游戏难度设置");// 添加菜单到主菜单 menu2.add(menu10); menu10.add(item5);// 添加选项到难度设置菜单 menu10.add(item6); menu10.add(item7); // menu2.add(item8); item5.setSelected(true);// 默认选项按钮 // 傻瓜难度设置的鼠标点击事件 item5.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新开始游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否保存设置并重新开始", "人机博弈设置", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { panel.setGameDifficulty(0); panel.Start(); item5.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 简单难度设置模式 item6.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新开始游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否保存设置并重新开始", "难度设置", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { panel.setGameDifficulty(1); panel.Start(); item6.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); // 普通难度设置 item7.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; Object[] options = { "保存并重新开始游戏", "不,谢谢" }; int n = JOptionPane.showOptionDialog(null, "是否保存设置并重新开始", "人机博弈设置", 0, 1, icon, options, "保存并重新开始游戏"); if (n == 0) { panel.setGameDifficulty(2); panel.Start(); item7.setSelected(true); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); //游戏帮助提示信息 menu6.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Icon icon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub } @Override public int getIconWidth() { // TODO Auto-generated method stub return 0; } @Override public int getIconHeight() { // TODO Auto-generated method stub return 0; } }; JOptionPane.showMessageDialog(null, "制作人员:韩红剑"); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } }); /* * 窗口里面的容器设置 */ Container con = this.getContentPane(); // 实例化一个容器父类 con.add(panel); // 将容器添加到父类 /* * 窗口里面的容器设置完毕 */ } }
2.第二步,设置显示棋盘的容器,文件源代码为MyPanel.java
package StartGame; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Panel; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.*; import java.io.File; import java.net.URL; import java.util.Arrays; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.JPanel; //设置窗体里面的容器操作 public class MyPanel extends JPanel implements MouseListener, Runnable { private static final Toolkit ResourceUtil = null; public Image boardImg; // 定义背景图片 static int[][] allChess = new int[15][15]; // 棋盘数组 static int[][] temporaryChess = new int[15][15]; int x;// 保存棋子的横坐标 int y;// 保存棋子的纵坐标 Boolean canPlay = false; // 游戏是否继续,默认为继续 Boolean isBlack = true;// 是否是黑子,默认为黑子 Boolean isManAgainst = false; // 判断是否是人人对战 String message = "用户下棋"; Thread t = new Thread(this); int maxTime = 120; int blackTime = 120; int whiteTime = 120; String blackMessage = "无限制"; String whiteMessage = "无限制"; static int gameDifficulty = 0;// 设置游戏难度,0为傻瓜模式,1为简单,2为普通,3为困难 // 获取isBlack的值 public boolean getIsBlack() { return this.isBlack; } // 设置isBlack的值 public void setIsBlack(boolean isBlack) { this.isBlack = isBlack; } // 获取isManAgainst的值 public boolean getIsManAgainst() { return this.isManAgainst; } // 获取isManAgainst的值 public void setIsManAgainst(boolean isManAgainst) { this.isManAgainst = isManAgainst; } // 获取isManAgainst的值 public int getGameDifficulty() { return this.gameDifficulty; } // 设置setGameDifficulty的值 public void setGameDifficulty(int gameDifficulty) { this.gameDifficulty = gameDifficulty; } // 构造函数 public MyPanel() { boardImg = Toolkit.getDefaultToolkit().getImage("./src/StartGame/fiveCHessBourd.jpg"); this.repaint(); // 添加鼠标监视器 addMouseListener((MouseListener) this); // addMouseMotionListener((MouseMotionListener) this); // this.requestFocus(); t.start(); t.suspend();// 线程挂起 // t.resume(); } // 数据初始化 @Override public void paint(Graphics g) { super.paint(g); int imgWidth = boardImg.getWidth(this); // 获取图片的宽度 int imgHeight = boardImg.getHeight(this); // 获取图片的高度 int FWidth = getWidth(); int FHeight = getHeight(); String message; // 标记谁下棋 int x = (FWidth - imgWidth) / 2; int y = (FHeight - imgHeight) / 2; g.drawImage(boardImg, x, y, null); // 添加背景图片到容器里面 g.setFont(new Font("宋体", 0, 14)); g.drawString("黑方时间:" + blackTime, 30, 470); g.drawString("白方时间:" + whiteTime, 260, 470); // 绘制棋盘 for (int i = 0; i < 15; i++) { g.drawLine(30, 30 + 30 * i, 450, 30 + 30 * i); g.drawLine(30 + 30 * i, 30, 30 + 30 * i, 450); } // 绘制五个中心点 g.fillRect(240 - 5, 240 - 5, 10, 10); // 绘制最中心的正方形 g.fillRect(360 - 5, 360 - 5, 10, 10); // 绘制右下的正方形 g.fillRect(360 - 5, 120 - 5, 10, 10); // 绘制右上的正方形 g.fillRect(120 - 5, 360 - 5, 10, 10);// 绘制左下的正方形 g.fillRect(120 - 5, 120 - 5, 10, 10);// 绘制左上的正方形 // 定义棋盘数组 for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { // if (allChess[i][j] == 1) { // // 黑子 // int tempX = i * 30 + 30; // int tempY = j * 30 + 30; // g.fillOval(tempX - 7, tempY - 7, 14, 14); // } // if (allChess[i][j] == 2) { // // 白子 // int tempX = i * 30 + 30; // int tempY = j * 30 + 30; // g.setColor(Color.WHITE); // g.fillOval(tempX - 7, tempY - 7, 14, 14); // g.setColor(Color.BLACK); // g.drawOval(tempX - 7, tempY - 7, 14, 14); // } draw(g, i, j); // 调用下棋子函数 } } } // 鼠标点击时发生的函数 @Override public void mousePressed(MouseEvent e) { // x = e.getX();// 获取鼠标点击坐标的横坐标 // y = e.getY();// 获取鼠标点击坐标的纵坐标 // if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // 鼠标点击在棋子框里面才有效 // // } if (canPlay == true) {// 判断是否可以开始游戏 x = e.getX(); // 获取鼠标的焦点 y = e.getY(); if (isManAgainst == true) {// 判断是否是人人对战 manToManChess(); } else { // 否则是人机对战,人机下棋 manToMachine(); } } } // 判断是否输赢的函数 private boolean checkWin(int x, int y) { // TODO Auto-generated method stub boolean flag = false; // 保存共有多少相同颜色棋子相连 int count = 1; // 判断横向 特点:allChess[x][y]中y值相同 int color = allChess[x][y]; // 判断横向 count = this.checkCount(x, y, 1, 0, color); if (count >= 5) { flag = true; } else { // 判断纵向 count = this.checkCount(x, y, 0, 1, color); if (count >= 5) { flag = true; } else { // 判断右上左下 count = this.checkCount(x, y, 1, -1, color); if (count >= 5) { flag = true; } else { // 判断左下右上 count = this.checkCount(x, y, 1, 1, color); if (count >= 5) { flag = true; } } } } return flag; } // 判断相同棋子连接的个数 private int checkCount(int x, int y, int xChange, int yChange, int color) { // TODO Auto-generated method stub int count = 1; int tempX = xChange; int tempY = yChange; while (x + xChange >= 0 && x + xChange <= 14 && y + yChange >= 0 && y + yChange <= 14 && 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 (x - xChange >= 0 && x - xChange <= 14 && y - yChange >= 0 && y - yChange <= 14 && color == allChess[x - xChange][y - yChange]) { count++; if (xChange != 0) { xChange++; } if (yChange != 0) { if (yChange > 0) { yChange++; } else { yChange--; } } } return count; } // 机器人判断黑棋相连的数量 private int checkCountMachine(int x, int y, int xChange, int yChange, int color) { // TODO Auto-generated method stub int count = 0; int tempX = xChange; int tempY = yChange; while (x + xChange >= 0 && x + xChange <= 14 && y + yChange >= 0 && y + yChange <= 14 && 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 (x - xChange >= 0 && x - xChange <= 14 && y - yChange >= 0 && y - yChange <= 14 && color == allChess[x - xChange][y - yChange]) { count++; if (xChange != 0) { xChange++; } if (yChange != 0) { if (yChange > 0) { yChange++; } else { yChange--; } } } return count; } public void paintConmponents(Graphics g) { super.paintComponents(g); } // 绘制黑白棋子 public void draw(Graphics g, int i, int j) { if (allChess[i][j] == 1) { g.setColor(Color.black);// 黑色棋子 g.fillOval(30 * i + 30 - 7, 30 * j + 30 - 7, 14, 14); g.drawString(message, 230, 20); } if (allChess[i][j] == 2) { g.setColor(Color.white);// 白色棋子 g.fillOval(30 * i + 30 - 7, 30 * j + 30 - 7, 14, 14); g.drawString(message, 230, 20); } } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(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 run() { // TODO Auto-generated method stub // 判断是否有时间限制 if (maxTime > 0) { while (true) { // System.out.println(canPlay + "11"); if (isManAgainst) { if (isBlack) { blackTime--; if (blackTime == 0) { JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!"); } } else { whiteTime--; if (whiteTime == 0) { JOptionPane.showMessageDialog(this, "白方超时,游戏结束!"); } } } else { // 监控黑子下棋的时间,也就是用户下棋的时间 blackTime--; if (blackTime == 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 void Start() { this.canPlay = true; for (int i = 0; i < 14; i++) { for (int j = 0; j < 14; j++) { allChess[i][j] = 0; } } if (canPlay == true) { t.resume(); } this.repaint(); JOptionPane.showMessageDialog(this, "游戏开始了,请开始下棋"); if (isBlack == false && isManAgainst == false) { machineChess(gameDifficulty); } // 另一种方式 allChess=new int[19][19] // 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.repaint();// 如果不重新调用,则界面不会刷新 } // 人人对战下棋函数 public void manToManChess() { if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // System.out.println("在棋盘范围内:"+x+"--"+y); x = (x + 15) / 30 - 1; // 是为了取得交叉点的坐标 y = (y + 15) / 30 - 1; if (allChess[x][y] == 0) { // 判断当前要下的是什么棋子 if (isBlack == true) { allChess[x][y] = 1; isBlack = false; blackTime = 120; message = "轮到白方"; } else { allChess[x][y] = 2; isBlack = true; whiteTime = 120; message = "轮到黑方"; } } // 判断这个棋子是否和其他棋子连成5个 boolean winFlag = this.checkWin(x, y); this.repaint(); // 绘制棋子 if (winFlag == true) { JOptionPane.showMessageDialog(this, "游戏结束," + (allChess[x][y] == 1 ? "黑方" : "白方") + "获胜!"); canPlay = false; } } else { // JOptionPane.showMessageDialog(this, // "当前位子已经有棋子,请重新落子!!!"); } } // 人机对战下棋函数 public void manToMachine() { if (x >= 29 && x <= 451 && y >= 29 && y <= 451) { // System.out.println("在棋盘范围内:"+x+"--"+y); x = (x + 15) / 30 - 1; // 是为了取得交叉点的坐标 y = (y + 15) / 30 - 1; if (allChess[x][y] == 0) { // 判断当前要下的是什么棋子 if (isBlack == true) { allChess[x][y] = 1; this.repaint(); // 绘制棋子 machineChess(gameDifficulty); isBlack = true; blackTime = 120; message = "用户下棋"; whiteTime = 120; boolean winFlag = this.checkWin(x, y); this.repaint(); // 绘制棋子 if (winFlag == true) { JOptionPane.showMessageDialog(this, "游戏结束," + (allChess[x][y] == 1 ? "黑方" : "白方") + "获胜!"); canPlay = false; } } else { allChess[x][y] = 1; // allChess[x][y] = 2; this.repaint(); isBlack = false; whiteTime = 120; blackTime = 120; boolean winFlag = this.checkWin(x, y); this.repaint(); // 绘制棋子 if (winFlag == true) { JOptionPane.showMessageDialog(this, "游戏结束," + (allChess[x][y] == 1 ? "黑方" : "白方") + "获胜!"); canPlay = false; } machineChess(gameDifficulty); } } // 判断这个棋子是否和其他棋子连成5个 // boolean winFlag = this.checkWin(x, y); // this.repaint(); // 绘制棋子 // // if (winFlag == true) { // JOptionPane.showMessageDialog(this, "游戏结束," + (allChess[x][y] == // 1 ? "黑方" : "白方") + "获胜!"); // canPlay = false; // } } } // 机器人下棋算法 public void machineChess(int gameDifficulty) { if (gameDifficulty == 0) { int i, j; boolean chessSucceed = true;// 下棋成功的标志 while (chessSucceed) { i = (int) (Math.random() * 15); j = (int) (Math.random() * 15); if (allChess[i][j] == 0) { allChess[i][j] = 2; chessSucceed = false; } } } else if (gameDifficulty == 1) { int max = 0; int m = 0, n = 0; int figureSort[] = new int[4];// 对棋子的四个方向进行打分 for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (allChess[i][j] == 0) { figureSort[0] = checkCountMachine(i, j, 0, 1, 1); figureSort[1] = checkCountMachine(i, j, 1, 0, 1); figureSort[2] = checkCountMachine(i, j, 1, -1, 1); figureSort[3] = checkCountMachine(i, j, 1, 1, 1); sortFourFigure(figureSort); temporaryChess[i][j] = figureSort[0] * 12 + figureSort[1] * 25 + figureSort[2] * 50// 算出空白棋子的最大分 + figureSort[3] * 100; } // System.out.print(temporaryChess[i][j] + " "); } // System.out.print("\n"); } // System.out.print("=============================\n"); for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (temporaryChess[i][j] > max && allChess[i][j] != 2 && allChess[i][j] != 1) { m = i; n = j; max = temporaryChess[i][j]; } } } allChess[m][n] = 2; } else if (gameDifficulty == 2) { } else { } this.repaint(); } // 对数组排序 public void sortFourFigure(int n[]) { Arrays.sort(n);// 正序排序 } }
3.最后设置整个游戏的入口主函数StartEntrance.java
package StartGame; import javax.swing.JFrame; public class StartEntrance { //public static boolean canPlay =false; public static void main(String[] args) { // 实例化一个窗口 MyFrame myFrame = new MyFrame(); myFrame.setVisible(true); // 设置窗口为可见 myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭操作属性 } }
以上所有代码均调试通过,人机算法较为简单,可实现简单的防守功能。
java源代码:链接
打包的jar文件:链接
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/183273/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)