この記事の例は、参照のためにJava Sudokuゲームの特定のコードを共有しています。特定のコンテンツは次のとおりです
Javafxが必要とするコンポーネントをインストールし、Javafxを直接使用する予定ですが、Eclipseのバージョンは間違っており、インストール後でも使用できないようです...
Sudokuコードは冬休み中に書かれました。私は1か月間Javaを学びました。今では少し不十分なように思えますが、結局のところ最初のプログラムなので、私はそれを直接置きました。 Sudoku Final Diskの実装は暴力を直接使用し、時間の複雑さは少し高いです。私はそれを変更してコードを直接置くのが面倒です。
最終ディスクの実装:
java.util.randomをインポートします。パブリッククラスsudokupuzzlegenerator {private random = new Random(); Private Static Final int max_call_random_array_times = 220; private 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(!iscandidatenmbfound(randommatrix、temprandomarray、row、col)){resetvaluesinrowtozero(randmatrix、row);行 - = 1; col = 8; temprandomarray = buildrandomarray(); }} else {row = -1; col = 8; resetvaluestozeros(randommatrix); currentTimes = 0; }}} 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 [col] = randomArray [i]; if(noconflict(randommatrix、row、col)){return true; }} falseを返します。 } private boolean noconflict(int [] [] candidatematrix、int row、int col){return noconflictinrow(candidatematrix、列、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 [baserow + colnum / 3] [basecol + colnum%3] {return fals; }}} 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 = array [0]; array [0] = array [randomint]; array [randomint] = temp; } return array; } public int getCurrentTimes(){return currentTimes; } public void setCurrentTimes(int currentTimes){this.currentTimes = currentTimes; }}インターフェースと判断:
スイングで書かれています
javax.swing。*; java.awt。*; java.awt.event。*; java.util.randomをインポートします。パブリッククラスのshudはjframeを拡張します{private static final long serialversionuid = 5952689219411916553l; // serialize field private static jtextfield a [] [] = new jtextfield [9] [9]; //テキストボックスの番号を保存Static Int ans [] [] = new int [9] [9]; //入力2桁のアレイsudokupuzzlegeneratorの保存例= new sudokupuzzlegenerator(); public int right [] [] = example.generepuzzLematrix(); public int rights [] []; private int [] [] wk(int a [] []){//非表示のランダム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); } return a; } public shud(){container c = getContentPane(); C.SetLayout(新しいBorderlayout(2、1)); // borderレイアウトjmenuitem jmiok = new jmenuitem( "submit"); //メニューを定義するjmenuitem jmiexplain = new jmenuitem( "詳細"); jmenuitem jmimessage = new jmenuitem( "Information"); jpanelパネル= new JPanel(); //コンテナパネルを定義します。Add(jmiok); //コンテナパネルにメニューを表示します。Add(jmiexplain); panel.add(jmimessage); jPanel P1 = new JPanel(New Gridlayout(9、9、5、5)); // 9行と9列の追加のグリッドレイアウトを定義します(パネル、borderlayout.north); //メニューを北に配置する(P1、borderlayout.center); //数字を中央の右ans= 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] .setedable(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(){//匿名イベントリスナーpublic void actionperformed(actionevent e){if(gettext()== 1){if(gud()== true){if(jod()= true){joptionpane.showmessagegog(null、 "null、" null、 "正しい! "、" results "、joptionpane.information_message);} else {joptionpane.showmessageialog(null、" your ness as as your ness is your nows is orid! "、" result "、joptionpane.information_message);} else {joptionpane.showmessialog(null、" null、 "null、" null、 "間違って! "、" result "、joptionpane.information_message);}}}}});説明listenerclassリスナー2 = new explastListenerClass(); jmiexplain.addactionlistener(listener2); MessageListenerclassリスナー3 = new MessageListenerClass(); jmimessage.addactionlistener(listerer3); } 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] [n] .getText()); //回答タイプをans} catch(numberformatexception nfe){joptionpane.showmessageialog(null、 "データに非数を含める、再入力してください"); 0を返します。 }}} return 1; } public static boolean Judge()//入力回答が正しいかどうかを判断{int i、j、k; int [] [] answer = ans; for(i = 0; i <9; i ++){if(j(answer [i])== false)//各列に重複した数字があるかどうかを判断します。 } for(j = 0; j <9; j ++)//各行に重複した数値があるかどうかを判断{int [] newanswerumn = new int [9]; for(i = 0; i <9; i ++){newanswercolumn [i] = answer [i] [j]; } if(j(newanswerumn)== false)falseを返します。 } for(i = 0; i <3; i ++)//各9番目のグリッドに重複した数値があるかどうかを判断{for(j = 0; j <3; j ++){k = 0; int [] newanswer = new int [9]; for(int m = i * 3; m <i * 3 +3; m ++){for(int n = j * 3; n <j * 3 +3; n ++){newanswer [k] = answer [m] [n]; K ++; }} if(judger9(newanswer)== false){return false; }} trueを返します。 } public static boolean Judge9(int [] Answer){int i、j; for(i = 0; i <9; i ++){for(j = 0; j <9; j ++){if(i == j)継続; if(Answer [i] == Answer [j])//重複番号がある場合、falseを返します{return false; }}} return true; //複製番号なし、trueを返してくださいframe.settitle( "suduku"); frame.setsize(600,900); frame.setlocationRelativeto(null); frame.setDefaultCloseoperation(jframe.exit_on_close); frame.setVisible(true); }} class explageListenerclassはactionlistener {// event ristenerpublic void actionperformed(actionevent e){joptionpane.showmessagedialog(null、 "null、"各列、各列、各列、およびそれぞれの小さなnineの数の複製がないことを確認するために数字を入力してください。 }} class messageListenerclassはactionlistener {public void actionperformed(actionevent e){joptionpane.showmessageialog(null、 "by wyx"、 "message"、joptionpane.information_message); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。