简单数独java源代码 数独代码Java
Java数独游戏代码
public class ShuDu {
创新互联是一家专注于成都做网站、网站制作与策划设计,米脂网站建设哪家好?创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:米脂等地区。米脂做网站价格咨询:13518219792
/**存储数字的数组*/
static int[][] n = new int[9][9];
/**生成随机数字的源数组,随机数字从该数组中产生*/
static int[] num = {1,2,3,4,5,6,7,8,9};
public static void main(String[] args) {
//生成数字
for(int i = 0;i 9;i++){
//尝试填充的数字次数
int time = 0;
//填充数字
for(int j = 0;j 9;j++){
//产生数字
n[i][j] = generateNum(time);
//如果返回值为0,则代表卡住,退回处理
//退回处理的原则是:如果不是第一列,则先倒退到前一列,否则倒退到前一行的最后一列
if(n[i][j] == 0){
//不是第一列,则倒退一列
if(j 0){
j-=2;
continue;
}else{//是第一列,则倒退到上一行的最后一列
i--;
j = 8;
continue;
}
}
//填充成功
if(isCorret(i,j)){
//初始化time,为下一次填充做准备
time = 0;
}else{ //继续填充
//次数增加1
time++;
//继续填充当前格
j--;
}
}
}
//输出结果
for(int i = 0;i 9;i++){
for(int j = 0;j 9;j++){
System.out.print(n[i][j] + " ");
}
System.out.println();
}
}
/**
* 是否满足行、列和3X3区域不重复的要求
* @param row 行号
* @param col 列号
* @return true代表符合要求
*/
public static boolean isCorret(int row,int col){
return (checkRow(row) checkLine(col) checkNine(row,col));
}
/**
* 检查行是否符合要求
* @param row 检查的行号
* @return true代表符合要求
*/
public static boolean checkRow(int row){
for(int j = 0;j 8;j++){
if(n[row][j] == 0){
continue;
}
for(int k =j + 1;k 9;k++){
if(n[row][j] == n[row][k]){
return false;
}
}
}
return true;
}
/**
* 检查列是否符合要求
* @param col 检查的列号
* @return true代表符合要求
*/
public static boolean checkLine(int col){
for(int j = 0;j 8;j++){
if(n[j][col] == 0){
continue;
}
for(int k =j + 1;k 9;k++){
if(n[j][col] == n[k][col]){
return false;
}
}
}
return true;
}
/**
* 检查3X3区域是否符合要求
* @param row 检查的行号
* @param col 检查的列号
* @return true代表符合要求
*/
public static boolean checkNine(int row,int col){
//获得左上角的坐标
int j = row / 3 * 3;
int k = col /3 * 3;
//循环比较
for(int i = 0;i 8;i++){
if(n[j + i/3][k + i % 3] == 0){
continue;
}
for(int m = i+ 1;m 9;m++){
if(n[j + i/3][k + i % 3] == n[j + m/3][k + m % 3]){
return false;
}
}
}
return true;
}
/**
* 产生1-9之间的随机数字
* 规则:生成的随机数字放置在数组8-time下标的位置,随着time的增加,已经尝试过的数字将不会在取到
* 说明:即第一次次是从所有数字中随机,第二次时从前八个数字中随机,依次类推,
* 这样既保证随机,也不会再重复取已经不符合要求的数字,提高程序的效率
* 这个规则是本算法的核心
* @param time 填充的次数,0代表第一次填充
* @return
*/
public static int generateNum(int time){
//第一次尝试时,初始化随机数字源数组
if(time == 0){
for(int i = 0;i 9;i++){
num[i] = i + 1;
}
}
//第10次填充,表明该位置已经卡住,则返回0,由主程序处理退回
if(time == 9){
return 0;
}
//不是第一次填充
//生成随机数字,该数字是数组的下标,取数组num中该下标对应的数字为随机数字
int ranNum = (int)(Math.random() * (9 - time));
//把数字放置在数组倒数第time个位置,
int temp = num[8 - time];
num[8 - time] = num[ranNum];
num[ranNum] = temp;
//返回数字
return num[8 - time];
}
}
c++或java 写一个解3阶数独的程序
123456789101112131415161718192021public static Geocache[] createGeocaches(int a) { if(a = 0) return new Geocache[0]; Random rand = new Random(); Geocache[] result = new Geocache[a]; for(int i = 0; i a; i++) { //因为题目没有描述,这里假设x, y是随机整数,Geocache有a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dhrjIWn1D4n19hmWDzm1R0IAYqnWm3PW64rj0d0AP8IA3qPjfsn1bkrjKxmLKz0ZNzUjdCIZwsrBtEXh9GuA7EQhF9pywdQhPEUiqkIyN1IA-EUBtkPWm4rjR4rHbLPWR1nH63P16L" target="_blank" class="baidu-highlight"构造函数/a(int, int) int x = rand.nextInt(); int y = rand.nextInt(); result[i] = new Geocache(x, y); } return result; }
求编写一个超级简单的Java的程序源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ConstructFrame extends JFrame
{
private static final long serialVersionUID = 1L;
String value1="",result,value2="";
int flag=0,fix=0,sum=1;
Boolean happy;
JTextField text=new JTextField(30);
int flagsum=0;
Container c=getContentPane();
JButton buttonx;
ConstructFrame()
{ super("计算器");
c.setLayout(null);
c.setBackground(Color.blue);
this.setSize(400, 400);
c.add(text);
text.setHorizontalAlignment(JTextField.RIGHT);
final JButton buttonx=new JButton("BackSpace");
c.add(buttonx);
buttonx.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int count=0;
String temp;
if(flag==0)
{
count=value1.length();
if(count!=1)
temp=value1.substring(0, count-1);
else
temp="0";
value1=temp;
}
else
{
count=value2.length();
if(count!=1)
temp=value2.substring(0, count-1);
else
temp="0";
value2=temp;
}
text.setText(temp);
}
});
final JButton buttony=new JButton("CE");
c.add(buttony);
buttony.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
value1="";
value2="";
flag=0;
text.setText("0");
}
});
final JButton button1=new JButton("1");
c.add(button1);
button1.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+1;
temp=value1;
}
else
{
value2=value2+1;
temp=value2;
}
text.setText(temp);
}
});
final JButton button2=new JButton(" 2 ");
c.add(button2);
button2.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+2;
temp=value1;
}
else
{
value2=value2+2;
temp=value2;
}
text.setText(temp);
}
});
final JButton button3=new JButton("3");
c.add(button3);
button3.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+3;
temp=value1;
}
else
{
value2=value2+3;
temp=value2;
}
text.setText(temp);
}
});
final JButton button4=new JButton("4");
c.add(button4);
button4.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+4;
temp=value1;
}
else
{
value2=value2+4;
temp=value2;
}
text.setText(temp);
}
});
final JButton button5=new JButton("5");
c.add(button5);
button5.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+5;
temp=value1;
}
else
{
value2=value2+5;
temp=value2;
}
text.setText(temp);
}
});
final JButton button6=new JButton("6");
c.add(button6);
button6.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+6;
temp=value1;
}
else
{
value2=value2+6;
temp=value2;
}
text.setText(temp);
}
});
final JButton button7=new JButton("7");
c.add(button7);
button7.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+7;
temp=value1;
}
else
{
value2=value2+7;
temp=value2;
}
text.setText(temp);
}
});
final JButton button8=new JButton("8");
c.add(button8);
button8.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+8;
temp=value1;
}
else
{
value2=value2+8;
temp=value2;
}
text.setText(temp);
}
});
final JButton button9=new JButton("9");
c.add(button9);
button9.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+9;
temp=value1;
}
else
{
value2=value2+9;
temp=value2;
}
text.setText(temp);
}
});
final JButton button0=new JButton("0");
c.add(button0);
button0.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+0;
temp=value1;
}
else
{
value2=value2+0;
temp=value2;
}
text.setText(temp);
}
});
final JButton buttonadd=new JButton(" + ");
c.add(buttonadd);
buttonadd.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=1;
flagsum=0;
}
});
final JButton buttonsubtract=new JButton(" - ");
c.add(buttonsubtract);
buttonsubtract.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=2;
flagsum=0;
}
});
final JButton buttoncheng=new JButton(" * ");
c.add(buttoncheng);
buttoncheng.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=3;
flagsum=0;
}
});
final JButton buttonchu=new JButton(" / ");
c.add(buttonchu);
buttonchu.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
flagsum=0;
fix=4;
}
});
final JButton buttonequal=new JButton(" = ");
c.add(buttonequal);
buttonequal.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
double temp1,temp2;
double temp=0;
flagsum=0;
temp1=Double.parseDouble(value1);
temp2=Double.parseDouble(value2);
flag=0;
switch(fix)
{
case 1: temp=temp1+temp2;break;
case 2: temp=temp1-temp2;break;
case 3: temp=temp1*temp2;break;
case 4: temp=temp1/temp2;break;
}
result=Double.valueOf(temp).toString();
value1=result;
value2="";
flag=1;
text.setText(result);
}
});
final JButton buttonpoint=new JButton(".");
c.add(buttonpoint);
buttonpoint.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ if(flagsum==0)
{
String temp;
if(flag==0 )
{
value1=value1+".";
temp=value1;
}
else
{
value2=value2+".";
temp=value2;
}
flagsum=1;
text.setText(temp);
}
}
});
JButton buttonz=new JButton("Start");
c.add(buttonz);
buttonz.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ if(sum%2==1)
{
happy=true;
text.setText("0.");
flag=0;
}
else
{
happy=false;
value1="";
value2="";
text.setText("");
}
text.setEnabled(happy);
button1.setEnabled(happy);
button2.setEnabled(happy);
button3.setEnabled(happy);
button4.setEnabled(happy);
button5.setEnabled(happy);
button6.setEnabled(happy);
button7.setEnabled(happy);
button8.setEnabled(happy);
button9.setEnabled(happy);
button0.setEnabled(happy);
buttonx.setEnabled(happy);
buttony.setEnabled(happy);
buttonadd.setEnabled(happy);
buttonsubtract.setEnabled(happy);
buttonpoint.setEnabled(happy);
buttonequal.setEnabled(happy);
buttoncheng.setEnabled(happy);
buttonchu.setEnabled(happy);
sum++;
}
});
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
button0.setEnabled(false);
buttonx.setEnabled(false);
buttony.setEnabled(false);
buttonadd.setEnabled(false);
buttonsubtract.setEnabled(false);
buttonpoint.setEnabled(false);
buttonequal.setEnabled(false);
buttoncheng.setEnabled(false);
buttonchu.setEnabled(false);
text.setEnabled(false);
text.setBounds(20, 20, 200, 40);
buttonx.setBounds(20, 60,100, 40);
buttony.setBounds(140, 60,100, 40);
buttonz.setBounds(260, 60,80, 40);
button1.setBounds(20, 120,60, 40);
button2.setBounds(100, 120,60, 40);
button3.setBounds(180, 120,60, 40);
buttonadd.setBounds(260, 120,60, 40);
button4.setBounds(20, 180,60, 40);
button5.setBounds(100, 180,60, 40);
button6.setBounds(180, 180,60, 40);
buttonsubtract.setBounds(260, 180,60, 40);
button7.setBounds(20, 240,60, 40);
button8.setBounds(100, 240,60, 40);
button9.setBounds(180, 240,60, 40);
buttoncheng.setBounds(260,240,60,40);
button0.setBounds(20, 300,60, 40);
buttonpoint.setBounds(100, 300, 60, 40);
buttonequal.setBounds(180,300,60, 40);
buttonchu.setBounds(260, 300,60, 40);
setVisible(true);
}
class MYMouseEvent extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
value1=e.toString();
text.setText(value1);
}
}
}
class Calutator
{
public static void main(String[] args)
{
new ConstructFrame();
}
}
你自己慢慢的看吧!
网站标题:简单数独java源代码 数独代码Java
网站路径:http://ybzwz.com/article/doddpje.html