Java实现五子棋(附详细源码)

本文实例为大家分享了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文件:链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • java实现单人版五子棋游戏

    本文实例讲述了java实现的五子棋游戏代码,分享给大家供大家参考,具体代码如下 package gyb.exam; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Point; import java.awt.Toolkit; import java.awt.

  • Java实现五子棋AI算法

    五子棋AI算法 也算是一个典型的游戏AI算法,一些棋类的AI算法都可以参考实现,下面是Java实现代码 棋盘抽象接口 import java.util.List; public interface IChessboard { //取得棋盘最大横坐标 public int getMaxX(); //最大纵坐标 public int getMaxY(); //取得当前所有空白点,这些点才可以下棋 public List<Point> getFreePoints(); } 棋子类实现 //棋子类 p

  • Java实现两人五子棋游戏(三) 画出棋子

    上一篇文章讲的是Java实现两人五子棋游戏(二) 画出棋盘,已经画好棋盘,接下来要实现控制功能,主要功能: 1)选择棋子 2)画棋子 3)判断胜负 4)交换行棋方 先实现画棋子PART -------------画棋子代码示例如下-------------- 首先,定义一个棋子类,这个类有两个属性,棋子颜色(0-表示黑色,1-表示白色),是否落子(我计划用一个二维数组才存储棋子的落子信息) Chessman.java package xchen.test.simpleGobang; public

  • java基于swing实现的五子棋游戏代码

    本文实例讲述了java基于swing实现的五子棋游戏代码.分享给大家供大家参考. 主要功能代码如下: 复制代码 代码如下: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Main extends JFrame implements ActionListener{         private static final long serialVersionUID = 1L;      

  • 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); //设置窗

  • Java棋类游戏实践之单机版五子棋

    本文实例讲述了java实现的五子棋游戏代码,分享给大家供大家参考,具体代码如下 一.实践目标        1.掌握JavaGUI界面设计        2.掌握鼠标事件的监听(MouseListener,MouseMotionListener) 二.实践内容       设计一个简单的五子棋程序,能够实现五子棋下棋过程.如下图所示 1.五子棋棋盘类 package cn.edu.ouc.fiveChess; import java.awt.Color; import java.awt.Curs

  • java实现单机版五子棋

    这个小游戏是我和我姐们儿的JAVA课程设计,也是我做的第一个JAVA项目,适合初学者,希望能帮到那些被JAVA课设所困扰的孩纸们~~~ 一.该游戏需要实现 1.设计主框架,界面. 2.利用ActionListener接口实现按钮事件的监听. 3.重新开始功能的实现. 4.悔棋功能的实现. 5.退出功能的实现. 6.棋盘中棋子点类的定义. 7.利用MouseListener接口实现事件监听,并实现接口里的所有方法. 8.当鼠标移动到棋盘上的交点上,且该点上无棋子时能够变成小手形状. 9.点击棋盘时

  • Java编程实现五子棋人人对战代码示例

    利用Java,在控制台操作下,编写的五子棋,作为复习二维数组,面向对象等基础知识.w表示白棋,b表示黑棋 import java.util.Scanner; public class MainMethod { public static char[][] c = new char[10][10]; public static void main(String[] args) { MainMethod mainMethod = new MainMethod(); mainMethod.init()

  • Java实现五子棋网络版

    本文实例为大家分享了Java实现五子棋网络版的具体代码,供大家参考,具体内容如下 需求分析: 对于网络五子棋而言,在普通五子棋的基础上需要添加以下功能: 1.拥有服务器端和客户端,用户通过客户端登录服务器后可与其他登录的用户进行对弈 2.服务器支持多组用户同时进行对弈 3.用户可以在服务器上创建新游戏或加入已创建的游戏 4.用户在下棋的时候可以进行聊天交流 由上可以知道需要实现的功能: ·提供服务器和客户端的功能 ·服务器将监听客户端的登录情况并允许多个客户端进行登录 ·用户通过客户端可以登录服

  • Java实现两人五子棋游戏(二) 画出棋盘

    本文为大家分享了java画出五子棋游戏棋盘的方法,供大家参考,具体内容如下 棋盘模块: 画五子棋棋盘:19条横线.19条竖线 步骤一:显示棋盘 我有一张名为chessboard.png的棋盘,位置为根目录/res/drawable/chessboard/png,现在我要显示这张图片. DrawChessBoard.java package xchen.test.simpleGobang; import java.awt.Graphics; import java.awt.Image; impor

随机推荐