Contoh -contoh dalam artikel ini berbagi kode spesifik dari game Java Sudoku untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
Saya berencana untuk menginstal komponen yang dibutuhkan oleh javafx dan menggunakan javaFx secara langsung, tetapi tampaknya versi gerhana salah dan tidak dapat digunakan bahkan setelah instalasi ...
Kode Sudoku ditulis selama liburan musim dingin. Saya belajar Java selama sebulan. Tampaknya sedikit tidak cukup sekarang, tetapi ini adalah program pertama, jadi saya meletakkannya secara langsung. Implementasi Disk Akhir Sudoku secara langsung menggunakan kekerasan, dan kompleksitas waktu agak tinggi. Saya terlalu malas untuk mengubahnya dan langsung meletakkan kode.
Implementasi disk akhir:
impor java.util.random; kelas publik sudokupuzzleGenerator {private acak acak = baru acak (); private static final int max_call_random_array_times = 220; Private int CurrentTimes = 0; publik int [] [] generatePuzzlematrix () {int [] [] randomMatrix = int int [9] [9]; untuk (int row = 0; baris <9; baris ++) {if (baris == 0) {currentTimes = 0; randommatrix [baris] = buildrandomarray (); } else {int [] temprandomarray = buildrandomarray (); untuk (int col = 0; col <9; col ++) {if (arusTimes <max_call_random_array_times) {if (! iScandidatenmbfound (randommatrix, temprandomarray, row, col)) {resetValueSinrowToZero (randommatrix, row); baris -= 1; col = 8; temprandomarray = buildrandomarray (); }} else {row = -1; col = 8; resetvaluestozeros (randommatrix); saat ini = 0; }}} return RandomMatrix; } private void resetValuesInrowToZero (int [] [] matriks, int row) {for (int j = 0; j <9; j ++) {matrix [baris] [j] = 0; }} private void resetValuestoZeros (int [] [] matriks) {for (int row = 0; baris <9; baris ++) {for (int col = 0; col <9; col ++) {matrix [baris] [col] = 0; }}} private boolean iscandidatenmbfound (int [] [] randomMatrix, int [] randomArray, int row, int col) {for (int i = 0; i <9; i ++) {randommatrix [baris] [col] = acak random [i]; if (noconflict (randommatrix, row, col)) {return true; }} return false; } private boolean noconflict (int [] [] candidatematrix, int row, int col) {return noconflictinrow (candidatematrix, row, col) && noconflictincolumn (candidatematrix, row, col) && noconfliclock (candidatematri, rowx) && noconfliclock (candidatematri (candidateMatri, col) && noconfliclock (candidatematri (candidateMatri); } private boolean noconflictinrow (int [] [] candidatematrix, int row, int col) {int currentValue = candidatematrix [baris] [col]; untuk (int colnum = 0; colnum <col; colnum ++) {if (currentValue == candidateMatrix [baris] [colnum]) {return false; }} return true; } private boolean noconflictincolumn (int [] [] candidatematrix, int row, int col) {int currentValue = candidatematrix [baris] [col]; untuk (int rownum = 0; rownum <row; rownum ++) {if (currentValue == candidateMatrix [rownum] [col]) {return false; }} return true; } private boolean noconflictinblock (int [] [] candidateMatrix, int row, int col) {int baserow = row / 3 * 3; int basecol = col / 3 * 3; untuk (int rownum = 0; rownum <8; rownum ++) {if (candidateMatrix [baserow + rownum / 3] [basecol + rownum % 3] == 0) {lanjut; } untuk (int colnum = rownum + 1; colnum <9; colnum ++) {if (candidateMatrix [baserow + rownum / 3] [basecol + rownum % 3] == candidatematrix [Baserow + colnum / 3] [basecol + colnum % 3]) {lalat lalat; }}} return true; } private int [] buildrandomArray () {currentTimes ++; int [] array = int int [] {1, 2, 3, 4, 5, 6, 7, 8, 9}; int randomint = 0; untuk (int i = 0; i <20; i ++) {randomInt = random.nextInt (8)+1; int temp = array [0]; array [0] = array [randomInt]; array [randomInt] = temp; } return array; } public int getCurrentTimes () {return CurrentTimes; } public void setCurrentTimes (int CurrentTimes) {this.currentTimes = CurrentTimes; }} Antarmuka dan penilaian:
Ditulis dengan ayunan
impor javax.swing.*; impor java.awt.*; impor java.awt.event.*; impor java.util.random; kelas publik shud memperluas jframe {private static final long serialversionuid = 5952689219411916553l; // Serialize Field Private Static JTextfield A [] [] = JTextField baru [9] [9]; // penyimpanan angka di kotak teks statis int ans [] [] = int new [9] [9]; // menyimpan input dua digit array sudokupuzzleGenerator contoh = sudokupuzzleGenerator () baru; public int right [] [] = example.generatePuzzlematrix (); hak int int [] []; private int [] [] wk (int a [] []) {// hidden acak r = acak baru (); int a1, a2; a1 = r.nextint (9); a2 = r.nextint (9); untuk (int i = 0; i <100; i ++) {a [a1] [a2] = 0; a1 = r.nextint (9); a2 = r.nextint (9); } return a; } public shud () {container c = getContentPane (); C.SetLayout (BorderLayout baru (2, 1)); // tata letak perbatasan jMenuitem jmiok = jMenuitem baru ("kirim"); // Tentukan menu jMenuitem jmiexplain = jMenuitem baru ("detail"); JMenuitem jMimessage = jMenuitem baru ("informasi"); Jpanel panel = jpanel baru (); // tentukan panel kontainer.add (jmiok); // Tampilkan menu di panel kontainer.add (jmiExplain); Panel.Add (JMimessage); Jpanel p1 = jpanel baru (gridlayout baru (9, 9, 5, 5)); // Tentukan tata letak grid 9 baris dan 9 kolom Tambah (Panel, BorderLayout.North); // Tempatkan menu ke utara Tambah (P1, BorderLayout.Center); // Tempatkan angka -angka di kanan tengah = minggu (kanan); untuk (int k = 0; k <9; k ++) {for (int n = 0; n <9; n ++) {if (rightans [k] [n]! = 0) {a [k] [n] = jTextField baru (""+rightans [k] [n]); a [k] [n] .sethorizontalAlignment (jtextfield.center); // Pusat angka secara horizontal a [k] [n] .setedable (false); // Hanya menampilkan p1.add yang tidak dimodifikasi (a [k] [n]); // Tambahkan kotak teks} else {a [k] [n] = new jTextField (); a [k] [n] .sethorizontalAlignment (jtextfield.center); p1.add (a [k] [n]); }} add (p1); // Tunjukkan panel digital di wadah jmiok.addactionListener (actionListener baru () {// buat pendengar acara anonim public void actionPerformed (actionEvent e) {if (getText () == 1) {if (judge () == true) {joptionpane.showrageialoG (null your, " Benar! "," Hasil ", joptionpane.information_message);} else {joptionpane.showmessagedialog (null," Jawaban Anda benar! "," Hasil ", joptionpane.information_message);} lain {joptionpane.showrageialog (null null);} lain {joptionpane.showrageialog (null null);" nakese, "{joptionpane.showrageialog (null nulling);" nake your, "joptionpane.showrageialog (null null);" nandal, "nake {joptionpane.showrageialog (null null," null, "nanse," joptionpane.showrageialog (null); salah! "," hasil ", joptionpane.information_message);}}}}}); menjelaskan listenerclass listener2 = new ExplainListenerClass (); jmiexplain.addactionListener (listener2); MessageListenerClass listener3 = baru messagelistenerclass (); jMimessage.addactionListener (listener3); } static int getText () // Dapatkan teks kotak teks {int i, j; untuk (i = 0; i <9; i ++) {untuk (j = 0; j <9; j ++) {ans [i] [j] = 0; }} untuk (int k = 0; k <9; k ++) {for (int n = 0; n <9; n ++) {coba // penanganan pengecualian {ans [k] [n] = integer.parseint (a [k] [n] .getText ()); // Konversikan jenis jawaban menjadi titch ans} (numberformatexception nfe) {joptionpane.showmessagealog (null, "termasuk non-number dalam data, mohon masuk kembali"); kembali 0; }}} return 1; } public static boolean judge () // menilai apakah jawaban input benar {int i, j, k; int [] [] jawaban = ans; untuk (i = 0; i <9; i ++) {if (j (Jawab [i]) == false) // menilai apakah ada angka duplikat di setiap kolom Return false; } untuk (j = 0; j <9; j ++) // menilai apakah ada angka duplikat di setiap baris {int [] newanswercolumn = new int [9]; untuk (i = 0; i <9; i ++) {Newanswercolumn [i] = Jawab [i] [j]; } if (j (newanswercolumn) == false) return false; } untuk (i = 0; i <3; i ++) // menilai apakah ada angka duplikat di setiap grid kesembilan {untuk (j = 0; j <3; j ++) {k = 0; int [] newanswer = new int [9]; untuk (int m = i * 3; m <i * 3 +3; m ++) {for (int n = j * 3; n <j * 3 +3; n ++) {newanswer [k] = Jawab [m] [n]; K ++; }} if (judget9 (newanswer) == false) {return false; }} return true; } public static boolean judge9 (int [] jawaban) {int i, j; untuk (i = 0; i <9; i ++) {untuk (j = 0; j <9; j ++) {if (i == j) lanjutkan; if (jawab [i] == Jawab [j]) // Jika ada angka duplikat, return false {return false; }}} return true; // tidak ada duplikat angka, kembalikan true} public static void main (string [] args) {jframe frame = new shud (); frame.settitle ("Suduku"); frame.setsize (600.900); frame.setLocationRelativeto (null); frame.setDefaultCloseOperation (jframe.exit_on_close); frame.setVisible (true); }} Class ExplainListenerClass mengimplementasikan ActionListener {// acara listenerpublic void actionPerformed (actionEvent e) {joptionpane.showmessagedialog (null, "Isi angka untuk memastikan bahwa tidak ada duplikasi angka di setiap baris, setiap kolom dan setiap grid sembilan kecil", "jelas," jelas, "jelas", JOP. }} kelas MessageListenerClass mengimplementasikan ActionListener {public void actionPerformed (ActionEvent e) {jOptionPane.showmessageDialog (null, "dibuat oleh wyx", "pesan", joptionpane.information_message); }}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.