Os exemplos deste artigo compartilham o código específico do jogo Java Sudoku para sua referência. O conteúdo específico é o seguinte
O jogo Sudoku que escrevi tem 9 níveis no total, e o código é o seguinte:
1. A classe Doshudu é usada para gerar matrizes sudoku
importar java.util.random; classe pública Doshudu { / ** * @param args * / public static void main (string [] args) {// TODO Method Auto-Generated Stub int [] [] células = Newshudu (); // células = changeshu (células, 9); for (int k = 0; k <9; k ++) {for (int i = 0; i <9; i ++) {System.out.print (células [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}, {2,3,1,5,6,4,8,9,7}, {5,6,4,8,9,7,2,3,1}, {8,9,7,2,3,1,5,6,4}, {3,1,2,6,4,5,9,7,8}, {6,4,5,9,7,8,3,1,2}, {9,7,8,3,1,2,6,4,5}}; int COUNth = new Random (). NextInt (10); for (int k = 0; k <Counth; k ++) {células = linetolie (células); } int count = 0; for (int k = 0; k <12; k ++) {count = new Random (). NextInt (9); células = trânsito (células, contagem); } int couth2 = new Random (). NextInt (10); for (int k = 0; k <Couth2; k ++) {células = linetolie (células); } Retornar células; } public static int [] [] Chankine (int [] [] células, int m) {// linha e troca de linha int n = m; int [] temp = new int [9]; n = ((m+3)> = 9)? (M+3-9): M+3; for (int j = 0; j <9; j ++) {temp [j] = células [m] [j]; células [m] [j] = células [n] [j]; células [n] [j] = temp [j]; } Retornar células; } public static int [] [] linetolie (int [] [] células) {// linha e coluna Exchange int temp = 0; for (int j = 0; j <9; j ++) {for (int k = j+1; k <9; k ++) {temp = células [k] [j]; células [k] [j] = células [j] [k]; células [j] [k] = temp; }} retornar células; }} 2. Classe de operação da interface
importar java.awt.button; importar java.awt.color; importar java.awt.flowlayout; importar java.awt.font; importar java.awt.gridlayout; importar java.awt.point; importar java.awt.textfield; importar java.awt.event.mouseAdapter; importar java.awt.event.mouseevent; importar java.awt.event.mousemotionAdapter; importar java.awt.event.textevent; importar java.awt.event.textListener; importar java.util.random; importar javax.swing.jframe; importar javax.swing.jlabel; importar javax.swing.jpanel; importação com.sun.awt.awtutities; classe pública sudoku estende o jframe {final privado textfield [] [] txtgame; estático int num = 20; // Número de espaços em branco estático int guan = 5; // número de níveis estático int add = 5; // número de espaços em branco adicionados após deixar o vazio estático público principal (string [] args) {sudoku shudu = new sudoku (); } public sudoku () {// layout inicial de jframe e configurações de escuta txtgame = new textfield [9] [9]; // crie 81 objetos de campo de texto Doshudu shudu = new Doshudu (); int [] [] células = shudu.getshudu (); // Obtenha a matriz sudoku final jpanel jpl = new jPanel (); // Crie JPanel Object final int espacenum = num; // espacenum significa o número de campos de texto em branco que precisam ser definidos. int [9] [9]; // Responda a Sudoku Array System.arraycopy (células, 0, celular, 0, célula.length); // A resposta é da cópia criada da matriz sudoku para (int i = 0; i <9; i ++) {// imprima a resposta do console para (j = 0; j <9; } System.out.println (); } // Fim da impressão this.setDefaultCloseoperation (this.exit_on_close); this.SetSize (600, 600); this.setResizable (false); this.settitle ("Dark Horse-Li Alemanha-Suku Game 9 nível"); for (int i = 0; i <9; i ++) {for (int j = 0; j <9; j ++) {txtgame [i] [j] = new textfield (); // Defina a cor de fundo do campo de texto if ((i <3 && j <3) || (i <6 && i> = 3 && j> = 3 && j <6) || (i <9 && i> = 6 && j> = 6 && j <9)) {txtgame [i] [j].. } 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 (new font ("diálogo", font.center_baseline, 60)); // defina o tamanho da fonte txtgame [i] [j] .settext (integer.toString (células [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 [espacenum] [2]; final jframe jfm = new jframe ("selecione número"); // cancelar título jframe jfm.setUndecorated (true); // Adicione a função final da função de arrasto JFrame = new Point (); jfm.addmouseListener (new mouseAdapter () {public void mousePressed (mouseevent e) {origem.x = e.getx (); origem.y = e.gety ();}}); jfm.addmousemOtionListener (new MouseMotionAdapter () {public void Mousedragged (mouseevent e) {ponto p = jfm.getLocation (); jfm.setLocation (px + e.getx () - origem.x, py e.gety () - origem); // defina jframe como awtutities.setWindowopacity translúcida (JFM, 0,7F); JPanel final JPNL = novo JPanel (New GridLayout (3, 3)); jfm.setLayout (nulo); JFM.SetSize (190, 200); jfm.SetResizable (false); jpnl.setbounds (0, 0, 190, 120); jfm.SetResizable (false); for (int i = 0; i <espacenum; i ++) {// Defina o campo de texto para estar vazio aleatoriamente com base no número de campos de texto em branco que precisam ser finais Int Rand1 = new Random (). NextInt (9); final int rand2 = novo aleatório (). 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 (cor.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] .setback (cor.Green); } if ((rand1 <9 && rand1> = 6 && rand2 <3) || (rand1 <9 && rand1> = 6 && rand2 <3) || (rand1 <1 <10 && rand1> = 6 && rand2 <3) || (Rand1 <100 3 &&2> = 3 && Rand2 <3) {txtgame [rand1] [rand2] .setbackground (color.pink); } txtgame [rand1] [rand2] .addmouseListener (new mouseAdapter () {public void mouseclicked (mouseevent mouseevent) {jfm.getContentPane (). Botão (F + 1) + "); Método gerado automaticamente, TXTGame [RAND1] [RAND2] .Settext (BTN.GetLabel () + TXTGame [Rand1] [Rand2] .getText (); btndel.setbackground (color.red); Método gerado automaticamente com TXTGame [RAND1] [RAND2] .Settext (""); txtgame [rand1] [rand2] .addTextListener (new textListener () {// Adicione um ouvinte ao campo de texto em branco, compare as respostas após a alteração do valor, imprimir "bom" no console se todas as respostas estiverem corretas @Override void textvaluechanged (textevent e) {textfield tmp = (textfield). Spacenum; JPL.Removeall (); mouseclicked (mouseevent e) {// Método gerado automático Stub Sudoku.This.Dispose (); JPL.UPDATEUI (); txtgame [rand1] [rand2] .setEnabled (true); } this.add (JPL); this.setVisible (true); }}O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.