ตัวอย่างในบทความนี้แบ่งปันรหัสเฉพาะของเกม Java Sudoku สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
ฉันวางแผนที่จะติดตั้งส่วนประกอบที่ Javafx ต้องการและใช้ Javafx โดยตรง แต่ดูเหมือนว่า Eclipse เวอร์ชันนั้นผิดและไม่สามารถใช้งานได้แม้หลังจากการติดตั้ง ...
รหัส Sudoku ถูกเขียนขึ้นในช่วงวันหยุดฤดูหนาว ฉันเรียนรู้ Java เป็นเวลาหนึ่งเดือน ตอนนี้ดูเหมือนว่าจะไม่เพียงพอเล็กน้อย แต่มันเป็นโปรแกรมแรกหลังจากทั้งหมดดังนั้นฉันจึงใส่มันโดยตรง การใช้ดิสก์สุดท้ายของ Sudoku ใช้ความรุนแรงโดยตรงและความซับซ้อนของเวลานั้นค่อนข้างสูง ฉันขี้เกียจเกินไปที่จะเปลี่ยนและใส่รหัสโดยตรง
การใช้ดิสก์สุดท้าย:
นำเข้า java.util.random; ชั้นสาธารณะ sudokupuzzlegenerator {สุ่มส่วนตัวสุ่ม = new random (); int สุดท้ายคงที่ int max_call_random_array_times = 220; INT INT CURRISTINTIME PRIVILED = 0; สาธารณะ int [] [] generatePuzzlematrix () {int [] [] randommatrix = new int [9] [9]; สำหรับ (int row = 0; row <9; row ++) {ถ้า (row == 0) {currenttimes = 0; Randommatrix [แถว] = buildRandomArray (); } else {int [] temprandomarray = buildRandomArray (); สำหรับ (int col = 0; col <9; col ++) {ถ้า (currenttimes <max_call_random_array_times) {ถ้า (! iscandidatenmbfound (randommatrix, temprandomarray, row, col)) แถว -= 1; col = 8; temprandomarray = buildrandomarray (); }} else {row = -1; col = 8; RESETVALUESTOZEROS (Randommatrix); currenttimes = 0; }}} return randommatrix; } โมฆะส่วนตัว ResetValuesInrowTozero (int [] [] เมทริกซ์, แถว int) {สำหรับ (int j = 0; j <9; j ++) {matrix [แถว] [j] = 0; }} โมฆะส่วนตัว ResetValuestozeros (int [] [] matrix) {สำหรับ (int row = 0; แถว <9; แถว ++) {สำหรับ (int col = 0; col <9; col ++) {matrix [แถว] [col] = 0; }}} บูลีนส่วนตัว iScandidatenmbfound (int [] [] randommatrix, int [] randomArray, แถว int, int col) {สำหรับ (int i = 0; i <9; i ++) {randommatrix [แถว] [col] = randomArray [i]; if (noconflict (randommatrix, row, col)) {return true; }} return false; } บูลีนส่วนตัว noconflict (int [] [] candidateMatrix, แถว int, int col) {return noconflictinrow (CandidateMatrix, แถว, col) && noconflictincolumn } บูลีนส่วนตัว noconflictinrow (int [] [] candidateMatrix, แถว int, int col) {int currentValue = candidateMatrix [แถว] [col]; สำหรับ (int colnum = 0; colnum <col; colnum ++) {ถ้า (currentValue == candidateMatrix [แถว] [colnum]) {return false; }} ส่งคืนจริง; } บูลีนส่วนตัว noconflictincolumn (int [] [] candidateMatrix, แถว int, int col) {int currentValue = candidateMatrix [แถว] [col]; สำหรับ (int rownum = 0; rownum <row; rownum ++) {ถ้า (currentValue == candidateMatrix [rownum] [col]) {return false; }} ส่งคืนจริง; } บูลีนส่วนตัว noconflictinblock (int [] [] candidateMatrix, แถว int, int col) {int baserow = แถว / 3 * 3; int basecol = col / 3 * 3; สำหรับ (int rownum = 0; rownum <8; rownum ++) {ถ้า (candidateMatrix [baserow + rownum / 3] [basecol + rownum % 3] == 0) {ดำเนินการต่อ; } สำหรับ (int colnum = rownum + 1; colnum <9; colnum ++) {ถ้า (candidateMatrix [baserow + rownum / 3] [basecol + rownum % 3] == candidateMatrix [baserow + colnum / 3] [basecol + colnum % 3] }}} ส่งคืนจริง; } int ส่วนตัว [] buildRandomArray () {CurrentTimes ++; int [] array = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9}; int randomint = 0; สำหรับ (int i = 0; i <20; i ++) {randomint = random.nextint (8)+1; int temp = array [0]; อาร์เรย์ [0] = อาร์เรย์ [สุ่ม]; อาร์เรย์ [randomint] = temp; } return array; } public int getCurrentTimes () {return currenttimes; } โมฆะสาธารณะ setCurrentTimes (int currenttimes) {this.currentTimes = currenttimes; - อินเทอร์เฟซและการตัดสิน:
เขียนด้วยการแกว่ง
นำเข้า Javax.swing.*; นำเข้า Java.awt.*; นำเข้า java.awt.event.*; นำเข้า java.util.random; ระดับสาธารณะ shud ขยาย JFrame {ส่วนตัวคงที่สุดท้าย Long SerialVersionUID = 59526892194119165553L; // serialize ฟิลด์ส่วนตัวคงที่ Jtextfield a [] [] = ใหม่ jtextfield [9] [9]; // จัดเก็บตัวเลขในกล่องข้อความ int int ans [] [] = new int [9] [9]; // การจัดเก็บอินพุตสองหลักอาร์เรย์ sudokupuzzlegenerator = ใหม่ sudokupuzzlegenerator (); INT ที่ถูกต้องสาธารณะ [] [] = example.generatePuzzlematrix (); สิทธิในการสาธารณะ [] []; private int [] [] wk (int a [] []) {// hidden random r = new random (); int a1, a2; a1 = r.nextint (9); a2 = r.nextint (9); สำหรับ (int i = 0; i <100; i ++) {a [a1] [a2] = 0; a1 = r.nextint (9); a2 = r.nextint (9); } return a; } public shud () {คอนเทนเนอร์ c = getContentPane (); C.SetLayout (New BorderLayout (2, 1)); // เค้าโครงชายแดน jmenuitem jmiok = new jmenuitem ("ส่ง"); // กำหนดเมนู jmenuitem jmiexplain = ใหม่ jmenuitem ("รายละเอียด"); JMenuitem JMimessage = new JMenuitem ("ข้อมูล"); JPanel Panel = new JPanel (); // กำหนดแผงคอนเทนเนอร์ ADD (JMIOK); // แสดงเมนูในแผงคอนเทนเนอร์ ADD (jmiexplain); Panel.Add (JMimessage); JPanel P1 = new JPanel (ใหม่ gridlayout (9, 9, 5, 5)); // กำหนดรูปแบบกริดของ 9 แถวและ 9 คอลัมน์เพิ่ม (แผง, borderlayout.north); // วางเมนูไปทางทิศเหนือเพิ่ม (P1, BorderLayout.Center); // วางตัวเลขใน Rightans กลาง = wk (ขวา); สำหรับ (int k = 0; k <9; k ++) {สำหรับ (int n = 0; n <9; n ++) {ถ้า (rightans [k] [n]! = 0) {a [k] [n] = ใหม่ jtextfield (""+rightans [k] [n]); a [k] [n] .Sethorizontalalignment (jtextfield.center); // ตรงกลางตัวเลขในแนวนอน a [k] [n]. seteditable (เท็จ); // แสดงเฉพาะ p1.add ที่ไม่ได้แก้ไข (a [k] [n]); // เพิ่มกล่องข้อความ} else {a [k] [n] = new jtextfield (); A [k] [n] .Sethorizontalalignment (jtextfield.center); p1.add (a [k] [n]); }} เพิ่ม (P1); // แสดงแผงดิจิตอลในคอนเทนเนอร์ jmiok.addactionListener (ใหม่ actionListener () {// สร้างเหตุการณ์ที่ไม่ระบุชื่อฟังโมฆะสาธารณะ ActionPerformed (ActionEvent E) {ถ้า (getText () == 1) {ถ้า () ถูกต้อง! "," ผลลัพธ์ ", joptionpane.information_message);} else {joptionpane.showmessagedialog (null," คำตอบของคุณถูกต้อง! "," result ", joptionpane.information_message); ผิด! "," ผลลัพธ์ ", joptionpane.information_message);}}}}}); Explainlistenerclass Listener2 = new Explainlistenerclass (); jmiexplain.addactionListener (Listener2); MessageListenerclass Listener3 = New MessageListenerclass (); jmimessage.addactionListener (Listener3); } int int getText () // รับข้อความของกล่องข้อความ {int i, j; สำหรับ (i = 0; i <9; i ++) {สำหรับ (j = 0; j <9; j ++) {ans [i] [j] = 0; }} สำหรับ (int k = 0; k <9; k ++) {สำหรับ (int n = 0; n <9; n ++) {ลอง // การจัดการข้อยกเว้น {ans [k] [n] = integer.parseint (a [k] [n] .getText ()); // แปลงประเภทคำตอบเป็น ANS} catch (numberFormatexception nfe) {joptionpane.showMessageDialog (null, "รวมถึงที่ไม่ใช่หมายเลขในข้อมูลโปรดกลับเข้ามาอีกครั้ง"); กลับ 0; }}} return 1; } Public Static Boolean Judge () // ตัดสินว่าคำตอบอินพุตนั้นถูกต้อง {int i, j, k; int [] [] คำตอบ = ตอบ; สำหรับ (i = 0; i <9; i ++) {ถ้า (j (คำตอบ [i]) == false) // ตัดสินว่ามีหมายเลขซ้ำในแต่ละคอลัมน์ส่งคืนเท็จ; } สำหรับ (j = 0; j <9; j ++) // ตัดสินว่ามีตัวเลขที่ซ้ำกันในแต่ละแถว {int [] newanswercolumn = ใหม่ int [9]; สำหรับ (i = 0; i <9; i ++) {newanswercolumn [i] = คำตอบ [i] [j]; } ถ้า (j (newanswercolumn) == false) return false; } สำหรับ (i = 0; i <3; i ++) // ตัดสินว่ามีตัวเลขที่ซ้ำกันในแต่ละกริดเก้า {สำหรับ (j = 0; j <3; j ++) {k = 0; int [] newanswer = new int [9]; สำหรับ (int m = i * 3; m <i * 3 +3; m ++) {สำหรับ (int n = j * 3; n <j * 3 +3; n ++) {newanswer [k] = คำตอบ [m] [n]; K ++; }} if (Judge9 (newanswer) == false) {return false; }} ส่งคืนจริง; } Public Static Boolean Judge9 (int [] คำตอบ) {int i, j; สำหรับ (i = 0; i <9; i ++) {สำหรับ (j = 0; j <9; j ++) {ถ้า (i == j) ดำเนินการต่อ; ถ้า (คำตอบ [i] == คำตอบ [j]) // หากมีหมายเลขที่ซ้ำกันให้ส่งคืน false {return false; }}} ส่งคืนจริง; // ไม่มีตัวเลขที่ซ้ำกัน, ส่งคืนจริง} โมฆะคงที่สาธารณะหลัก (สตริง [] args) {jframe frame = new shud (); frame.settitle ("suduku"); frame.setsize (600,900); frame.setLocationRelativeto (null); frame.setDefaultCloseoperation (jframe.exit_on_close); frame.setVisible (จริง); }} คลาส ExplainListenerclass ใช้ ActionListener {// เหตุการณ์ listenerpublic void actionperformed (actionEvent e) {joptionpane.showmessagedialog (Null "เติมตัวเลขเพื่อให้แน่ใจว่าไม่มีการทำซ้ำตัวเลขในแต่ละแถวแต่ละคอลัมน์ }} คลาส MessageListenerclass ใช้ ActionListener {โมฆะสาธารณะ ActionPerformed (ActionEvent E) {Joptionpane.showMessagedialog (Null, "Made by Wyx", "Message", Joptionpane.information_message); -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น