Initial version of Gobang

Initial version of Gobang
1. Design the main frame, interface, draw the chess board.
2. Using ActionListener interface inheritance to realize button event monitoring, using MouseListener interface to realize event monitoring, and implementing all methods in the interface.
3. Restart the implementation of the function
4. The realization of regret function
5. The definition (x,y,color) of the chess point class in the chessboard.
6. When clicking the chessboard, the drawchess method calculates the chessboard coordinates and draws the chessboard within the error range. The array records the operation and empty points of the whole chessboard and judges the color.
7. When drawing the pieces, judge the winning or losing in time. Use the index of the pieces and for loop to traverse all directions of the last piece. If the number of pieces on the same straight line is greater than or equal to five, it means the winning side represented by the current piece.
8. When the winner is determined, corresponding information can pop up.
package wuziqi;

import java.awt.Color;
import java.awt.Graphics;
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;
import javax.swing.JFrame;
import wuziqi.Config;
import wuziqi.FiveChessUI;
import wuziqi.isWin;
import wuziqi.Point;

public class ChessListener extends MouseAdapter implements ActionListener,MouseListener{
private Graphics g;
private int count=0;
Color c;
int x,y;
int index=0;
public int[][] chesses;
public Point[] ChessList;
JFrame jf=new JFrame();
String type;
public ChessListener(JFrame jf,int[][] chesses,Point[] ChessList){
//this.g=g;
this.jf=jf;
this.chesses=chesses;
this.ChessList=ChessList;
}
public void setG(Graphics arg) {
g=arg;
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand(); / / the default is taken from
type=action;
if("regret". equals(type)){
if(index!=0) {
x=ChessList[index-1].getX();
y=ChessList[index-1].getY();
x=(x-50)/50;y=(y-60)/50;
ChessList[index-1]=null;
chesses[x][y]=0;
jf.repaint();
index–;
if(index%2==0)
count=0;
else
count=1;
}
}
if("restart". equals(type)){
for(int i=0;i<index;i++) {
ChessList[i]=null;
}
for(int i=0;i<14;i++) {
for(int j=0;j<14;j++)
chesses[i][j]=0;
}
index=0;
jf.repaint();
count=0;
}

}
public void mouseReleased(MouseEvent e) {
	int x1=e.getX();
	int y1=e.getY();
	//Color c;
	for(int j=0;j<Config.ROWS;j++) {
		for(int i=0;i<Config.COLUMNS;i++) {
			 int x=Config.X0+Config.SIZE*i;
			 int y=Config.Y0+Config.SIZE*j;
			if(x1>x-Config.SIZE/3&&x1<x+Config.SIZE/3&&y1>y-Config.SIZE/3&&y1<y+Config.SIZE/3) {
				if(chesses[i][j]==0) {
					if(count==0) {
						chesses[i][j]=1;//Black one
						c=Color.black;
						g.setColor(Color.black);
						count++;
					}
					else {
						chesses[i][j]=-1;
						c=Color.WHITE;
						g.setColor(Color.WHITE);
						count--;
					}
					g.fillOval(x-Config.CHESS_SIZE/2, y-Config.CHESS_SIZE/2, Config.CHESS_SIZE, Config.CHESS_SIZE);
					Point ch=new Point(x,y,c);  
			        ChessList[index++]=ch;
					isWin kk = new isWin(i,j,c);
					kk.iswin();  
				}
			}
		}
	}
}
public void mouseClicked(MouseEvent e) {
}

}
The above code is the chessboard listener
package wuziqi;

public interface Config {
public static final int X0=50;
public static final int Y0=60;
public static final int ROWS=14;
public static final int COLUMNS=14;
public static final int CHESS_SIZE=40;
public static final int SIZE=50;
public static final int[][] chesses = new int[Config.ROWS][Config.COLUMNS];
public static final Point[] ChessList=new Point[(Config.ROWS+1)*(Config.COLUMNS+1)];
}
The specific location of the chessboard in the above code
package wuziqi;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import wuziqi.Config;
import wuziqi.FiveChessUI;
import wuziqi.isWin;
import wuziqi.Point;

public class FiveChessUI extends JFrame implements Config{
public void showUI() {
this.setTitle("Gobang");
this.setSize(800,800);
this.setResizable(false);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setLayout(new FlowLayout());
this.setBackground(Color.WHITE);

	ChessListener drawL = new ChessListener(this,chesses, ChessList);
	//Add listener to form
	this.addMouseListener(drawL);
	String name[]= {"Regret chess","restart"};
	for(int i=0;i<name.length;i++) {
		JButton btn=new JButton(name[i]);
		this.add(btn);
		btn.addActionListener(drawL);
	}
	
	this.setVisible(true);
	Graphics g=this.getGraphics();
	drawL.setG(g);
}
public void drawChessTable(Graphics g) {
	for(int i=0;i<Config.ROWS;i++) {
		g.drawLine(Config.X0, Config.Y0+i*Config.SIZE, Config.X0+(Config.COLUMNS-1)*Config.SIZE, Config.Y0+i*Config.SIZE);
	}
	for(int j=0;j<Config.COLUMNS;j++) {
		g.drawLine(Config.X0+j*Config.SIZE, Config.Y0, Config.X0+j*Config.SIZE, Config.Y0+(Config.ROWS-1)*Config.SIZE);
	}
}
public void paint(Graphics g) {
	super.paint(g);
	drawChessTable(g);
	drawchess(g);
}
public void drawchess(Graphics g) {
	for(int j=0;j<Config.ROWS;j++) {
		for(int i=0;i<Config.COLUMNS;i++) {
			int x=50+i*50;
			int y=60+j*50;
			if(chesses[i][j]==1) {
				g.setColor(Color.BLACK);
				g.fillOval(x-Config.CHESS_SIZE/2, y-Config.CHESS_SIZE/2, Config.CHESS_SIZE, Config.CHESS_SIZE);
			}
			else if(chesses[i][j]==-1){
				g.setColor(Color.white);
				g.fillOval(x-Config.CHESS_SIZE/2, y-Config.CHESS_SIZE/2, Config.CHESS_SIZE, Config.CHESS_SIZE);
			}
			}
		}
	}
public static void main(String args[]) {
	FiveChessUI sd=new FiveChessUI();
	sd.showUI();
}

}
Main function of the above code (draw chessboard, chessboard, display form function)
package wuziqi;

import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class isWin implements Config{
private JFrame frame;
private int x,y;
private int count;
private Color c;
String cc;
public isWin(int x,int y,Color c) {
this.x = x;
this.y = y;
this.c = c;
count = 1;
frame = new JFrame();
frame.setTitle("results");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(200,100);
frame.setLayout(new FlowLayout());
}

public void iswin() {
	if(c==Color.BLACK)
		cc="black ";
	else if(c==Color.WHITE)
		cc="White chess";
	count=0;
	for(int i=x+1;i<chesses.length;i++) {//Right search
		if(chesses[i][y]==chesses[x][y]) {
			count++;
		System.out.println(count);}
		else
			break;
	}
	for(int i=x;i>=0;i--) {//Left search
		if(chesses[i][y]==chesses[x][y])
			count++;
		else
			break;
	}
	if(count==6) {
		System.out.println("Horizontal game over!"); 
		JLabel textLabel = new JLabel("congratulations"+cc+"Win");
		//Add label to form
		frame.add(textLabel);
		frame.setVisible(true);  
		count=0;
	}
	for(int i=y+1;i<chesses.length;i++) {//Next search
		if(chesses[x][i]==chesses[x][y])
			count++;
		else
			break;
	}
	for(int i=y;i>=0;i--) {//Search for
		if(chesses[x][i]==chesses[x][y])
			count++;
		else
			break;
	}
	if(count==6) {
		System.out.println("End of vertical game!");
		System.out.println("congratulations"+cc+"Win");
		JLabel textLabel = new JLabel("congratulations"+cc+"Win");
		//Add label to form
		frame.add(textLabel);
		frame.setVisible(true);  
		count=0;
	}
	for(int i=x+1,j=y+1;j<=chesses.length&&i<=chesses.length;i++,j++){//Bottom right search  
        if(chesses[i][j]==chesses[x][y]){  
            count++;  
        }else  
            break;  
    }  
    for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--){//Top left search  
    	 if(chesses[i][j]==chesses[x][y]){  
             count++;  
         }else  
             break;  
    }  
    if(count==6) {
		System.out.println("Northwest game over!");
		System.out.println("congratulations"+cc+"Win");
		JLabel textLabel = new JLabel("congratulations"+cc+"Win");
		//Add label to form
		frame.add(textLabel);
		frame.setVisible(true);   
        count=0;
	}
    for(int i=x+1,j=y-1;j>=0&&i<=chesses.length;i++,j--){//Look up right  
        if(chesses[i][j]==chesses[x][y]){  
            count++;  
        }else  
            break;  
    }  
    for(int i=x-1,j=y+1;i>=0&&j<=chesses.length;i--,j++){//Bottom left search  
    	 if(chesses[i][j]==chesses[x][y]){  
             count++;  
         }else  
             break;  
    }  
    if(count==6) {
		System.out.println("Southwest game over!");
		System.out.println("congratulations"+cc+"Win");
		JLabel textLabel = new JLabel("congratulations"+cc+"Win");
		//Add label to form
		frame.add(textLabel);
		frame.setVisible(true);   
        count=0;
	}
    
}

}
The above code is to determine who wins
package wuziqi;

import java.awt.Color;

public class Point {
private int x; / / the X index value of the chess piece in the chessboard
private int y; / / the y index value of the chess piece in the chessboard
private Color color; / / color
Public static int diameter = 30; / / diameter
public Point(int x,int y,Color color){
this.x=x;
this.y=y;
this.color=color;
}
//Get the x index value of the chess piece in the chessboard
public int getX(){
return x;
}
//Get the y index value of the chess piece in the chessboard
public int getY(){
return y;
}
//Get the chess color
public Color getColor(){
return color;
}

}
The above code is a kind of specified chess piece

Tags: Java

Posted on Sun, 07 Jun 2020 02:08:26 -0700 by alco19357