この記事の例は、参照のためにJava Sudokuゲームの特定のコードを共有しています。特定のコンテンツは次のとおりです
私が書いたSudokuゲームには合計9つのレベルがあり、コードは次のとおりです。
1。Doshuduクラスは、Sudokuアレイを生成するために使用されます
java.util.randomをインポートします。 public class doshudu { / ** * @param args * / public static void main(string [] args){// todo auto-enerated method stub int [] [] cells = newshudu(); // Cells = Changeshu(Cell、9); for(int k = 0; k <9; k ++){for(int i = 0; i <9; i ++){system.out.print(cells [k] [i]); } system.out.println(); }} public static int [] [] newshudu(){int [] [] cells = new int [] [] {{1,2,3,4,5,5,6,7,8,9}、{4,5,1,2,3}、{7,8,9,9,1,2,3,3,4,5,6}、 {2,3,1,5,6,4,8,9,7,7}、{5,6,4,8,9,7,2,3,1}、{8,9,7,2,3,1,5,6,4,4,4,4,4}、{3,1,2,6,4,5,9,7,8,8}、 {9,7,8,3,1,2,6,4,5}}; int counth = new Random()。nextint(10); for(int k = 0; k <counth; k ++){cells = linetolie(cells); } int count = 0; for(int k = 0; k <12; k ++){count = new Random()。nextint(9); Cells = Changeline(Cells、Count); } int counth2 = new Random()。nextint(10); for(int k = 0; k <counth2; k ++){cells = linetolie(cells); } returnセル。 } public static int [] [] changeline(int [] [] cells、int m){// line and row exchange int n = m; int [] temp = new int [9]; n =((m+3)> = 9)?(m+3-9):m+3; for(int j = 0; j <9; j ++){temp [j] = cells [m] [j];セル[M] [j] =セル[n] [j];セル[n] [j] = temp [j]; } returnセル。 } public static int [] [] linetolie(int [] [] cells){// line and column Exchange int temp = 0; for(int j = 0; j <9; j ++){for(int k = j+1; k <9; k ++){temp = cells [k] [j];細胞[k] [j] =細胞[j] [k];セル[j] [k] =温度; }} return Cell; }} 2。インターフェイス操作クラス
java.awt.buttonをインポートします。 java.awt.colorをインポートします。 java.awt.flowlayoutをインポートします。 java.awt.fontをインポートします。 java.awt.gridlayoutをインポートします。 java.awt.pointをインポートします。 java.awt.textfieldをインポートします。 java.awt.event.mouseadapterをインポートします。 java.awt.event.MouseEventをインポートします。 java.awt.event.mousemotionAdapterをインポートします。 java.awt.event.texteventをインポートします。 java.awt.event.textlistenerをインポートします。 java.util.randomをインポートします。 javax.swing.jframeをインポートします。 javax.swing.jlabelをインポートします。 javax.swing.jpanelをインポートします。 com.sun.awt.awtutilitiesをインポートします。パブリッククラスSudokuはjframeを拡張します{最終的なプライベートテキストフィールド[] [] txtgame;静的int num = 20; //空白のスペースの数static int guan = 5; // static int add = 5; // public static void main(string [] args)を終了した後に追加された空白のスペースの数{sudoku shudu = new sudoku(); } public sudoku(){// JFrameおよびリスニング設定の初期レイアウトtxtgame = new Textfield [9] [9]; // 81 Textfieldオブジェクトを作成するDoshudu Shudu = new Doshudu(); int [] [] cells = shudu.getshudu(); // sudoku array final jpanel jpl = new jpanel(); // jpanelオブジェクト最終int spaceNum = num; // spaceNumは、jpl.setlayout(9、9 faniant(9、9); int [9] [9]; // sudoku array system.arraycopy(cells、0、cellan、0、length)への回答; //答えは、(int i = 0; i <9; i ++)の作成されたsudoku配列コピーからのものです。 } system.out.println(); } //印刷の終わり。 this.setsize(600、600); this.SetSizable(false); this.settitle( "dark Horse-liドイツ-Sukuゲーム9レベル"); for(int i = 0; i <9; i ++){for(int j = 0; j <9; j ++){txtgame [i] [j] = new 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.orange); } 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(new font( "dialog"、font.center_baseline、60)); // font size txtgame [i] [j] .settext(integer.tostring(cells [i] [j])); txtgame [i] [j] .setEnabled(false); txtgame [i] [j] .setVisible(true); jpl.add(txtgame [i] [j]); jpl.setVisible(true); }} final int [] [] temparray = new int [spacenum] [2]; final jframe jfm = new jframe( "select number"); // JFrameタイトルjfm.setundecorated(true)をキャンセルします。 // jframeドラッグ関数の追加final Point Origin = new Point(); jfm.addmouseListener(new MouseAdapter(){public void mousepressed(mouseEvent e){origin.x = e.getx(); origin.y = e.gety();}}); jfm.addmousemotionlistener(new MousemotionAdapter(){public void mousedRagged(mouseEvent e){point p = jfm.getlocation(); jfm.setlocation(px + e.getx() - ovirion.x、py + e.gety() - origin.y);}); // jframeを半透明のawtutivities.setWindowopacity(JFM、0.7F)に設定します。 final jpanel jpnl = new jpanel(new gridlayout(3、3)); jfm.setlayout(null); jfm.setsize(190、200); jfm.SetSizable(false); jpnl.setbounds(0、0、190、120); jfm.SetSizable(false); for(int i = 0; i <spacenum; i ++){// textfieldを空にするように設定します。最終的なint rand1 = new Random()。 final int rand2 = new Random()。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] [rand1] .setbackground(color.orange); } 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(color.green); } if((rand1 <9 && rand1> = 6 && rand2 <3)||(rand1 <9 && rand1> = 6 && rand2 <3)||(rand1 <1 <10 && rand1> = 6 && rand2 <3)||(rand1 <100 3 && rand2> = 3 && rand2 <6) {txtgame [rand1] [rand2] .setbackground(color.pink); } txtgame [rand1] [rand2] .addmouseListener(new Mouseadapter(){public void mouseClicked(mouseEvent mouseEvent){jfm.getContentPane()。新しいボタン(F + 1) + ""); // todo自動化された方法txtgame [rand2] .settext() + txtgame [rand2] .gettext()}}); btndel.setbackground(color.red); // todo自動化された方法txtgame [rand2]。 txtgame [rand1] [rand2] .addtextlistener(new TextListener(){//ブランクテキストフィールドにリスナーを追加する、値が変更された後の回答を比較し、コンソールで「良い」を印刷し、allsが@override public void textValueChanged(TextEvent e){textfield tmp = 0; inturce(in int count) spaceNum; u ++){(txtgame [u] [0]] [gettext()).equals(cellan [u] [0]] [temparray [u] [1])) JPL.REMOVEALL();マウスエベントe){// dispose() jpl.updateui(); out.println( "good")}); txtgame [rand1] [rand2] .setEnabled(true); } this.add(jpl); this.setVisible(true); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。