接上面 public static void main(String args[]) { int a[][] = new int[9][9]; NewYear fr = new NewYear("Shu Du---版权硕有"); fr.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(1); } }); fr.setSize(560, 460); fr.setBackground(Color.CYAN); fr.setVisible(true); fr.setLayout(null); CreateShuDu(a); // ShowShuDu(a, fr.getGraphics()); Button b1 = new Button("Star GAME"); b1.setVisible(true); fr.add(b1); b1.setBounds(460, 60, 80, 30); Button b2 = new Button("Show Answer"); fr.add(b2); b2.setBounds(460, 110, 80, 30); Lb.setBounds(460, 150, 80, 30); fr.add(Lb); Lb.setFont(new Font("宋体", 1, 13)); Lb.setText("00:00:00"); // timer=new Timer(10,this); b1.addActionListener(new ButtonHandler()); b2.addActionListener(new ButtonHandler()); do { SearchMinOptions(a); } while (CheckFinished(a)); /// ShowShuDu(a, fr.getGraphics()); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { System.out.print(a[i][j] + " "); } System.out.println(""); } } public NewYear(String str) { super(str); } static int min = 0, sec = 0, mSec = 0; static Timer timer; static class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { String timeStr, str1, str2, str3; if (e.getSource() == timer) { //时间更新 mSec++; if (mSec == 100) { mSec = 0; sec++; } if (sec == 60) { sec = 0; min++; } if (min == 60) { min = 0; } //显示新时间 if (min < 10) { str1 = "0" + min + ":"; } else { str1 = min + ":"; } if (sec < 10) { str2 = "0" + sec + ":"; } else { str2 = sec + ":"; } if (mSec < 10) { str3 = "0" + mSec; } else { str3 = mSec + ""; } timeStr = str1 + str2 + str3; Lb.setText(timeStr); } /* if (e.getSource() == b1) { timer.start(); } if (e.getSource() == b2) { timer.stop(); } if (e.getSource() == b1) { min = 0; sec = 0; mSec = 0; }*/ } }
package Sudoku; import java.awt.*; import javax.swing.*; import javax.swing.table.*; import java.awt.event.*; import java.util.*; /** */ public class SudokuApplet extends JApplet { JTable table = new JTable(9,9); JButton btnCheck = new JButton(); JLabel lblStatus = new JLabel(); GridBagLayout gridBagLayout1 = new GridBagLayout(); int aa[][]=new int [9][9];
public SudokuApplet() {}
public void init() { try { guiInit(); } catch(Exception e) { e.printStackTrace(); } } private boolean check() { // Effects: returns true if the state satisfies the basic Sudoku rule // check columns // check rows //check squares int i=0,j=0; try{ for (i = 0; i < 9; i++ ) for (j = 0; j < 9; j++ ) aa[j] = Integer.parseInt(table.getValueAt(i, j).toString()); } catch(Exception e){ aa[j]=0; } for(i=0;i<9;i++)//check the rows for(j=0;j<9;j++) if(aa[j]>9||aa[j]<1) return false;
for(i=0;i<9;i++) for(j=0;j<9;j++){ int p[]=new int [10]; p[aa[j]]++; if(p[aa[j]]>1) return false;
} for(j=0;j<9;j++){//check the columns int p[]=new int[10]; for(i=0;i<9;i++){ p[aa[j]]++; if(p[aa[j]]>1) return false; } } // Requires: startRow, startCol are either 0, 3, or 6 // Effects: returns true if the state satisfies the basic Sudoku rule
int startRow=0; int startCol=0; for(startRow=0;startRow<=6;startRow+=3)//check the squares for(i=startRow;i<startRow+3;i++){ for(startCol=0;startCol<=6;startCol+=3) for(j=startCol;j<startCol+3;j++){ int p[]=new int[10]; p[aa[j]]++; if(p[aa[j]]>1) return false; } } return true; }
public static void main(String args[]) {
int a[][] = new int[9][9];
NewYear fr = new NewYear("Shu Du---版权硕有");
fr.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
fr.setSize(560, 460);
fr.setBackground(Color.CYAN);
fr.setVisible(true);
fr.setLayout(null);
CreateShuDu(a);
// ShowShuDu(a, fr.getGraphics());
Button b1 = new Button("Star GAME");
b1.setVisible(true);
fr.add(b1);
b1.setBounds(460, 60, 80, 30);
Button b2 = new Button("Show Answer");
fr.add(b2);
b2.setBounds(460, 110, 80, 30);
Lb.setBounds(460, 150, 80, 30);
fr.add(Lb);
Lb.setFont(new Font("宋体", 1, 13));
Lb.setText("00:00:00");
// timer=new Timer(10,this);
b1.addActionListener(new ButtonHandler());
b2.addActionListener(new ButtonHandler());
do {
SearchMinOptions(a);
} while (CheckFinished(a));
/// ShowShuDu(a, fr.getGraphics());
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println("");
}
} public NewYear(String str) {
super(str);
}
static int min = 0, sec = 0, mSec = 0;
static Timer timer; static class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) {
String timeStr, str1, str2, str3;
if (e.getSource() == timer) {
//时间更新
mSec++;
if (mSec == 100) {
mSec = 0;
sec++;
}
if (sec == 60) {
sec = 0;
min++;
}
if (min == 60) {
min = 0;
}
//显示新时间
if (min < 10) {
str1 = "0" + min + ":";
} else {
str1 = min + ":";
}
if (sec < 10) {
str2 = "0" + sec + ":";
} else {
str2 = sec + ":";
}
if (mSec < 10) {
str3 = "0" + mSec;
} else {
str3 = mSec + "";
}
timeStr = str1 + str2 + str3;
Lb.setText(timeStr);
} /* if (e.getSource() == b1) {
timer.start();
} if (e.getSource() == b2) {
timer.stop();
} if (e.getSource() == b1) {
min = 0;
sec = 0;
mSec = 0;
}*/
}
}
package Sudoku;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.util.*;
/**
*/
public class SudokuApplet extends JApplet {
JTable table = new JTable(9,9);
JButton btnCheck = new JButton();
JLabel lblStatus = new JLabel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
int aa[][]=new int [9][9];
public SudokuApplet() {}
public void init() {
try {
guiInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private boolean check() {
// Effects: returns true if the state satisfies the basic Sudoku rule
// check columns
// check rows
//check squares
int i=0,j=0;
try{
for (i = 0; i < 9; i++ )
for (j = 0; j < 9; j++ )
aa[j] = Integer.parseInt(table.getValueAt(i, j).toString());
}
catch(Exception e){
aa[j]=0;
}
for(i=0;i<9;i++)//check the rows
for(j=0;j<9;j++)
if(aa[j]>9||aa[j]<1)
return false;
for(i=0;i<9;i++)
for(j=0;j<9;j++){
int p[]=new int [10];
p[aa[j]]++;
if(p[aa[j]]>1)
return false;
}
for(j=0;j<9;j++){//check the columns
int p[]=new int[10];
for(i=0;i<9;i++){
p[aa[j]]++;
if(p[aa[j]]>1)
return false;
}
}
// Requires: startRow, startCol are either 0, 3, or 6
// Effects: returns true if the state satisfies the basic Sudoku rule
int startRow=0;
int startCol=0;
for(startRow=0;startRow<=6;startRow+=3)//check the squares
for(i=startRow;i<startRow+3;i++){
for(startCol=0;startCol<=6;startCol+=3)
for(j=startCol;j<startCol+3;j++){
int p[]=new int[10];
p[aa[j]]++;
if(p[aa[j]]>1)
return false;
}
}
return true;
}
private void guiInit() throws Exception {
table.setEnabled(true);
table.setRequestFocusEnabled(false);
table.setRowHeight(40);
table.setRowSelectionAllowed(false);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
table.setDefaultRenderer(Object.class, new CustomCellRenderer());
this.getContentPane().setLayout(gridBagLayout1);
btnCheck.setBorder(BorderFactory.createRaisedBevelBorder());
btnCheck.setActionCommand("btnCheck");
btnCheck.setText("Check");
btnCheck.addActionListener(new SudokuApplet_btnCheck_actionAdapter(this));
lblStatus.setText(" Sudoku Game");
this.getContentPane().add(table, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(57, 62, 0, 62), -200, 1));
this.getContentPane().add(btnCheck, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 276, 19, 260), 40, 8));
this.getContentPane().add(lblStatus, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 62, 0, 62), 478, 30));
}
//Action perform class
class SudokuApplet_btnCheck_actionAdapter implements java.awt.event.ActionListener {
SudokuApplet adaptee;
SudokuApplet_btnCheck_actionAdapter(SudokuApplet adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCheck_actionPerformed(e);
}
}
// Custom Render class to display table cell in different colors
private class CustomCellRenderer extends DefaultTableCellRenderer {
private Color pBLue = new Color(0, 128, 128);
private Color sGreen = new Color(51, 153, 102);
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
JLabel label = (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setHorizontalAlignment(JLabel.CENTER);
label.setFont(new Font("Dialog",Font.PLAIN,20));
label.setOpaque(true);
if((row /3 == 0 || row/3 == 2) &&
(column/3 == 0 || column /3 == 2) ||
(row/3 == 1 && column /3 == 1)) {
label.setBackground(sGreen);
}else{
label.setBackground(pBLue);
}
return label;
}
}
void btnCheck_actionPerformed(ActionEvent e) {
check();
if(check()) JOptionPane.showMessageDialog(null,"congratulations to you!","the result",
JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null,"Your numbers don't satisfy the basic Sudoku rule.",
"the result", JOptionPane.INFORMATION_MESSAGE);
}
}
一个简单的applet程序 N久以前写的 希望对你有帮助
static void SearchMinOptions(int a[][]) {
class DataItem {
public int i;
public int j;
public int value;
}
int iMaxCount = 0;
int iRow = 0, iColumn = 0;
int H[] = new int[10];
int iMinNum = 0;
int i, j, k, m;
// boolean bContinue = true;
Stack<DataItem> trackStack = new Stack<DataItem>();
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
if (a[i][j] == 0) {
int[] iFlag = new int[10];
for (m = 0; m < 10; m++) {
iFlag[m] = 0;
} // 待选的数都为0
for (k = 0; k < 9; k++) {
iFlag[a[i][k]] = 1;
iFlag[a[k][j]] = 1;
iFlag[a[i / 3 * 3 + k % 3][j / 3 * 3 + k % 3]] = 1;
} // 如果在行,列,宫,内出现了的,则将待选数赋值为1
int iCount = 0;
int iCurrMinNum = 0;
int t = 0;
for (m = 1; m < 10; m++) {
if (iFlag[m] == 1) {
iCount++; // 统计在行,列,宫内出现的数的个数
}
if (iFlag[m] == 0) {
iCurrMinNum = t++; // **********************************+++++++=========????????????????????????
H[t] = m;
}
}
if (iCount > iMaxCount) {
iRow = i;
iColumn = j;
iMaxCount = iCount;
iMinNum = iCurrMinNum;
} // 如果在行,列,宫内出现的数的个数大于iMaxCount的个数,则将他的位置保留下来
}
}
}
DataItem item2 = new DataItem();
item2.i = iRow;
item2.j = iColumn;
item2.value = H[iMinNum + 1];
trackStack.push(item2);
a[iRow][iColumn] = H[iMinNum + 1];
if (iMinNum == 0) {
DataItem item = trackStack.pop(); // 栈顶的元素出栈
item.value = H[iMinNum ];
}
}