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 panneaux d'échecs et des pièces d'échecs, et pouvons faire librement des mouvements et juger s'il y a cinq perles consécutives dans les échecs. Le travail suivant consiste à échanger le côté d'échecs après chaque étape (c'est-à-dire que la couleur de la pièce suivante est blanche-> noir ou noir-> blanc), et ajouter les conditions de jugement de couleur au jugement gagnant (il y a cinq perles consécutives sur l'échecteur -> cinq perles consécutives de la même couleur sur le manche).
Le module principal Main.java et la classe d'échecs Chessman.java restent inchangés. Nous devons apporter des ajustements à la fonction soupçonnée et à la fonction de jugement à cinq perles Iswin dans Drawchessboard.java.
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); }} 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 première étape consiste à modifier la fonction soupçonnée de manière à ce qu'après chaque mouvement, le jeu d'échecs sera donné à l'adversaire (c'est-à-dire que la couleur de la pièce d'échecs sera modifiée à chaque fois que vous bougez)
La partie de la fonction soupçonnée dans Drawchessboard.java, tant que la valeur de Chesscolor est modifiée à chaque fois.
@Override // public void Mousdressressed (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 (ChessColor, true); ChessStatus [Status_X] [Status_y] = Chessman; System.out.println ("Couleur d'échecs:" + ChessColor); if (ChessColor == Black) {ChessColor = blanc; } else {ChessColor = Black; } repeain (); if (iswin (status_x, status_y, ChessStatus)) {System.out.println ("Win !!!"); }}}Courir
La deuxième étape consiste à ajouter des conditions de jugement de couleur au jugement gagnant (il y a cinq perles consécutives sur les échecs -> Il y a cinq perles consécutives de la même couleur sur l'échecteur)
Ajoutez le jugement de couleur de Chessman.getColor au jugement de la fonction Iswin
code de fonction iswin
Boolean iswin (int point_x, int point_y, Chessman [] [] cm) {for (int i = 0; i <lignes + 1; i ++) {for (int j = 0; j <lignes + 1; j ++) {// landwise search if (Chessstatus [i] [j]! = null && chessstatus [i] [j]. MatchColor = Chessstatus [i] [J] .getColor (); // 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 && chessstatu [i + n] [j] .getcol {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; }}} // recherche (int n = 1; n <= 4; n ++) {if (in> = 0) && (in) <= lignes) {if (Chessstatus [in] [j]! = Null && Chessstatus [in] [j] .getplaced () == true && chessstatus [in] [j] .getcolor 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}}} pour (int i = 0; i <lignes + 1; i ++) {pour (int j = 0; j <lignes + 1; j ++) {// longitudinal if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {int matchColor = Chessstatus [i] [j] .getColor (); // 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 ++) {if ((j + n> = 0) && (j + n) <= lignes) { if (Chessstatus [i] [j + n]! = null && Chessstatus [i] [j + n] .getplaced () == true && chessstatu [i] [j + n] .getColor () == matchColor) {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 && chessstatu [i] [jn] .getColor () == matchColor) {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; } 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) {int matchColor = Chessstatus [i] [j] .getColor (); // Regardez vers le bas, le coin supérieur gauche est l'origine de coordonnées, la direction positive de l'axe y est en baisse 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 && chessstatu [in] [jn] .getColor () == matchColor) {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 && chessstatu [i + n] [j + n] .getColor () == matchColor) {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) {int matchColor = Chessstatus [i] [j] .getColor (); // 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 ++) {if ((j + n> = 0) && (j + n) <= lignes && (in)> = 0 && (in) <= lignes) {in) {in)> = 0 && (in) <= lignes) {) {in) if (chessstatus [in] [j + n]! = null && Chessstatus [in] [j + n] .getplaced () == true && chessstatu [in] [j + n] .getColor () == matchColor) {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) <= lignes) { 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 + "" + "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!
Drawchessboard complet.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; }}} if (Chessstatus [status_x] [status_y] == null || Chessstatus [status_x] [status_y] .getplaced () == false) {Chessman Chessman = new Chessman (ChessColor, true); ChessStatus [Status_X] [Status_y] = Chessman; System.out.println ("Couleur d'échecs:" + ChessColor); if (ChessColor == Black) {ChessColor = blanc; } else {ChessColor = Black; } repeain (); 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 MouseLelent (Mouseevent E) {// TODO Méthode générée automatique void MouseExited (MouseEvent E) {// TODO Méthode générée automatiquement Stub} Boolean Iswin (int point_x, int point_y, Chessman [] [] cm) {for (int i = 0; i <rows + 1; i ++) {pour (int j = 0; j <rouns + 1; j ++) {// recherche terrestre if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {int matchColor = Chessstatus [i] [j] .getColor (); // 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 && chessstatu [i + n] [j] .getcol {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; }}} // recherche (int n = 1; n <= 4; n ++) {if (in> = 0) && (in) <= lignes) {if (Chessstatus [in] [j]! = Null && Chessstatus [in] [j] .getplaced () == true && chessstatus [in] [j] .getcolor 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}}} pour (int i = 0; i <lignes + 1; i ++) {pour (int j = 0; j <lignes + 1; j ++) {// longitudinal if (Chessstatus [i] [j]! = null && Chessstatus [i] [j] .getplaced () == true) {int matchColor = Chessstatus [i] [j] .getColor (); // 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 ++) {if ((j + n> = 0) && (j + n) <= lignes) { if (Chessstatus [i] [j + n]! = null && Chessstatus [i] [j + n] .getplaced () == true && chessstatu [i] [j + n] .getColor () == matchColor) {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 && chessstatu [i] [jn] .getColor () == matchColor) {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; } 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) {int matchColor = Chessstatus [i] [j] .getColor (); // Regardez vers le bas, le coin supérieur gauche est l'origine de coordonnées, la direction positive de l'axe y est en baisse 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 && chessstatu [in] [jn] .getColor () == matchColor) {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 && chessstatu [i + n] [j + n] .getColor () == matchColor) {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) {int matchColor = Chessstatus [i] [j] .getColor (); // 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 ++) {if ((j + n> = 0) && (j + n) <= lignes && (in)> = 0 && (in) <= lignes) {in) {in)> = 0 && (in) <= lignes) {) {in) 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) + "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) <= lignes) { 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 + "" + "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; }}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.