数独 JAVA实现

时间:2022-07-29 04:11:20

数独游戏的规则从很久之前就知道,但是一直都没怎么玩过,然后到了大学,大一下学期自己学dfs的时候,刚刚好碰到了一个数独的题目,做出来后,感觉还是挺有成就感的

然后大二学了JAVA,看了下那个一些有关于界面的一些函数的使用这些,就写出来一个比较粗糙的数独游戏,这个游戏我打算一直维护更新,直到我大学毕业,看看最后可以变成什么样子

 public class Main {
public static void main(String[] args) {
gra f = new gra("数独",400,400,400,400);
}
}
 package sodu;
import java.io.*;
import java.util.*;
import java.math.*;
import java.security.PrivateKey;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.security.auth.login.CredentialExpiredException;
import javax.security.cert.CertificateNotYetValidException;
import javax.swing.*;
import javax.swing.text.AbstractDocument.LeafElement; import org.omg.PortableServer.ServantLocator;
public class gra extends JFrame {
Scanner cin = new Scanner(System.in);
Random ran = new Random();
JMenuBar mbar;
JMenuItem ito,itt,its;
JMenu menu;
JTextField [][]tx = new JTextField [9][9];
GridLayout gri;
JPanel chess= new JPanel();;
int[][] ans = new int[9][9];
int []a = new int [11];
int [][]num = new int[9][9];
int [][]fat = {
{8,7,1,9,3,2,6,4,5},
{4,9,5,8,6,1,2,3,7},
{6,3,2,7,5,4,8,1,9},
{5,2,8,4,7,3,1,9,6},
{9,1,3,6,2,5,7,8,4},
{7,6,4,1,9,8,3,5,2},
{2,8,7,3,4,9,5,6,1},
{1,4,6,5,8,7,9,2,3},
{3,5,9,2,1,6,4,7,8},
};
gra(){
}
gra(String name,int a,int b,int c,int d){
ints(name);
setLocation(a,b);
setSize(c,d);
setVisible(true);
set();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
void Show(){
chess.updateUI();
chess.removeAll();
gri = new GridLayout(9,9);
for(int i = 0 ; i < 9 ; i++)
for(int j = 0 ; j <9 ; j++){
String s = String.valueOf(num[i][j]);
int n = ran.nextInt(13);
if(n%4==0){
ans [i][j] = num [i][j];
JLabel text = new JLabel(s,JLabel.CENTER);
text.setForeground(Color.white);
chess.add(text);
}else{
tx[i][j] = new JTextField();
tx[i][j].setHorizontalAlignment(SwingConstants.CENTER);
chess.add(tx[i][j]);
}
}
chess.setLayout(gri);
chess.setBackground(Color.DARK_GRAY);
add(chess);
}
void set(){
ito.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
setVisible(false);
calc();
Show();
setVisible(true);
}
});
itt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
judge();
}
});
its.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}); }
void ints(String s){
setTitle(s);
mbar = new JMenuBar();
menu = new JMenu("菜单");
ito = new JMenuItem("开始");
itt = new JMenuItem("提交");
its = new JMenuItem("退出");
menu.add(ito);
menu.add(itt);
menu.add(its);
mbar.add(menu);
setJMenuBar(mbar);
}
void calc(){
boolean []b = new boolean[11];
int i = 0;
b[0] = true;
while(i<9){
int n = ran.nextInt(10);
if(!b[n]) {
a[i++] = n;
b[ n ] = true;
}
}
for(int k = 0 ; k < 9 ; k++)
for(int m = 0 ; m < 9 ; m++)
for(int n = 0 ; n < 9 ; n++){
if(fat[m][n]==a[ k ] && k != 8){
num[m][n] = a[k+1];
}else if(fat[m][n]==a[k]&&k == 8){
num[m][n] = a[0];
}
}
void judge(){
int falg = 0;
for(int i = 0 ; i < 9 ; i++)
for(int j = 0 ; j < 9 ; j++){
if(ans[i][j] == 0) {
int tmp = Integer.valueOf(tx[i][j].getText());
ans[i][j] = tmp;
}
}
for(int i = 0 ; i < 9 ; i++){
boolean[]h = new boolean[10];
boolean[]s = new boolean[10];
for(int j = 0 ; j < 9 ; j++){
if(!h[ans[i][j]]&&!s[ans[i][j]]){
h[ans[i][j]] = true;
s[ans[i][j]] = true;
}else {
falg = 1;
break;
}
}
if(falg == 1) break;
}
if(falg == 1) {
JFrame a = new JFrame("错误");
JTextField text = new JTextField("你的答案错了");
text.setHorizontalAlignment(SwingConstants.CENTER);
a.add(text);
a.setSize(200,200);
a.setLocation(500,500);
a.setVisible(true);
}else{
JFrame a = new JFrame("成功");
JTextField text = new JTextField("你的答案正确");
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setForeground(Color.red);
a.add(text);
a.setSize(200,200);
a.setLocation(500,500);
a.setVisible(true);
}
}
}

gra