Les exemples de cet article partagent le code spécifique du jeu Java Sudoku pour votre référence. Le contenu spécifique est le suivant
Le jeu Sudoku que j'ai écrit moi-même a 9 niveaux au total, et le code est le suivant:
1. La classe Doshudu est utilisée pour générer des tableaux sudoku
import java.util.random; classe publique Doshudu {/ ** * @param args * / public static void main (String [] args) {// TODO Méthode générée automatiquement Stub int [] [] CelL = newshudu (); // cellules = ChangeShu (cellules, 9); pour (int k = 0; k <9; k ++) {pour (int i = 0; i <9; i ++) {System.out.print (cellules [k] [i]); } System.out.println (); } } public static int[][] newshudu(){ int[][] cells=new int[][]{ {1,2,3,4,5,6,7,8,9}, {4,5,6,7,8,9,1,2,3}, {7,8,9,1,2,3,4,5,6}, Ot {9,7,8,3,1,2,6,4,5}}; int counth = new Random (). NextInt (10); pour (int k = 0; k <counth; k ++) {cells = linetolie (cellules); } int count = 0; pour (int k = 0; k <12; k ++) {count = new random (). NextInt (9); Cellules = Changeline (cellules, nombre); } int counth2 = new Random (). NextInt (10); pour (int k = 0; k <counth2; k ++) {cellules = linetolie (cellules); } cellules de retour; } public static int [] [] changeline (int [] [] cellules, int m) {// échange de lignes et de ligne int n = m; int [] temp = new int [9]; n = ((m + 3)> = 9)? (m + 3-9): m + 3; pour (int j = 0; j <9; j ++) {temp [j] = cellules [m] [j]; Cellules [M] [J] = cellules [n] [J]; Cellules [n] [j] = temp [J]; } cellules de retour; } public static int [] [] linetolie (int [] [] cellules) {// Ligne et colonne échange int temp = 0; pour (int j = 0; j <9; j ++) {pour (int k = j + 1; k <9; k ++) {temp = celaires [k] [j]; cellules [k] [j] = cellules [J] [k]; Cellules [J] [k] = temp; }} cellules de retour; }} 2. Classe d'opération d'interface
import java.awt.button; import java.awt.color; import java.awt.flowlayout; Importer java.awt.font; import java.awt.gridLayout; Importer java.awt.point; import java.awt.textfield; Importer java.awt.event.mousEadapter; Importer java.awt.event.mousevent; Importer java.awt.event.mousmotionAdapter; import java.awt.event.textevent; import java.awt.event.textListener; import java.util.random; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import com.sun.awt.awtutilities; La classe publique Sudoku étend JFrame {final privé TextField [] [] txtGame; static int num = 20; // nombre d'espaces vierges static int guan = 5; // nombre de niveaux static int compd = 5; // nombre d'espaces vierges ajoutés après avoir quitté le public statique void main (String [] args) {sudoku shudu = new sudoku (); } public sudoku () {// mise en page initial de jframe et paramètres d'écoute txtgame = new TextField [9] [9]; // Créer 81 objets TextField Doshudu Shudu = new Doshudu (); int [] [] cellules = shudu.getshudu (); // obtenir le tableau sudoku final jpanel jpl = new jpanel (); // créer un objet JPanel final int spacenum = num; // spacenum signifie le nombre de texte de texte vierge qui doivent être définis jpl.setlayout (new Gridlayout (9, 9)); // jpanel newing final int [] lacet-celber = 9)); int [9] [9]; // Réponse au système de tableau Sudoku.ArrayCopy (Cell, 0, Cellan, 0, Cells.length); // La réponse est de la copie du tableau Sudoku créée pour (int i = 0; i <9; i ++) {// imprime la réponse de la console pour (int j = 0; j <9; j ++) {System.out.out.print (cellulan [i]); } System.out.println (); } // fin d'impression this.setSize (600, 600); this.setResiSable (false); this.settitle ("Niveau du jeu 9 de l'Allemagne-suku de Dark Horse-Li"); for (int i = 0; i <9; i ++) {for (int j = 0; j <9; j ++) {txtgame [i] [j] = new textfield (); // Définit la couleur d'arrière-plan de TextField if ((i <3 && j <3) || (i <6 && i> = 3 && j> = 3 && j <6) || (i <9 && i> = 6 && j> = 6 && j <9)) {txtgame [i] [j] .setbackground (colorange); } if ((i <6 && i> = 3 && j <3) || (i <3 && j> = 6 && j <9) || (i <9 && i> = 6 && j> = 3 && j <6)) {txtgame [i] [j] .setbackground (colore.green); } if ((i <9 && i> = 6 && j <6)) {txtgame [i] [j] .setbackground (colore.green); } if ((i <9 && i> = 6 && j <6)) {txtgame [i] [j] .setbackground (colore.green); } if ((i <9 && i> = 6 && j <3) || (i <3 && j> = 3 && j <6) || (i <6 && i> = 3 && j <9 && j> = 6)) {txtgame [i] [j] .setbackground (couleur.pink); } txtgame [i] [j] .setfont (new Font ("Dialog", font.center_baseline, 60)); // définir la taille de la police txtgame [i] [j] .setText (Integer.ToString (Celles [i] [j])); txtgame [i] [j] .setEnabled (false); txtgame [i] [j] .setVisible (true); jpl.add (txtgame [i] [j]); jpl.setVisible (true); }} final int [] [] temparray = new int [spacenum] [2]; final jframe jfm = new JFrame ("SELECT NUMBER"); // annuler le titre JFrame jfm.SetUndeCorated (true); // Ajouter la fonction de glisser JFrame final Point Origin = new Point (); JFM.AddMouseSeListener (new MousEadapter () {public void Mousdressress (MouseEvent e) {Origin.x = e.getx (); origin.y = e.gety ();}}); JFM.AddMousMotionListener (new MousmotionAdapter () {public void MouseDragged (Mouseevent E) {Point p = jfm.getLocation (); jfm.setLocation (px + e.getx () - origin.x, py + e.gety () - origin.y);}}); // Définit JFrame sur translucide awtutilities.setwindowopacity (jfm, 0,7f); jpanel final jpnl = nouveau jpanel (nouveau gridLayout (3, 3)); JFM.SetLayout (null); JFM.SetSize (190, 200); JFM.SetResiSable (false); jpnl.setBounds (0, 0, 190, 120); JFM.SetResiSable (false); for (int i = 0; i <spacenum; i ++) {// set textfield pour être vide au hasard en fonction du nombre de champs de texte vierges qui doivent être finaux int rand1 = new random (). nextInt (9); final int rand2 = new random (). nextInt (9); temparray [i] [0] = rand1; temparray [i] [1] = rand2; txtgame [rand1] [rand2] .setText (""); if ((rand1 <3 && rand2 <3) || (rand1 <6 && rand1> = 3 && rand2> = 3 && rand2 <6) || (rand1 <9 && i> = 6 && rand2> = 6 && rand2 <9)); } if ((rand1 <6 && rand1> = 3 && rand2 <3) || (rand1 <3 && rand2> = 6 && rand2 <9) || (rand1 <9 && rand1> = 6 && rand2> = 3 && rand2 <6)) {txtgame [rand1] [rand2] .setbackground (colore.Green); } if ((rand1 <9 && rand1> = 6 && rand2 <3) || (rand1 <9 && rand1> = 6 && rand2 <3) || (rand1 <1 <10 && rand1> = 6 && rand2 <3) || (rand1 <100 3 && rand2> = 3 && rand2 <6) || (rand1 <6 && rand1> = 3 && rand2 && rand2 <9 & °))) {txtgame [rand1] [rand2] .setbackground (colore.pink); } txtgame [rand1] [rand2] .AddMouseListener (new MouseAdapter () {public void MouseClicked (Mouseevent Mousevent) {jfm.getContentPane (). RemiveAll (); // RETRAITEM Button (f + 1) + ""); Méthode générée automatique Btndel.setbackground (Color.Red); Méthode générée par TODI txtgame [rand1] [rand2] .addTextListener (new TextListener () {// ajouter un écouteur à la carte vide, comparez les réponses après les changements de valeur, imprimez "GOOD" dans la console si toutes les réponses sont correctes @Override public void textValuechanged (textevent e) {TextField tmp = (TextField) E.GetSource (); Spacenum; u ++) {if (txtgame [temparray [u] [0]] [temparray [u] [1]] .getText ()) .equals (entier. JPL.REMOVEALL (); Mouseclicked (MousEevent E) {// TODE GÉNÉRAGE AUTRE jpl.updateUi (); System.out.println ("bon"); txtgame [rand1] [rand2] .setEnabled (true); } this.add (jpl); this.setVisible (true); }}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.