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 cells;
}
public int [][] changeLine(int[][] cells,int m){//对数组进行行与行交换
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];
cells[m][j]=cells[n][j];
cells[n][j]=temp[j];
}
return cells;
}
public int[][] lineTolie(int[][] cells){//对数组进行行与列交换
int temp=0;
for(int j=0;j<9;j++){
for(int k=j+1;k<9;k++){
temp=cells[k][j];
cells[k][j]=cells[j][k];
cells[j][k]=temp;
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();//获取数独数组
final JPanel jpl = new JPanel();//建立JPanel对象
final int spaceNum = 50;//spaceNum表示需要设置空白TextField的数量
jpl.setLayout(new GridLayout(9, 9));//JPanel布局
final int[][] cellAn = new int[9][9];//数独数组的答案
System.arraycopy(cells, 0, cellAn, 0, cells.length);//答案从建立的数独数组中Copy
for (int i = 0; i < 9; i++) {//把答案从Console打印出来
final int[][] tempArray = new int[spaceNum][2];
for (int i = 0; i < spaceNum; i++) {//依据需要空白的TextField数量,随机对TextField设置为空
final int ranD1 = new Random().nextInt(9);
final int ranD2 = new Random().nextInt(9);
tempArray[0] = ranD1;
tempArray[1] = ranD2;
txtGame[ranD1][ranD2].setText("");
txtGame[ranD1][ranD2].setBackground(Color.WHITE);
txtGame[ranD1][ranD2].addTextListener(new TextListener() {//对空白的TextField添加监听,数值发生变化后进行答案对比,如果全部答对在Console打印“good”
@Override
public void textValueChanged(TextEvent e) {
TextField tmp = (TextField) e.getSource();
int count = 0;
for (int u = 0; u < spaceNum; u++) {
if ((txtGame[tempArray[0]][tempArray[1]]
.getText())
.equals(Integer
.toString(cellAn[tempArray[0]][tempArray[1]]))) {
count++;
}
}
if (count == spaceNum) {
static 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();// 获取数独数组
final JPanel jpl = new JPanel();// 建立JPanel对象
final int spaceNum = num;// spaceNum表示需要设置空白TextField的数量
jpl.setLayout(new GridLayout(9, 9));// JPanel布局
final int[][] cellAn = new int[9][9];// 数独数组的答案
System.arraycopy(cells, 0, cellAn, 0, cells.length);// 答案从建立的数独数组中Copy
for (int i = 0; i < 9; i++) {// 把答案从Console打印出来
for (int i = 0; i < spaceNum; i++) {// 依据需要空白的TextField数量,随机对TextField设置为空
final int ranD1 = new Random().nextInt(9);
final int ranD2 = new Random().nextInt(9);
tempArray[i][0] = ranD1;
tempArray[i][1] = ranD2;
txtGame[ranD1][ranD2].setText("");
public void addListener() {// 设置空白TextField 并添加监听
final int[][] tempArray = new int[num][2];
for (int i = 0; i < num; i++) {// 依据需要空白的TextField数量,随机对TextField设置为空
final int ranD1 = new Random().nextInt(9);
final int ranD2 = new Random().nextInt(9);
tempArray[i][0] = ranD1;
tempArray[i][1] = ranD2;
txtGame[ranD1][ranD2].setText("");
txtGame[ranD1][ranD2].setForeground(Color.BLUE);
if ((ranD1 < 3 && ranD2 < 3)
|| (ranD1 < 6 && ranD1 >= 3 && ranD2 >= 3 && ranD2 < 6)
|| (ranD1 < 9 && ranD1 >= 6 && ranD2 >= 6 && ranD2 < 9)) {
txtGame[ranD1][ranD2].setBackground(Color.ORANGE);
txtGame[ranD1][ranD2].addTextListener(new TextListener() {// 对空白的TextField添加监听,数值发生变化后进行答案对比,如果全部答对在Console打印“good”
@Override
public void textValueChanged(TextEvent e) {
int count = 0;
for (int u = 0; u < num; u++) {
if ((txtGame[tempArray[u][0]][tempArray[u][1]]
.getText())
.equals(Integer
.toString(cellAn[tempArray[u][0]][tempArray[u][1]]))) {
count++;
}
}
if (count == num) {
jpl.removeAll();
FlowLayout blt = new FlowLayout();
jpl.setLayout(blt);
if (num <= 50) {
jpl.add(new JLabel("恭喜你过关"));
Button btn = new Button("进入下一关");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
num = num + add;
CopyOfSudoku.this.setLayout(null);
jpl.removeAll();
jpl.setLayout(new GridLayout(9, 9));
jpnl.removeAll();
printFrame();
addListener();
}
});