Contoh -contoh dalam artikel ini berbagi kode spesifik dari game Java Sudoku untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
Game Sudoku yang saya tulis sendiri memiliki total 9 level, dan kodenya adalah sebagai berikut:
1. Kelas Doshudu digunakan untuk menghasilkan array sudoku
impor java.util.random; kelas publik doshudu { / ** * @param args * / public static void main (string [] args) {// TODO Metode yang dihasilkan otomatis stub int [] [] sel = newshudu (); // sel = changeshu (sel, 9); untuk (int k = 0; k <9; k ++) {untuk (int i = 0; i <9; i ++) {System.out.print (sel [k] [i]); } System.out.println (); }} public static int [] [] newshudu () {int [] [] sel = int baru [] [] {{1,2,3,4,5,6,7,8,9}, {4,5,6,7,8,1,2,3}, {7,8,9,9,3,4,4,1,5,6}, {7,8,9,1,3,4,4,4,4,6}, {7,8,9,9,3,4,4,4,4,6 {{7,9,9,3,2 {2,3,1,5,6,4,8,9,7}, {5,6,4,8,9,7,2,3,1}, {8,9,2,3,1,1,5,6,4}, {3,1,2,2,9,9,7,8}, {6,4,1,2,2,4,7,9,7,8}, {6,4,1,2,2,4,4,9,7 { {9,7,8,3,1,2,6,4,5}}; int counth = random baru (). NextInt (10); untuk (int k = 0; k <counth; k ++) {sel = linetolie (sel); } int count = 0; untuk (int k = 0; k <12; k ++) {count = acak baru (). NextInt (9); sel = Changeline (sel, hitungan); } int counth2 = random baru (). NextInt (10); untuk (int k = 0; k <counth2; k ++) {sel = linetolie (sel); } mengembalikan sel; } public static int [] [] changeline (int [] [] sel, int m) {// garis dan baris pertukaran int n = m; int [] temp = int new [9]; n = ((m+3)> = 9)? (M+3-9): m+3; untuk (int j = 0; j <9; j ++) {temp [j] = sel [m] [j]; sel [m] [j] = sel [n] [j]; sel [n] [j] = temp [j]; } mengembalikan sel; } public static int [] [] linetolie (int [] [] sel) {// garis dan pertukaran kolom int temp = 0; untuk (int j = 0; j <9; j ++) {untuk (int k = j+1; k <9; k ++) {temp = sel [k] [j]; sel [k] [j] = sel [j] [k]; sel [j] [k] = temp; }} mengembalikan sel; }} 2. Kelas Operasi Antarmuka
impor java.awt.button; impor java.awt.color; impor java.awt.flowlayout; impor java.awt.font; impor java.awt.gridlayout; impor java.awt.point; impor java.awt.textfield; impor java.awt.event.mouseadapter; impor java.awt.event.mouseevent; impor java.awt.event.mousemotionAdapter; impor java.awt.event.textEvent; impor java.awt.event.textListener; impor java.util.random; impor javax.swing.jframe; impor javax.swing.jlabel; impor javax.swing.jpanel; impor com.sun.awt.awtutilities; kelas publik Sudoku memperluas jframe {final private textfield [] [] txtgame; static int num = 20; // Jumlah ruang kosong int statis static = 5; // Jumlah level static int add = 5; // Jumlah spasi kosong yang ditambahkan setelah berhenti dari public static void main (string [] args) {sudoku shudu = new sudoku (); } public sudoku () {// tata letak awal jFrame dan pengaturan mendengarkan txtgame = new TextField [9] [9]; // Buat 81 objek TextField doshudu shudu = doshudu baru (); int[][] cells = shudu.getShudu();// Get Sudoku array final JPanel jpl = new JPanel();// Create JPanel object final int spaceNum = num;// spaceNum means the number of blank TextFields that need to be set jpl.setLayout(new GridLayout(9, 9));// JPanel layout final int[][] cellAn = new int [9] [9]; // Jawaban ke Sudoku Array System.Arraycopy (Cell, 0, Cellan, 0, Cells.length); // Jawabannya adalah dari salinan array Sudoku yang dibuat untuk (int i = 0; i <9; i ++) {// Cetak jawaban dari konsol untuk (int j = 0; j <9; i ++) {// cetak dari konsol untuk (int j = 0; j <9; j.) {/ cell. } System.out.println (); } // akhir pencetakan this.setDefaultCloseOperation (this.exit_on_close); this.setsize (600, 600); this.setresizable (false); this.settitle ("Level Game 9 Jerman-Suku Dark Horse-Li Game 9"); untuk (int i = 0; i <9; i ++) {for (int j = 0; j <9; j ++) {txtgame [i] [j] = TextField baru (); // Atur warna latar belakang 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.atane); } 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 (font baru ("dialog", font.center_baseline, 60)); // Atur font ukuran txtgame [i] [j] .setText (integer.tostring (sel [i] [j]))); txtgame [i] [j] .setenabled (false); txtgame [i] [j] .setVisible (true); jpl.add (txtgame [i] [j]); jpl.setVisible (true); }} final int [] [] temparray = int int [spacenum] [2]; JFRame final JFM = JFrame baru ("Pilih Nomor"); // Batalkan judul jframe jfm.setundedecorated (true); // tambahkan fungsi drag jframe titik akhir poin = new point (); jfm.addmouseListener (mouseadapter baru () {public void mousepressed (mouseEvent e) {origin.x = e.getx (); origin.y = e.gety ();}}); jfm.addmouseMotionListener (mousemotionAdapter baru () {public void mouseDragged (mouseEvent e) {point p = jfm.getLocation (); jfm.setlocation (px + e.getx () - origin.x, py + e.gety () - origin.y);}); // atur jframe ke Awtutilities tembus pandang.setWindowopacity (JFM, 0.7F); jpanel final jpnl = jpanel baru (gridlayout baru (3, 3)); JFM.SetLayout (null); JFM.Setsize (190, 200); JFM.SetResizable (false); jpnl.setbounds (0, 0, 190, 120); JFM.SetResizable (false); untuk (int i = 0; i <spacenum; i ++) {// Set TextField menjadi kosong secara acak berdasarkan jumlah bidang teks kosong yang perlu menjadi int rand1 = random baru (). NextInt (9); final int rand2 = random baru (). 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. } 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.grebround.ground)) {txtgame [rand1] [rand2] .setBackground.greeG.ground. } if ((rand1 <9 && rand1> = 6 && rand2 <3) || (rand1 <9 && rand1> = 6 && rand2 <3) || (rand1 <1 <10 && rand1> = 6 && rand2 <3) || (rand1 <100 3 && rand2> = rand2 && rand2 <6) || {txtgame [rand1] [rand2] .setBackground (color.pink); } txtgame [rand1] [rand2] .addmouseListener (mouseadapter baru () {public void mouseclicked (mouseEvent MouseEvent) {jfm.getContentPane (). foron (); fored (); for = 0; for; for; for = 9; for; for = 9; for-; for = 9; for; for = 9; for; for = 9; for = 9; for; for = 9; for = 9; for = 9; for = 9; for = 9; for; for = 9; for = 9; for = 9; for = 9; for = 9; for = 9; for; for = 9; for = 9; for = 9; for = 9; fored (); fored (); fored (); for = 9; (F + 1) + ""); Metode yang dihasilkan secara otomatis txtgame [rand1] [rand2] .settext (btn.getLabel () + txtgame [rand1] [rand2] .getText ()); Btndel.setbackground (Color.Red); Metode yang dihasilkan secara otomatis txtgame [rand1] [rand2] .settext (""); txtgame [rand1] [rand2] .addtextListener (TextListener baru () {// Tambahkan pendengar ke Textfield kosong, bandingkan jawaban setelah nilai berubah, cetak "baik" di konsol jika semua jawaban benar @Override public textValueChanged (TextEvent e) {Textfield TMP = (u -over = TextValueChanged (TextEvent E) {Textfield TMP = (u -over (u -u) (TextEvent (Textfield Textfield TMP = (u -u -u -u) (Text -OURE (TextEvent E) {Textfield TMP = (u u -u -u); Spacenum; jpl.removeall (); MouseClicked (MouseEvent E) {// Metode yang dihasilkan secara otomatis Stub Sudoku. jpl.updateUi (); txtgame [rand1] [rand2] .setEnabled (true); } this.add (jpl); this.setVisible (true); }}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.