Dos artículos anteriores: Java implementa dos juegos de Gozi (dos) para dibujar un tablero de ajedrez; Java implementa dos juegos de Gozi (tres) para dibujar una pieza de ajedrez; Java implementa dos juegos de Gozi (cuatro) para realizar el movimiento; Java implementa dos juegos de Gozi (seis) para transformar el lado del ajedrez, puedes hacer clic para ver.
La siguiente función que queremos implementar es solicitar información en la pantalla cuando haya cinco cuentas consecutivas. Simplemente use ShowMessedialog en la parte Iswin de la función mousepresada.
El código de función de mousePressed es el siguiente:
@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; }}} if (ChessStatus [status_x] [status_y] == NULL || CHESSSTATUS [STATE_X] [STATE_Y] .GETPLACED () == FALSE) {Chessman Chessman = new Chessman (ChessColor, True); Chessstatus [status_x] [status_y] = Chessman; System.out.println ("Color de ajedrez:"+ChessColor); if (ChessColor == Black) {ChessColor = White; } else {ChessColor = Black; } repint (); // Si gana, da un mensaje de inmediato si (iswin (status_x, status_y, thessstatus)) {system.out.println ("win!"); Ganador de cadenas; // Si el siguiente niño es blanco, entonces esta vez es negro si (ChessColor == White) ganador = "negro"; else ganador = "en blanco"; String mString = String.Format ("Felicitaciones, %s Win!", Ganador); JOptionPane.ShowMessEdialog (this, mString); }}}}Ejecutarlo:
En este punto, hemos completado la función gozi más básica. Publicaré el código completo a continuación:
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; }} 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); }} 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.JOptionPane; import javax.swing.jpanel; Public Class DrawChessboard extiende Jpanel implementa 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 (point_y <= y+chessman_width/2-1+i*span_y) {//system.println(`+"+i+"+punto_y+" "+(Y-CHESSMAN_WIDTH/2+1+I*SPAN_Y)+" "+(Y+CHESSMAN_WIDTH/2-1+I*SPAN_Y)); status_y = i; }}} if (ChessStatus [status_x] [status_y] == NULL || CHESSSTATUS [STATE_X] [STATE_Y] .GETPLACED () == FALSE) {Chessman Chessman = new Chessman (ChessColor, True); Chessstatus [status_x] [status_y] = Chessman; System.out.println ("Color de ajedrez:"+ChessColor); if (ChessColor == Black) {ChessColor = White; } else {ChessColor = Black; } repint (); // Si gana, da un mensaje de inmediato si (iswin (status_x, status_y, Chessstatus)) {System.out.println ("Win!"); Ganador de cadenas; // Si el siguiente niño es blanco, entonces esta vez es negro si (ChessColor == White) ganador = "negro"; else ganador = "blanco"; String mString = String.Format ("Felicitaciones, %s Win!", Ganador); JOptionPane.ShowMessEdialog (this, mString); }}}}} @Override // public void mouseClicked (mouseEvent e) {// toDO metodio de método generado automático} @Override public void mouserElimed (mouseEvent e) {// toDO metodio automático de método @Override } @Override public void mouseEndered (MouseEvent E) {// TODO Método Generado automático} @Override public void MouseExited (MouseEvent E) {// TODO Auto Método Generado stub} boolean iswin (int Point_X, int Point_y, Chessman [] [] CM) {For (Intas i = 0; i <filas+1; i ++) {for (int j = 0; j <filas+1; j ++) {// búsqueda de lado largo if (thessStatus [i] [j]! = null && chessstatus [i] [j] .getPleated () == true) {int coincho = chessstatus [i] [j] .getColor (); // 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+n] [j] .getColor () {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) <= Rows) {if (ChessStatus [in] [J]! = Null && ChessStatus [in] [J] .getPlaced () == True && CheSStatus [in]. 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; // 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) {int matchColor = ChessStatus [i] [j] .getColor (); // Mira hacia abajo, la esquina superior izquierda es el origen de la coordenada, la dirección positiva del eje y está abajo para (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= filas) { if (ChessStatus [i] [j+n]! = Null && ChessStatus [i] [j+n] .getplaced () == true && ChessStatus [i] [j+n] .getColor () == MatchColor) {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) <= filas) { if (ChessStatus [i] [jn]! = Null && ChessStatus [i] [jn] .getPlaced () == True && ChessStatus [i] [jn] .getColor () == MatchColor) {Chessstatus [i] [j] .matchCount ++; 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; // Cuento de actualización}}} // Dirección: superior izquierda e inferior derecha para (int i = 0; i <filas+1; i ++) {para (int j = 0; j <filas+1; j ++) {// arriba izquierda izquierda) if (ChessStatus [i] [j]! = Null && ChessStatus [i] [j] .getplaced () == true) {int matchColor = ChessStatus [i] [j] .getColor (); // Mira hacia abajo, la esquina superior izquierda es el origen de la coordenada, y la dirección positiva del eje y está abajo para (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= shows && (in)> = 0 && (in) <= rows) { if (ChessStatus [in] [jn]! = Null && ChessStatus [in] [jn] .getPlaced () == true && ChessStatus [in] [jn] .getColor () == MatchColor) {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; }}} // abajo a la derecha para (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= shows && (i+n)> = 0 && (i+n) <= filas) { if (ChessStatus [i+n] [j+n]! = Null && ChessStatus [i+n] [j+n] .getplaced () == true && ChessStatus [i+n] [j+n] .getColor () == Matchcolor) {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 izquierda superior derecha superior para (int i = 0; i <filas+1; i ++) {para (int j = 0; j <filas+1; j ++) {// inferior izquierda if (ChessStatus [i] [j]! = Null && ChessStatus [i] [j] .getplaced () == true) {int matchColor = ChessStatus [i] [j] .getColor (); // Mira hacia abajo, la esquina superior izquierda es el origen de la coordenada, y la dirección positiva del eje y está abajo 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 [in] [j+n] .getColor () == matchColor) {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) <= filas) { if (ChessStatus [i+n] [jn]! = Null && ChessStatus [i+n] [jn] .getplaced () == true && ChessStatus [i+n] [jn] .getColor () == Matchcolor) {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; }}Para juegos más emocionantes, consulte el tema especial "Java Classic Games"
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.