import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Stack;
import java.util.Timer;
public class NewYear extends Frame {
static Label Lb = new Label("Time Count");
int i, j, k, p, t = 1;
@Override
public void paint(Graphics g) {
String c;
g.setColor(Color.WHITE);
g.fill3DRect(35, 35, 405, 405, true);
g.setColor(Color.pink);
for (i = 1; i < 11; i++) {
g.drawLine(40 * i + 20, 60, 20 + 40 * i, 420);
g.drawLine(60, 40 * i + 20, 420, 40 * i + 20);
}
g.setColor(Color.black);
for (i = 0; i < 4; i++) {
g.drawLine(60 + 120 * i, 60, 60 + 120 * i, 420);
g.drawLine(60, 60 + 120 * i, 420, 60 + 120 * i);
}
g.setColor(Color.BLUE);
for (i = 0; i < 9; i++) {
c = String.format("%c", 49 + i);
g.drawString(c, 75 + 40 * i, 58);
}
for (i = 0; i < 9; i++) {
c = String.format("%c", 65 + i);
g.drawString(c, 45, 85 + 40 * i);
}
} //对九宫格进行定位
static void CreateShuDu(int a[][]) {
int i, j, k, p, t = 1;
int b[] = new int[9];
int mid[][] = new int[9][9];
i = 0;
while (i < 9) {
j = 1;
b[i] = 1 + (int) (Math.random() * 9);
for (k = 0; k < i; k++) {
if (b[k] == b[i]) {
j = 0;
}
}
if (j != 0) {
b[i] = b[i];
i++;
}
}
t = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
a[i][j] = b[t];
t++;
}
} // //第1宫
for (i = 0; i < 3; i++) {
for (j = 1; j < 3; j++) {
a[i + 3][j - 1] = a[i][j];
}
}
for (i = 0; i < 3; i++) {
a[i + 3][2] = a[i][0];
} // //第4宫
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
a[i + 6][j + 1] = a[i][j];
}
}
for (i = 0; i < 3; i++) {
a[i + 6][0] = a[i][2];
} // //第6宫
for (t = 1; t < 9; t++) {
for (i = 0; i < 3; i++) {
a[t - 1][i + 3] = a[t][i];
}
}
for (i = 0; i < 3; i++) {
a[8][i + 3] = a[0][i];
} // //第 2,5,7宫
for (t = 1; t < 9; t++) {
for (i = 3; i < 6; i++) {
a[t - 1][i + 3] = a[t][i];
}
}
for (i = 0; i < 3; i++) {
a[8][i + 6] = a[1][i];
} // //第3,6,9宫
for (i = 0; i < 9; i++) {
b[i] = a[i][6];
a[i][6] = a[i][7];
a[i][7] = a[i][8];
a[i][8] = b[i];
} //6 ,7,8,列互换
for (i = 0; i < 9; i++) {
b[i] = a[i][0];
a[i][0] = a[i][2];
a[i][2] = b[i];
}
for (i = 0; i < 9; i++) {
b[i] = a[8][i];
a[8][i] = a[7][i];
a[7][i] = a[6][i];
a[6][i] = b[i];
} //6,7,8,行互换
/*
九宫格的生成 ,生成了一个有解的九宫格
*/
}
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的个数,则将他的位置保留下来
}
}
}
if (iMinNum == 0) {
DataItem item = trackStack.pop(); //栈顶的元素出栈
item.value = H[iMinNum - 1];
}
DataItem item2 = new DataItem();
item2.i = iRow;
item2.j = iColumn;
item2.value = H[iMinNum + 1];
trackStack.push(item2);
a[iRow][iColumn] = H[iMinNum + 1];
}
static boolean CheckFinished(int a[][]) {
boolean bFinished = true;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (a[i][j] == 0) {
bFinished = false;
break;
}
}
}
return bFinished;
}
public static void ShowShuDu(int a[][], Graphics g) {
int i, j, k, p, t = 1;
int mid[][] = new int[9][9];
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
mid[i][j] = a[i][j];
}
}
for (k = 0; k < 45; k++) {
i = 1 + (int) (Math.random() * 9);
j = 1 + (int) (Math.random() * 9);
a[i - 1][j - 1] = 0;
}
//挖洞法生成了数独 随机的将<=45个位置赋值为0
//随机生成的数组放在了a[9][9]中了,其中mid[9][9]保留的是原来的数独,即一个完整的数独
Font f1 = new Font("宋体", Font.BOLD, 16);
String a0;
g.setColor(Color.RED);
for (j = 0; j < 9; j++) {
for (i = 0; i < 9; i++) {
a0 = String.format("%c", 48 + a[j][i]);
if (a0.equals("0")) {
a0 = "";
}
g.setFont(f1);
g.drawString(a0, 75 + 40 * i, 84 + 40 * j);
}
}
}
}
按b1开始计时,按b2显示答案
15 个解决方案
#1
接上面
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;
}*/
}
}
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;
}*/
}
}
#2
求修改,当b1按下时,开始计时,按下b2时间停止,答案显示出来
#3
非常的着急,请大家帮忙
#4
数独不是很会
#5
生成数独没做过
只写过解答数独的
只写过解答数独的
#6
#7
数独是什么,没研究过。
#8
说说你的思路呀,,这么多,谁没事研究你代码呀
#9
首先生成一个数独,生成数独就是先产生一个宫,然后变换出一个数独,解数独是用先找到最小的可填入点,不行就出栈,载重新填,我想的是再按b1的时候那个时间开始走,在按b2的时候时间停止,同时显示出答案。谢谢大家回复我的帖子,我真的很着急,大家知道的快点帮帮我吧
#10
我无解......
#11
大家快点帮帮我,在线等待回复
#12
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久以前写的 希望对你有帮助
#13
谢谢你啦
#14
你没向栈里放,却要往外出栈能不出错吗, 异常先在没有了, 第一个数好像是错的,剩下的自己改吧
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 ];
}
}
#15
谢谢你啦
#1
接上面
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;
}*/
}
}
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;
}*/
}
}
#2
求修改,当b1按下时,开始计时,按下b2时间停止,答案显示出来
#3
非常的着急,请大家帮忙
#4
数独不是很会
#5
生成数独没做过
只写过解答数独的
只写过解答数独的
#6
#7
数独是什么,没研究过。
#8
说说你的思路呀,,这么多,谁没事研究你代码呀
#9
首先生成一个数独,生成数独就是先产生一个宫,然后变换出一个数独,解数独是用先找到最小的可填入点,不行就出栈,载重新填,我想的是再按b1的时候那个时间开始走,在按b2的时候时间停止,同时显示出答案。谢谢大家回复我的帖子,我真的很着急,大家知道的快点帮帮我吧
#10
我无解......
#11
大家快点帮帮我,在线等待回复
#12
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久以前写的 希望对你有帮助
#13
谢谢你啦
#14
你没向栈里放,却要往外出栈能不出错吗, 异常先在没有了, 第一个数好像是错的,剩下的自己改吧
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 ];
}
}
#15
谢谢你啦