이 기사의 예는 참조를 위해 Java Suboku 게임의 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
Javafx가 요구하는 구성 요소를 설치하고 Javafx를 직접 사용할 계획이지만 Eclipse 버전은 잘못되었으며 설치 후에도 사용할 수없는 것 같습니다 ...
스도쿠 코드는 겨울 방학 동안 작성되었습니다. 한 달 동안 자바를 배웠습니다. 지금은 조금 불충분 해 보이지만 결국 첫 번째 프로그램이므로 직접 넣습니다. 스도쿠 최종 디스크의 구현은 폭력을 직접 사용하며 시간 복잡성은 약간 높습니다. 나는 그것을 변경하고 직접 코드를 넣기에는 너무 게으르다.
최종 디스크 구현 :
java.util.random import; 공개 클래스 sudokupuzzleGenerator {private random = new random (); 비공개 정적 최종 int max_call_random_array_times = 220; 개인 int currentTimes = 0; public int [] [] generatepuzzlematrix () {int [] [] randommatrix = new int [9] [9]; for (int row = 0; row <9; row ++) {if (row == 0) {currentTimes = 0; randommatrix [row] = buildrandomarray (); } else {int [] temprandomarray = buildrandomarray (); for (int col = 0; col <9; col ++) {if (currentTimes <max_call_random_array_times) {if (! iscandidatenmatrix, temprandomarray, row, col)) {resetvaluesinrowtozero (randommatrix, gow); 행 -= 1; col = 8; temprandomarray = buildrandomarray (); }} else {row = -1; col = 8; ResetValuestozeros (randommatrix); currentTimes = 0; }}} return randommatrix; } private void resetValuesInrowtozero (int [] [] matrix, int row) {for (int j = 0; j <9; j ++) {matrix [row] [j] = 0; }} private void resetvaluestozeros (int [] [] matrix) {for (int row = 0; row <9; row ++) {for (int col = 0; col <9; col ++) {matrix [row] [col] = 0; }}} private boolean iscandidatenmbfound (int [] []] randommatrix, int [] randomarray, int row, int col) {for (int i = 0; i <9; i ++) {randommatrix [row] [col] = randomarray [i]; if (noconflict (randommatrix, row, col)) {return true; }} 거짓을 반환합니다. } private boolean noconflict (int [] [] Candidatematrix, int row, int col) {return noconflictinrow (Candidatematrix, row, col) && noconflictincolumn (Candidatematrix, Row, Col) && noconflictinblock (Candidatematrix, row, col); } private boolean noconflictinrow (int [] [] Candidatematrix, int row, int col) {int currentValue = CandidAtematrix [row] [col]; for (int colnum = 0; colnum <col; colnum ++) {if (currentValue == CandidAtematrix [row] [colnum]) {return false; }} true를 반환합니다. } private boolean noconflictincolumn (int [] [] Candidatematrix, int row, int col) {int currentValue = CandidAtematrix [row] [col]; for (int rownum = 0; rownum <row; rownum ++) {if (currentValue == CandidAtematrix [rownum] [col]) {return false; }} true를 반환합니다. } private boolean noconflictinBlock (int [] [] Candidatematrix, int row, int col) {int baserow = row / 3 * 3; int basecol = col / 3 * 3; for (int rownum = 0; rownum <8; rownum ++) {if (candidatematrix [baserow + rownum / 3] [Basecol + rownum 3] == 0) {계속; } for (int colnum = rownum + 1; colnum <9; colnum ++) {if (candidatematrix [baserow + rownum / 3] [basecol + rownum % 3] == Candidatematrix [baseerow + colnum / 3] [basecol + colnum % 3]) {return false; }}} return true; } private int [] buildRandomArray () {currentTimes ++; int [] array = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9}; int randomint = 0; for (int i = 0; i <20; i ++) {randomint = random.nextint (8)+1; int temp = 배열 [0]; 배열 [0] = 배열 [randomint]; 배열 [randomint] = temp; } 반환 배열; } public int getCurrentTimes () {return currentTimes; } public void setCurrentTimes (int currentTimes) {this.currentTimes = currentTimes; }} 인터페이스 및 판단 :
스윙으로 작성되었습니다
javax.swing을 가져옵니다.*; java.awt.*; import java.awt.event.*; java.util.random import; 공개 클래스 shud는 jframe {private static final long serialversionuid = 5952689219411916553L; // 필드 개인 정적 Jtextfield A [] [] = New Jtextfield [9] [9]; // 텍스트 상자에 숫자를 저장하십시오. static int ans [] = new int [9] [9]; // 입력 저장 2 자리 배열 SudokupuzzleGenerator 예제 = 새로운 SudokupuzzleGenerator (); public int right [] [] = example.generatepuzzlematrix (); 공개 int 권리 [] []; private int [] [] wk (int a [] []) {// hidden random r = new random (); int a1, a2; A1 = R.NextInt (9); A2 = R.NextInt (9); for (int i = 0; i <100; i ++) {a [a1] [a2] = 0; A1 = R.NextInt (9); A2 = R.NextInt (9); } 반환 a; } public shud () {컨테이너 c = getContentPane (); C.SetLayout (New BorderLayout (2, 1)); // 경계 레이아웃 jmenuitem jmiok = new jmenuitem ( "제출"); // 메뉴 정의 jmenuitem jmiexplain = new Jmenuitem ( "세부 사항"); jmenuitem jmimessage = new jmenuitem ( "정보"); JPANEL 패널 = 새로운 JPANEL (); // 컨테이너 패널을 정의합니다 .add (jmiok); // 컨테이너 패널에 메뉴를 표시합니다 .add (jmiexplain); PALEN.ADD (jmimessage); JPANEL P1 = 새로운 JPANEL (New GridLayout (9, 9, 5, 5)); // 9 행과 9 열의 그리드 레이아웃을 정의합니다 (PANEL, BORTHLAYOUT.NORTH); // 메뉴를 North Add (P1, BorderLayout.Center)에 배치합니다. // 숫자를 가운데에 배치합니다. rightans = wk (오른쪽); for (int k = 0; k <9; k ++) {for (int n = 0; n <9; n ++) {if (rightans [k] [n]! = 0) {a [k] [n] = new jtextfield ( ""+rightans [k] [n]); a [k] [n] .Sethorizontalalignment (jtextfield.center); // 숫자 중심 a [k] [n] .setedible (false); // 수정되지 않은 p1.add 만 표시합니다 (a [k] [n]); // 텍스트 상자 추가} else {a [k] [n] = new jtextfield (); a [k] [n] .Sethorizontalalignment (jtextfield.center); p1.add (a [k] [n]); }} add (p1); // 컨테이너 jmiok.addactionListener (new ActionListener () {// 익명 이벤트 생성 공개 void actionPerformed (actionEvent e) {if (getText () == 1) {if (judge () == true) {juptionpane.showmessagedialog (null, " 오른쪽! ","결과 ", joptionpane.information_message);} else {joptionpane.showmessagedialog (null,"당신의 대답이 맞다! ","결과 ", joptionpane.information_message);} else {joptionpane.showmessagedialog (null,"귀하의 대답은 "귀하의 답변입니다. 잘못! ","결과 ", joptionpane.information_message);}}}}); explyListEnerClass LEASER2 = New DeltingListenerClass (); jmiexplain.addactionListener (Listener2); MessagelistenerClass Listener3 = New MessagelistenerClass (); jmimessage.addactionListener (Leater3); } static int getText () // 텍스트 상자 텍스트 {int i, j; for (i = 0; i <9; i ++) {for (j = 0; j <9; j ++) {ans [i] [j] = 0; }} for (int k = 0; k <9; k ++) {for (int n = 0; n <9; n ++) {try // 예외 처리 {ans [k] [n] = integer.parseint (a [k] [] .getText ()); // 답변 유형을 ANS로 변환} catch (numberformatexception nfe) {joptionpane.showmessagedialog (null, "데이터에 비 지구 포함, 다시 입력하십시오"); 반환 0; }}} 반환 1; } public static boolean judge () // 입력 답변이 올바른지 {int i, j, k; int [] [] 답 = ans; for (i = 0; i <9; i ++) {if (j (answer [i]) == false) // 각 열에 중복 번호가 있는지 여부를 판단합니다. } for (j = 0; j <9; j ++) // 각 행에 중복 숫자가 있는지 판단 {int [] newAnswerColumn = new int [9]; for (i = 0; i <9; i ++) {NewAnswerColumn [i] = 답변 [i] [j]; } if (j (newanswercolumn) == false) false를 반환합니다. } for (i = 0; i <3; i ++) // 각 아홉 번째 그리드에 중복 숫자가 있는지 판단합니다 (or (j = 0; j <3; j ++) {k = 0; int [] newanswer = 새로운 int [9]; for (int m = i * 3; m <I * 3 +3; K ++; }} if (judge9 (newanswer) == false) {return false; }} true를 반환합니다. } public static boolean judge9 (int [] 답변) {int i, j; for (i = 0; i <9; i ++) {for (j = 0; j <9; j ++) {if (i == j) 계속; if (답변 [i] == 답변 [j]) // 중복 번호가있는 경우 retud false {return false; }}} return true; // 중복 번호 없음, return 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 extrenceListenerClass는 ActionListener를 구현합니다. {// event LeargerPublic void actionPerformed (actionEvent e) {joptionpane.showMessageDialog (null, 각 열, 각 열에 숫자가 복제되지 않도록하기 위해 숫자를 채우고 각 작은 그리드 ",", JoptionPane.information_message "; }} Class MessageListenerClass는 ActionListener를 구현합니다. {public void actionPerformed (ActionEvent e) {joptionpane.showmessagedialog (null, "made by Wyx", "message", joptionpane.information_message); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.