Los ejemplos en este artículo comparten el código específico del juego Java Sudoku para su referencia. El contenido específico es el siguiente
El juego de Sudoku que escribí tiene 9 niveles en total, y el código es el siguiente:
1. La clase Doshudu se usa para generar matrices de sudoku
import java.util.random; clase pública Doshudu { / ** * @param args * / public static void main (string [] args) {// tODO Método generado automático intu [] [] celdas = newShudu (); // celdas = Changeshu (células, 9); para (int k = 0; k <9; k ++) {for (int i = 0; i <9; i ++) {system.out.print (celdas [k] [i]); } System.out.println (); }} public static int [] [] newshudu () {int [] [] cells = new int [] [] {{1,2,3,4,5,6,7,8,9,9}, {4,5,6,7,8,9,1,2,3}, {7,8,9,1,2,3,4,6}, {2,3,1,1,5,6,44,4,98,9}, {5,6,4,4,8,9,7,2,2,3,1}, {8,9,9,7,2,3,3,6,4}, {9,7,8,3,1,2,6,4,5}}; int Counth = new Random (). NextInt (10); para (int k = 0; k <counh; k ++) {celdas = linetolie (celdas); } int count = 0; para (int k = 0; k <12; k ++) {count = new Random (). NextInt (9); celdas = cambiante (celdas, recuento); } int Counth2 = new Random (). NextInt (10); para (int k = 0; k <counth2; k ++) {celdas = linetolie (celdas); } celdas de retorno; } public static int [] [] cambiante (int [] [] celdas, int m) {// line y fila intercambio int n = m; int [] temp = new int [9]; n = ((m+3)> = 9)? (M+3-9): M+3; para (int j = 0; j <9; j ++) {temp [j] = celdas [m] [j]; células [m] [j] = células [n] [j]; células [n] [j] = temp [j]; } celdas de retorno; } public static int [] [] linetolie (int [] [] celdas) {// Línea y columna intercambio int temp = 0; para (int j = 0; j <9; j ++) {for (int k = j+1; k <9; k ++) {temp = cells [k] [j]; células [k] [j] = células [j] [k]; células [j] [k] = temp; }} celdas de retorno; }} 2. Clase de operación de interfaz
import java.awt.button; import java.awt.color; import java.awt.flowlayout; import java.awt.font; import java.awt.gridLayout; import java.awt.point; import java.awt.textfield; import java.awt.event.mouseadapter; import java.awt.event.mousevent; import java.awt.event.mousemotionAdapter; 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.awtutilidades; La clase pública sudoku extiende jframe {final de textfield privado [] [] txtgame; static int num = 20; // Número de espacios en blanco INTAT int Guan = 5; // Número de niveles static int add = 5; // Número de espacios en blanco agregados después de dejar el público void estático principal (string [] args) {sudoku shudu = new Sudoku (); } public suDoku () {// Diseño inicial de JFRame y configuración de escucha txtgame = new TextField [9] [9]; // Cree 81 TextField Objects Doshudu Shudu = new Doshudu (); int [] [] cells = shudu.getShudu (); // get sudoku array final jpanel jpl = new jpanel (); // crea el objeto jpanel final int spacenum = num; // spacenum significa el número de textios en blanco que deben configurar jpl.setLaTlout (new GridLayout (9, 9)); // Jpanel. int [9] [9]; // Responda a la matriz de sudoku System.ArrayCopy (Cells, 0, Cellan, 0, Cells.Length); // La respuesta es de la copia de matriz de Sudoku creada para (int i = 0; i <9; i ++) {// imprima la respuesta de la consola para (int j = 0; j <9; j ++) {System.print (Cellan [i] [i]; } System.out.println (); } // final de la impresión this.setDefaultCloseOperation (this.exit_on_close); this.setsize (600, 600); this.setResizable (falso); this. for (int i = 0; i <9; i ++) {for (int j = 0; j <9; j ++) {txtgame [i] [j] = new TextField (); // Establecer color de fondo 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 (color.orange); } 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 (color.green); } if ((i <9 && i> = 6 && j <6)) {txtgame [i] [j] .setbackground (color.green); } if ((i <9 && i> = 6 && j <6)) {txtgame [i] [j] .setbackground (color.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 (color.pink); } txtgame [i] [j] .setFont (nuevo font ("diálogo", font.center_baseline, 60)); // establece el tamaño de fuente txtgame [i] [j] .settext (integer.toString (celdas [i] [j])); txtgame [i] [j] .setenable (falso); txtgame [i] [j] .setVisible (true); jpl.add (txtgame [i] [j]); jpl.setVisible (verdadero); }} final int [] [] temparray = new int [Spacenum] [2]; JFRame final jfm = new Jframe ("Número de selección"); // Cancelar el título de JFrame JFM.SetUndeCorated (verdadero); // Agregar JFRame Drag Función Punto final origin = new Point (); jfm.AddMousElistener (new Mouseadapter () {public void Mousepressed (MouseEvent e) {origin.x = e.getx (); origen.y = e.gety ();}}); jfm.AddMousEmotionListener (New MouseMotionAdapter () {public void Mousedragged (MouseEvent E) {Point P = Jfm.GetLocation (); Jfm.SetLocation (px + e.getx () - origen.x, py + e.gety () - origen.y);}); // establecer jframe en translúcido awtutilidades.setwindowopacity (jfm, 0.7f); JPANEL final jpnl = nuevo JPanel (nuevo GridLayout (3, 3)); JFM.SetLayout (NULL); JFM.Setsize (190, 200); JFM.SetResizable (falso); jpnl.setBounds (0, 0, 190, 120); JFM.SetResizable (falso); for (int i = 0; i <spacenum; i ++) {// Establezca el campo de texto para estar vacío al azar en función del número de campos de texto en blanco que necesitan ser finales 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)) {txtgame [rand1] [rand2] .setbackground (color.orange); } 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 (color.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 &&&&DRY1> = 3 & 3 && Rand2 <9) {txtgame [rand1] [rand2] .setbackground (color.pink); } txtgame [rand1] [rand2] .AddMousElSistener (new Mooneadapter () {public void MouseClicked (MouseEvent MouseEvent) {jfm.getContentPane (). RemoveAll (); // todos los componentes jpnl.removeall (); para (int f = 0; f <9; f ++) eliminados Botón ((f + 1) + ""); TODO Método Generado Txtgame [rand1] [rand2] .settext (btn.getLabel () + txtgame [rand1] [rand2] .gettext ()) btndel.setbackground (color.red); TODO Método Generado Txtgame [rand1] [rand2] .settext (""); txtgame [rand1] [rand2] .AddTextListener (new TextListener () {// Agregue un oyente al campo de texto en blanco, compare las respuestas después de que cambie el valor, imprima "bueno" en la consola si todas las respuestas son correctas @Override public void void void void (textEvent e) {textField tmp = (textField) e.getSource (); intte = 0; Spacenum; JPL.REMOVEALL (); MouseClicked (MouseEvent E) {// TODO Método Generado jpl.updateui (); txtgame [rand1] [rand2] .SetEnabled (true); } this.Add (JPL); this.setVisible (verdadero); }}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.