数独游戏的规则从很久之前就知道,但是一直都没怎么玩过,然后到了大学,大一下学期自己学dfs的时候,刚刚好碰到了一个数独的题目,做出来后,感觉还是挺有成就感的
然后大二学了JAVA,看了下那个一些有关于界面的一些函数的使用这些,就写出来一个比较粗糙的数独游戏,这个游戏我打算一直维护更新,直到我大学毕业,看看最后可以变成什么样子
1 public class Main { 2 public static void main(String[] args) { 3 gra f = new gra("数独",400,400,400,400); 4 } 5 }
1 package sodu; 2 import java.io.*; 3 import java.util.*; 4 import java.math.*; 5 import java.security.PrivateKey; 6 import java.awt.*; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 10 import javax.security.auth.login.CredentialExpiredException; 11 import javax.security.cert.CertificateNotYetValidException; 12 import javax.swing.*; 13 import javax.swing.text.AbstractDocument.LeafElement; 14 15 import org.omg.PortableServer.ServantLocator; 16 public class gra extends JFrame { 17 Scanner cin = new Scanner(System.in); 18 Random ran = new Random(); 19 JMenuBar mbar; 20 JMenuItem ito,itt,its; 21 JMenu menu; 22 JTextField [][]tx = new JTextField [9][9]; 23 GridLayout gri; 24 JPanel chess= new JPanel();; 25 int[][] ans = new int[9][9]; 26 int []a = new int [11]; 27 int [][]num = new int[9][9]; 28 int [][]fat = { 29 {8,7,1,9,3,2,6,4,5}, 30 {4,9,5,8,6,1,2,3,7}, 31 {6,3,2,7,5,4,8,1,9}, 32 {5,2,8,4,7,3,1,9,6}, 33 {9,1,3,6,2,5,7,8,4}, 34 {7,6,4,1,9,8,3,5,2}, 35 {2,8,7,3,4,9,5,6,1}, 36 {1,4,6,5,8,7,9,2,3}, 37 {3,5,9,2,1,6,4,7,8}, 38 }; 39 gra(){ 40 } 41 gra(String name,int a,int b,int c,int d){ 42 ints(name); 43 setLocation(a,b); 44 setSize(c,d); 45 setVisible(true); 46 set(); 47 setDefaultCloseOperation(EXIT_ON_CLOSE); 48 } 49 void Show(){ 50 chess.updateUI(); 51 chess.removeAll(); 52 gri = new GridLayout(9,9); 53 for(int i = 0 ; i < 9 ; i++) 54 for(int j = 0 ; j <9 ; j++){ 55 String s = String.valueOf(num[i][j]); 56 int n = ran.nextInt(13); 57 if(n%4==0){ 58 ans [i][j] = num [i][j]; 59 JLabel text = new JLabel(s,JLabel.CENTER); 60 text.setForeground(Color.white); 61 chess.add(text); 62 }else{ 63 tx[i][j] = new JTextField(); 64 tx[i][j].setHorizontalAlignment(SwingConstants.CENTER); 65 chess.add(tx[i][j]); 66 } 67 } 68 chess.setLayout(gri); 69 chess.setBackground(Color.DARK_GRAY); 70 add(chess); 71 } 72 void set(){ 73 ito.addActionListener(new ActionListener(){ 74 public void actionPerformed(ActionEvent e) { 75 setVisible(false); 76 calc(); 77 Show(); 78 setVisible(true); 79 } 80 }); 81 itt.addActionListener(new ActionListener(){ 82 public void actionPerformed(ActionEvent e) { 83 judge(); 84 } 85 }); 86 its.addActionListener(new ActionListener() { 87 public void actionPerformed(ActionEvent e) { 88 setVisible(false); 89 } 90 }); 91 92 } 93 void ints(String s){ 94 setTitle(s); 95 mbar = new JMenuBar(); 96 menu = new JMenu("菜单"); 97 ito = new JMenuItem("开始"); 98 itt = new JMenuItem("提交"); 99 its = new JMenuItem("退出"); 100 menu.add(ito); 101 menu.add(itt); 102 menu.add(its); 103 mbar.add(menu); 104 setJMenuBar(mbar); 105 } 106 void calc(){ 107 boolean []b = new boolean[11]; 108 int i = 0; 109 b[0] = true; 110 while(i<9){ 111 int n = ran.nextInt(10); 112 if(!b[n]) { 113 a[i++] = n; 114 b[ n ] = true; 115 } 116 } 117 for(int k = 0 ; k < 9 ; k++) 118 for(int m = 0 ; m < 9 ; m++) 119 for(int n = 0 ; n < 9 ; n++){ 120 if(fat[m][n]==a[ k ] && k != 8){ 121 num[m][n] = a[k+1]; 122 }else if(fat[m][n]==a[k]&&k == 8){ 123 num[m][n] = a[0]; 124 } 125 } 126 void judge(){ 127 int falg = 0; 128 for(int i = 0 ; i < 9 ; i++) 129 for(int j = 0 ; j < 9 ; j++){ 130 if(ans[i][j] == 0) { 131 int tmp = Integer.valueOf(tx[i][j].getText()); 132 ans[i][j] = tmp; 133 } 134 } 135 for(int i = 0 ; i < 9 ; i++){ 136 boolean[]h = new boolean[10]; 137 boolean[]s = new boolean[10]; 138 for(int j = 0 ; j < 9 ; j++){ 139 if(!h[ans[i][j]]&&!s[ans[i][j]]){ 140 h[ans[i][j]] = true; 141 s[ans[i][j]] = true; 142 }else { 143 falg = 1; 144 break; 145 } 146 } 147 if(falg == 1) break; 148 } 149 if(falg == 1) { 150 JFrame a = new JFrame("错误"); 151 JTextField text = new JTextField("你的答案错了"); 152 text.setHorizontalAlignment(SwingConstants.CENTER); 153 a.add(text); 154 a.setSize(200,200); 155 a.setLocation(500,500); 156 a.setVisible(true); 157 }else{ 158 JFrame a = new JFrame("成功"); 159 JTextField text = new JTextField("你的答案正确"); 160 text.setHorizontalAlignment(SwingConstants.CENTER); 161 text.setForeground(Color.red); 162 a.add(text); 163 a.setSize(200,200); 164 a.setLocation(500,500); 165 a.setVisible(true); 166 } 167 } 168 } 169