java Gobang implementation

preface

After learning the basic course of java, I tried to do a Gobang game of java and record it.

Interface

Because a GUI written directly with some basic controls in the java library has not been optimized too much, it feels ugly
The following is the interface display:


Sunspot first, but I simplified the rules and did not consider some prohibition of sunspot first.

Paste the code directly below

Interface class

I defined some constants of Gobang interface in this interface class, including the starting coordinates of chessboard, the spacing of chessboard lines and the radius of chessboard

public interface constant {

    int[][] chessLocation = new int[15][15];
    static final int x = 50;   //Upper left corner position
    static final int y = 50;
    static final int LN = 15;  //Some constants of chessboard
    static final int R = 45;
}

Implementation class

Interface

This class inherits three interfaces: constant, MouseListener and ActionListener
Of which:
constant defines itself
MouseListener listens for the mouse
ActionListener listens for events

function

show() draws the basic frame of the window
paint() draws checkerboard gridlines and pieces
IsWin() is the basic logic for judging whether to win or lose
mouseClicked() to obtain the mouse position, judge the falling point of the chess piece, etc
actionPerformed() determines which button the mouse clicks (start the game or admit defeat or repent chess) to perform the corresponding operation

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 javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class game_logic extends JPanel implements constant, MouseListener, ActionListener {
    int chess_x = 0, chess_y = 0;
    int X = 0, Y = 0;
    boolean IsBlack = true; //Judge black and white
    boolean flag = false;  //Have you started the game
	//Generate three response buttons
    JFrame frame = new JFrame();
    JButton start = new JButton("Start the game");
    JButton regret = new JButton("Repentance chess");
    JButton Lost = new JButton("Admit defeat");
	
    public void ShowUI() {
        frame.setSize(740, 800);
        frame.setTitle("Gobang");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Click close to end the program
        frame.setLocationRelativeTo(null);//Center window
        frame.setVisible(true);//Form visualization
        frame.setResizable(false);//The form size is not adjustable
        frame.add(this);

        this.setBackground(Color.LIGHT_GRAY);//Set background color
        this.addMouseListener(this);//Add mouse listener to form

        start.setSize(50, 80);//Set button size
        start.addActionListener(this);//Button to add an event listener
        Lost.setSize(50, 80);
        Lost.addActionListener(this);
        regret.setSize(50, 80);
        regret.addActionListener(this);

        this.add(start);//Add button to chessboard
        this.add(Lost);
        this.add(regret);

    }

    /**
     * Drawing method
     * Draw Gobang board
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (int i = 0; i < LN; i++) {       //Draw a chessboard
            g.drawLine(x, y + i * R, x + (LN - 1) * R, y + i * R);//Line * 15
            g.drawLine(x + i * R, y, x + i * R, y + (LN - 1) * R);//Column * 15
        }

        for (int i = 0; i < LN; i++) {       //Draw chess pieces
            for (int j = 0; j < LN; j++) {

                if (chessLocation[i][j] == 1) {
                    g.setColor(Color.BLACK);//Black chess first
                    g.fillOval(50 + i * R - 23, 50 + j * R - 23, R, R);
                }
                if (chessLocation[i][j] == 2) {
                    g.setColor(Color.WHITE);
                    g.fillOval(50 + i * R - 23, 50 + j * R - 23, R, R);
                }
                repaint();
            }
        }
    }

    /**
    *Judge whether to win or lose
    *
    */
    public int IsWin() {
        int k = 0;
        for (int f = 2; f < 12; f++) {
            for (int g = 2; g < 12; g++) {
                if (chessLocation[f][g] == 1) {
                    if (chessLocation[f][g] == chessLocation[f - 1][g] && chessLocation[f - 1][g] == chessLocation[f - 2][g] && chessLocation[f - 2][g] == chessLocation[f + 1][g] && chessLocation[f + 1][g] == chessLocation[f + 2][g]) {
                        k = 1;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f][g - 1] && chessLocation[f][g - 1] == chessLocation[f][g - 2] && chessLocation[f][g - 2] == chessLocation[f][g + 1] && chessLocation[f][g + 1] == chessLocation[f][g + 2]) {
                        k = 1;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f - 1][g - 1] && chessLocation[f - 1][g - 1] == chessLocation[f - 2][g - 2] && chessLocation[f - 2][g - 2] == chessLocation[f + 1][g + 1] && chessLocation[f + 1][g + 1] == chessLocation[f + 2][g + 2]) {
                        k = 1;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f - 1][g + 1] && chessLocation[f - 1][g + 1] == chessLocation[f - 2][g + 2] && chessLocation[f - 2][g + 2] == chessLocation[f + 1][g - 1] && chessLocation[f + 1][g - 1] == chessLocation[f + 2][g - 2]) {
                        k = 1;
                        break;
                    }
                }
                if (chessLocation[f][g] == 2) {
                    if (chessLocation[f][g] == chessLocation[f - 1][g] && chessLocation[f - 1][g] == chessLocation[f - 2][g] && chessLocation[f - 2][g] == chessLocation[f + 1][g] && chessLocation[f + 1][g] == chessLocation[f + 2][g]) {
                        k = 2;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f][g - 1] && chessLocation[f][g - 1] == chessLocation[f][g - 2] && chessLocation[f][g - 2] == chessLocation[f][g + 1] && chessLocation[f][g + 1] == chessLocation[f][g + 2]) {
                        k = 2;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f - 1][g - 1] && chessLocation[f - 1][g - 1] == chessLocation[f - 2][g - 2] && chessLocation[f - 2][g - 2] == chessLocation[f + 1][g + 1] && chessLocation[f + 1][g + 1] == chessLocation[f + 2][g + 2]) {
                        k = 2;
                        break;
                    }
                    if (chessLocation[f][g] == chessLocation[f - 1][g + 1] && chessLocation[f - 1][g + 1] == chessLocation[f - 2][g + 2] && chessLocation[f - 2][g + 2] == chessLocation[f + 1][g - 1] && chessLocation[f + 1][g - 1] == chessLocation[f + 2][g - 2]) {
                        k = 2;
                        break;
                    }
                }
            }
        }
        return k;

    }

    @Override
    public void mouseClicked(MouseEvent e) {

        X = e.getX();
        Y = e.getY();                          //Get mouse position
        if (flag == true) {
            if (X >= 25 && X <= 705 && Y >= 25 && Y <= 705) {   //The falling area slightly larger than the chessboard, that is, the edge position of the chessboard
                //The position of the pieces that should be placed
                chess_x = (X - 20) / R;
                chess_y = (Y - 20) / R;

                if (chessLocation[chess_x][chess_y] == 0) {   //Store the state of chess pieces and convert the color of chess pieces
                    if (IsBlack == true) {
                        chessLocation[chess_x][chess_y] = 1;
                        IsBlack = false;
                    } else {
                        chessLocation[chess_x][chess_y] = 2;
                        IsBlack = true;
                    }

                    if (IsWin() == 1) {
                        JOptionPane.showMessageDialog(this, "Black chess wins");
                        flag = false;

                    }
                    if (IsWin() == 2) {
                        JOptionPane.showMessageDialog(this, "White chess wins");
                        flag = false;
                    }
                    repaint();
                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String buttonName = e.getActionCommand();

        if (buttonName.equals("Start the game") && flag == false) {//Start the game and clear the chessboard
            flag = true;
            for (int i = 0; i < LN; i++) {
                for (int j = 0; j < LN; j++) {
                    chessLocation[i][j] = 0;
                }
            }
            IsBlack = true;
            repaint();
        }

        if (buttonName.equals("Admit defeat") && flag == true) {
            flag = false;
            if (IsBlack) {
                JOptionPane.showMessageDialog(this, ",White concedes defeat and black wins");
            } else {
                JOptionPane.showMessageDialog(this, ",Black chess concedes defeat and white chess wins");
            }
        }

        if (buttonName.equals("Repentance chess") && flag == true) {
            if (chessLocation[chess_x][chess_y] == 1) {
                JOptionPane.showMessageDialog(this, "Black repentance chess");
            }
            if (chessLocation[chess_x][chess_y] == 2) {
                JOptionPane.showMessageDialog(this, "Bai Fang repentance chess");
            }
            chessLocation[chess_x][chess_y] = 0;
            IsBlack = !IsBlack;
            repaint();
        }
    }
}

One of the more interesting is the winning method of Gobang. Assuming that the size of the chessboard is 15 * 15, I only need to judge the 13*13d grid in the middle and expand to both sides to judge whether the five pieces are connected.

There are comments in the code, so I won't elaborate.

Main function class

public class Main_game {
    public static void main(String[] args) {
        game_logic start=new game_logic();
        start.ShowUI();
    }
}

summary

The basic functions of Gobang games are realized, but they are slightly rough and lack of details. For basic control calls, you can do a small game demo as soon as you learn. This is a very effective way to train process control and operation logic. I thought it was simple to read other people's code before, but when I wrote it myself, it was often difficult to continue the logical process and confused thinking. Some processes only knew the pit when I wrote it myself.

Keywords: Java

Added by bakaneko on Sun, 16 Jan 2022 03:28:52 +0200