Java实现的五子棋游戏 ~java.awtjava.swing

news/2024/10/29 3:31:17/

文章目录

  • Java实现的五子棋游戏
      • 1.实现效果
      • 2.实现源码
        • 2.1运行主函数main.java
        • 2.2 棋盘布局Chessboard.java
        • 3.Algorithm算法
  • 点击下载链接:Java实现的五子棋游戏源码下载

Java实现的五子棋游戏

作业要求:
(1)课题代号: 2
(2)课题名称: 2D 游戏设计
(3)课题要求:设计一种二维游戏(如数独,扫雷,飞机大战,贪食蛇,五子棋等),完成界面设计和必要的游戏功能

以下主要实现的功能有:

一、下棋功能,在棋盘的交点处落子。

二、简单人机对战功能。

1.实现效果

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2.实现源码

2.1运行主函数main.java

package com.fivechess;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/*** @author RFOS~五子棋😋* @date 2021年7月6日 下午1:45:36*/
public class Main extends JFrame{/** 用户登录*/private static final long servialVersionUID = 1L;final JLabel logoLabel = new JLabel("RFOS~五子棋");final JLabel logo = new JLabel();final JButton loginButton = new JButton("                  登   陆                  ");final JLabel registerLabel = new JLabel("立即注册");final JLabel userLabel = new JLabel("账号:");final JLabel passwordLabel = new JLabel("密码:");final static JTextField userjt = new JTextField(11);final JPasswordField passwordjt = new JPasswordField(11);final JCheckBox rememberPasswordjcb = new JCheckBox();final JLabel rememberPasswordjl = new JLabel("记住密码");final JCheckBox automaticLoginjcb = new JCheckBox();final JLabel automaticLoginjl = new JLabel("自动登录");final JLabel promptPasswordFalse = new JLabel("密码错误!");final JLabel promptRegister = new JLabel("该账号还未注册!");final JLabel promptUserNameEmpty = new JLabel("请输入账号!");final JLabel prompPasswordEmpty = new JLabel("请输入密码!");final Color color = new Color(255, 218, 185);final FileOperation read = new FileOperation();//创建文件对象final FileOperation f = new FileOperation();public Main() {setTitle("RFOS~五子棋");setBounds(200, 200, 500, 500);setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);setVisible(true);//基本布局设置SpringLayout springLayout = new SpringLayout();//使用弹簧布局管理器Container c = getContentPane();//创建容器c.setBackground(new Color(205, 133, 63));c.setLayout(springLayout);userjt.setFont(new Font("微软雅黑", 0, 18 ));userjt.setText(Register.userName);passwordjt.setFont(new Font("微软雅黑", 0, 18));passwordjt.setText(Register.password);logoLabel.setFont(new Font("微软雅黑", 1, 48));logoLabel.setForeground(Color.pink);ImageIcon logoimage = new ImageIcon(Main.class.getResource("/image/logo5.jpg"));logoimage.setImage(logoimage.getImage().getScaledInstance(260, 130, Image.SCALE_DEFAULT));logo.setIcon(logoimage);userLabel.setFont(new Font("微软雅黑", 1, 20));passwordLabel.setFont(new Font("微软雅黑", 1, 20));rememberPasswordjl.setFont(new Font("微软雅黑", 0, 14));rememberPasswordjl.setForeground(Color.gray);automaticLoginjl.setFont(new Font("微软雅黑", 0, 14));automaticLoginjl.setForeground(Color.gray);loginButton.setFont(new Font("微软雅黑", 1, 16));registerLabel.setFont(new Font("微软雅黑", 1, 13));registerLabel.setForeground(Color.gray);promptPasswordFalse.setFont(new Font("微软雅黑", 0, 13));promptPasswordFalse.setForeground(Color.red);promptUserNameEmpty.setFont(new Font("微软雅黑", 0, 13));promptUserNameEmpty.setForeground(Color.red);prompPasswordEmpty.setFont(new Font("微软雅黑", 0, 13));prompPasswordEmpty.setForeground(Color.red);promptRegister.setFont(new Font("微软雅黑", 0, 13));promptRegister.setForeground(Color.red);rememberPasswordjcb.setBackground(new Color(255, 218, 185));automaticLoginjcb.setBackground(new Color(255, 218, 185));c.add(logo);//首页图标springLayout.putConstraint(springLayout.NORTH, logo, 40, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, logo, 115, springLayout.WEST, c);c.add(logoLabel);//标题“RFOS~五子棋”springLayout.putConstraint(springLayout.NORTH, logoLabel, 100, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, logoLabel, 120, springLayout.WEST, c);logoLabel.setVisible(false);c.add(userLabel);//用户名springLayout.putConstraint(springLayout.NORTH, userLabel, 35, springLayout.SOUTH, logoLabel);springLayout.putConstraint(springLayout.WEST, userLabel, 110, springLayout.WEST, c);c.add(userjt);springLayout.putConstraint(springLayout.NORTH, userjt, 35, springLayout.SOUTH, logoLabel);springLayout.putConstraint(springLayout.WEST, userjt, 10, springLayout.EAST, userLabel);c.add(passwordLabel);//密码springLayout.putConstraint(springLayout.NORTH, passwordLabel, 10, springLayout.SOUTH, userLabel);springLayout.putConstraint(springLayout.WEST, passwordLabel, 110, springLayout.WEST, c);c.add(passwordjt);springLayout.putConstraint(springLayout.NORTH, passwordjt, 10, springLayout.SOUTH, userjt);springLayout.putConstraint(springLayout.WEST, passwordjt, 10, springLayout.EAST, passwordLabel);c.add(rememberPasswordjcb);//复选框springLayout.putConstraint(springLayout.NORTH, rememberPasswordjcb, 10, springLayout.SOUTH, passwordLabel);springLayout.putConstraint(springLayout.WEST, rememberPasswordjcb, 175, springLayout.WEST, c);c.add(rememberPasswordjl);springLayout.putConstraint(springLayout.NORTH, rememberPasswordjl, 10, springLayout.SOUTH, passwordjt);springLayout.putConstraint(springLayout.WEST, rememberPasswordjl, 5, springLayout.EAST, rememberPasswordjcb);c.add(automaticLoginjcb);springLayout.putConstraint(springLayout.NORTH, automaticLoginjcb, 10, springLayout.SOUTH, passwordjt);springLayout.putConstraint(springLayout.WEST, automaticLoginjcb, 30, springLayout.EAST, rememberPasswordjl);c.add(automaticLoginjl);springLayout.putConstraint(springLayout.NORTH, automaticLoginjl, 10, springLayout.SOUTH, passwordjt);springLayout.putConstraint(springLayout.WEST, automaticLoginjl, 5, springLayout.EAST, automaticLoginjcb);c.add(loginButton);//登陆按钮springLayout.putConstraint(springLayout.NORTH, loginButton, 20, springLayout.SOUTH, rememberPasswordjl);springLayout.putConstraint(springLayout.WEST, loginButton, 110, springLayout.WEST, c);c.add(registerLabel);//注册按钮springLayout.putConstraint(springLayout.NORTH, registerLabel, 5, springLayout.SOUTH, loginButton);springLayout.putConstraint(springLayout.WEST, registerLabel, 320, springLayout.WEST, c);c.add(promptRegister);//账号未注册提示promptRegister.setVisible(false);springLayout.putConstraint(springLayout.NORTH, promptRegister, 41, springLayout.SOUTH, logoLabel);springLayout.putConstraint(springLayout.WEST, promptRegister, 5, springLayout.EAST, userjt);c.add(promptUserNameEmpty);//请输入账号promptUserNameEmpty.setVisible(false);springLayout.putConstraint(springLayout.NORTH, promptUserNameEmpty, 41, springLayout.SOUTH, logoLabel);springLayout.putConstraint(springLayout.WEST, promptUserNameEmpty, 5, springLayout.EAST, userjt);c.add(promptPasswordFalse);//密码错误提示promptPasswordFalse.setVisible(false);springLayout.putConstraint(springLayout.NORTH, promptPasswordFalse, 20, springLayout.SOUTH, promptRegister);springLayout.putConstraint(springLayout.WEST, promptPasswordFalse, 5, springLayout.EAST, passwordjt);c.add(prompPasswordEmpty);//密码为空提示prompPasswordEmpty.setVisible(false);springLayout.putConstraint(springLayout.NORTH, prompPasswordEmpty, 20, springLayout.SOUTH, promptRegister);springLayout.putConstraint(springLayout.WEST, prompPasswordEmpty, 5, springLayout.EAST, passwordjt);//设置文本框鼠标点击事件userjt.addMouseListener(new MouseAdapter() {//文本框public void mouseClicked(MouseEvent e) {userjt.setText("");}});passwordjt.addMouseListener(new MouseAdapter() {//密码框public void mouseClicked(MouseEvent e) {passwordjt.setText("");}});//设置登陆按钮单击事件loginButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String userName = userjt.getText().trim();//获取用户输入的账号和密码String Password = new String(passwordjt.getPassword()).trim();//判断账号和密码if(userName.length() != 0) {//用户名不为空promptUserNameEmpty.setVisible(false);//关闭账号为空显示if(Password.length() != 0) {//密码不为空if(f.readData("user.xls", userName) && Password.equals(f.backData("user.xls", userName, "password"))) {//用户输入的账号和密码正确promptRegister.setVisible(false);//隐藏提示信息promptPasswordFalse.setVisible(false);prompPasswordEmpty.setVisible(false);loginButton.setText("                登 陆 中...               ");new Chessboard();//跳转到五子棋棋盘页面dispose();//销毁当前页面}else if( f.readData("user.xls", userName) && !Password.equals(f.backData("user.xls", userName, "password"))) {//用户输入密码错误promptPasswordFalse.setVisible(true);//显示密码错误提示promptRegister.setVisible(false);prompPasswordEmpty.setVisible(false);passwordjt.setText("");//密码框清空passwordjt.requestFocus();//光标定位到密码框}else {//账号还未注册promptRegister.setVisible(true);promptPasswordFalse.setVisible(false);prompPasswordEmpty.setVisible(false);}}else {//密码为空if(userName.equals("admin")) {//用户名已经注册, 提示输入密码prompPasswordEmpty.setVisible(true);promptUserNameEmpty.setVisible(false);promptRegister.setVisible(false);promptPasswordFalse.setVisible(false);}else {//用户名未注册prompPasswordEmpty.setVisible(false);promptUserNameEmpty.setVisible(false);promptRegister.setVisible(true);promptPasswordFalse.setVisible(false);}}}else {//用户名为空promptUserNameEmpty.setVisible(true);//提示输入账号promptRegister.setVisible(false);promptPasswordFalse.setVisible(false);prompPasswordEmpty.setVisible(false);passwordjt.setText("");//将密码框置为空if(Password.length() == 0) {//密码为空prompPasswordEmpty.setVisible(true);promptRegister.setVisible(false);promptPasswordFalse.setVisible(false);}}}});//注册标签监听器registerLabel.addMouseListener(new MouseListener() {public void mouseClicked(MouseEvent e) {dispose();new Register();}public void mouseEntered(MouseEvent e) {registerLabel.setForeground(Color.red);;}public void mouseExited(MouseEvent e) {registerLabel.setForeground(Color.black);}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}});}public static void main(String[] args) {// TODO 自动生成的方法存根new Main();}}

2.2 棋盘布局Chessboard.java

package com.fivechess;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
import java.util.Random;
/*** @author RFOS~五子棋😋* @date 2021年7月6日 下午1:45:36*/
public class Chessboard extends JFrame{/** 棋盘页面*//**************************     变量定义区域             ****************************************/private static final long serialVersionUID = 1L;final JLabel userNameLabel = new JLabel("账号:");final JLabel userName = new JLabel();final JLabel pointsLabel = new JLabel("积分:");final JLabel classLabel = new JLabel("等级:");final JLabel headImage = new JLabel();final JLabel winproLabel = new JLabel("胜率:");final JButton newRound = new JButton("新局");final JButton back = new JButton("悔棋");final JButton returnback = new JButton("返回");final JButton exit = new JButton("退出");final Color colorLightYellow = new Color(205, 133, 63);final Color colorHeavry = new Color(205, 133, 63);final DrawPanel drawPanel = new DrawPanel();final JPanel board = new JPanel();final JLabel logotext = new JLabel("RFOS~五子棋");final JLabel logoImage = new JLabel();final JLabel difficulityLabel = new JLabel("难度:");private JComboBox<String> difficulityClass = new JComboBox<>();final JLabel selectChessLabel = new JLabel("棋色选择:");final JRadioButton whiteChessjr = new JRadioButton();//白棋final JLabel whiteChessLabel = new JLabel();final JRadioButton blackChessjr = new JRadioButton();//黑棋final JLabel blackChessLabel = new JLabel();final ButtonGroup buttongroup = new ButtonGroup();final Random r = new Random();//用于产生随机数final FileOperation f = new FileOperation();//实例化文件操作对象Thread thread;//用线程调用电脑和电脑下棋SpringLayout springLayout = new SpringLayout();Image image = getToolkit().getImage(Chessboard.class.getResource("/image/background.jpg"));Image happy = getToolkit().getImage(Chessboard.class.getResource("/image/红黑点.png"));Image win = getToolkit().getImage(Chessboard.class.getResource("/image/win.png"));Image lose = getToolkit().getImage(Chessboard.class.getResource("/image/lose.png"));Image white = getToolkit().getImage(Chessboard.class.getResource("/image/whiteChess.png"));Image black = getToolkit().getImage(Chessboard.class.getResource("/image/blackChess.png"));Image star = getToolkit().getImage(Chessboard.class.getResource("/image/star.png"));Image moon= getToolkit().getImage(Chessboard.class.getResource("/image/moon.png"));Image sun = getToolkit().getImage(Chessboard.class.getResource("/image/sun.png"));int flag = 0;//行列标记数组下标int winFLAG = 0;//棋子连接数量达到5个的标志int playerColor = -1;//标记玩家棋子颜色int computerColor = -1;//标记电脑棋子颜色int CHESSCOLOR = 1;//棋子颜色。在计算棋子是否连成一行时用到int player = 1;//玩家下棋标志int computer = 0;//电脑下棋标志int chessboardEmpty = 0;//棋盘为空标记int dogfall = 0;//平局标记,若平局标记在最后变为0,则表示没有执行检测循环,表示玩家和电脑平局int newchessX = 0;//新棋子坐标int newchessY = 0;int x, y;long pointsNum = 0;//从文件中获取积分double gamewinNum = 0.0;//从文件中获取获胜局数double gameNum = 0.0;//获取总对局数目int winNum = 0;//胜率int starNum = 0;//定义需要的数目int moonNum = 0;int sunNum = 0;int classNum  = 0;//获取等级int comeX = 0; //用于在判断棋盘是否有同一方棋子连成五个int comeY = 0;int toX = 0; int toY = 0;int winX = 0;//标记赢的方向和坐标int winY = 0;int winWay = 0;int t = 0, s = 0;int count = 0;int depth = 0;//计算棋盘搜索的深度int judgeFlag = 0;//是否调用judge函数的标志int map[][] = new int[15][15]; {//0表示无子,1表示白子, 2表示黑子for(int i = 0; i < map.length; i++) {for(int j = 0; j < map[i].length; j++) {map[i][j] = 0;}}}int mapflag[][]= new int[15][15];{//位置标记数组,0表示无子,1表示有子for(int i = 0; i < mapflag.length; i++) {for(int j = 0; j < mapflag[i].length; j++) {map[i][j] = 0;}}}int promptBoxFlag[][] = new int[15][15]; {//提示框标记,鼠标划过棋盘显示提示方框for(int i = 0; i < promptBoxFlag.length; i++) {for(int j = 0; j < promptBoxFlag[i].length; j++) {promptBoxFlag[i][j] = 0;}}};int imapflag[] = new int[225];{//列标记数组for(int i = 0; i < imapflag.length; i++) {imapflag[i] = 0;}}int jmapflag[] = new int[225];{//行标记数组for(int j = 0; j < jmapflag.length; j++) {jmapflag[j] = 0;}}int position[] = new int[] {0, 0};//电脑下棋位置坐标public Chessboard() {super();GetClickPosition();//点击棋盘画棋子函数
//		simuPlayer();//模拟玩家Container c = getContentPane();c.setBackground(colorLightYellow);setVisible(true);setBounds(300, 40, 1000, 900);setTitle("RFOS~五子棋");setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);c.setLayout(springLayout);c.add(drawPanel);/**************************     组件设置区域             ****************************************/logotext.setFont(new Font("微软雅黑", 1, 34));logotext.setForeground(new Color(250, 128, 114));ImageIcon image = new ImageIcon(Chessboard.class.getResource("/image/image.jpg"));headImage.setIcon(image);Font font = new Font("微软雅黑", 1, 24);userNameLabel.setFont(font);userName.setFont(new Font("宋体", 1, 22));userName.setText(Main.userjt.getText());pointsLabel.setFont(font);classLabel.setFont(font);winproLabel.setFont(font);newRound.setFont(font);newRound.setBackground(colorHeavry);back.setFont(font);back.setBackground(colorHeavry);returnback.setFont(font);returnback.setBackground(colorHeavry);exit.setFont(font);exit.setBackground(colorHeavry);difficulityLabel.setFont(font);difficulityClass.setFont(new Font("微软雅黑", 1, 22));difficulityClass.setBackground(colorLightYellow);difficulityClass.addItem("初级");difficulityClass.addItem("中级");difficulityClass.addItem("高级");selectChessLabel.setFont(font);whiteChessjr.setFont(new Font("微软雅黑", 1, 22));whiteChessjr.setBackground(colorLightYellow);whiteChessjr.setSelected(true);blackChessjr.setFont(new Font("微软雅黑", 1, 22));blackChessjr.setBackground(colorLightYellow);buttongroup.add(whiteChessjr);buttongroup.add(blackChessjr);//从文件中获取用户信息pointsNum = Integer.parseInt(f.backData("user.xls", userName.getText(), "points"));classNum = Integer.parseInt(f.backData("user.xls", userName.getText(), "class"));gamewinNum = Double.parseDouble(f.backData("user.xls", userName.getText(), "winNum"));gameNum = Double.parseDouble(f.backData("user.xls", userName.getText(), "totalNum"));ImageIcon whiteChess = new ImageIcon(Chessboard.class.getResource("/image/whiteChess.png"));whiteChess.setImage(whiteChess.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));whiteChessLabel.setIcon(whiteChess);ImageIcon blackChess = new ImageIcon(Chessboard.class.getResource("/image/blackChess.png"));blackChess.setImage(blackChess.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));blackChessLabel.setIcon(blackChess);/**************************     页面布局区域             ****************************************///界面布局c.add(logoImage);//logoImage图片springLayout.putConstraint(springLayout.NORTH, logoImage, 40, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, logoImage, 10, springLayout.WEST, c);c.add(headImage);//头像springLayout.putConstraint(springLayout.NORTH, headImage, 200, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, headImage, 15, springLayout.WEST, c);c.add(userNameLabel);//账号springLayout.putConstraint(springLayout.NORTH, userNameLabel, 30, springLayout.SOUTH, headImage);springLayout.putConstraint(springLayout.WEST, userNameLabel, 15, springLayout.WEST, c);c.add(userName);springLayout.putConstraint(springLayout.NORTH, userName, 35, springLayout.SOUTH, headImage);springLayout.putConstraint(springLayout.WEST, userName, 6, springLayout.EAST, userNameLabel);c.add(pointsLabel);//积分springLayout.putConstraint(springLayout.NORTH, pointsLabel, 20, springLayout.SOUTH, userNameLabel);springLayout.putConstraint(springLayout.WEST, pointsLabel, 15, springLayout.WEST, c);c.add(classLabel);//等级springLayout.putConstraint(springLayout.NORTH, classLabel, 20, springLayout.SOUTH, pointsLabel);springLayout.putConstraint(springLayout.WEST, classLabel, 15, springLayout.WEST, c);c.add(winproLabel);//胜率springLayout.putConstraint(springLayout.NORTH, winproLabel, 20, springLayout.SOUTH, classLabel);springLayout.putConstraint(springLayout.WEST, winproLabel, 15, springLayout.WEST, c);c.add(difficulityLabel);//难度下拉框springLayout.putConstraint(springLayout.NORTH, difficulityLabel, 20, springLayout.SOUTH, winproLabel);springLayout.putConstraint(springLayout.WEST, difficulityLabel, 15, springLayout.WEST, c);c.add(difficulityClass);springLayout.putConstraint(springLayout.NORTH, difficulityClass, 640, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, difficulityClass, 6, springLayout.EAST, difficulityLabel);c.add(newRound);//新局springLayout.putConstraint(springLayout.NORTH, newRound, 780, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, newRound, 320, springLayout.WEST, c);c.add(back);//悔棋springLayout.putConstraint(springLayout.NORTH, back, 780, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, back, 80, springLayout.EAST, newRound);c.add(returnback);//返回springLayout.putConstraint(springLayout.NORTH, returnback, 780, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, returnback, 80, springLayout.EAST, back);c.add(exit);//退出springLayout.putConstraint(springLayout.NORTH, exit, 780, springLayout.NORTH, c);springLayout.putConstraint(springLayout.WEST, exit, 80, springLayout.EAST, returnback);c.add(selectChessLabel);//棋色选择springLayout.putConstraint(springLayout.NORTH, selectChessLabel, 108, springLayout.SOUTH, difficulityLabel);springLayout.putConstraint(springLayout.WEST, selectChessLabel, 15, springLayout.WEST, c);c.add(whiteChessjr);//白棋单选框springLayout.putConstraint(springLayout.NORTH, whiteChessjr, 111, springLayout.SOUTH, difficulityClass);springLayout.putConstraint(springLayout.WEST, whiteChessjr, 6, springLayout.EAST, selectChessLabel);c.add(whiteChessLabel);//白棋图片springLayout.putConstraint(springLayout.NORTH, whiteChessLabel, 96, springLayout.SOUTH, difficulityClass);springLayout.putConstraint(springLayout.WEST, whiteChessLabel, 6, springLayout.EAST, whiteChessjr);c.add(blackChessjr);//黑棋单选框springLayout.putConstraint(SpringLayout.NORTH, blackChessjr, 111, springLayout.SOUTH, difficulityClass);springLayout.putConstraint(springLayout.WEST, blackChessjr, 6, springLayout.EAST, whiteChessLabel);c.add(blackChessLabel);//黑棋图片springLayout.putConstraint(springLayout.NORTH, blackChessLabel, 96, springLayout.SOUTH, difficulityClass);springLayout.putConstraint(springLayout.WEST, blackChessLabel, 6, springLayout.EAST, blackChessjr);/**************************     鼠标监听器区域             ****************************************///为棋色选择添加事件监听器whiteChessLabel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(chessboardEmpty == 0) {//只有棋盘为空的时候才能选择棋子颜色whiteChessjr.setSelected(true);}}});blackChessLabel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(chessboardEmpty == 0) {//只有棋盘为空的时候才能选择棋子颜色blackChessjr.setSelected(true);}}});//为新局按钮添加事件监听器newRound.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {for(int i = 0; i < map.length; i++) {//将map数组置0for(int j = 0; j < map[i].length; j++) {map[i][j] = 0;mapflag[i][j] = 0;}}for(int j = 0; j < 225; j++) {//将悔棋标记数组置为0imapflag[j] = 0;jmapflag[j] = 0;}flag = 0;//行列标记数组下表置0winFLAG = 0;//输赢标记置0playerColor = -1;//玩家棋色标记置-1computerColor = -1;//电脑棋色标记为-1chessboardEmpty = 0;//棋盘标记为空player = 1;//玩家先下棋computer = 0;//电脑后下newchessX = 0;//新棋子标记置0newchessY = 0;depth = 0;repaint();}});//为悔棋添加事件监听器back.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(flag - 1 >= 0) {//棋盘上面有棋子的时候, 点击一次棋盘上面少两颗棋子,一颗是自己的,另一颗是电脑的for(int i = flag - 1; i > flag - 3; i--) {map[imapflag[i]][jmapflag[i]] = 0;mapflag[imapflag[i]][jmapflag[i]] = 0;imapflag[i] = 0;//将坐标存放在悔棋标记数组中jmapflag[i] = 0;}flag = flag - 2;//表示每次悔棋棋盘上双方均少一颗子winFLAG = 0;if(flag - 1 >= 0) {newchessX = imapflag[flag - 1];newchessY = jmapflag[flag - 1];}if(flag == 0) {//表示棋盘为空chessboardEmpty = 0;//棋盘为空playerColor = -1;//玩家和电脑棋子颜色置0computerColor = -1;depth = 0;}repaint();}else {;}}});//返回按钮添加事件监听器returnback.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {dispose();new Main();}});//退出按钮事件监听器exit.addActionListener(new ActionListener() {//点击退出按钮退出程序public void actionPerformed(ActionEvent e) {System.exit(0);}});//判断玩家和电脑的棋子颜色}/**************************     棋盘事件监听区域             ****************************************/public void GetClickPosition() {//鼠标点击事件addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {//判断玩家和电脑颜色if(whiteChessjr.isSelected()) {//玩家选择白棋子playerColor = 1;computerColor = 2;}else {playerColor = 2;computerColor = 1;}x = e.getX();y = e.getY();if(x >= 247 && x <= 951 && y >=77 && y <= 781) {//如果鼠标点击的点在棋盘内或者边上一点double m = (x - 270.0)/47.0;//判断点击位置最靠近的哪个交叉点double n = (y - 100.0)/47.0;int p = (x - 270)/47;int q = (y - 100)/47;int i, j;if(m - p >= 0.5 || m - p <= -0.5) {i = p + 1;}else {i = p;}if(n - q >= 0.5 || n - q <= -0.5) {j = q + 1;}else {j = q;}if(i >=0 && i <= 15 && j >= 0 && j <= 15) {//识别到的区域为棋盘之内if(mapflag[i][j] == 0 && winFLAG == 0 && player == 1) {//表示这个地方没有棋子,并且还没有赢map[i][j] = playerColor;imapflag[flag] = i;//将坐标存放在悔棋标记数组中jmapflag[flag] = j;flag ++;chessboardEmpty = 1;//棋盘标记为有棋子player = 0;//玩家下棋标志置0computer = 1;//电脑下棋标志newchessX = i;//新棋子标记记录新棋子坐标newchessY = j;judgeFlag = 1;repaint();}}int a = Math.max(Math.abs(i - 7), Math.abs(j - 7));//计算该点到中心的最大的距离depth = Math.max(depth, a);//不断更新depth的值}}}); //鼠标进入棋盘区域内,用于显示提示方框,表示点击之后可以在哪个区域内下棋addMouseMotionListener(new MouseMotionListener() {public void mouseMoved(MouseEvent e) {// TODO 自动生成的方法存根x = e.getX();y = e.getY();if(x >= 247 && x <= 951 && y >=77 && y <= 781) {//如果鼠标点击的点在棋盘内或者边上一点double m = (x - 270.0)/47.0;//判断点击位置最靠近的哪个交叉点double n = (y - 100.0)/47.0;int p = (x - 270)/47;int q = (y - 100)/47;int i, j;if(m - p >= 0.5 || m - p <= -0.5) {i = p + 1;}else {i = p;}if(n - q >= 0.5 || n - q <= -0.5) {j = q + 1;}else {j = q;}if(i >=0 && i <= 15 && j >= 0 && j <= 15) {//识别到的区域为棋盘之内if(mapflag[i][j] == 0 && winFLAG == 0 && player == 1) {//表示这个地方没有棋子,并且还没有赢promptBoxFlag[i][j] = 1;repaint();}}}		}public void mouseDragged(MouseEvent e) {}});}/**************************     电脑下棋函数            ****************************************/private void tuntoComputer() {//电脑下棋// TODO 自动生成的方法存根if(depth >= 7) {depth = 6;}position = Algorithm.evalute(map, depth, computerColor);//调用估值函数map[position[0]][position[1]] = computerColor;imapflag[flag] = position[0];//将坐标存放在悔棋标记数组中jmapflag[flag] = position[1];newchessX = position[0];//新棋子标记记录坐标newchessY = position[1];int a = Math.max(Math.abs(position[0] - 7), Math.abs(position[1] - 7));//计算该点到中心的最大的距离depth = Math.max(depth, a);//不断更新depth的值flag ++;chessboardEmpty = 1;//棋盘标记为有棋子player = 1;//玩家下棋标志置0computer = 0;//电脑下棋标志为1judgeFlag = 1;repaint();}/**************************     绘图类             ****************************************/class DrawPanel extends JPanel {/*** */private static final long serialVersionUID = 1L;//绘图函数public void paint(Graphics g) {setBounds(0, 0, 968, 760);super.paintComponents(g);Graphics2D g2 = (Graphics2D)g;g2.drawImage(image, 230, 33, 719, 720, this);//棋盘背景图片g2.setStroke(new BasicStroke(3.0f));//设置线条大小g2.setColor(Color.red);g2.fillOval(395, 197, 15, 15);//第一个 红点g2.fillOval(771, 197, 15, 15);//第二个红点g2.fillOval(580, 382, 20, 20);//第三个红点//中间红点g2.fillOval(395, 573, 15, 15);//第四个红点g2.fillOval(771, 573, 15, 15);//第五个红点Color color = new Color(210, 105, 30);g2.setColor(color);/**************************************绘制棋盘***************************************/for(int i = 0; i < 15; i++) {//横线g2.drawLine(261, i * 47 + 63, 919, i * 47 + 63);}for(int j = 0; j < 15; j++) {//竖线g2.drawLine(j * 47 + 261, 64, j * 47 + 261, 721);}//绘制棋子for(int i = 0; i < map.length; i++) {for(int j = 0; j < map[i].length; j++) {//白棋if(map[i][j] == 1) {g2.drawImage(white, i * 47 + 241, j * 47 + 43, 40, 40, this);mapflag[i][j] = 1;//标记位置表示这个地方已经有棋子}//黑棋if(map[i][j] == 2) {g2.drawImage(black, i * 47 + 241, j * 47 + 43, 40, 40, this);mapflag[i][j] = 1;}	}}//判断棋子是否连成五个if(judgeFlag == 1) {judge();judgeFlag = 0;}//绘制新棋子红点标记if(chessboardEmpty != 0) {g2.setColor(Color.red);g2.fillOval(newchessX * 47 + 254, newchessY * 47 + 55, 15, 15);}/***************************************绘制鼠标移动方框***********************************/for(int i = 0; i < 15; i++) {for(int j = 0; j < 15; j++) {if(promptBoxFlag[i][j] == 1) {g2.setColor(Color.RED);g2.setStroke(new BasicStroke(2.5f));//设置线条大小g2.drawLine(238 + i * 47, 40 + j * 47, 248 + i * 47, 40 + j * 47);//上左横线g2.drawLine(275 + i * 47, 40 + j * 47, 285 + i * 47, 40 + j * 47);//上右横线g2.drawLine(238 + i * 47, 40 + j * 47, 238 + i * 47, 50 + j * 47);//左上竖线g2.drawLine(285 + i * 47, 40 + j * 47, 285 + i * 47, 50 + j * 47);//右上竖线g2.drawLine(238 + i * 47, 77 + j * 47, 238 + i * 47, 87 + j * 47);//左下竖线g2.drawLine(285 + i * 47, 77 + j * 47, 285 + i * 47, 87 + j * 47);//右下竖线g2.drawLine(238 + i * 47, 87 + j * 47, 248 + i * 47, 87 + j * 47);//下左横线g2.drawLine(275 + i * 47, 87 + j * 47, 285 + i * 47, 87 + j * 47);//下右横线promptBoxFlag[i][j] = 0;}}}if(winFLAG == 0 && player == 0) {//表示还未分出胜负并且玩家下了,调用电脑下棋tuntoComputer();}/*********************************设置下拉框和单选按钮在棋盘不为空的是否不能进行选择*********************///设置棋子颜色单选框可用状态if(chessboardEmpty == 1) {//棋盘不为空difficulityClass.setEnabled(false);//设置下拉框不可用if(whiteChessjr.isSelected()) {//白棋子单选框被选中blackChessjr.setEnabled(false);whiteChessjr.setEnabled(true);}else {//黑棋子单选框被选中blackChessjr.setEnabled(true);whiteChessjr.setEnabled(false);}}else {//棋盘为空blackChessjr.setEnabled(true);//释放两个单选框whiteChessjr.setEnabled(true);difficulityClass.setEnabled(true);//释放下拉框}/**************************************重绘积分,等级,胜率**********************/classNum = (int)((int)(pointsNum /100 * 0.4 + gamewinNum * 0.4 + gameNum * 0.2) * 0.8);sunNum = (int) (classNum / 100);moonNum = (int)(classNum - sunNum * 100) / 50;starNum = (int)(classNum - sunNum * 100 - moonNum * 50) / 10;for(t = 0; t < sunNum; t++) {//绘画太阳g2.drawImage(sun, 75 + t * 30, 538, 30, 30, null);}for(t = sunNum ; t < moonNum + sunNum; t++) {//绘画月亮g2.drawImage(moon, 75 + t * 30, 540, 25, 25, null);}if(moonNum > 0 || sunNum > 0) {//绘画星星for(t = moonNum + sunNum ; t < starNum + moonNum + sunNum; t ++) {g2.drawImage(star, 75 + t * 30, 540, 25, 25, null);}}else {for(t = moonNum ; t < starNum + 1; t ++) {g2.drawImage(star, 75 + t * 30, 538, 30, 30, null);}}/*****************************************胜负之后操作************************************************/if(winFLAG != 0) {//判断有一方赢了之后/************************************打印获胜或者失败标志并且计算分数***********************************/if(winFLAG == playerColor) {//表示玩家获胜g2.drawImage(win, 260, 18, 153, 42, null);if(difficulityClass.getSelectedItem().equals("初级")) {//用户选择初级难度按钮pointsNum = pointsNum + 100;}else if(difficulityClass.getSelectedItem().equals("中级")){//用户选择中级难度pointsNum = pointsNum + 200;}else {//用户选择高级难度pointsNum = pointsNum + 300;}gamewinNum = gamewinNum + 1.0;gameNum = gameNum + 1.0;}else {g2.drawImage(lose, 260, 18, 153, 42, null);gameNum = gameNum + 1.0;}/**************************************将相关信息写入文件中*********************/f.writeData("user.xls", userName.getText(), "points", String.valueOf(pointsNum));f.writeData("user.xls", userName.getText(), "class", String.valueOf(classNum));f.writeData("user.xls", userName.getText(), "winNum", Integer.toString((int)gamewinNum));f.writeData("user.xls", userName.getText(), "totalNum", Integer.toString((int)gameNum));/***************************************绘制笑脸标记**********************************/int m = 0, n = 0;if(winWay == 1) {//表示横向胜利for(m = winX; m < winX + 5; m ++) {//绘制笑脸标记g2.drawImage(happy, m * 47 + 241, winY * 47 + 42, 40, 40, this);}}else if(winWay == 2) {//表示纵向胜利for(m = winY; m < winY + 5; m ++) {//绘制笑脸标记g2.drawImage(happy, winX * 47 + 241, m * 47 + 42, 40, 40, this);}}else if(winWay == 3) {//表示左上到右下胜利for(m = winX, n = winY; m < winX + 5; m ++, n++) {//绘制笑脸标记g2.drawImage(happy, m * 47 + 241, n * 47 + 42, 40, 40, this);}}else {//表示右上到左下胜利for(m = winX, n = winY; n < winY + 5; m -- , n ++) {//绘制笑脸标记g2.drawImage(happy, m * 47 + 241, n * 47 + 42, 40, 40, this);}}}/***********************************绘制积分和胜率********************************/g2.setFont(new Font("微软雅黑", 1, 22 ));g2.setColor(Color.black);g2.drawString(String.valueOf((int)pointsNum), 75, 509);//分数winNum = (int) Math.rint(gamewinNum / gameNum * 100);//计算胜率if(gameNum == 0) {//对局数为0g2.drawString("0%", 75, 617);}else {g2.drawString(String.valueOf(winNum) + "%", 75, 616);}}}/********************************判断棋子是否连成五个**********************************/public void judge() {for(t = newchessX,s = newchessY,count = 0; t >=0 && s >= 0 && count <= 4; t--,s--,count++) {comeX = t;comeY = s;}for(t = newchessX, s = newchessY, count = 0; t <=14 && s >= 0 && count <= 4; t++, s--, count++) {toX = t;toY = s;}if(winFLAG == 0) {for(int ch = 1; ch <=2; ch++) {CHESSCOLOR = ch;//判断横向棋子for(s = (newchessX - 4) >=0 ? (newchessX - 4) : 0 ; s <= newchessX; s++) {//表示玩家获胜t = newchessY;if(map[s][t] == CHESSCOLOR && s < 11) {//行棋子数量计算if(map[s + 1][t] == CHESSCOLOR) {if(map[s + 2][t] == CHESSCOLOR) {if(map[s + 3][t] == CHESSCOLOR) {if(map[s + 4][t] == CHESSCOLOR) {winX = s;winY = t;winWay = 1;if(CHESSCOLOR == 1) {//白棋winFLAG = 1;}else {//黑棋winFLAG = 2;}break;}}}}}}if(winFLAG != 0) {//如果某一方赢了就直接退出break;}//判断列项棋子for(t = (newchessY - 4) >=0 ? (newchessY - 4) : 0 ; t <= newchessY; t ++) {s = newchessX;if(map[s][t] == CHESSCOLOR && t < 11) {//列棋子数量计算if(map[s][t + 1] == CHESSCOLOR) {if(map[s][t + 2] == CHESSCOLOR) {if(map[s][t + 3] == CHESSCOLOR) {if(map[s][t + 4] == CHESSCOLOR) {winX = s;winY = t;winWay = 2;if(CHESSCOLOR == 1) {//白棋winFLAG = 1;}else {//黑棋winFLAG = 2;}break;}}}}}}if(winFLAG != 0) {//如果某一方赢了就直接退出break;}//判断左上到右下棋子for(s = comeX, t = comeY; s <= newchessX && t <= newchessY; s ++, t++) {if(map[s][t] == CHESSCOLOR && s < 11 && t < 11) {//斜下棋子数量计算if(map[s + 1][t + 1] == CHESSCOLOR) {if(map[s + 2][t + 2] == CHESSCOLOR) {if(map[s + 3][t + 3] == CHESSCOLOR) {if(map[s + 4][t + 4] == CHESSCOLOR) {winX = s;winY = t;winWay = 3;if(CHESSCOLOR == 1) {//白棋winFLAG = 1;}else {//黑棋winFLAG = 2;}break;}}}}}}if(winFLAG != 0) {//如果某一方赢了就直接退出break;}//判断右上到左下棋子for(s = toX, t = toY; s >= newchessX && t <= newchessY; s --, t++) {if(map[s][t] == CHESSCOLOR && s >= 4 && t < 11) {//斜上棋子数量计算if(map[s - 1][t + 1] == CHESSCOLOR) {if(map[s - 2][t + 2] == CHESSCOLOR) {if(map[s - 3][t + 3] == CHESSCOLOR) {if(map[s - 4][t + 4] == CHESSCOLOR) {winX = s;winY = t;winWay = 4;if(CHESSCOLOR == 1) {//白棋winFLAG = 1;}else {//黑棋winFLAG = 2;}break;}}}}}}if(winFLAG != 0) {//如果某一方赢了就直接退出break;}}}}
//	/**************************     主函数             ****************************************/
//	public static void main(String[] args) {
//		new Chessboard();
//	}
}

3.Algorithm算法

package com.fivechess;import java.util.Random;
/*** @author RFOS~五子棋😋* @date 2021年7月6日 下午1:45:36*/
public class Algorithm {//返回棋盘上某个空点的分数public static int countScore(int map[][], int X, int Y, int computerColor) {int sum = 0;int count = 0;int value[] = new int[] {0, 0, 0, 0};int upcount[] = new int[] {0, 0, 0, 0};int downcount[] = new int[] {0, 0, 0, 0};int upflag[] = new int[] {0, 0, 0, 0};int downflag[] = new int[] {0, 0, 0, 0};for(int color = 1; color <= 2; color++) {//计算双方的分数map[X][Y] = color;//先将该点放白子/*******************************************计算横向棋子***********************/for(int i = X - 1; i >=0; i--) {//计算左边棋子数量if(map[i][Y] == color) {upcount[0]++;}else if(map[i][Y] != 0 && map[i][Y] != color) {//表示有对方棋子upflag[0] = -1;break;}else {//表示为空upflag[0] = 1;if(i - 1 >= 0 && map[i][Y] == 0) {upflag[0] = 2;//表示两个空格}else {break;}if(i - 2 >= 0 && map[i][Y] == 0) {upflag[0] = 3;//表示有三个空格}else {break;}break;}}for(int j = X + 1; j <= 14; j++) {//计算右边棋子数量if(map[j][Y] == color) {downcount[0]++;}else if(map[j][Y] != 0 && map[j][Y] != color) {downflag[0] = -1;break;}else {//表示为空downflag[0] = 1;if(j + 1 <= 14 && map[j][Y] == 0) {downflag[0] = 2;}else {break;}if(j + 2 <= 14 && map[j][Y] == 0) {downflag[0] = 3;}else {break;}break;}}/******************************************************计算列项棋子***************************************/for(int i = Y - 1; i >= 0; i--) {//计算方向向上if(map[X][i] == color) {upcount[1]++;}else if(map[X][i] != 0 && map[X][i] != color) {//表示该点是对方棋子upflag[1] = -1;break;}else {//表示为空upflag[1] = 1;if(i - 1 >= 0 && map[X][i] == 0) {upflag[1] = 2;}else {break;}if(i - 2 >= 0 && map[X][i] == 0) {upflag[1] = 3;}else {break;}break;}}for(int j = Y + 1; j <= 14; j++) {//计算方向向下if(map[X][j] == color) {downcount[1]++;}else if(map[X][j] != 0 && map[X][j] != color) {//表示该点是对方棋子downflag[1] = -1;break;}else {//表示为空downflag[1] = 1;if(j + 1 >= 0 && map[X][j] == 0) {downflag[1] = 2;}else {break;}if(j + 2 >= 0 && map[X][j] == 0) {downflag[1] = 3;}else {break;}break;}}/****************************************************计算斜向下棋子*********************************************/int i = 0;int j = 0;for(i = X - 1, j = Y - 1; i >= 0 && j >= 0; i--, j--) {//计算斜向上if(map[i][j] == color) {upcount[2]++;}else if(map[i][j] != 0 && map[i][j] != color) {upflag[2] = -1;break;}else {//为空upflag[2] = 1;if(i - 1 >= 0 && j - 1 >= 0 && map[i][j] == 0) {upflag[2] = 2;}else {break;}if(i - 2 >= 0 && j - 2 >= 0 && map[i][j] == 0) {upflag[2] = 3;}else {break;}break;}}for(i = X + 1, j = Y + 1; i <= 14 && j <= 14; i++, j++) {//计算斜向下if(map[i][j] == color) {downcount[2]++;}else if(map[i][j] != 0 && map[i][j] != color) {downflag[2] = -1;break;}else {//为空downflag[2] = 1;if(i + 1 <= 14 && j + 1 <= 14 && map[i][j] == 0) {downflag[2] = 2;}else {break;}if(i + 2 <= 14 && j + 2 <= 14 && map[i][j] == 0) {downflag[2] = 3;}else {break;}break;}}/****************************************************计算斜向上棋子*************************************************/for(i = X + 1, j = Y - 1; i <= 14 && j >= 0; i++, j--) {if(map[i][j] == color) {upcount[3]++;}else if(map[i][j] != 0 && map[i][j] != color) {upflag[3] = -1;break;}else {upflag[3] = 1;if(i + 1 <= 14 && j - 1 >= 0 && map[i][j] == 0) {upflag[3] = 2;}else {break;}if(i + 2 <= 14 && j - 2 >= 0 && map[i][j] == 0) {upflag[3] = 3;}else {break;}break;}}for(i = X - 1, j = Y + 1; i >= 0 && j <= 14; i--, j++) {//计算斜向下if(map[i][j] == color) {downcount[3]++;}else if(map[i][j] != 0 && map[i][j] != color) {downflag[3] = -1;break;}else {//为空downflag[3] = 1;if(i - 1 >= 0 && j + 1 <= 14 && map[i][j] == 0) {downflag[3] = 2;}else {break;}if(i - 2 >= 0 && j + 2 <= 14 && map[i][j] == 0) {downflag[3] = 3;}else {break;}break;}}//数据处理if(map[X][Y] == computerColor) {//如果是电脑方的话分数要高一点for(i =0; i < 4; i++) {count = upcount[i] + downcount[i] + 1;if(count == 5) {//成五value[i] = 40000;}else if(count == 4) {if(upflag[i] >= 1 && downflag[i] >= 1) {//活四value[i] = 19000;}if((upflag[i] >= 1 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 1)) {//眠四value[i] = 3000;}if(upflag[i] == -1 && downflag[i] == -1) {//死四value[i] = -50;}}else if(count == 3) {if((upflag[i] >= 2 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 2)) {//活三value[i] = 4000;}if((upflag[i] >= 2 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 2) ||(upflag[i] == 1 && downflag[i] == 1)){//眠三value[i] = 800;}if(upflag[i] == -1 && downflag[i] == -1) {//死三value[i] = -50;}}else if(count == 2) {if((upflag[i] >= 1 && downflag[i] >= 3) || (upflag[i] >=2 && downflag[i] >= 2) || (upflag[i] >= 3 && downflag[i] >= 1)) {//活二value[i] = 1050;}if((upflag[i] == -1 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] == -1) ||(upflag[i] == 2 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {//眠二value[i] = 350;}if(upflag[i] == -1 && downflag[i] == -1) {//死二value[i] = -50;}}else {if((upflag[i] >= 2 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] >= 2)) {//活1value[i] = 80;}if((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) ||(upflag[i] == 3 && downflag[i] == 1)) {//眠1value[i] = 20;}if((upflag[i] <= 1 && downflag[i] <= 2) || (upflag[i] <= 2 && downflag[i] <= 1)) {value[i] = -50;}}}}else {for(i =0; i < 4; i++) {count = upcount[i] + downcount[i] + 1;if(count == 5) {//成五value[i] = 30000;}else if(count == 4) {if(upflag[i] >= 1 && downflag[i] >= 1) {//活四value[i] = 15000;}if((upflag[i] >= 1 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 1)) {//眠四value[i] = 2500;}if(upflag[i] == -1 && downflag[i] == -1) {//死四value[i] = -50;}}else if(count == 3) {if((upflag[i] >= 2 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 2)) {//活三value[i] = 3000;}if((upflag[i] >= 2 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 2) ||(upflag[i] == 1 && downflag[i] == 1)){//眠三value[i] = 500;}if(upflag[i] == -1 && downflag[i] == -1) {//死三value[i] = -50;}}else if(count == 2) {if((upflag[i] >= 1 && downflag[i] >= 3) || (upflag[i] >=2 && downflag[i] >= 2) || (upflag[i] >= 3 && downflag[i] >= 1)) {//活二value[i] = 650;}if((upflag[i] == -1 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] == -1) ||(upflag[i] == 2 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {//眠二value[i] = 150;}if((upflag[i] == -1 && downflag[i] == -1) || (upflag[i] == 1 && downflag[i] == 1) ||(upflag[i] == -1 && downflag[i] == 2) || (upflag[i] == 2 && downflag[i] == -1)) {//死二value[i] = -50;}}else {if((upflag[i] >= 2 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] >= 2)) {//活1value[i] = 50;}if((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) ||(upflag[i] == 3 && downflag[i] == 1)) {//眠1value[i] = 10;}if((upflag[i] <= 1 && downflag[i] <= 2) || (upflag[i] <= 2 && downflag[i] <= 1)||(upflag[i] <= 3 && downflag[i] == -1)|| (upflag[i] == -1 && downflag[i] <= 3)) {value[i] = -50;}}}}for(i = 0; i < 4; i++) {sum += value[i];value[i] = 0;upcount[i] = 0;downcount[i] = 0;upflag[i] = 0;downflag[i] = 0;}	}map[X][Y] = 0;return sum;}//估值算法,返回一个数组,用于记录坐标public static int[] evalute(int map[][], int depth, int computerColor) {int maxscore = 0;Random r = new Random();int pos[][] = new int[10][2];{for(int i = 0; i < pos.length; i++) {for(int j = 0; j < pos[i].length; j++) {pos[i][j] = 0;}}}int FLAG = 0;int score[][] = new int[15][15];{//初始化计分数组for(int i = 0; i < 15; i++) {for(int j = 0; j < 15; j++) {score[i][j] = 0;}}}int position[] = new int[]{0, 0};//初始化位置坐标数组for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {//搜索横坐标for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {//搜索纵坐标if(map[i][j] == 0) {//表示该点在棋盘上面为空score[i][j] = countScore(map, i, j, computerColor);if(maxscore < score[i][j]) {maxscore = score[i][j];//记录当前棋盘分数的最大值}}}}for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {if(score[i][j] == maxscore) {pos[FLAG][0] = i;pos[FLAG++][1] = j;}}}int m = r.nextInt(FLAG);position[0] = pos[m][0];position[1] = pos[m][1];return position;}//极大极小值算法public int minimax(int map[][], int chessColor) {return chessColor;}//alpha beta剪枝public void alphaBetaCutting(int map[][], int chessColor){}
}

点击下载链接:Java实现的五子棋游戏源码下载

https://m.tb.cn/h.UBrlWw1?tk=0dMbdJSevg9%20CZ0001


http://www.ppmy.cn/news/383102.html

相关文章

Rust 基础语法

Rust 基础语法 变量&#xff0c;基本类型&#xff0c;函数&#xff0c;注释和控制流&#xff0c;这些几乎是每种编程语言都具有的编程概念。 这些基础概念将存在于每个 Rust 程序中&#xff0c;及早学习它们将使你以最快的速度学习 Rust 的使用。 变量 首先必须说明&#x…

YUV420笔记

YUV420 有YU12、YV12、NV12、NV21 YU12存储格式是 YU12存储格式是YU13中的UV顺序反过来 NV12存储格式是 NV21是NV12数据取反 YUV420_888 是YCbCr的泛化格式&#xff0c;不会具体指明是YU12&#xff0c;YV12&#xff0c;NV12&#xff0c;或是是NV21。它能够表示任何4:2:0的平…

(五) ElasticSearch 数据类型和文档CRUD操作

1.ES数据类型 官方文档地址&#xff1a;https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html#_complex_datatypes 核心数据类型&#xff08;Core Data Types&#xff09;&#xff1a; 核心数据类型是 Elasticsearch 最基本和常用的数据类型…

PMP证书能直接升级项目管理专业人员能力评价(CSPM)三级吗?

2021年10月&#xff0c;中共中央、国务院发布的《国家标准化发展纲要》明确提出构建多层次从业人员培养培训体系&#xff0c;开展专业人才培养培训和国家质量基础设施综合教育。建立健全人才的职业能力评价和激励机制。由中国标准化协会&#xff08;CAS&#xff09;组织开展的项…

关于 QQ泫舞刷点卷软件

力求。。。 各位 哥哥 姐姐 们 你们谁会搞 这 高端外挂吗刷 点卷 给个我 好吗 嘿嘿 刷J别的也可以也忽忽

php按选定日期查询,php mysql查询指定范围内日期

摘要 腾兴网为您分享:php mysql查询指定范围内日期&#xff0c;掌上新华&#xff0c;掌上公交&#xff0c;学习帮&#xff0c;喜马拉雅等软件知识&#xff0c;以及mts&#xff0c;97漫画网&#xff0c;三菱plc编程软件win10&#xff0c;零点八&#xff0c;2016考研国家线&#…

按键精灵--炫舞游戏源码

按键精灵–炫舞游戏源码 ImgID findImg(0, 0, 1366, 768, "按键精灵.bmp") 起点x Imgx : 起点y Imgy //850,676 四方向图片 "上.bmp|下.bmp|左.bmp|右.bmp" x1 起点x 494 - 850 : y1 起点y 555 - 676 x2 起点x 594 - 850 : y2 起点y 628 - 67…

3D游戏设计读书笔记一

3D游戏设计读书笔记一 二、游戏分类与热点探索 1.使用思维导图描述游戏的分类。&#xff08;游戏分类方法特别多&#xff09; 2. 结合手机游戏市场的下载量与排名等数据&#xff0c;结合游戏分类图&#xff0c;描述游戏市场的热点。 &#xff08;1&#xff09;2016年手机游戏…