Los dos artículos anteriores: Java implementa dos juegos de Gozi (dos) para dibujar un tablero de ajedrez; Java implementa dos juegos de Gozi (dos) para dibujar una pieza de ajedrez; Java implementa dos juegos de Gozi (cuatro) para darse cuenta del movimiento de dos juegos de Gozi (dos) para verlo.
Ya hemos dibujado tableros de ajedrez y piezas de ajedrez antes y podemos hacer movimientos libremente. La siguiente función a realizar es determinar si hay cinco perlas consecutivas (el juego de ajedrez no se considera por el momento).
Utilizamos la posición transversal donde el tablero ya ha aterrizado y verificamos si hay cinco piezas de ajedrez consecutivas en cualquier dirección de sus cuatro direcciones: arriba y abajo, izquierda y derecha, inferior a la izquierda y abajo a la derecha.
El primer paso es transformar la clase de piezas de ajedrez. Anteriormente, nuestra clase de ajedrez solo tenía información de color y estado de caída. Ahora necesitamos agregar datos de tipo INT para registrar cuántas perlas se sabe actualmente por ser continuas durante el proceso de transferencia.
Chessman.java
paquete xchen.test.simplegoBang; Public Class Chessman {private int color; // 1-White, 0-Black Private Boolean colocado = falso; int matchCount = 1; Public Chessman (int color, boolean colocado) {this.color = color; this. -colgado = colocado; } public boolean getplaced () {return colocado; } public void setplaced (boolean colocado) {this.placed = colocado; } public int getColor () {return color; } public void setColor (int color) {this.color = color; }}El segundo paso es determinar primero si hay cinco cuentas consecutivas desde una dirección, y usar las direcciones izquierda y derecha como un intento aquí.
Se ha agregado una función de Iswin para hacer un juicio ganador atravesando piezas efectivas en todo el tablero.
DrawChessboard.java
paquete 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.mousevent; import java.awt.event.mouselistener; import java.awt.color; import javax.swing.jpanel; Public Class DrawChessboard extiende las implementaciones de JPanel MouseListener {final static int black = 0; Final static int white = 1; public int ChessColor = Black; int Chessman_Width = 30; Imagen pública Boardimg; Final privado int filas = 19; Chessman [] [] Chessstatus = nuevo Chessman [filas+1] [filas+1]; public drawChessboard () {boardImg = Toolkit.getDefaultToolkit (). getImage ("res/drawable/Chessboard2.png"); if (boardimg == null) system.err.println ("Png no existe"); addMouselistener (esto); } @Override Protected void PaintComponent (Graphics g) {// TODO Método generado automático 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/filas; int span_y = imgheight/filas; G.Drawimage (Boardimg, X, Y, Null); // Dibuja la línea horizontal para (int i = 0; i <filas; i ++) {g.drawline (x, y+i*span_y, fwidth-x, y+i*span_y); } // dibujar línea vertical para (int i = 0; i <shows; i ++) {g.drawline (x+i*span_x, y, x+i*span_x, fheight-y); } // Dibuja piezas de ajedrez para (int i = 0; i <filas+1; i ++) {for (int j = 0; j <shows+1; j ++) {if (thessStatus [i] [j]! = Null && ChessStatus [i] [j] .getPlaced () == true) {//system.out.preintln("DRAW Chessman "+I; int pos_x = x+i*span_x; int pos_y = y+j*span_y; flotante radio_b = 40; flotante radio_w = 80; flotante [] fracciones = new float [] {0f, 1f}; java.awt.color [] colores_b = new java.awt.color [] {color.black, color.white}; Color [] colores_w = nuevo color [] {color.white, color.black}; Pintura radialgradientPaint; if (ChessStatus [i] [j] .getColor () == 1) {//system.out.println("draw White Chess "); pintar = nuevo RadialGradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fracciones, colores_w); } else {//system.out.println("Draw Black Achess "); pintura = nuevo RadialGradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fracciones, colores_b); } ((Graphics2d) g) .SetPaint (pintura); ((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/filas; int span_y = imgheight/filas; //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 <filas+1; i ++) {if (point_x> = x-chessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*span_x) // si es width/2, dos valores coincidentes aparecerán en el punto medio {//system.out. "+Point_X+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; }}} para (int i = 0; i <filas+1; i ++) {if (point_y> = y-chessman_width/2+1+i*span_y) {if (punto_y <= y+chessman_width/2-1+i*span_y) {//system.println(( "+(Y-CHESSMAN_WIDTH/2+1+I*SPAN_Y)+" "+(Y+CHESSMAN_WIDTH/2-1+I*SPAN_Y)); status_y = i; }}} Chessman Chessman = New Chessman (negro, verdadero); Chessstatus [status_x] [status_y] = Chessman; repintado (); if (iswin (status_x, status_y, ChessStatus)) {System.out.println ("Win!"); }}} @Override // public void mouseClicked (mouseEvent e) {// toDO metod de método generado automático} @Override public void Mousereledesed (MouseeVent e) {// TODO Auto-Generado Método Generado} @Override Public Void MouseEnedered (mouseEvent e) {// ado au @Override public void mouseentered (MouseEvent E) {// TODO Auto Generado Método STUB} @Override public void MouseExited (MouseEvent E) {// TODO Auto Generado Método STUB} boolean iswin (int Point_X, int Point_y, CHESSMAN [] [] cm) {// Int MatchCount the///////////////////////////////////////Registro de Registro de 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); // Busque (int n = 1; n <= 4; n ++) {if ((i+n> = 0) && (i+n) <= shows) {if (thessstatus [i+n] [j]! = Null && thessstatus [i+n] [j] .getPlaced () == true) {Chessstatus [i] [j] .matchCount; System.out.println ("pos:"+i+""+j+"recuento correcto ++:"+(i+n)+""+j+"recuento:"+ChessStatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // busque (int n = 1; n <= 4; n ++) {if (in> = 0) && (in) <= shows) {if (ChessStatus [in] [j]! = Null && ChessStatus [in] [j] .getplaced () == true) {Chessstatus [i] [j] .matchCount; System.out.println ("Pos:"+i+""+j+""+"Conteo izquierdo ++:"+(in)+""+j+"Conde:"+Chessstatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {if (ChessStatus [in] [j]! = Null) {ChessStatus [i] [J] .matchCount = 1; } romper; }}} ChessStatus [i] [j] .matchCount = 1; // Cuento de actualización}} return false; }}}}Paso 3 : El módulo principal permanece sin cambios. Ejecute y pruebe si nuestro algoritmo es correcto.
Main.java
paquete xchen.test.simplegoBang; import java.awt.container; import javax.swing.jframe; importar xchen.test.SimpleGobang.DrawChessboard; Public Class Main extiende JFrame {DrawChessboard private DrawChessboard; public main () {drawChessboard = new DrawChessboard (); // Marco de título Settitle ("Stand-Alone goji"); Contenedor contenedorpane = getContentPane (); ContainerPane.Add (DrawChessboard); } public static void main (string [] args) {main m = new main (); M.Setsize (800, 800); M.SetVisible (verdadero); }}Paso 4 : Ahora que hemos hecho un juicio en una dirección, completaremos el código de juicio en las otras tres direcciones.
Complementar la función iswin () en DrawChessboard.java
paquete 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.mousevent; import java.awt.event.mouselistener; import javax.swing.jpanel; Public Class DrawChessboard extiende las implementaciones de JPanel MouseListener {final static int black = 0; Final static int white = 1; public int ChessColor = Black; int Chessman_Width = 30; Imagen pública Boardimg; Final privado int filas = 19; Chessman [] [] Chessstatus = nuevo Chessman [filas+1] [filas+1]; public drawChessboard () {boardImg = Toolkit.getDefaultToolkit (). getImage ("res/drawable/Chessboard2.png"); if (boardimg == null) system.err.println ("Png no existe"); addMouselistener (esto); } @Override Protected void PaintComponent (Graphics g) {// TODO Método generado automático 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/filas; int span_y = imgheight/filas; G.Drawimage (Boardimg, X, Y, Null); // Dibuja la línea horizontal para (int i = 0; i <filas; i ++) {g.drawline (x, y+i*span_y, fwidth-x, y+i*span_y); } // dibujar línea vertical para (int i = 0; i <shows; i ++) {g.drawline (x+i*span_x, y, x+i*span_x, fheight-y); } // Dibuja piezas de ajedrez para (int i = 0; i <filas+1; i ++) {for (int j = 0; j <shows+1; j ++) {if (thessStatus [i] [j]! = Null && ChessStatus [i] [j] .getPlaced () == true) {//system.out.preintln("DRAW Chessman "+I; int pos_x = x+i*span_x; int pos_y = y+j*span_y; flotante radio_b = 40; flotante radio_w = 80; flotante [] fracciones = new float [] {0f, 1f}; java.awt.color [] colores_b = new java.awt.color [] {color.black, color.white}; Color [] colores_w = nuevo color [] {color.white, color.black}; Pintura radialgradientPaint; if (ChessStatus [i] [j] .getColor () == 1) {//system.out.println("draw White Chess "); pintar = nuevo RadialGradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fracciones, colores_w); } else {//system.out.println("Draw Black Achess "); pintura = nuevo RadialGradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fracciones, colores_b); } ((Graphics2d) g) .SetPaint (pintura); ((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/filas; int span_y = imgheight/filas; //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 <filas+1; i ++) {if (point_x> = x-chessman_width/2+1+i*span_x) {if (point_x <= x+chessman_width/2-1+i*span_x) // si es width/2, dos valores coincidentes aparecerán en el punto medio {//system.out. "+Point_X+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; }}} para (int i = 0; i <filas+1; i ++) {if (point_y> = y-chessman_width/2+1+i*span_y) {if (punto_y <= y+chessman_width/2-1+i*span_y) {//system.println(( "+(Y-CHESSMAN_WIDTH/2+1+I*SPAN_Y)+" "+(Y+CHESSMAN_WIDTH/2-1+I*SPAN_Y)); status_y = i; }}} Chessman Chessman = New Chessman (negro, verdadero); Chessstatus [status_x] [status_y] = Chessman; repintado (); if (iswin (status_x, status_y, ChessStatus)) {System.out.println ("Win!"); }}} @Override // public void mouseClicked (mouseEvent e) {// toDO metod de método generado automático} @Override public void Mousereledesed (MouseeVent e) {// TODO Auto-Generado Método Generado} @Override Public Void MouseEnedered (mouseEvent e) {// ado au @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 <filas+1; j ++) {// live horizontal search if (thessStatus [i] [j]! = null && ChessStatus [i] [j] .getPlaced () == true) {// busca (int n = 1; n <= 4; n ++) {if ((i+n> = 0) && (i+n) if (ChessStatus [i+n] [j]! = Null && ChessStatus [i+n] [j] .getplaced () == true) {ChessStatus [i] [j] .matchCount ++; System.out.println ("pos:"+i+""+j+"recuento correcto ++:"+(i+n)+""+j+"recuento:"+ChessStatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // Busque (int a la izquierda n = 1; n <= 4; n ++) {if ((in> = 0) && (in) <= Rows) {if (ChessStatus [in] [j]! = Null && ChessStatus [in] [j] .getPlaced () == true) {Chessstatus [i] [j]. System.out.println ("Pos:"+i+""+j+""+"Conteo izquierdo ++:"+(in)+""+j+"Conde:"+Chessstatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {if (ChessStatus [in] [j]! = Null) {ChessStatus [i] [J] .matchCount = 1; } romper; }}}} ChessStatus [i] [j] .matchCount = 1; // Count de actualización}}} para (int i = 0; i <filas+1; i ++) {para (int j = 0; j <filas+1; j ++) {// perpendicular if (ChessStatus [i] [j]! = Null && ChessStatus [i] [j] .getplaced () == true) {// Buscar hacia abajo, la esquina superior izquierda es el origen coordinado, y la dirección positiva del eje y es hacia abajo (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) 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)+"Conde:"+Chessstatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // busca (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= shows) {if (ChessStatus [i] [jn]! = Null && ChessStatus [i] [jn] .getplaced () ==) {ChessStatus [i] [j]. System.out.println ("Pos:"+i+""+j+""+"Conteo izquierdo ++:"+(i)+""+(Jn)+"Conde:"+ChessStatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {if (ChessStatus [i] [jn]! = Null) {ChessStatus [i] [j] .matchCount = 1; } romper; }}}}} Chessstatus [i] [j] .matchCount = 1; // recuento de refrescos}}} // dirección: superior izquierda e inferior derecha para (int i = 0; i <filas+1; i ++) {para (int j = 0; j <filas+1; j ++) {// superior izquierdo if (ChessStatus [i] [j]! = Null && ChessStatus [i] [j] .getplaced () == true) {// Mira hacia abajo, la esquina superior izquierda es el origen coordinado, y la dirección positiva del eje y es hacia abajo para (int n = 1; n <= 4; n ++) { if ((jn> = 0) && (jn) <= Rows && (in)> = 0 && (in) <= shows) {if (ChessStatus [in] [jn]! = Null && ChessStatus [in] [jn] .getPlaced () == true) {Chessstatus [i] [j] .matchCount ++; System.out.println ("Pos:"+i+""+j+"Up Count ++:"+(in)+""+(Jn)+"Conde:"+Chessstatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // inferior a la derecha para (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= filas && (i+n)> = 0 && (i+n) <= filas) { if (ChessStatus [i+n] [j+n]! = null && chessstatus [i+n] [j+n] .getplaced () == true) {ChessStatus [i] [j] .matchCount ++; System.out.println ("Pos:"+i+""+j+""+"Conteo izquierdo ++:"+(i+n)+""+(j+n)+"Conde:"+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; } romper; }}}}} ChessStatus [i] [j] .matchCount = 1; // Cuento de refrescos}}} // Dirección: Bajo esencia superior izquierda superior derecha para (int i = 0; i <filas+1; i ++) {para (int j = 0; j <filas+1; j ++) {// baja izquierda izquierda if (ChessStatus [i] [j]! = Null && ChessStatus [i] [j] .getPlaced () == true) {// Mira hacia abajo, la esquina superior izquierda es el origen coordinado, la dirección positiva del eje y es baja para (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)+"Conde:"+Chessstatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // superior a la derecha para (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]. Chessstatus [i] [j] .MatchCount ++; System.out.println ("Pos:"+i+""+j+""+"Conteo izquierdo ++:"+(i+n)+""+(Jn)+"Conde:"+ChessStatus [i] [j] .matchCount); if (ChessStatus [i] [j] .matchCount == 5) {return true; }} else {if (ChessStatus [i+n] [jn]! = null) {ChessStatus [i] [j] .matchCount = 1; } romper; }}} ChessStatus [i] [j] .matchCount = 1; // cuento de actualización}}} return false; }}Ejecutarlo de nuevo
Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.