Please refer to the code for renju man to man and anything else I want to say

Java applet – simple gobang _ such as cut such as grinding, such as cut such as grinding -CSDN blog _java applet

For some reason, here I give the AI algorithm is relatively simple, so led to the computer is not very strong, even more you play can also find the “fatal weakness” of the computer, because I just use this project to do a Java introduction to the practice, and I’m in graduate school, so I don’t prepare for AI algorithm was improved.

Gobang game interface:

public interface WZQConfig {
    /* * start position X */
    public static final int START_X = 60;
    /* * start position Y */
    public static final int START_Y = 60;
    /* * the number of lines */
    public static final int H_LINE = 15;
    /* * The number of vertical lines */
    public static final int V_LINE = 15;
    /* * Five checkerboard grid size */
    public static final int SIZE = 60;
    /* * Store the x position of the piece */
    public static final int[][] bx = new int[17] [17];
    public static final int[][] bx_lastStep = new int[17] [17];

    public static final int[][] weightArray = new int [17] [17];


}
Copy the code

Interface:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
/* * Interface class for backgammon, which inherits JFrame and then implements the WZQConfig interface */
import javax.swing.JPanel;

public class WZQFrame extends JPanel implements WZQConfig {
    public void WZQFrame(a) {
        // WZQFrame ui = new WZQFrame();
        JFrame jf = new javax.swing.JFrame();
        jf.setTitle("Gobang");
        jf.setDefaultCloseOperation(3);
        jf.setSize(1246.1080);
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);

        jf.setLayout(new FlowLayout());
        this.setLayout(new FlowLayout());

        this.setPreferredSize(new Dimension(1030.1080));

        // this.setBackground(Color.CYAN);
        // Add the panel object to the form
        jf.add(this);
        JPanel jp1 = new JPanel();
        jp1.setPreferredSize(new Dimension(200.1080));
        jp1.setLayout(new FlowLayout());
        jf.add(jp1);
        LoginListener ll = new LoginListener();
        String[] str = { "Back"."Start over" };
        for (int i = 0; i < str.length; i++) {
            JButton jbu1 = new JButton(str[i]);
            jbu1.setPreferredSize(new Dimension(150.80));
            jbu1.setFont(new Font("Regular script", Font.BOLD,20));// Set the font
            jp1.add(jbu1);
            jbu1.addActionListener(ll);
        }

        jf.setVisible(true);

        Graphics g = this.getGraphics();

        this.addMouseListener(ll);

        ll.setG(g);
        ll.setU(this);
    }

    /* * Overwrites the formdraw container method */
    public void paint(Graphics g) {
        super.paint(g);

        ImageIcon im2 = new ImageIcon(this.getClass().getResource("2.jpg"));

        g.drawImage(im2.getImage(), 0.0.1030.1080.null);

        for (int i = 1; i < 17; i++) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(4));
            g2.drawLine(START_X, START_Y * i, START_X + SIZE * V_LINE, START_Y
                    * i);/ / horizontal
            g2.drawLine(START_X * i, START_Y, START_X * i, START_Y + SIZE
                    * V_LINE);/ / vertical lines

            g2.setStroke(new BasicStroke(8));
            / / picture frame
            g2.drawLine(35.35.990.35);
            g2.drawLine(35.990.990.990);
            g2.drawLine(35.35.35.990);
            g2.drawLine(990.35.990.990);
        }
        for (int k = 0; k < 17; k++) {
            for (int k1 = 0; k1 < 17; k1++) {
                if (bx[k][k1] == 1) {
                    g.setColor(Color.BLACK);
                    g.fillOval(Math.abs(k * SIZE - 25),
                            Math.abs(k1 * SIZE - 25), 50.50);

                } else if (bx[k][k1] == 2) {
                    g.setColor(Color.WHITE);
                    g.fillOval(Math.abs(k * SIZE - 25),
                            Math.abs(k1 * SIZE - 25), 50.50); }}}}public static void main(String[] args) {
        WZQFrame l = newWZQFrame(); l.WZQFrame(); }}Copy the code

Controller/Listener class:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class LoginListener implements MouseListener.WZQConfig.ActionListener {
    private int x, y;// Mouse click position
    private int x1, y1, xx, yy, x2, y2;// Mouse click on the coordinate of the nearby grid intersection
    private Graphics g;
    private int a = 0, i = 0, j = 0, count1 = 0;// count Counts the number of chess pieces currently played
    private String Str;// Define a global variable to get the string on the button
    private JButton jbu1, jbu2;
    private int GetX[] = new int[256];
    private int GetY[] = new int[256];
    WZQFrame ui;
    HashMap<String,Integer> map = new HashMap<String,Integer>();

    public LoginListener(a){

        / / live in a row
        map.put("010".20);
        map.put("020".20);
        map.put("01".20);
        map.put(".".20);
        map.put("10".20);
        map.put("20".20);
        / / live one
        map.put("0110".100);
        map.put("0220".100);
        map.put("011".100);
        map.put("110".100);
        map.put("022".100);
        map.put("220".100);
        / / live even three
        map.put("01110".500);
        map.put("02220".500);
        map.put("1110".500);
        map.put("0111".500);
        map.put("2220".500);
        map.put("0222".500);
        / / live quadruple
        map.put("011110".10000);
        map.put("022220".10000);
        map.put("01111".10000);
        map.put("11110".10000);
        map.put("02222".10000);
        map.put("22220".10000);
        / / for death
        map.put("012".10);
        map.put("021".10);
        map.put("120".10);
        map.put("210".10);
        / / another death
        map.put("0112".70);
        map.put("0221".70);
        map.put("2110".70);
        map.put("1220".70);
        / / 3 even to death
        map.put("01112".200);
        map.put("02221".200);
        map.put("21110".200);
        map.put("12220".200);
        / / four even to death
        map.put("011112".10000);
        map.put("022221".10000);
        map.put("211110".10000);
        map.put("122220".10000);

    }

    public void setG(Graphics g) {
        this.g = g;
    }

    public void setT(JButton b) {
        jbu1 = b;
    }

    public void setU(WZQFrame u) {
        ui = u;
    }

    public void mouseClicked(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        x1 = Math.abs(x - START_X);
        y1 = Math.abs(y - START_Y);
        xx = x1 % SIZE;
        if (xx >= SIZE / 2) {
            x1 = (x1 / SIZE) + 2;
        } else {
            x1 = (x1 / SIZE) + 1;
        }

        yy = y1 % SIZE;// Determine if the abscissa is more than half the length of the grid (to prevent players from skewing)
        if (yy >= SIZE / 2) {
            y1 = (y1 / SIZE) + 2;
        } else {
            y1 = (y1 / SIZE) + 1;
        }
        g.setColor(Color.BLACK);
        if (bx[x1][y1] == 0) {
            bx[x1][y1] = 1;// represents black
            g.fillOval(Math.abs(x1 * SIZE - 25), Math.abs(y1 * SIZE - 25), 50.50);
            count1++;// The number of pieces played increases by one
            GetX[count1] = x1;// Record the piece x value of the first count1 step
            GetY[count1] = y1;// Record the chess y value of the first count1 step
            if (CheckRow(x1, y1) >= 5) {
                JOptionPane.showMessageDialog(null."BLACK WIN!!");
            }
            if (CheckList(x1, y1) >= 5) {
                JOptionPane.showMessageDialog(null."BLACK WIN!!");
            }
            if (UpperRight(x1, y1) >= 5) {
                JOptionPane.showMessageDialog(null."BLACK WIN!!");
            }
            if (UpperLeft(x1, y1) >= 5) {
                JOptionPane.showMessageDialog(null."BLACK WIN!!");
            }
        }
        g.setColor(Color.WHITE);
        WZQAI();// iterate over the board
        AIcount();// The computer calculates the coordinates where the chess should be played
        if (bx[x1][y1] == 0) {
            bx[x1][y1] = 2;// represents white
            if(x1==0)
            {g.fillOval(Math.abs((x1+1)* SIZE - 25), Math.abs((y1+1) * SIZE - 25), 50.50); }else
            {
                g.fillOval(Math.abs(x1* SIZE - 25), Math.abs(y1 * SIZE - 25), 50.50);
            }
            count1++;// The number of pieces played increases by one
            GetX[count1] = x1;// Record the piece x value of the first count1 step
            GetY[count1] = y1;// Record the chess y value of the first count1 step
            if (CheckRow(x1, y1) >= 5) {
                JOptionPane.showMessageDialog(null."WHITE WIN!!");
            }
            if (CheckList(x1, y1) >= 5) {
                JOptionPane.showMessageDialog(null."WHITE WIN!!");
            }
            if (UpperRight(x1, y1) >= 5) {
                JOptionPane.showMessageDialog(null."WHITE WIN!!");
            }
            if (UpperLeft(x1, y1) >= 5) {
                JOptionPane.showMessageDialog(null."WHITE WIN!!"); }}// Clear the weight array
        for (int r = 0; r < weightArray.length; r++) {
            for (int c = 0; c < weightArray.length; c++) {
                weightArray[r][c] = 0; }}}public void actionPerformed(ActionEvent e) {
        setT(jbu1);
        Str = e.getActionCommand();// Read the string on the click button
        if ("Back".equals(Str)) {
            if (g.getColor() == Color.BLACK) {
                g.setColor(Color.WHITE);
            }
            if (g.getColor() == Color.WHITE) {
                g.setColor(Color.BLACK);
            }
            Regret();
            ui.repaint();
        } else if ("Start over".equals(Str)) { Restart(); ui.repaint(); }}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public int CheckRow(int x, int y)// The five pieces are connected in a straight line
    {
        int count = 0;
        for (int i = x + 1; i < bx.length; i++)// Check to the right if the pieces are the same
        {
            if (bx[i][y] == bx[x][y])
                count++;
            else
                break;
        }
        for (int i = x; i >= 0; i--)// Check to the right if the pieces are the same
        {
            if (bx[i][y] == bx[x][y])
                count++;
            else
                break;
        }
        return count;
    }

    public int CheckList(int x, int y)// The five pieces are vertically connected to form a straight line
    {
        int count = 0;
        for (int i = y + 1; i < bx.length; i++)// Check to see if the pieces are the same
        {
            if (bx[x][i] == bx[x][y])
                count++;
            else
                break;
        }
        for (int i = y; i >= 0; i--)// Check to see if the pieces are the same
        {
            if (bx[x][i] == bx[x][y])
                count++;
            else
                break;
        }
        return count;
    }

    public int UpperRight(int x, int y)// From top right to bottom left the five pieces are connected in a straight line
    {
        int count = 0;
        for (int i = x + 1, j = y - 1; i < bx.length && j >= 0; i++, j--)// Check to see if the pieces are the same
        {
            if (bx[i][j] == bx[x][y])
                count++;
            else
                break;
        }
        for (int i = x, j = y; i >= 0 && j < bx.length; i--, j++)// Check to see if the pieces are the same
        {
            if (bx[i][j] == bx[x][y])
                count++;
            else
                break;
        }
        return count;
    }

    public int UpperLeft(int x, int y)// From top left to bottom right the five pieces are connected in a straight line
    {
        int count = 0;
        for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--)// Check to see if the pieces are the same
        {
            if (bx[i][j] == bx[x][y])
                count++;
            else
                break;
        }
        for (int i = x, j = y; i < bx.length && j < bx.length; i++, j++)// Check to see if the pieces are the same
        {
            if (bx[i][j] == bx[x][y])
                count++;
            else
                break;
        }
        return count;
    }

    public void Regret(a) {/ / back
        assert count1 >= 2;

        bx[GetX[count1]][GetY[count1]] = 0;
        bx[GetX[count1-1]][GetY[count1-1]] = 0;
        if (count1 > 0) {
            count1 = count1 - 2; }}public void Restart(a) {// Start over
        {
            for (int k = 0; k <= count1; k++) {
                bx[GetX[k]][GetY[k]] = 0;
            }
        }
        count1 = 0;
        for (int i1 = 0; i1 < bx.length; i1++) {
            for (int j1 = 0; j1 < bx.length; j1++) {
                bx[i][j] = 0; }}}public void WZQAI(a) {

        // These loops traverse the entire array of stored pieces
        for (int r = 0; r < bx.length; r++) {
            for (int c = 0; c < bx[r].length; c++) {
                if (bx[r][c] == 0) {// Check whether it is empty
                    int ch = 0;// Store the variable of the first occurrence of the chess piece
                    String chessCode = "0";// Store the variables that count the chessmen connected
                    /* * Horizontal left and left pieces in chessCode are left, and right pieces in chessCode are right */
                    for (int c1 = c - 1; c1 >= 0; c1--) {
                        if (bx[r][c1] == 0) {// Check whether it is empty
                            if (c1 + 1 == c) {// Check whether it is adjacent
                                break;
                            } else {// Check whether the neighbor is not adjacent
                                chessCode = bx[r][c1] + chessCode;// Record the chessmen connected
                                break; }}else {// Check if it is a chess piece
                            if (ch == 0) {// Check if it is the first checker to appear
                                chessCode = bx[r][c1] + chessCode;// Record the chessmen connected
                                ch = bx[r][c1];// Store the first chess piece
                            } else if (ch == bx[r][c1]) {// Check if it is the same color
                                chessCode = bx[r][c1] + chessCode;// Record the chessmen connected
                            } else {
                                chessCode = bx[r][c1] + chessCode;// Record the chessmen connected
                                break; }}}// Get the weights stored in the HashMap based on where the pieces are connected
                    Integer weight = map.get(chessCode);
                    if(null==weight){
                        System.out.println(" get one null.....");
                    }
                    else{
                        int a;
                        a=weight.intValue();
                        // Store it in the weight array
                        weightArray[r][c] += a;
                    }

                    ch = 0;// Reset to the initial state
                    chessCode = "0";// Reset to the initial state

                    /* * Horizontal right-left pieces are left in chessCode, and right-sided pieces are right in chessCode */
                    for (int c2 = c + 1; c2 <= 15; c2++) {// Check if it is empty
                        if (bx[r][c2] == 0) {// Determine whether the space is adjacent
                            if (c2 - 1 == c) {
                                break;
                            } else {// Check that the empty space is not adjacent
                                chessCode = chessCode + bx[r][c2];// Record the chessmen connected}}// Judgment is a pawn
                        else {// It is the first time that a chess piece appears
                            if (ch == 0) {
                                chessCode = chessCode + bx[r][c2];// Record the chessmen connected
                                ch = bx[r][c2];// Store the first occurrence of the chessman
                            } else if (ch == bx[r][c2])// Check the color of the pieces
                            {
                                chessCode = chessCode + bx[r][c2];
                            }// Record the chessman connection
                            else {// The adjacent pieces have different colors
                                chessCode = chessCode + bx[r][c2];
                                break;
                            }
                        }
                        weight = map.get(chessCode);
                        if(null==weight){
                            System.out.println(" get one null.....");
                        }
                        else{
                            int a;
                            a=weight.intValue();
                            // Store it in the weight array
                            weightArray[r][c] += a;
                        }

                        ch = 0;// Reset to the initial state
                        chessCode = "0";// Reset to the initial state
                    }

                    /* * the top piece in chessCode is left, and the bottom piece in chessCode is right */
                    for (int r1 = r - 1; r1 >= 0; r1--) {
                        if (bx[r1][c] == 0) {// Check whether it is empty
                            if (r1 + 1 == r) {// Check whether it is adjacent
                                break;
                            } else {// Check whether the neighbor is not adjacent
                                chessCode = bx[r1][c] + chessCode;// Record the chessmen connected
                                break; }}else {// Check if it is a chess piece
                            if (ch == 0) {// Check if it is the first checker to appear
                                chessCode = bx[r1][c] + chessCode;// Record the chessmen connected
                                ch = bx[r1][c];// Store the first chess piece
                            } else if (ch == bx[r1][c]) {// Check if it is the same color
                                chessCode = bx[r1][c] + chessCode;// Record the chessmen connected
                            } else {
                                chessCode = bx[r1][c] + chessCode;// Record the chessmen connected
                                break; }}}// Get the weights stored in the HashMap based on where the pieces are connected
                    weight = map.get(chessCode);
                    if(null==weight){
                        System.out.println(" get one null.....");
                    }
                    else{
                        int a;
                        a=weight.intValue();
                        // Store it in the weight array
                        weightArray[r][c] += a;
                    }

                    ch = 0;// Reset to the initial state
                    chessCode = "0";// Reset to the initial state

                    /* * the top piece in chessCode is left, and the bottom piece in chessCode is right */
                    for (int r2 = r + 1; r2 <= 15; r2++) {// Check if it is empty
                        if (bx[r2][c] == 0) {// Determine whether the space is adjacent
                            if (r2 - 1 == r) {
                                break;
                            } else {// Check that the empty space is not adjacent
                                chessCode = chessCode + bx[r2][c];// Record the chessmen connected}}// Judgment is a pawn
                        else {// It is the first time that a chess piece appears
                            if (ch == 0) {
                                chessCode = chessCode + bx[r][c];// Record the chessmen connected
                                ch = bx[r2][c];// Store the first occurrence of the chessman
                            } else if (ch == bx[r2][c])// Check the color of the pieces
                            {
                                chessCode = chessCode + bx[r2][c];
                            }// Record the chessman connection
                            else {// The adjacent pieces have different colors
                                chessCode = chessCode + bx[r2][c];
                                break;
                            }
                        }
                        weight = map.get(chessCode);
                        if(null==weight){
                            System.out.println(" get one null.....");
                        }
                        else{
                            int a;
                            a=weight.intValue();
                            // Store it in the weight array
                            weightArray[r][c] += a;
                        }

                        ch = 0;// Reset to the initial state
                        chessCode = "0";// Reset to the initial state
                    }

                    /* * The upper left piece is left in chessCode, and the lower right piece is right in chessCode */
                    for (int r1 = r - 1, c1 = c - 1; c1 >= 0 && r1 >= 0; c1--, r1--) {
                        if (bx[r1][c1] == 0) {// Check whether it is empty
                            if (c1 + 1 == c && r1 + 1 == c) {// Check whether it is adjacent
                                break;
                            } else {// Check whether the neighbor is not adjacent
                                chessCode = bx[r1][c1] + chessCode;// Record the chessmen connected
                                break; }}else {// Check if it is a chess piece
                            if (ch == 0) {// Check if it is the first checker to appear
                                chessCode = bx[r1][c1] + chessCode;// Record the chessmen connected
                                ch = bx[r1][c1];// Store the first chess piece
                            } else if (ch == bx[r1][c1]) {// Check if it is the same color
                                chessCode = bx[r1][c1] + chessCode;// Record the chessmen connected
                            } else {
                                chessCode = bx[r1][c1] + chessCode;// Record the chessmen connected
                                break; }}}// Get the weights stored in the HashMap based on where the pieces are connected
                    weight = map.get(chessCode);
                    if(null==weight){
                        System.out.println(" get one null.....");
                    }
                    else{
                        int a;
                        a=weight.intValue();
                        // Store it in the weight array
                        weightArray[r][c] += a;
                    }

                    ch = 0;// Reset to the initial state
                    chessCode = "0";// Reset to the initial state

                    /* * Left slant downward The upper left piece is left in chessCode, and the lower right piece is right in chessCode */
                    for (int r1 = r + 1, c1 = c + 1; c1 <= 15 && r1 <= 15; c1++, r1++) {
                        if (bx[r1][c1] == 0) {// Check whether it is empty
                            if (c1 - 1 == c && r1 - 1 == c) {// Check whether it is adjacent
                                break;
                            } else {// Check whether the neighbor is not adjacent
                                chessCode = chessCode + bx[r1][c1];// Record the chessmen connected
                                break; }}else {// Check if it is a chess piece
                            if (ch == 0) {// Check if it is the first checker to appear
                                chessCode = chessCode + bx[r1][c1];// Record the chessmen connected
                                ch = bx[r1][c1];// Store the first chess piece
                            } else if (ch == bx[r1][c1]) {// Check if it is the same color
                                chessCode = chessCode + bx[r1][c1];// Record the chessmen connected
                            } else {
                                chessCode = chessCode + bx[r1][c1];// Record the chessmen connected
                                break; }}}/* * The lower left piece is left in chessCode, and the upper right piece is right in chessCode */
                    for (int r1 = r - 1, c1 = c + 1; c1 <= 15 && r1 >= 0; c1++, r1--) // row r, column C
                    {
                        if (bx[r1][c1] == 0) {// Check whether it is empty
                            if (c1 - 1 == c && r1 + 1 == c) {// Check whether it is adjacent
                                break;
                            } else {// Check whether the neighbor is not adjacent
                                chessCode = chessCode + bx[r1][c1];// Record the chessmen connected
                                break; }}else {// Check if it is a chess piece
                            if (ch == 0) {// Check if it is the first checker to appear
                                chessCode = chessCode + bx[r1][c1];// Record the chessmen connected
                                ch = bx[r1][c1];// Store the first chess piece
                            } else if (ch == bx[r1][c1]) {// Check if it is the same color
                                chessCode = chessCode + bx[r1][c1];// Record the chessmen connected
                            } else {
                                chessCode = chessCode + bx[r1][c1];// Record the chessmen connected
                                break;
                            }
                        }

                        weight = map.get(chessCode);
                        if(null==weight){
                            System.out.println(" get one null.....");
                        }
                        else{
                            int a;
                            a=weight.intValue();
                            // Store it in the weight array
                            weightArray[r][c] += a;
                        }

                        ch = 0;// Reset to the initial state
                        chessCode = "0";// Reset to the initial state
                    }

                    /* * The lower left piece in chessCode is left, the upper right piece in chessCode is right */
                    for (int r1 = r + 1, c1 = c - 1; c1 >= 0 && r1 <= 15; c1--, r1++) // row r, column C
                    {
                        if (bx[r1][c1] == 0) {// Check whether it is empty
                            if (c1 + 1 == c && r1 - 1 == c) {// Check whether it is adjacent
                                break;
                            } else {// Check whether the neighbor is not adjacent
                                chessCode = bx[r1][c1] + chessCode;// Record the chessmen connected
                                break; }}else {// Check if it is a chess piece
                            if (ch == 0) {// Check if it is the first checker to appear
                                chessCode = bx[r1][c1] + chessCode;// Record the chessmen connected
                                ch = bx[r1][c1];// Store the first chess piece
                            } else if (ch == bx[r1][c1]) {// Check if it is the same color
                                chessCode = bx[r1][c1] + chessCode;// Record the chessmen connected
                            } else {
                                chessCode = bx[r1][c1] + chessCode;// Record the chessmen connected
                                break; }}}// Get the weights stored in the HashMap based on where the pieces are connected
                    weight = map.get(chessCode);
                    if(null==weight){
                        System.out.println(" get one null.....");
                    }
                    else{
                        int a;
                        a=weight.intValue();
                        // Store it in the weight array
                        weightArray[r][c] += a;
                    }

                    ch = 0;// Reset to the initial state
                    chessCode = "0";// Reset to the initial state}}}}public void AIcount(a) {// The computer calculates the coordinates that should be played
        int max = -1;// Store the maximum weight
        int rx = 0;// Store the row coordinates of the maximum value
        int cx = 0;// Store the maximum column coordinates
        int k = 0;
        // The maximum value is x1,y1
        for (int r = 0; r < weightArray.length; r++) {
            for (int c = 0; c < weightArray.length; c++) {
                if (weightArray[r][c] > max) {
                    System.out.println("weightArray=" + weightArray[r][c]);
                    max = weightArray[r][c];
                    rx = r;
                    cx = c;
                } else {
                    continue;
                }
            }
        }
        x1 = rx;
        y1 = cx;
        x2 = rx * SIZE - 25;
        y2 = cx * SIZE - 25; }}Copy the code