⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 九宫图结点.java

📁 JAVA源码下载 人工智能八数码(九宫重排)问题
💻 JAVA
字号:
/*
 * Created on 2005-4-8
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

import java.awt.*;

public class 九宫图结点 
{
	//以下定义一组常量
	//颜色常量表,用于九宫图结点的绘制
	public static final Color[] 颜色表=
		{
			new Color(255,230,240),
			new Color(255,240,230),
			new Color(255,250,220),
			
			new Color(230,255,240),			
			new Color(240,255,230),
			new Color(250,255,220),
			
			new Color(240,230,255),
			new Color(230,240,255),
			new Color(250,220,255)
		};
	//“奖牌家的位置”保存了每个奖牌的“家"的位置
	public static final int[] 将牌家的位置=
		{
			5,1,2,3,6,9,8,7,4
		};
	
	//以下是九宫图结点的操作常量
	public static final int 	空格上移=1;
	public static final int 	空格下移=2;
	public static final int 	空格左移=3;
	public static final int 	空格右移=4;
	
	//”预置状态数据“是一个九宫图状态数据的范例
	public static final int[] 	预置状态数据={8,2,8,3,1,6,4,7,0,5};
	//”目标状态数据“保存了目标状态的数据
	public static final int[] 	目标状态数据={5,1,2,3,8,0,4,7,6,5};
	
	//这里使用一个10个元素的数组来表示九宫图的状态
	//其中0号元素表示空格位于哪个方格,1-9号元素分
	//别代表九宫图的9个方格	
	private int[] 		结点状态=new int[10];
	private int			结点深度=0;
	private int 		代价估计值=0;
	private double		估计函数系数=1;
	private 九宫图结点 	返回指针=null;
	//生成操作属性的含义是:结点是其父结点经过什么操作生成的
	private int			生成操作=0;
	
	//空构造函数
	九宫图结点()
	{
		
	}
	
	//从一个存有格局数据的数组构造
	九宫图结点(int[] data)
	{
		if(data.length>=10)
		{
			for(int i=0;i<10;i++)
			{
				this.结点状态[i]=data[i];
			}
		}
	}
	
	//从一个已有的九宫图结点对象构造
	九宫图结点(九宫图结点 node)
	{
		this.结点状态=(int[])node.结点状态.clone();
		this.返回指针=null;
	}
	
	public int 取结点深度()
	{
		return this.结点深度;
	}
	
	public void 置结点深度(int d)
	{
		this.结点深度=d;
	}
	
	//将结点置为预置状态
	public void 到预置状态()
	{
		this.结点状态=(int[])九宫图结点.预置状态数据.clone();
		this.结点深度=0;
	}
	
	//将结点置为目标状态
	public void 到目标状态()
	{
		this.结点状态=(int[])九宫图结点.目标状态数据.clone();
	}
	
	//从一个数组设置结点状态
	public boolean 置结点状态(int[] data)
	{
		if(data.length>=10)
		{
			this.结点状态=(int[])data.clone();
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public void 修改返回指针(九宫图结点 pret)
	{
		this.返回指针=pret;
	}
	
	public 九宫图结点 取得返回指针()
	{
		return this.返回指针;
	}
	
	//计算结点的代价估计置
	public void 计算代价估计值()
	{
		int p=0;
		int s=0;
		
		int cur_row=0;
		int cur_col=0;		
		int home_row=0;
		int home_col=0;
		
		int 将牌;
		for(int i=1;i<=9;i++)
		{
			将牌=this.结点状态[i];
			cur_row=i/3;
			cur_col=i%3;
			home_row=九宫图结点.将牌家的位置[将牌]/3;
			home_col=九宫图结点.将牌家的位置[将牌]%3;
			p+=Math.abs(cur_row-home_row)+Math.abs(cur_col-home_col);
		}
		
		if(this.结点状态[1]+1!=this.结点状态[2])
		{
			s+=2;
		}
		if(this.结点状态[2]+1!=this.结点状态[3])
		{
			s+=2;
		}
		if(this.结点状态[3]+1!=this.结点状态[6])
		{
			s+=2;
		}
		if(this.结点状态[6]+1!=this.结点状态[9])
		{
			s+=2;
		}
		if(this.结点状态[9]+1!=this.结点状态[8])
		{
			s+=2;
		}
		if(this.结点状态[8]+1!=this.结点状态[7])
		{
			s+=2;
		}
		if(this.结点状态[7]+1!=this.结点状态[4])
		{
			s+=2;
		}
		if(this.结点状态[5]==0)
		{
			s+=1;
		}
		
		this.代价估计值=this.结点深度+p+3*s;
	}
	
	public int 取代价估计值()
	{
		return this.代价估计值;
	}
	
	//返回一个值,这个值指出结点是由其父结点经过何种操作生成的
	public int 取生成操作()
	{
		return this.生成操作;
	}
	
	//这个方法用来设置结点的生成操作
	public void 置生成操作(int op)
	{
		this.生成操作=op;
	}
	

	//以下是对九宫图结点的操作,即所谓的发生器函数
	//如果操作成功返回true,否则返回false
	public boolean _操作_空格上移()
	{
		int 空格位置=this.结点状态[0];
		
		if(空格位置>=4&&空格位置<=9)
		{
			this.结点状态[空格位置]
				=this.结点状态[空格位置-3];
			this.结点状态[空格位置-3]=0;
			this.结点状态[0]=空格位置-3;
			
			return true;
		}
		
		return false;
	}
	
	public boolean _操作_空格下移()
	{				
		int 空格位置=this.结点状态[0];
		
		if(空格位置>=1&&空格位置<=6)
		{
			this.结点状态[空格位置]
				=this.结点状态[空格位置+3];
			this.结点状态[空格位置+3]=0;
			this.结点状态[0]=空格位置+3;
			
			return true;
		}
		
		return false;
	}

	public boolean _操作_空格左移()
	{	
		int 空格位置=this.结点状态[0];
		
		if(空格位置>=2&&空格位置<=3
			||空格位置>=5&&空格位置<=6
			||空格位置>=8&&空格位置<=9)
		{
			this.结点状态[空格位置]
				=this.结点状态[空格位置-1];
			this.结点状态[空格位置-1]=0;
			this.结点状态[0]=空格位置-1;
			
			return true;
		}
		
		return false;
	}

	public boolean _操作_空格右移()
	{				
		int 空格位置=this.结点状态[0];
		
		if(空格位置>=1&&空格位置<=2
			||空格位置>=4&&空格位置<=5
			||空格位置>=7&&空格位置<=8)
		{
			this.结点状态[空格位置]
				=this.结点状态[空格位置+1];
			this.结点状态[空格位置+1]=0;
			this.结点状态[0]=空格位置+1;
			
			return true;
		}
		
		return false;
	}
	
	//对结点进行随机化
	public void 随机化()
	{
		int i;
		int j;
		
		for(int k=1;k<=9;k++)
		{
			this.结点状态[k]=k-1;
		}
		
		for(int k=0;k<50;k++)
		{
			i=1+(int)(Math.random()*9);
			j=1+(int)(Math.random()*9);			
			
			int temp=this.结点状态[i];
			this.结点状态[i]=this.结点状态[j];
			this.结点状态[j]=temp;
		}
		
		for(int k=1;k<=9;k++)
		{
			if(this.结点状态[k]==0)
			{
				this.结点状态[0]=k;
				break;
			}
		}
		
		this.结点深度=0;
	}
	
	public boolean equals(Object obj)
	{
		boolean retv=true;
		
		for(int i=0;i<10;i++)
		{
			if(this.结点状态[i]!=((九宫图结点)obj).结点状态[i])
			{
				retv=false;
				break;
			}
		}
		
		return retv;
	}
	
	public String toString()
	{
		String str=new String();
		
		str+=this.结点状态[1]+" ";
		str+=this.结点状态[2]+" ";
		str+=this.结点状态[3]+" ";
		str+="\n";
		
		str+=this.结点状态[4]+" ";
		str+=this.结点状态[5]+" ";
		str+=this.结点状态[6]+" ";
		str+="\n";

		str+=this.结点状态[7]+" ";
		str+=this.结点状态[8]+" ";
		str+=this.结点状态[9]+" ";
		
		return str;
	}
	
	public void 绘制九宫图结点(Graphics g,int x,int y,int size)
	{
		int cellsize=size/3;
		
		g.setColor(Color.DARK_GRAY);
		g.drawLine(x+size,y,x+size,y+size);
		g.drawLine(x,y+size,x+size,y+size);
				
		for(int i=1;i<=9;i++)
		{
			int row=(i-1)/3;
			int col=(i-1)%3;

			g.setColor(九宫图结点.颜色表[this.结点状态[i]]);
			g.fillRect(x+col*cellsize,y+row*cellsize,cellsize,cellsize);
			if(this.结点状态[i]!=0)
			{
				g.setColor(Color.BLACK);
				g.drawString(""+this.结点状态[i],
						x+col*cellsize+10,
						y+row*cellsize+16);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -