java项目五子棋含人机对战AI算法

五子棋人机对战

  • UI初始化与主方法
  • 棋盘控件重绘方法重写
  • 常量与全局数据结构接口
  • 事件监听类
  • 判定胜负
  • 人机对弈方法与数据结构

UI初始化与主方法

private void initUI()
    {
        /** 窗体初始化 */
        this.setTitle("Gobang");
        this.setSize(760, 630);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);   //设置窗体的关闭动作
        this.setLocationRelativeTo(null);       //让窗体位于屏幕中央
        this.setResizable(false);               //让窗体不可改变大小
        this.setLayout(new BorderLayout());     //设置窗体的边界布局

        /** 左侧棋盘初始化 */
        chessboard = new ChessBoard();
        chessboard.setBackground(Color.GRAY);
        this.add(chessboard, BorderLayout.CENTER);      //将棋盘面板添加在边界布局的中央

        /** 右侧菜单初始化 */
        JPanel menu = new JPanel();
        menu.setLayout(new FlowLayout(FlowLayout.LEFT));    //面板设置流式布局
        menu.setPreferredSize(new Dimension(150, 0));   //面板设置大小

        /** 菜单上的控件声明 */
        JButton start = new JButton(START);
        JButton retract_the_false = new JButton(RETRACT);
        JButton give_up = new JButton(GIVEUP);

        /** 菜单上的按钮初始化 */
        start.setPreferredSize(new Dimension(140, 60));
        retract_the_false.setPreferredSize(new Dimension(140, 60));
        give_up.setPreferredSize(new Dimension(140, 60));
        menu.add(start);
        menu.add(retract_the_false);
        menu.add(give_up);

        /** 菜单上的模式选项初始化 */
        JRadioButton pvp = new JRadioButton(PVP);
        JRadioButton pve = new JRadioButton(PVE);
        pvp.setPreferredSize(new Dimension(140, 60));
        pve.setPreferredSize(new Dimension(140, 60));
        pvp.setSelected(true);

        ButtonGroup bg = new ButtonGroup();
        bg.add(pvp);
        bg.add(pve);

        menu.add(pvp);
        menu.add(pve);

        JCheckBox AI_offensive = new JCheckBox(AI);
        menu.add(AI_offensive);

        this.add(menu, BorderLayout.EAST);      //将菜单面板添加在边界布局的右边

        this.setVisible(true);

        GobangListener gl = new GobangListener(this,chessboard,start,retract_the_false,give_up,pvp,pve,AI_offensive);    //事件处理

        start.addActionListener(gl);
        retract_the_false.addActionListener(gl);
        give_up.addActionListener(gl);
        pve.addActionListener(gl);
        pvp.addActionListener(gl);
        AI_offensive.addActionListener(gl);
    }

棋盘控件重绘方法重写

import javax.swing.*;
import java.awt.*;

public class ChessBoard extends JPanel implements GobangConfig
{
    /**
     * 重写绘制棋盘的方法
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        for(int i = 0; i < ROW; i++)
            g.drawLine(X,Y+SIZE*i,X+SIZE*(COLUMN-1),Y+SIZE*i);
        for(int i = 0; i < COLUMN; i++)
            g.drawLine(X+SIZE*i,Y,X+SIZE*i,Y+SIZE*(ROW-1));
        for(int r = 0; r < ROW; r++)
        {
            for(int c = 0; c < COLUMN; c++)
            {
                if(chessArray[r][c] != 0)
                {
                    int x = c*SIZE + X - CHESS_SIZE/2;
                    int y = r*SIZE + Y - CHESS_SIZE/2;

                    if(chessArray[r][c] == 1)
                        g.setColor(Color.BLACK);
                    else
                        g.setColor(Color.WHITE);

                    g.fillOval(x,y,CHESS_SIZE,CHESS_SIZE);
                }
            }
        }
    }
}

常量与全局数据结构接口

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

public interface GobangConfig
{
    /** 棋盘左上角距窗体左上角的距离 */
    public static final int X = 50;
    public static final int Y = 50;

    /** 棋盘的行数和列数 */
    public static final int ROW = 15;
    public static final int COLUMN = 15;

    /** 棋盘的单元格大小 */
    public static final int SIZE = 35;

    /** 棋子大小 */
    public static final int CHESS_SIZE = 35;

    /** 游戏菜单按钮文字 */
    public static final String START = "开始新游戏";
    public static final String RETRACT = "悔棋";
    public static final String GIVEUP = "认输";

    /** 对战模式 */
    public static final String PVE = "人机对战";
    public static final String PVP = "人人对战";

    /** AI先手 */
    public static final String AI = "AI先手";

    /** 黑棋胜利 */
    public static final String BLACK_WIN = "黑棋胜利!";

    /** 白棋认输,黑棋胜利 */
    public static final String GIVEUP_BLACK_WIN = "白棋认输,黑棋胜利!";

    /** 白棋胜利 */
    public static final String WHITE_WIN = "白棋胜利!";

    /** 黑棋认输,白棋胜利 */
    public static final String GIVEUP_WHITE_WIN = "黑棋认输,白棋胜利!";

    /** 记录棋盘上是否有棋子 */
    public static final int[][] chessArray = new int[ROW][COLUMN];


    public static final char[][] AIchessArray = new char[ROW][COLUMN];

    /** 计算棋盘上的权值 */
    public static final int[][] valuematrix = new int[ROW][COLUMN];

    /** 记录棋子的下棋顺序 */
    public static final ArrayList orderRecoder = new ArrayList();

    /** 定义存储棋子相连情况和对应权值的Map集合对象。 */
    public static final HashMap situationmap = new HashMap();
}

事件监听类

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import static gobang.WeightedMethod.calculateValue;

public class GobangListener extends MouseAdapter implements GobangConfig, ActionListener
{
    /** 窗体控件 */
    private JFrame mainframe;

    /** 棋盘控件 */
    private ChessBoard chessboard;

    /** 棋盘面板画笔*/
    private Graphics g;

    /** 菜单栏的开始、悔棋、认输按钮*/
    private JButton start,retract_the_false,give_up;

    /** 游戏模式选项 */
    private JRadioButton pve, pvp;

    /** 先手选项 */
    private JCheckBox AI_offensive;

    /** 游戏模式标识 true 代表人人对战;false 代表人机对战*/
    private boolean mode = true;

    /** 先手标识 true 代表 AI先手;false 代表 人先手*/
    public static boolean offensive = false;

    /** 黑白棋标志位 true 表示黑棋; false 表示白棋 */
    private boolean flag_chess = true;

    /** 比赛结果 */
    private String result_message;


    /**
     * 构造函数,初始化窗口控件
     * @param chessboard
     * @param start
     * @param retract_the_false
     * @param give_up
     * @param pvp
     * @param pve
     * @param AI_offensive
     */
    public GobangListener(JFrame frame,ChessBoard chessboard,JButton start, JButton retract_the_false,JButton give_up,JRadioButton pvp, JRadioButton pve,JCheckBox AI_offensive)
    {
        this.mainframe = frame;
        this.chessboard = chessboard;
        this.start = start;
        this.retract_the_false = retract_the_false;
        this.give_up = give_up;
        this.pvp = pvp;
        this.pve = pve;
        this.AI_offensive = AI_offensive;
        g = chessboard.getGraphics();
    }

    /**
     * 处理鼠标在棋盘面板的操作
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e)
    {
        super.mouseClicked(e);
        int x = e.getX();
        int y = e.getY();

        if(mode == true)
        {
            /** 点击棋盘内部 */
            if(x > X && x <= X + SIZE*(COLUMN-1) && y >= Y && y <= Y + SIZE*(ROW-1))
            {
                /** 计算最近的交叉点的行数和列数 */
                int r = (y - Y + SIZE/2)/SIZE;
                int c = (x - X + SIZE/2)/SIZE;

                /** 判断该位置是否已经有棋子 */
                if(chessArray[r][c] == 0)
                {
                    /** 计算交叉点坐标值 */
                    x = c*SIZE + X - CHESS_SIZE/2;
                    y = r*SIZE + Y - CHESS_SIZE/2;

                    /** 存储黑棋 */
                    if(flag_chess)
                    {
                        g.setColor(Color.BLACK);
                        chessArray[r][c] = 1;
                    }
                    /** 存储白棋 */
                    else
                    {
                        g.setColor(Color.WHITE);
                        chessArray[r][c] = 2;
                    }
                    /** 画棋子 */
                    g.fillOval(x,y,CHESS_SIZE,CHESS_SIZE);

                    /** 记录棋子顺序 */
                    orderRecoder.add(new Point(r,c));

                    /** 判断胜负 */
                    if(GobangWin.judge(r,c))
                    {
                        result_message = flag_chess ? BLACK_WIN : WHITE_WIN;
                        gameOver();
                        return;
                    }
                    /** 交换选手 */
                    flag_chess = !flag_chess;
                }
            }
        }
        else
        {
            //JOptionPane.showMessageDialog(mainframe,"人机对战尚未实现");
            if(offensive == true)
            {
                /** 点击棋盘内部 */
                if(x > X && x <= X + SIZE*(COLUMN-1) && y >= Y && y <= Y + SIZE*(ROW-1))
                {
                    /** 计算最近的交叉点的行数和列数 */
                    int r = (y - Y + SIZE/2)/SIZE;
                    int c = (x - X + SIZE/2)/SIZE;

                    /** 判断该位置是否已经有棋子 */
                    if(chessArray[r][c] == 0)
                    {
                        /** 计算交叉点坐标值 */
                        x = c*SIZE + X - CHESS_SIZE/2;
                        y = r*SIZE + Y - CHESS_SIZE/2;

                        /** 存储白棋 */
                        chessArray[r][c] = 2;
                        System.out.println("r:"+r+" c:"+c +" "+ chessArray[r][c]);
                        g.setColor(Color.WHITE);


                        /** 画棋子 */
                        g.fillOval(x,y,CHESS_SIZE,CHESS_SIZE);

                        /** 记录棋子顺序 */
                        orderRecoder.add(new Point(r,c));

                        /** 判断胜负 */
                        if(GobangWin.judge(r,c))
                        {
                            result_message = flag_chess ? BLACK_WIN : WHITE_WIN;
                            gameOver();
                            return;
                        }
                        calculateValue();
                        for(int i = 0; i < ROW; i++)
                        {
                            for(int j = 0; j < ROW; j++)
                            {
                                System.out.print(chessArray[i][j] + " ");
                            }
                            System.out.println();
                        }
                        System.out.println();
                        int maxI = 0, maxJ = 0;
                        for(int i = 0; i < ROW; i++)
                        {
                            for(int j = 0; j < COLUMN; j++)
                            {
                                if((valuematrix[i][j] > valuematrix[maxI][maxJ]) && (chessArray[i][j] == 0))
                                {
                                    maxI = i;
                                    maxJ = j;
                                }
                            }
                        }
                        System.out.println("r:"+ maxI + "c:" + maxJ);
                        /** 计算交叉点坐标值 */
                        x = maxJ*SIZE + X - CHESS_SIZE/2;
                        y = maxI*SIZE + Y - CHESS_SIZE/2;

                        /** 存储黑棋 */
                        g.setColor(Color.BLACK);
                        chessArray[maxI][maxJ] = 1;

                        /** 画棋子 */
                        g.fillOval(x,y,CHESS_SIZE,CHESS_SIZE);

                        /** 记录棋子顺序 */
                        orderRecoder.add(new Point(maxI,maxJ));

                        /** 判断胜负 */
                        if(GobangWin.judge(maxI,maxJ))
                        {
                            result_message = flag_chess ? BLACK_WIN : WHITE_WIN;
                            gameOver();
                            return;
                        }
                    }
                }
            }
        }
    }


    /**
     *
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e)
    {
        /** 获取按钮上的文字信息 */
        String actioncommand = e.getActionCommand();
        //System.out.println(actioncommand);

        /** 开始新游戏 */
        if(actioncommand.equals(START))
        {
            //System.out.println("开始新游戏");
            /** 设置菜单状态 */
            start.setEnabled(false);
            retract_the_false.setEnabled(true);
            give_up.setEnabled(true);

            pvp.setEnabled(false);
            pve.setEnabled(false);
            AI_offensive.setEnabled(false);

            /** 给棋盘面板添加鼠标监听方法,指定事件处理对象为当前类的对象。 */
            chessboard.addMouseListener(this);

            /** 清空存储棋子的数组 */
            for(int r = 0; r < ROW;r++)
            {
                for(int c = 0; c < COLUMN; c++)
                {
                    chessArray[r][c] = 0;
                }
            }

            /** 界面重绘 */
            chessboard.repaint();

            /** 黑白棋的标志位回到初始状态 */
            flag_chess = true;

            if(mode == false && offensive == true)
            {
                int x, y;
                /** 计算交叉点坐标值 */
                x = 7*SIZE + X - CHESS_SIZE/2;
                y = 7*SIZE + Y - CHESS_SIZE/2;

                g.setColor(Color.BLACK);
                chessArray[7][7] = 1;

                /** 画棋子 */
                g.fillOval(x,y,CHESS_SIZE,CHESS_SIZE);

                /** 记录棋子顺序 */
                orderRecoder.add(new Point(7,7));

                //flag_chess = !flag_chess;
            }
        }

        /** 悔棋 */
        else if(actioncommand.equals(RETRACT))
        {
            /** 前两步不允许悔棋 */
            if(orderRecoder.size() > 2)
            {
                /** 获取最近的棋子 */
                Point point = orderRecoder.remove(orderRecoder.size()-1);

                /** 清除存储棋子数组中对应的棋子 */
                chessArray[point.x][point.y] = 0;

                /** 改变黑白棋的标志 */
                flag_chess = !flag_chess;

                /** 界面重绘 */
                chessboard.repaint();
            }
        }

        /** 认输 */
        else if(actioncommand.equals(GIVEUP))
        {
            result_message = flag_chess ? GIVEUP_WHITE_WIN : GIVEUP_BLACK_WIN;
            gameOver();
        }
        /** 选中人人对战 */
        else if(actioncommand.equals(PVP))
        {
            AI_offensive.setEnabled(false);
            mode = true;
        }
        /** 选中人机对战 */
        else if(actioncommand.equals(PVE))
        {
            AI_offensive.setEnabled(true);
            mode = false;
        }
        /** 选中AI先手 */
        else if(actioncommand.equals(AI))
        {
            offensive = !offensive;
        }
    }

    /**
     * 游戏结束
     */
    private void gameOver()
    {
        JOptionPane.showMessageDialog(mainframe,result_message);

        /** 菜单面板处理 */
        chessboard.removeMouseListener(this);

        start.setEnabled(true);
        retract_the_false.setEnabled(false);
        give_up.setEnabled(false);

        pvp.setEnabled(true);
        pve.setEnabled(true);

        AI_offensive.setEnabled(offensive ? true : false);
    }
}

判定胜负

public class GobangWin implements GobangConfig
{
    /**
     * 判断输赢
     * @param r
     * @param c
     * @return
     */
    public static boolean judge(int r, int c)
    {
        if(countH(r,c) >= 5 || countV(r,c) >= 5 || countLB_RU(r,c) >= 5 || countLU_RB(r,c) >= 5)
            return true;
        return false;
    }

    /**
     * 水平方向判断
     * @param r
     * @param c
     * @return
     */
    private static int countH(int r, int c)
    {
        int count = 1;

        for(int c1 = c-1; c1 >= 0; c1--)
        {
            if(chessArray[r][c] == chessArray[r][c1])
                count++;
            else
                break;
        }

        for(int c1 = c+1; c1 < ROW; c1++)
        {
            if(chessArray[r][c] == chessArray[r][c1])
                count++;
            else
                break;
        }

        return count;
    }

    /**
     * 垂直方向判断
     * @param r
     * @param c
     * @return
     */
    private static int countV(int r, int c)
    {
        int count = 1;

        for(int r1 = r-1; r1 >= 0; r1--)
        {
            if(chessArray[r][c] == chessArray[r1][c])
                count++;
            else
                break;
        }

        for(int r1 = r+1; r1 < COLUMN; r1++)
        {
            if(chessArray[r][c] == chessArray[r1][c])
                count++;
            else
                break;
        }
        return count;
    }

    /**
     * 左上方至右下方判断
     * @param r
     * @param c
     * @return
     */
    private static int countLU_RB(int r, int c)
    {
        int count = 1;

        for(int r1 = r-1,c1 = c-1; r1 >= 0 && c1 >= 0; r1--,c1--)
        {
            if(chessArray[r][c] == chessArray[r1][c1])
                count++;
            else
                break;
        }

        for(int r1 = r+1,c1 = c+1; r1 < COLUMN && c1 < COLUMN; r1++, c1++)
        {
            if(chessArray[r][c] == chessArray[r1][c1])
                count++;
            else
                break;
        }
        return count;
    }

    /**
     * 左下方至右上方判断
     * @param r
     * @param c
     * @return
     */
    private static int countLB_RU(int r, int c)
    {
        int count = 1;

        for(int r1 = r-1,c1 = c+1; r1 >= 0 && c1 <= ROW; r1--,c1++)
        {
            if(chessArray[r][c] == chessArray[r1][c1])
                count++;
            else
                break;
        }

        for(int r1 = r+1,c1 = c-1; r1 < COLUMN && c1 >= 0; r1++,c1--)
        {
            if(chessArray[r][c] == chessArray[r1][c1])
                count++;
            else
                break;
        }
        return count;
    }
}

人机对弈方法与数据结构

public class WeightedMethod implements GobangConfig
{

    /**
     * 初始化棋局情况权值表
     */
    public static void initMap()
    {
        /** 己方 眠 1 连 */
        situationmap.put("H00",2);//0
        situationmap.put("00H",2);
        situationmap.put("00",2);
        situationmap.put("0",2);

        /** 己方 活 1 连 */
        situationmap.put("000",7);

        /** 己方 眠 2 连 */
        situationmap.put("HA00",8);
        situationmap.put("00AH",8);

        /** 己方 眠 3 连 */
        situationmap.put("HAA00",15);
        situationmap.put("00AAH",15);
        situationmap.put("HA0A0",15);
        situationmap.put("0A0AH",15);

        /** 己方 活 2 连 */
        situationmap.put("0A00",31);
        situationmap.put("00A0",31);



        /** 己方 活 3 连 */
        situationmap.put("0AA00",44);
        situationmap.put("00AA0",44);
        situationmap.put("0A0A0",44);

        /** 己方 眠 4 连 */
        situationmap.put("HAAA00",250);
        situationmap.put("00AAAH",250);
        situationmap.put("HA0AA0",250);
        situationmap.put("0AA0AH",250);
        situationmap.put("HAA0A0",250);
        situationmap.put("0A0AAH",250);

        /** 己方 活 4 连 */
        situationmap.put("00AAA0",1025);
        situationmap.put("0AAA00",1025);
        situationmap.put("0A0AA0",1025);
        situationmap.put("0AA0A0",1025);

        /** 己方 眠 5 连 */
        situationmap.put("HAAAA00",16405);
        situationmap.put("00AAAAH",16405);
        situationmap.put("HAAAA0H",16405);
        situationmap.put("H0AAAAH",16405);
        situationmap.put("HA0AAA0",16405);
        situationmap.put("0AAA0AH",16405);
        situationmap.put("HA0AAAH",16405);
        situationmap.put("HAAA0AH",16405);
        situationmap.put("HAA0AA0",16405);
        situationmap.put("0AA0AAH",16405);
        situationmap.put("HAA0AAH",16405);

        /** 己方 活 5 连 */
        situationmap.put("00AAAA0",16405);
        situationmap.put("0AAAA00",16405);
        situationmap.put("0A0AAA0",16405);
        situationmap.put("0AAA0A0",16405);
        situationmap.put("0AA0AA0",16405);


        /** 对方 眠 1 连 */
        situationmap.put("AH00",2);
        situationmap.put("00HA",2);
        situationmap.put("AH0A",2);
        situationmap.put("A0HA",2);


        /** 对方 活 1 连 */
        situationmap.put("00H0",3);
        situationmap.put("0H00",3);

        /** 对方 眠 2 连 */
        situationmap.put("AHH00",8);
        situationmap.put("00HHA",8);
        situationmap.put("AH0H0",8);
        situationmap.put("0H0HA",8);

        /** 对方 活 2 连 */
        situationmap.put("0HH00",13);
        situationmap.put("00HH0",13);
        situationmap.put("0H0H0",13);

        /** 对方 眠 3 连 */
        situationmap.put("AHHH00",15);
        situationmap.put("00HHHA",15);
        situationmap.put("AH0HH0",15);
        situationmap.put("0HH0HA",15);
        situationmap.put("AHH0H0",15);
        situationmap.put("0H0HHA",15);

        /** 对方 活 3 连 */
        situationmap.put("00HHH0",256);
        situationmap.put("0HHH00",256);
        situationmap.put("0H0HH0",256);
        situationmap.put("0HH0H0",256);

        /** 对方 眠 4 连 */
        situationmap.put("AHHHH00",4101);
        situationmap.put("00HHHHA",4101);
        situationmap.put("AHHHH0A",4101);
        situationmap.put("A0HHHHA",4101);

        situationmap.put("AH0HHH0",4101);
        situationmap.put("0HHH0HA",4101);
        situationmap.put("AH0HHHA",4101);
        situationmap.put("AHHH0HA",4101);

        situationmap.put("AHH0HH0",4101);
        situationmap.put("0HH0HHA",4101);
        situationmap.put("AHH0HHA",4101);

        /** 对方 活 4 连 */
//        situationmap.put("0HHHH00",2);
//        situationmap.put("00HHHH0",2);
//        situationmap.put("0H0HHH0",2);
//        situationmap.put("A0HHHHA",2);
    }
    public static void calculateValue()
    {
        sweepMatrix();
        for(int i = 0; i  < ROW; i++)
        {
            for(int j = 0; j < COLUMN; j++)
            {
                int score = 0;
                if(chessArray[i][j] == 0)
                {
                    /** 斜向下找自己 */
                    String hvs = hvSweepSelf(i,j);
                    /** 斜向下找对方 */
                    String hvo = hvSweepOpponent(i,j);
                    /** 斜向上找自己 */
                    String vhs = vhSweepSelf(i,j);
                    /** 斜向上找对方 */
                    String vho = vhSweepOpponent(i,j);
                    /** 竖直方向找对方 */
                    String vo = verticalSweepOpponent(i,j);
                    /** 竖直方向找自己 */
                    String vs = verticalSweepSelf(i,j);
                    /** 水平方向找对方 */
                    String ho = horizontalSweepOpponent(i,j);
                    /** 水平方向找自己 */
                    String hs = horizontalSweepSelf(i,j);

                    if(situationmap.get(hvs) != null)
                        score += situationmap.get(hvs);
                    if(situationmap.get(hvo) != null)
                        score += situationmap.get(hvo);
                    if(situationmap.get(vhs) != null)
                        score += situationmap.get(vhs);
                    if(situationmap.get(vho) != null)
                        score += situationmap.get(vho);
                    if(situationmap.get(vo) != null)
                        score += situationmap.get(vo);
                    if(situationmap.get(vs) != null)
                        score += situationmap.get(vs);
                    if(situationmap.get(ho) != null)
                        score += situationmap.get(ho);
                    if(situationmap.get(hs) != null)
                        score += situationmap.get(hs);
                }
                valuematrix[i][j] = score;
                //System.out.print(valuematrix[i][j] + " ");
            }
            //System.out.println();
        }
        //System.out.println();
    }

    /**
     * 斜向下找自己
     * @param i
     * @param j
     * @return
     */
    public static String hvSweepSelf(int i, int j)
    {
        String result = "";
        int l = i-1;
        int r = i+1;
        int t = j-1;
        int b = j+1;

        while (l >= 0 && t >= 0)
        {
            if(AIchessArray[l][t] == 'H')
            {
                result +='H';
                break;
            }
            if(AIchessArray[l][t] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[l][t] == 'A')
            {
                result += 'A';
                l--;
                t--;
            }
        }

        result = new StringBuilder(result).reverse().toString();

        result += AIchessArray[i][j];

        while (b < COLUMN  && r < ROW)
        {
            if(AIchessArray[r][b] == 'H')
            {
                result +='H';
                break;
            }
            if(AIchessArray[r][b] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[r][b] == 'A')
            {
                result += 'A';
                r++;
                b++;
            }
        }
        return result;
    }

    /**
     * 斜向下找对方
     * @param i
     * @param j
     * @return
     */
    public static String hvSweepOpponent(int i, int j)
    {
        String result = "";
        int l = i-1;
        int r = i+1;
        int t = j-1;
        int b = j+1;

        while (l >= 0 && t >= 0)
        {
            if(AIchessArray[l][t] == 'A')
            {
                result +='A';
                break;
            }
            if(AIchessArray[l][t] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[l][t] == 'H')
            {
                result += 'H';
                l--;
                t--;
            }
        }

        result = new StringBuilder(result).reverse().toString();

        result += AIchessArray[i][j];

        while (b < COLUMN  && r < ROW)
        {
            if(AIchessArray[r][b] == 'A')
            {
                result +='A';
                break;
            }
            if(AIchessArray[r][b] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[r][b] == 'H')
            {
                result += 'H';
                r++;
                t++;
            }
        }
        return result;
    }

    /**
     * 斜向上找自己
     * @param i
     * @param j
     * @return
     */
    public static String vhSweepSelf(int i, int j)
    {
        String result = "";
        int l = i-1;
        int r = i+1;
        int t = j-1;
        int b = j+1;

        while (l >= 0 && b < COLUMN)
        {
            if(AIchessArray[l][b] == 'H')
            {
                result +='H';
                break;
            }
            if(AIchessArray[l][b] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[l][b] == 'A')
            {
                result += 'A';
                l--;
                b++;
            }
        }

        result = new StringBuilder(result).reverse().toString();

        result += AIchessArray[i][j];

        while (t >= 0 && r < ROW)
        {
            if(AIchessArray[r][t] == 'H')
            {
                result +='H';
                break;
            }
            if(AIchessArray[r][t] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[r][t] == 'A')
            {
                result += 'A';
                r++;
                t--;
            }
        }

        return result;
    }
    /**
     * 斜向上找对方
     * @param i
     * @param j
     * @return
     */
    public static String vhSweepOpponent(int i, int j)
    {
        String result = "";
        int l = i-1;
        int r = i+1;
        int t = j-1;
        int b = j+1;

        while (l >= 0 && b < COLUMN)
        {
            if(AIchessArray[l][b] == 'A')
            {
                result +='A';
                break;
            }
            if(AIchessArray[l][b] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[l][b] == 'H')
            {
                result += 'H';
                l--;
                b++;
            }
        }

        result = new StringBuilder(result).reverse().toString();

        result += AIchessArray[i][j];

        while (t >= 0 && r < ROW)
        {
            if(AIchessArray[r][t] == 'A')
            {
                result +='A';
                break;
            }
            if(AIchessArray[r][t] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[r][t] == 'H')
            {
                result += 'H';
                r++;
                t--;
            }
        }
        return result;
    }

    /**
     * 竖直方向找对方
     * @param i
     * @param j
     * @return
     */
    public static String verticalSweepOpponent(int i, int j)
    {
        String result = "";
        int t = j-1;
        int b = j+1;

        while (t > 0)
        {
            if(AIchessArray[i][t] == 'A')
            {
                result +='A';
                break;
            }
            if(AIchessArray[i][t] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[i][t] == 'H')
            {
                result += 'H';
                t--;
            }
        }

        result = new StringBuilder(result).reverse().toString();

        result += AIchessArray[i][j];

        while (b < COLUMN)
        {
            if(AIchessArray[i][b] == 'A')
            {
                result +='A';
                break;
            }
            if(AIchessArray[i][b] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[i][b] == 'H')
            {
                result += 'H';
                b++;
            }
        }

        return result;
    }

    /**
     * 竖直方向找自己
     * @param i
     * @param j
     * @return
     */
    public static String verticalSweepSelf(int i, int j)
    {
        String result = "";
        int t = j-1;
        int b = j+1;
        while (t > 0)
        {
            if(AIchessArray[i][t] == 'H')
            {
                result +='H';
                break;
            }
            if(AIchessArray[i][t] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[i][t] == 'A')
            {
                result += 'A';
                t--;
            }
        }

        result = new StringBuilder(result).reverse().toString();

        result += AIchessArray[i][j];

        while (b < COLUMN)
        {
            if(AIchessArray[i][b] == 'H')
            {
                result +='H';
                break;
            }
            if(AIchessArray[i][b] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[i][b] == 'A')
            {
                result += 'A';
                b++;
            }
        }
        return result;
    }

    /**
     * 水平方向找对方
     * @param i
     * @param j
     * @return
     */
    public static String horizontalSweepOpponent(int i, int j)
    {
        String result = "";
        int l = i-1;
        int r = i+1;
        while (l >= 0)
        {
            if(AIchessArray[l][j] == 'A')
            {
                result +='A';
                break;
            }
            if(AIchessArray[l][j] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[l][j] == 'H')
            {
                result += 'H';
                l--;
            }
        }

        result = new StringBuilder(result).reverse().toString();

        result += AIchessArray[i][j];

        while (r < COLUMN)
        {
            if(AIchessArray[r][j] == 'A')
            {
                result +='A';
                break;
            }
            if(AIchessArray[r][j] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[r][j] == 'H')
            {
                result += 'H';
                r++;
            }
        }

        return result;
    }

    /**
     * 水平方向找自己
     * @param i
     * @param j
     * @return
     */
    public static String horizontalSweepSelf(int i,int j)
    {
        String result = "";
        int l = i-1;
        int r = i+1;
        while (l >= 0)
        {
            if(AIchessArray[l][j] == 'H')
            {
                result +='H';
                break;
            }
            if(AIchessArray[l][j] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[l][j] == 'A')
            {
                result += 'A';
                l--;
            }
        }

        result = new StringBuilder(result).reverse().toString();

        result += AIchessArray[i][j];

        while (r < COLUMN)
        {
            if(AIchessArray[r][j] == 'H')
            {
                result +='H';
                break;
            }
            if(AIchessArray[r][j] == '0')
            {
                result += '0';
                break;
            }
            if(AIchessArray[r][j] == 'A')
            {
                result += 'A';
                r++;
            }
        }
        return result;
    }

    public static void sweepMatrix()
    {
        if(gobang.GobangListener.offensive == true)
        {
            for(int i = 0; i < ROW; i++)
            {
                for(int j = 0; j < COLUMN; j++)
                {
                    if(chessArray[i][j] == 1)
                        AIchessArray[i][j] = 'A';
                    else if(chessArray[i][j] == 2)
                        AIchessArray[i][j] = 'H';
                    else
                        AIchessArray[i][j] = '0';
                }
            }
        }
        else
        {
            for(int i = 0; i < ROW; i++)
            {
                for(int j = 0; j < COLUMN; j++)
                {
                    if(chessArray[i][j] == 1)
                        AIchessArray[i][j] = 'H';
                    else if(chessArray[i][j] == 2)
                        AIchessArray[i][j] = 'A';
                }
            }
        }
    }
}

你可能感兴趣的:(Java项目)