Draw form
For a project, human-computer interaction is necessary, so let's draw a form first
The class used is JFrame, and the code on it
this.setSize(800, 800); this.setTitle("Five Chess Game "); //Set centered display this.setLocationRelativeTo(null); //Exit process this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);
At this time, there is a problem. Didn't you agree on the JFrame object and what is this?
public class GameUI extends JFrame implements Config
Oh, originally inherited the JFrame class. this is actually jf below
JFrame jf = new JFrame(); jf.setSize(800, 800); jf.setTitle("Five Chess Game "); //Set centered display jf.setLocationRelativeTo(null); //Exit process jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true);
Draw a chessboard
for(int i=0;i<LINE;i++) { g.drawLine(X0, Y0+i*SIZE, X0+(LINE-1)*SIZE, Y0+i*SIZE); g.drawLine(X0+i*SIZE, Y0, X0+i*SIZE, Y0+(LINE-1)*SIZE); }
You can adjust the window now. Eh, it's strange why it's gone
This requires rewriting a paint function and redrawing the box every time. Naturally, the chess pieces are saved
//Override the method of drawing components public void paint(Graphics g) { //1. Draw components super.paint(g); System.out.println("paint"); //2. Draw chessboard / pieces //Hard coding for(int i=0;i<LINE;i++) { g.drawLine(X0, Y0+i*SIZE, X0+(LINE-1)*SIZE, Y0+i*SIZE); g.drawLine(X0+i*SIZE, Y0, X0+i*SIZE, Y0+(LINE-1)*SIZE); } for (int i=0;i<chess.length;i++){ Chess chess2 = chess[i]; if(chess2!=null){ g.setColor(chess2.getColor()); g.fillOval(chess2.getX(),chess2.getY(),CHESS,CHESS); } } }
Draw chess pieces
After drawing the window, draw chess pieces and draw pictures
The brush is obtained from the component on which the graphic content is displayed
Get the brush object from the form and get the brush object
Here is an introduction to the use of the brush tool Graphics class
Graphics g = jf.getGraphics();
With the brush, where the painting is, it depends on the mouse click
Come on, the listener is used to get the mouse click position
public interface MouseListener extends EventListener { /** * Invoked when the mouse button has been clicked (pressed * and released) on a component. */ public void mouseClicked(MouseEvent e); /** * Invoked when a mouse button has been pressed on a component. */ public void mousePressed(MouseEvent e); /** * Invoked when a mouse button has been released on a component. */ public void mouseReleased(MouseEvent e); /** * Invoked when the mouse enters a component. */ public void mouseEntered(MouseEvent e); /** * Invoked when the mouse exits a component. */ public void mouseExited(MouseEvent e); }
What the hell can't understand!!!
Don't worry. Explain slowly. This is the listener interface provided by java
So what do we need to do? Create a class and write methods for these interfaces
I created the GameMouse class
GameMouse mouse = new GameMouse(); this.addMouseListener(mouse);
The listener is written. What do we need the listener for
Naturally, it is the code to draw the pieces
g.setColor(Color.WHITE); g.fillOval(x, y, CHESS, CHESS);
What is CHESS? This is a constant defined by me, which represents the size of CHESS pieces, so as to facilitate subsequent code modification and avoid hard coding
Calculate chess position
Now, where the pieces are drawn, what should we do? We need an algorithm
Near that chessboard, where is it
int x = e.getX(); if ((x - X0) % SIZE > SIZE / 2) x = ((x - X0) / SIZE + 1) * SIZE + X0 - CHESS / 2; else x = (x - X0) / SIZE * SIZE + X0 - CHESS / 2; int y = e.getY(); if ((y - Y0) % SIZE > SIZE / 2) y = ((y - Y0) / SIZE + 1) * SIZE + Y0 - CHESS / 2; else y = (y - Y0) / SIZE * SIZE + Y0 - CHESS / 2;
PS:SIZE is the direct distance between two lines on the chessboard
Judge whether it is connected into a line
Only after playing the chess piece can we judge whether the periphery of the chess piece is connected into a line
The idea of the algorithm is to calculate how many pieces on the left and how many pieces on the right add up to five
There are four directions altogether
public void JudgeRow(int x,int y){ int count = 0; for (int i = (x - X0 + CHESS / 2) / SIZE + 1; i < LINE; i++) { if (chessBoard[i][(y - Y0 + CHESS / 2) / SIZE] == chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE]) { count++; } else break; } for (int i = (x - X0 + CHESS / 2) / SIZE; i >= 0; i--) { if (chessBoard[i][(y - Y0 + CHESS / 2) / SIZE] == chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE]) { count++; } else break; } if (count == 5) { this.startFlag=0; if (chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE] == 1) JOptionPane.showMessageDialog(null, "Black chess victory"); else JOptionPane.showMessageDialog(null, "White chess victory"); } }
Gobang is finished, but it's lonely. No one plays with you 🐕
Man machine combat (weight algorithm)
In fact, the purpose of Gobang is to prevent each other's five pieces from connecting into a line, so we can judge the situation of the chess game, give different weights, and place them where the weights are large to ensure that we can't lose
Weight: you can select the location of the falling piece, calculate the chess pieces in 8 directions, and accumulate the weight
My weight assignment is as follows
public void InitHm(){ hm.put("1111112",9000); hm.put("111112",8000); hm.put("1111",6000); hm.put("11112",4000); hm.put("111",200); hm.put("1112",40); hm.put("11",40); hm.put("112",20); hm.put("1",20); hm.put("12",2); hm.put("2",1); hm.put("",0); }
What is hm? Ha ha
private HashMap<String,Integer> hm = new HashMap<String,Integer>();
Give an algorithm example and it's over
public int CountRowZuo(int i,int j){ int color = 0; String chessString = new String(); for(int z = 1;(i-z) > 0 &&chessBoard[i-z][j]!=0;z++) { color = chessBoard[i - 1][j]; if (chessBoard[i - z][j] == color) { chessString = chessString + "1"; } else { chessString = chessString + "2"; break; } } return hm.get(chessString).intValue(); }
git is not open source until it is built (lazy). Ha ha