D言語バージョンの華容道

5377 ワード

华容道を游んだことがあって、Dで练习します.
粗雑な書き方ですが、お許しください.
 
 
 
import dfl.all;
import std.stdio;
import std.math;
import std.string; 

class Block:Label
{
	bool caps=false;
	bool canMove = false;
	huaForm pars;
	Rect curRect,savRect;
	int id;
	int w =100;
	
	this(int i,huaForm f,Color c,int px,int py,int lw,int lh)
	{
		id = i;
		curRect = dfl.base.Rect(px,py,lw,lh);		
		savRect = curRect;
		this.bounds =  dfl.base.Rect(px*w+10, py*w+10, lw*w -10,lh*w -10);
		this.parent = f;
		pars = f;
		this.text = format("x=%d,y=%d,w=%d,h=%d.",curRect.x,curRect.y,curRect.width,curRect.height);
		this.backColor = c;
	}
	
	
	
	void Move(int px,int py)
	{		
		//X: 0-3,Y: 0-4.
		//writefln("initBloc,x:%d,y:%d.moveTo:x=%d,y=%d",this.left,this.top,px,py);
		savRect = curRect;
		double a,b;
		a = px;
		b = py;
		px = cast(int)floor(a /100);
		py = cast(int)floor(b /100);
	
		
		if(py < 0 ) py =0;		
		if(px < 0 ) px =0;
		if(py + curRect.height > 5) py = py -1;
		if(px + curRect.width  > 4) px = px -1;
		
		if( py == curRect.y ||  px == curRect.x)
		{
			if( px == curRect.x)
			{
				if( py > curRect.y ) curRect.y++; 
				if( py < curRect.y ) curRect.y--;
			}
			
			if( py == curRect.y)
			{
				if( px > curRect.x ) curRect.x++; 
				if( px < curRect.x ) curRect.x--;
			}
		} 
		else
		{
			return;
			
		}
		//writefln("calc,x:%d,y:%d.",curRect.x,curRect.y);
		if( pars.canMov(curRect,id) == false )
		{
			curRect = savRect;
			return;
		}
		//save to current;
		px = curRect.x;
		py = curRect.y;
		
		//writefln("initMove,x:%d,y:%d.",px,py);
		this.top = py * w + 10;
		this.left = px * w +10;
		//writefln("caleMove,x:%d,y:%d.",this.left,this.top);
		
		this.text = format("x=%d,y=%d,w=%d,h=%d.",curRect.x,curRect.y,curRect.width,curRect.height);
	}
	
	protected void onMouseDown(MouseEventArgs mea)
	{
		
		super.onMouseDown(mea);
		caps = true;
		//writefln("caps!");		
	}
	

}


Rect getRect(int px,int py,int w,int h)
{
	double a,b;
	a = px;
	b = py;
	px = cast(int)floor(a /100);
	py = cast(int)floor(b /100);
	
	if(py < 0 ) py =0;		
	if(px < 0 ) px =0;
	if(py + h > 5) py = py -1;
	if(px + w  > 4) px = px -1;
	
	return Rect(px,py,w,h);
}





class huaForm: dfl.form.Form
{
	
	int w = 100;
	static Block[10] blocks;
    
    
    
	this()
	{
		this.text ="Chinese Sliding Block -HuaRongDao";
		this.width = 4* w + 20;
		this.height = 5* w + 60;	
		this.maximizeBox = false;
		addBlock();	
	}
	
	void addBlock()
	{
		
		blocks[0] = new Block(0,this,Color(255,0,0),  0,0,1,2);
		blocks[1] = new Block(1,this,Color(255,255,0),1,0,2,2);
		blocks[2] = new Block(2,this,Color(255,0,0),  3,0,1,2);
		blocks[3] = new Block(3,this,Color(0,255,0),  0,2,1,2);
		blocks[4] = new Block(4,this,Color(0,255,0),  3,2,1,2);
		blocks[5] = new Block(5,this,Color(0,255,255),1,2,2,1);
		blocks[6] = new Block(6,this,Color(0,0,255),  0,4,1,1);
		blocks[7] = new Block(7,this,Color(0,0,255),  3,4,1,1);
		blocks[8] = new Block(8,this,Color(0,0,255),  1,3,1,1);
		blocks[9] = new Block(9,this,Color(0,0,255),  2,3,1,1);	
	}
	
	
	protected void onMouseUp(MouseEventArgs mea)
	{
		
		super.onMouseUp(mea);
		
		foreach(Block b;blocks)
		{
			if( canMove(Point(mea.x,mea.y)) == true && b.caps==true)
			{
				b.Move(mea.x,mea.y);
				b.caps = false;
				return;
			}
		}
	}
	
	bool canMov(Rect npt,int i)
	{
		foreach(int j,Block b;blocks)
		{
			
			if(i==j) continue;
			Rect r = dfl.base.Rect(npt.x*w+10, npt.y*w+10, npt.width*w -10,npt.height*w -10);		
			if(r.contains(b.bounds.x,b.bounds.y)==true) 
			{
				//writefln("Baohan:,x:%d,y:%d.ID:%d",b.curRect.x,b.curRect.y,b.id);
				return false;		
			}
			if(b.curRect.contains(npt) == true)
			{
				//writefln("beiBaohan:,x:%d,y:%d.ID:%d",b.curRect.x,b.curRect.y,b.id);
				return false;	
			}		
		}		
		return true;
	}
		
	
	
	bool canMove(Point npt)
	{
		foreach(Block b;blocks)
		{
			if(b.bounds.contains(npt) == true) return false;							
		}		
		return true;
	}	
}


int main()
{
	int result = 0;
	
	try
	{		
		Application.run(new huaForm());
	}
	catch(Object o)
	{
		msgBox(o.toString(), "Fatal Error", MsgBoxButtons.OK, MsgBoxIcon.ERROR);
		
		result = 1;
	}
	
	return result;
}