The previous two articles: Java implements two Gozi game (two) to draw a chess board; Java implements two Gozi game (two) to draw a chess piece; Java implements two Gozi game (four) to realize the move of two Gozi game (two) to view it.
We have already drawn chessboards and chess pieces before and can freely make moves. The next function to be realized is to determine whether there are five consecutive beads (the chess game is not considered for the time being).
We use the traversal position where the board has already landed and check whether there are five consecutive chess pieces in any direction of its four directions: up and down, left and right, lower left and lower right.
The first step is to transform the chess piece class. Previously, our chess piece class only had color information and drop status. Now we need to add an int-type data to record how many beads are currently known to be continuous during the traversal process.
Chessman.java
package xchen.test.simpleGobang; public class Chessman { private int color;//1-white, 0-black private boolean placed = false; int matchCount = 1; public Chessman(int color,boolean placed){ this.color=color; this.placed=placed; } public boolean getPlaced() { return placed; } public void setPlaced(boolean placed) { this.placed = placed; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } }The second step is to first determine whether there are five consecutive beads from one direction, and use the left and right directions as an attempt here.
An isWin function has been added to make a winning judgment by traversing effective pieces on the entire board.
DrawChessBoard.java
package xchen.test.simpleGobang; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RadialGradientPaint; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.Color; import javax.swing.JPanel; public class DrawChessBoard extends JPanel implementations MouseListener{ final static int BLACK=0; final static int WHITE=1; public int chessColor = BLACK; int chessman_width=30; public Image boardImg; final private int ROWS = 19; Chessman[][] chessStatus=new Chessman[ROWS+1][ROWS+1]; public DrawChessBoard() { boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard2.png"); if(boardImg == null) System.err.println("png do not exist"); addMouseListener(this); } @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); int imgWidth = boardImg.getHeight(this); int imgHeight = boardImg.getWidth(this); int FWidth = getWidth(); int FHeight= getHeight(); int x=(FWidth-imgWidth)/2; int y=(FHeight-imgHeight)/2; int span_x=imgWidth/ROWS; int span_y=imgHeight/ROWS; g.drawImage(boardImg, x, y, null); //Draw horizontal line for(int i=0;i<ROWS;i++) { g.drawLine(x, y+i*span_y, FWidth-x,y+i*span_y); } //Draw vertical line for(int i=0;i<ROWS;i++) { g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y); } //Draw chess pieces for(int i=0;i<ROWS+1;i++) { for(int j=0;j<ROWS+1;j++) { if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) { //System.out.println("draw chessman "+i+" "+j); int pos_x=x+i*span_x; int pos_y=y+j*span_y; float radius_b=40; float radius_w=80; float[] fractions = new float[]{0f,1f}; java.awt.Color[] colors_b = new java.awt.Color[]{Color.BLACK,Color.WHITE}; Color[] colors_w = new Color[]{Color.WHITE,Color.BLACK}; RadialGradientPaint paint; if(chessStatus[i][j].getColor()==1) { //System.out.println("draw white chess"); paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w); }else{ //System.out.println("draw black chess"); paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b); } ((Graphics2D)g).setPaint(paint); ((Graphics2D)g).fillOval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width); } } } } @Override //Public void mousePressed(MouseEvent e) { int point_x=e.getX(); int point_y=e.getY(); int imgWidth = boardImg.getHeight(this); int imgHeight = boardImg.getWidth(this); int FWidth = getWidth(); int FHeight= getHeight(); int x=(FWidth-imgWidth)/2; int y=(FHeight-imgHeight)/2; int span_x=imgWidth/ROWS; int span_y=imgHeight/ROWS; //System.out.println("press"); int status_x = 0; int status_y = 0; if(point_x>=x && point_x<=x+imgWidth && point_y>=y && point_y <= y+imgHeight) { //System.out.println("Legal"); for(int i=0;i<ROWS+1;i++) { if(point_x>=x-chessman_width/2+1+i*span_x) { if(point_x<=x+chessman_width/2-1+i*span_x)//If it is width/2, two matching values will appear at the middle point{ //System.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; } } } for(int i=0;i<ROWS+1;i++) { if(point_y>=y-chessman_width/2+1+i*span_y) { if(point_y <= y+chessman_width/2-1+i*span_y) { //System.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_y = i; } } } Chessman chessman = new Chessman(BLACK, true); chessStatus[status_x][status_y]=chessman; repaint(); if(isWin(status_x, status_y, chessStatus)) { System.out.println("WIN!!!!"); } } } @Override //Public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } boolean isWin(int point_x,int point_y,Chessman[][] cm) { //int matchCount = 1;//Record the number of beads//Horizontal search for(int i=0;i<ROWS+1;i++) { for(int j=0;j<ROWS+1;j++) { if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) { //System.out.println("isWin:"+i+" "+j); //Look for(int n=1;n<=4;n++) { if((i+n>=0)&&(i+n)<=ROWS) { if(chessStatus[i+n][j]!=null&&chessStatus[i+n][j].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" right count++:"+(i+n)+" "+j+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { break; } } } //Look for(int n=1;n<=4;n++) { if(in>=0)&&(in)<=ROWS) { if(chessStatus[in][j]!=null&&chessStatus[in][j].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" "+"left count++:"+(in)+" "+j+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { if(chessStatus[in][j]!=null) { chessStatus[i][j].matchCount = 1; } break; } } } chessStatus[i][j].matchCount=1;//refresh count } } return false; } } } }Step 3 : The main module remains unchanged. Run and test whether our algorithm is correct.
Main.java
package xchen.test.simpleGobang; import java.awt.Container; import javax.swing.JFrame; import xchen.test.simpleGobang.DrawChessBoard; public class Main extends JFrame{ private DrawChessBoard drawChessBoard; public Main() { drawChessBoard = new DrawChessBoard(); //Frame title setTitle("Stand-alone Goji"); Container containerPane =getContentPane(); containerPane.add(drawChessBoard); } public static void main(String[] args) { Main m = new Main(); m.setSize(800, 800); m.setVisible(true); } }Step 4 : Now that we have made a judgment in one direction, we will complete the judgment code in the other three directions.
Complementing the isWin() function in DrawChessBoard.java
package xchen.test.simpleGobang; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RadialGradientPaint; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JPanel; public class DrawChessBoard extends JPanel implementations MouseListener{ final static int BLACK=0; final static int WHITE=1; public int chessColor = BLACK; int chessman_width=30; public Image boardImg; final private int ROWS = 19; Chessman[][] chessStatus=new Chessman[ROWS+1][ROWS+1]; public DrawChessBoard() { boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard2.png"); if(boardImg == null) System.err.println("png do not exist"); addMouseListener(this); } @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); int imgWidth = boardImg.getHeight(this); int imgHeight = boardImg.getWidth(this); int FWidth = getWidth(); int FHeight= getHeight(); int x=(FWidth-imgWidth)/2; int y=(FHeight-imgHeight)/2; int span_x=imgWidth/ROWS; int span_y=imgHeight/ROWS; g.drawImage(boardImg, x, y, null); //Draw horizontal line for(int i=0;i<ROWS;i++) { g.drawLine(x, y+i*span_y, FWidth-x,y+i*span_y); } //Draw vertical line for(int i=0;i<ROWS;i++) { g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y); } //Draw chess pieces for(int i=0;i<ROWS+1;i++) { for(int j=0;j<ROWS+1;j++) { if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) { //System.out.println("draw chessman "+i+" "+j); int pos_x=x+i*span_x; int pos_y=y+j*span_y; float radius_b=40; float radius_w=80; float[] fractions = new float[]{0f,1f}; java.awt.Color[] colors_b = new java.awt.Color[]{Color.BLACK,Color.WHITE}; Color[] colors_w = new Color[]{Color.WHITE,Color.BLACK}; RadialGradientPaint paint; if(chessStatus[i][j].getColor()==1) { //System.out.println("draw white chess"); paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w); }else{ //System.out.println("draw black chess"); paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b); } ((Graphics2D)g).setPaint(paint); ((Graphics2D)g).fillOval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width); } } } } @Override //Public void mousePressed(MouseEvent e) { int point_x=e.getX(); int point_y=e.getY(); int imgWidth = boardImg.getHeight(this); int imgHeight = boardImg.getWidth(this); int FWidth = getWidth(); int FHeight= getHeight(); int x=(FWidth-imgWidth)/2; int y=(FHeight-imgHeight)/2; int span_x=imgWidth/ROWS; int span_y=imgHeight/ROWS; //System.out.println("press"); int status_x = 0; int status_y = 0; if(point_x>=x && point_x<=x+imgWidth && point_y>=y && point_y <= y+imgHeight) { //System.out.println("Legal"); for(int i=0;i<ROWS+1;i++) { if(point_x>=x-chessman_width/2+1+i*span_x) { if(point_x<=x+chessman_width/2-1+i*span_x)//If it is width/2, two matching values will appear at the middle point{ //System.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; } } } for(int i=0;i<ROWS+1;i++) { if(point_y>=y-chessman_width/2+1+i*span_y) { if(point_y <= y+chessman_width/2-1+i*span_y) { //System.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_y = i; } } } Chessman chessman = new Chessman(BLACK, true); chessStatus[status_x][status_y]=chessman; repaint(); if(isWin(status_x, status_y, chessStatus)) { System.out.println("WIN!!!!"); } } } @Override //Public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } boolean isWin(int point_x,int point_y,Chessman[][] cm) { for(int i=0;i<ROWS+1;i++) { for(int j=0;j<ROWS+1;j++) { //Live horizontal search if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) { //Look for(int n=1;n<=4;n++) { if((i+n>=0)&&(i+n)<=ROWS) { if(chessStatus[i+n][j]!=null&&chessStatus[i+n][j].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" right count++:"+(i+n)+" "+j+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { break; } } } //Look for(int to the left n=1;n<=4;n++) { if((in>=0)&&(in)<=ROWS) { if(chessStatus[in][j]!=null&&chessStatus[in][j].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" "+"left count++:"+(in)+" "+j+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { if(chessStatus[in][j]!=null) { chessStatus[i][j].matchCount = 1; } break; } } } } chessStatus[i][j].matchCount=1;//refresh count } } } for(int i=0;i<ROWS+1;i++) { for(int j=0;j<ROWS+1;j++) { //Perpendicular if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) { //Search downward, the upper left corner is the coordinate origin, and the positive direction of the y-axis is downward for(int n=1;n<=4;n++) { if((j+n>=0)&&(j+n)<=ROWS) { if(chessStatus[i][j+n]!=null&&chessStatus[i][j+n].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" up count++:"+(i)+" "+(j+n)+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { break; } } } //Look up for(int n=1;n<=4;n++) { if((jn>=0)&&(jn)<=ROWS) { if(chessStatus[i][jn]!=null&&chessStatus[i][jn].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" "+"left count++:"+(i)+" "+(jn)+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { if(chessStatus[i][jn]!=null) { chessStatus[i][j].matchCount = 1; } break; } } } } chessStatus[i][j].matchCount=1;//refresh count } } } // Direction: upper left and lower right for(int i=0;i<ROWS+1;i++) { for(int j=0;j<ROWS+1;j++) { //Upper left if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) { //Look down, the upper left corner is the coordinate origin, and the positive direction of the y-axis is downward for(int n=1;n<=4;n++) { if((jn>=0)&&(jn)<=ROWS&&(in)>=0&&(in)<=ROWS) { if(chessStatus[in][jn]!=null&&chessStatus[in][jn].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" up count++:"+(in)+" "+(jn)+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { break; } } } //Lower right for(int n=1;n<=4;n++) { if((j+n>=0)&&(j+n)<=ROWS&&(i+n)>=0&&(i+n)<=ROWS) { if(chessStatus[i+n][j+n]!=null&&chessStatus[i+n][j+n].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" "+"left count++:"+(i+n)+" "+(j+n)+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { if(chessStatus[i+n][j+n]!=null) { chessStatus[i][j].matchCount = 1; } break; } } } } chessStatus[i][j].matchCount=1;//refresh count } } } // Direction: lower left upper right upper for(int i=0;i<ROWS+1;i++) { for(int j=0;j<ROWS+1;j++) { // lower left if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) { //Look down, the upper left corner is the coordinate origin, the positive direction of the y-axis is down for(int n=1;n<=4;n++) { if((j+n>=0)&&(j+n)<=ROWS&&(in)>=0&&(in)<=ROWS) { if(chessStatus[in][j+n]!=null&&chessStatus[in][j+n].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" up count++:"+(in)+" "+(j+n)+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { break; } } } //Upper right for(int n=1;n<=4;n++) { if(jn>=0)&&(jn)<=ROWS&&(i+n)>=0&&(i+n)<=ROWS) { if(chessStatus[i+n][jn]!=null&&chessStatus[i+n][jn].getPlaced()==true) { chessStatus[i][j].matchCount++; System.out.println("pos:"+i+" "+j+" "+"left count++:"+(i+n)+" "+(jn)+" count:"+chessStatus[i][j].matchCount); if(chessStatus[i][j].matchCount==5) { return true; } }else { if(chessStatus[i+n][jn]!=null) { chessStatus[i][j].matchCount = 1; } break; } } } chessStatus[i][j].matchCount=1;//refresh count } } } return false; } }Run it again
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.