import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
/**
* 数独的面板
* @author 夕阳
*
*/
public class Bord extends JPanel{
/**
* 用来设定显示的风格(常量)
*/
public final int STYLE_1 = 1, STYLE_2 = 2, STYLE_3 = 3;
/**
* 页面风格(默认为风格1)
*/
private int style = STYLE_1;
/**
* 游戏难度,缺省值为3,共5个级别
*/
private int lvl = 3;
/**
* 用来显示用户的输入界面
*/
private JButton[][] bords = new JButton[9][9];
/**
* 用来处理相应的数据
*/
private int[][] form1 = new int[9][9];
/**
* 用来辅助form1进行相关判断
*/
private int[][] form2 = new int[9][9];
/**
* 再定义一个form3用来检验答案是否唯一
*/
private int[][] form3 = new int[9][9];
/**
* 用来储存form1 的数据,用于重新开始菜单
*/
private int[][] form4 = new int[9][9];
/**
* 为了方便控制,将小窗口的Button 放在 Bord 的 data fields里面
*/
JButton[] numbers = new JButton[9];
/**
* 用来选择的副窗口
*/
JFrame subFrame = new JFrame();
/**
* 用来储存待检查的数字z
*/
private int z;
/**
* 用来储存位置
*/
private int i = 0, j = 0;
/**
* 无参构造器,其中包含一些初始化处理
*/
public Bord(){
/**
* 初始化subFrame
*/
subFrame.setTitle("请选择要填入的数字");
for(int i = 0; i < 9; i++){
numbers[i] = new JButton(new ImageIcon("image/" + (i + 1) + "g" + style + ".gif"));
numbers[i].addActionListener(new NumActionListener());
subFrame.add(numbers[i]);
}
subFrame.pack();
subFrame.setLayout(new GridLayout(3,3));
/**
* 设置布局GridLayout(9,9)
*/
this.setLayout(new GridLayout(9,9));
for(int x = 0; x < 9; x++){
for(int y = 0; y < 9; y++){
/**
* 实例化每一个JButton
*/
bords[x][y] = new JButton(new ImageIcon("image/0.gif"));
bords[x][y].setSize(34, 38);
/**
* 添加到面板中
*/
bords[x][y].addActionListener(new LabelActionListener());
this.add(bords[x][y]);
}
}
setBackground(3, 0, 5, 2);
setBackground(0, 3, 2, 5);
setBackground(6, 3, 8, 5);
setBackground(3, 6, 5, 8);
//让格子不能填
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++)
bords[i][j].setEnabled(false);
}
}
/**
* 将form1的数据还原成初值
*/
public void restart(){
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++){
form1[i][j] = form4[i][j];
if(form1[i][j] != 0)
bords[i][j].setIcon(new ImageIcon("image/" + form1[i][j] + "g" + getStyle() + ".gif"));
else
bords[i][j].setIcon(new ImageIcon("image/0.gif"));
}
}
/**
* 用来更新面板的风格
* @param n 风格
*/
public void update(int n){
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if(form1[i][j] != 0){
if(bords[i][j].getIcon().getIconHeight() == 37)
bords[i][j].setIcon(new ImageIcon("image/" + form1[i][j] + "t" + this.style + ".gif"));
else
bords[i][j].setIcon(new ImageIcon("image/" + form1[i][j] + "g" + this.style + ".gif"));
}
else
bords[i][j].setIcon(new ImageIcon("image/0.gif"));
}
}
}
/**
* @return 返回界面风格
*/
public int getStyle(){
return this.style;
}
/**
* 改变面板的风格
* @param n 风格类型
*/
public void setStyle(int n){
this.style = n;
}
/**
* 用来检查一个输入是否合法
* @param x 数据的x坐标
* @param y 数据的y坐标
* @param z 待检查的数据
* @param form 要检查的数据表格
* @return 如果没有冲突则返回TRUE,否则返回FALSE
*/
private boolean check(int x, int y, int z, int[][] form){
boolean isOk = true;
if(form[x][y] != 0)
isOk = false;
for(int i = 0; i < 9; i++){
if(form[i][y] == z || form[x][i] == z)
isOk = false;
}
for(int i = (x / 3) * 3; i <(x / 3) * 3 + 3; i++){
for(int j = ( y / 3) * 3; j < (y / 3) * 3 + 3; j++){
if(form[i][j] == z)
isOk = false;
}
}
return isOk;
}
/**
* 用来检查一个输入是否合法,不合法则提示用户
* @param x 数据的x坐标
* @param y 数据的y坐标
* @param z 待检查的数据
* @param form 要检查的数据表格
*/
private void checkAndPrint(int x, int y, int z, int[][] form){
for(int i = 0; i < 9; i++){
if(form[i][y] == z){
JOptionPane.showMessageDialog(
null,"填入数字与第"+(i+1)+"行第"+(y+1)+"列数字冲突!请从新填入!");
return;
}
}
for(int i = 0; i < 9; i++){
if(form[x][i] == z){
JOptionPane.showMessageDialog(
null,"填入数字与第"+(x+1)+"行第"+(i+1)+"列数字冲突!请从新填入!");
return;
}
}
for(int i = (x / 3) * 3; i <(x / 3) * 3 + 3; i++){
for(int j = ( y / 3) * 3; j < (y / 3) * 3 + 3; j++){
if(form[i][j] == z){
JOptionPane.showMessageDialog(
null,"填入数字与第"+(i+1)+"行第"+(j+1)+"列数字冲突!请从新填入!" );
return;
}
}
}
}
/**
* 和upcheck()方法相互调用,用来判断是否是死局。当然也可用来智能解题,和用来随机产生一个数独题
* @param x 只含一个数的一位数组,用来 表示x坐标
* @param y 只含一个数的一位数组,用来 表示y坐标
* @param a 待检查的数组
* @param b 辅助的数组
* @return 若x > 8则返回TRUE; x < 0 则返回FALSE
*/
private boolean downCheck(int[] x, int[] y, int[][] a,int[][] b){
if(y[0] > 8){ //check y[0]
y[0] = 0; //换行
x[0] += 1;
if(x[0] > 8)
return true;
}
if(x[0] < 0)
return false;
else if(x[0] > 8)
return true;
else{
while(a[x[0]][y[0]] != 0){
y[0] += 1;
if(y[0] > 8){
y[0] = 0;
x[0] += 1;
if(x[0] > 8)
return true;
}
} //to examine whether this unit has a number
body:{ for(int i = 1; i < 10; i++){
if(check(x[0],y[0],i,b)){ //to check whether a particular number can be put there
b[x[0]][y[0]] = i; //to store number i
y[0] += 1;
downCheck(x,y,a,b);
break body; //if finding one then break
}
}
b[x[0]][y[0]] = 0; //clear b[x][y[0]]
y[0] -= 1;
upCheck(x,y,a,b); //the last unit
}
}
if(x[0] > 8){ //check again
return true;
}
else
return false;
}
/**
* 和downCheck方法相互调用,用来判断是否是死局。当然也可用来智能解题,和用来随机产生一个数独题
* @param x 只含一个数的一位数组,用来 表示x坐标
* @param y 只含一个数的一位数组,用来 表示y坐标
* @param a 待检查的数组
* @param b 辅助的数组
*/
private void upCheck(int[] x, int[] y, int[][] a, int[][] b){
if(y[0] < 0){ //check y[0]
y[0] = 8;
x[0] -= 1;
if(x[0] < 0)
return;
}
while(a[x[0]][y[0]] != 0){ //move back
y[0] -= 1;
if(y[0] < 0){
y[0] = 8;
x[0] -= 1;
if(x[0] < 0)
return;
}
}
body: {for(int i = b[x[0]][y[0]] + 1; i < 10; i++){
b[x[0]][y[0]] = 0;
if(check(x[0],y[0],i,b)){
b[x[0]][y[0]] = i;
y[0] += 1;
downCheck(x,y,a,b);
break body;
}
}
b[x[0]][y[0]] = 0;
y[0] -= 1;
upCheck(x,y,a,b);
}
if(x[0] < 0)
return;
}
/**
* 和downChcek方法一样,只不过是从9~1检索。
* @param x 只含一个数的一位数组,用来 表示x坐标
* @param y 只含一个数的一位数组,用来 表示y坐标
* @param a
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java 数独游戏 带源码 用户手册 设计文档 (145个子文件)
双击运行.bat 20B
新建位图图像.bmp 0B
Bord.class 12KB
Main.class 11KB
Main$5.class 3KB
Bord$NumActionListener.class 2KB
Main$18.class 2KB
Main$6.class 2KB
Bord$LabelActionListener.class 2KB
Main$17.class 1KB
Main$7.class 1KB
Main$13.class 1KB
Main$14.class 1KB
Main$8.class 1KB
FixedLengthStringIO.class 1KB
Main$1.class 1KB
Main$12.class 1009B
Main$11.class 1009B
Main$10.class 1009B
Main$3.class 912B
Main$4.class 901B
Main$2.class 889B
Main$4$1.class 831B
Main$7$1.class 831B
Main$8$1.class 831B
Main$15.class 776B
Main$16.class 745B
Main$6$1.class 711B
Main$5$1.class 711B
Main$9.class 630B
Bord$1.class 163B
stylesheet.css 1KB
rank_5.dat 30B
rank_3.dat 30B
rank_2.dat 30B
rank_4.dat 30B
rank_1.dat 30B
用户手册.docx 1.39MB
新建 Microsoft Office Word 文档.docx 12KB
~$简单游戏.docx 162B
lvl3.gif 3KB
lvl4.gif 3KB
lvl5.gif 3KB
lvl2.gif 3KB
lvl1.gif 3KB
jin.gif 2KB
tong.gif 2KB
yin.gif 2KB
8g2.gif 2KB
3g2.gif 2KB
6g2.gif 2KB
9g2.gif 2KB
5g2.gif 2KB
9t2.gif 2KB
6t2.gif 2KB
5t2.gif 2KB
2g3.gif 2KB
8t2.gif 2KB
3t2.gif 2KB
8g3.gif 2KB
9g3.gif 2KB
2g2.gif 2KB
6g3.gif 2KB
3g3.gif 2KB
5g3.gif 2KB
1g2.gif 2KB
2t3.gif 1KB
4g2.gif 1KB
2t2.gif 1KB
4g3.gif 1KB
2g1.gif 1KB
5t3.gif 1KB
1t2.gif 1KB
4t2.gif 1KB
3t3.gif 1KB
8t3.gif 1KB
6t3.gif 1KB
9g1.gif 1KB
9t3.gif 1KB
7g3.gif 1KB
8g1.gif 1KB
4g1.gif 1KB
7g2.gif 1KB
2t1.gif 1KB
9t1.gif 1KB
3g1.gif 1KB
6g1.gif 1KB
4t3.gif 1KB
5g1.gif 1KB
4t1.gif 1KB
7t3.gif 1KB
6t1.gif 1KB
7t2.gif 1KB
8t1.gif 1KB
3t1.gif 1KB
1g3.gif 1KB
7g1.gif 1KB
1g1.gif 1KB
5t1.gif 1KB
1t3.gif 1KB
共 145 条
- 1
- 2
lujian863
- 粉丝: 6
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页