Les deux articles précédents: Java implémente deux gozi jeu (deux) pour dessiner un tableau d'échecs; Java implémente deux gozi jeu (deux) pour dessiner une pièce d'échecs; Java implémente deux gozi (quatre) pour réaliser le mouvement de deux matchs de Gozi (deux) pour le voir.
Nous avons déjà dessiné des échecs et des pièces d'échecs auparavant et pouvons faire librement des mouvements. La fonction suivante à réaliser est de déterminer s'il y a cinq billes consécutives (le jeu d'échecs n'est pas pris en compte pour le moment).
Nous utilisons la position de traversée où la planche a déjà atterri et vérifions s'il y a cinq pièces d'échecs consécutives dans n'importe quelle direction de ses quatre directions: haut et bas, gauche et droite, inférieur à gauche et en bas à droite.
La première étape consiste à transformer la classe d'échecs. Auparavant, notre classe d'échecs ne comptait que des informations sur les couleurs et le statut de chute. Maintenant, nous devons ajouter des données de type int pour enregistrer combien de perles sont actuellement connues pour être continues pendant le processus de traversée.
Chessman.java
Package xchen.test.simplegobang; classe publique Chessman {private int couleur; // 1-blanc, 0-noir privé boolean placé = false; int matchCount = 1; public Chessman (int couleur, booléen placé) {this.color = couleur; this.placed = placé; } public boolean getplace () {return placé; } public void setplaced (booléen placé) {this.placed = placé; } public int getColor () {return couleur; } public void setColor (int colore) {this.color = couleur; }}La deuxième étape consiste d'abord à déterminer s'il y a cinq billes consécutives à partir d'une direction et d'utiliser les directions gauche et droite comme tentative ici.
Une fonction ISWIN a été ajoutée pour porter un jugement gagnant en traversant des pièces efficaces sur l'ensemble du conseil d'administration.
Drawchessboard.java
Package xchen.test.simplegobang; import java.awt.graphics; import java.awt.graphics2d; Importer java.awt.radialgradientpaint; import java.awt.image; import java.awt.toolkit; Importer java.awt.event.mousevent; Importer java.awt.event.mouseListener; import java.awt.color; import javax.swing.jpanel; La classe publique Drawchessboard étend les implémentations JPanel MouseListener {final static int noir = 0; Final Static int blanc = 1; public int chesscolor = noir; int Chessman_Width = 30; Image publique BoardImg; lignes int privées finales = 19; Chessman [] [] Chessstatus = new Chessman [lignes + 1] [lignes + 1]; public drawchessboard () {boardimg = toolkit.getDefaultToolkit (). getImage ("Res / Drawable / Chessboard2.png"); if (boardimg == null) System.err.println ("png n'existe pas"); AddMouseListener (this); } @Override Protected void PaintComponent (Graphics G) {// TODO Méthode générée automatique 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 / lignes; int span_y = imgheight / lignes; G.DrawImage (BoardImg, X, Y, NULL); // dessine la ligne horizontale pour (int i = 0; i <lignes; i ++) {g.drawline (x, y + i * span_y, fwidth-x, y + i * span_y); } // dessiner une ligne verticale pour (int i = 0; i <lignes; i ++) {g.drawline (x + i * span_x, y, x + i * span_x, fheight-y); } // dessiner des pièces d'échecs pour (int i = 0; i <lignes + 1; i ++) {for (int j = 0; j <lignes + 1; j ++) {if (Chessstatus [i] [j]! = Null && Chessstatus [i] [j] .getplaced () == true) {//system.out.println("Draw chessman "+" + 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 [] couleurs_b = new java.awt.color [] {color.black, colore.white}; Couleur [] couleurs_w = new Color [] {Color.white, Color.Black}; Peinture radialgradientpaint; if (ChessStatus [i] [j] .getColor () == 1) {//system.out.println("raw échose blanc "); peinture = new RadialgradIentPaint (pos_x-chessman_width / 2f, pos_y-chessman_width / 2f, radius_w * 2, fractions, couleurs_w); } else {//system.out.println("raw échose noir "); peinture = new RadialgradIentPaint (pos_x-bessman_width / 2f, pos_y-bessman_width / 2f, radius_b * 2, fractions, couleurs_b); } ((Graphics2d) g) .setpaint (peinture); ((Graphics2d) g) .Filloval (pos_x-chessman_width / 2, pos_y-chessman_width / 2, Chessman_width, Chessman_width); }}}} @Override // public void Mousreprim (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 / lignes; int span_y = imgheight / lignes; //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 "); pour (int i = 0; i <lignes + 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 c'est width / 2, deux valeurs de correspondance apparaîtront au milieu {//System.out.Printlnwn. "+ Point_x +" "+ (x-chessman_width / 2 + i * span_x) +" "+ (x + Chessman_width / 2 + i * span_x)); status_x = i; }}} pour (int i = 0; i <lignes + 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 +" "" + "+" "+ (y-chessman_width / 2 + 1 + i * span_y) +" "+ (y + Chessman_width / 2-1 + i * span_y)); status_y = i; }}} Chessman Chessman = new Chessman (noir, vrai); ChessStatus [Status_X] [Status_y] = Chessman; repeindre(); if (iswin (status_x, status_y, ChessStatus)) {System.out.println ("Win !!!!"); }}} @Override // public void MouseClicked (Mouseevent E) {// Todo Méthode générée automatiquement Stub} @Override public void Mouselele tour (Mouseevent E) {// Todo Auto-généré par Stub} @Override Public Void Mouseented (Mousevent E) {// TODO Auto-généré par la méthode Auto-générée Auto-Generate Mouseentered (Mouseevent E) {// Todo Générations auto-générées Stub} @Override public void MouseExit (Mouseevent E) {// TODO Méthode générée automatiquement Stub} Boolean Iswin (int point_x, int point_y, Chessman [] [] CM) {// Intalcount = 1; // enregistrer le nombre de compats // HORIZETTAL (INTTALLAGE = 1; // enregistrer le numéro de réserve // i = 0; i <lignes + 1; i ++) {for (int j = 0; j <lignes + 1; j ++) {if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {//system.out.println(âtwin:"+i+ "" + j); // recherche (int n = 1; n <= 4; n ++) {if ((i + n> = 0) && (i + n) <= lignes) {if (Chessstatus [i + n] [j]! = Null && Chessstatus [i + n] [j] .getplaced () == true) {Chesssatus [i] [j]. System.out.println ("pos:" + i + "" + j + "Right Count ++:" + (i + n) + "" + j + "Count:" + Chessstatu [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {Break; }}} // recherche (int n = 1; n <= 4; n ++) {if (in> = 0) && (in) <= lignes) {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:" + Chessstatu [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {if (Chessstatus [in] [j]! = null) {Chessstatus [i] [j] .matchCount = 1; } casser; }}} Chessstatus [i] [j] .matchCount = 1; // Refresh Count}} return false; }}}}Étape 3 : Le module principal reste inchangé. Exécutez et testez si notre algorithme est correct.
Main.java
Package xchen.test.simplegobang; Importer Java.awt.Container; import javax.swing.jframe; import xchen.test.simplegobang.Drawchessboard; La classe publique Main étend JFrame {Private Drawchess Drawchessboard; public main () {drawchessboard = new Drawchessboard (); // Cadre Title Setttitle ("autonome goji"); Conteneur ContainerPane = getContentPane (); ContainerPane.Add (Drawchessboard); } public static void main (String [] args) {main m = new Main (); M.SetSize (800, 800); M.SetVisible (vrai); }}Étape 4 : Maintenant que nous avons porté un jugement dans une direction, nous terminerons le code du jugement dans les trois autres directions.
Complétant la fonction iswin () dans Drawchessboard.java
Package xchen.test.simplegobang; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; Importer java.awt.radialgradientpaint; import java.awt.toolkit; Importer java.awt.event.mousevent; Importer java.awt.event.mouseListener; import javax.swing.jpanel; La classe publique Drawchessboard étend les implémentations JPanel MouseListener {final static int noir = 0; Final Static int blanc = 1; public int chesscolor = noir; int Chessman_Width = 30; Image publique BoardImg; lignes int privées finales = 19; Chessman [] [] Chessstatus = new Chessman [lignes + 1] [lignes + 1]; public drawchessboard () {boardimg = toolkit.getDefaultToolkit (). getImage ("Res / Drawable / Chessboard2.png"); if (boardimg == null) System.err.println ("png n'existe pas"); AddMouseListener (this); } @Override Protected void PaintComponent (Graphics G) {// TODO Méthode générée automatique 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 / lignes; int span_y = imgheight / lignes; G.DrawImage (BoardImg, X, Y, NULL); // dessine la ligne horizontale pour (int i = 0; i <lignes; i ++) {g.drawline (x, y + i * span_y, fwidth-x, y + i * span_y); } // dessiner une ligne verticale pour (int i = 0; i <lignes; i ++) {g.drawline (x + i * span_x, y, x + i * span_x, fheight-y); } // dessiner des pièces d'échecs pour (int i = 0; i <lignes + 1; i ++) {for (int j = 0; j <lignes + 1; j ++) {if (Chessstatus [i] [j]! = Null && Chessstatus [i] [j] .getplaced () == true) {//system.out.println("Draw chessman "+" + 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 [] couleurs_b = new java.awt.color [] {color.black, colore.white}; Couleur [] couleurs_w = new Color [] {Color.white, Color.Black}; Peinture radialgradientpaint; if (ChessStatus [i] [j] .getColor () == 1) {//system.out.println("raw échose blanc "); peinture = new RadialgradIentPaint (pos_x-chessman_width / 2f, pos_y-chessman_width / 2f, radius_w * 2, fractions, couleurs_w); } else {//system.out.println("raw échose noir "); peinture = new RadialgradIentPaint (pos_x-bessman_width / 2f, pos_y-bessman_width / 2f, radius_b * 2, fractions, couleurs_b); } ((Graphics2d) g) .setpaint (peinture); ((Graphics2d) g) .Filloval (pos_x-chessman_width / 2, pos_y-chessman_width / 2, Chessman_width, Chessman_width); }}}} @Override // public void Mousreprim (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 / lignes; int span_y = imgheight / lignes; //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 "); pour (int i = 0; i <lignes + 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 c'est width / 2, deux valeurs de correspondance apparaîtront au milieu {//System.out.Printlnwn. "+ Point_x +" "+ (x-chessman_width / 2 + i * span_x) +" "+ (x + Chessman_width / 2 + i * span_x)); status_x = i; }}} pour (int i = 0; i <lignes + 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 +" "" + "+" "+ (y-chessman_width / 2 + 1 + i * span_y) +" "+ (y + Chessman_width / 2-1 + i * span_y)); status_y = i; }}} Chessman Chessman = new Chessman (noir, vrai); ChessStatus [Status_X] [Status_y] = Chessman; repeindre(); if (iswin (status_x, status_y, ChessStatus)) {System.out.println ("Win !!!!"); }}} @Override // public void MouseClicked (Mouseevent E) {// Todo Méthode générée automatiquement Stub} @Override public void Mouselele tour (Mouseevent E) {// Todo Auto-généré par Stub} @Override Public Void Mouseented (Mousevent E) {// TODO Auto-généré par la méthode Auto-générée Auto-Generate Mouseentered (Mouseevent E) {// Todo Méthode générée automatiquement Stub} @Override public void MouseExit (Mouseevent E) {// TODO Méthode générée automatique j = 0; j <lignes + 1; j ++) {// recherche horizontale en direct if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {// recherche (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 + "Right Count ++:" + (i + n) + "" + j + "Count:" + Chessstatu [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {Break; }}} // Recherchez (int à gauche n = 1; n <= 4; n ++) {if ((in> = 0) && (in) <= lignes) {if (Chessstatus [in] [j]! = Null && chessstatus [in] [j] .getplaced () == true) {Chesssatus [i] [j]. System.out.println ("pos:" + i + "" + j + "" + "Left Count ++:" + (in) + "" + j + "count:" + Chessstatu [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {if (Chessstatus [in] [j]! = null) {Chessstatus [i] [j] .matchCount = 1; } casser; }}} che if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getPlaced () == true) {// Recherche vers le bas, le coin supérieur gauche est l'origine de coordonnées, et la direction positive de l'axe y est vers le bas pour (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) + "count:" + Chessstatus [i] [j] .matchCount); if (Chessstatus [i] [j] .matchCount == 5) {return true; }} else {Break; }}} // Recherchez (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= lignes) {if (Chessstatus [i] [jn]! = Null && chessstatus [i] [jn] .getplaced () == true) {Chessstatus [i] [j]. 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; } casser; }}}} Chessstatus [i] [j] .matchCount = 1; // Refresh Count}}} // Direction: Upper gauche et inférieur à droite pour (int i = 0; i <lignes + 1; i ++) {pour (int j = 0; j <lignes + 1; j ++) {// supérieur gauche supérieur gauche if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getPlaced () == true) {// Regarde vers le bas, le coin supérieur gauche est l'origine coordonnée, et la direction positive de l'axe y est vers le bas pour (int n = 1; n <= 4; n ++) {a l'axe y est vers le bas pour (int n = 1; n <= 4; n ++) { if ((jn> = 0) && (jn) <= lignes && (in)> = 0 && (in) <= lignes) {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; }}} // inférieur à droite pour (int n = 1; n <= 4; n ++) {if ((j + n> = 0) && (j + n) <= lignes && (i + n)> = 0 && (i + n) <= lignes) { 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; } casser; }}}} Chessstatus [i] [J] .matchCount = 1; // Refresh Count}}} // Direction: inférieur gauche supérieure droite pour (int i = 0; i <lignes + 1; i ++) {pour (int j = 0; j <lignes + 1; j ++) {// inférieur gauche gauche if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {// Regarde vers le bas, le coin supérieur gauche est l'origine coordonnée, la direction positive de l'axe y est en panne pour (int n = 1; n <= 4; n ++) { if ((j + n> = 0) && (j + n) <= lignes && (in)> = 0 && (in) <= lignes) {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; }}} // supérieur à droite pour (int n = 1; n <= 4; n ++) {if (jn> = 0) && (jn) <= lignes && (i + n)> = 0 && (i + n) <= rows) {if (Chessstatus [i + n] [Jn]! = Null && chessstatue [i + n] [Jn]. 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; } casser; }}} Chessstatus [i] [j] .matchCount = 1; // Refresh Count}}} return false; }}Exécutez-le à nouveau
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.