六边形2048小游戏

界面

在这里插入图片描述

我的构思

1.得有个六边形

2.要有不同颜色

3.六边形要有个值

4.值相同的六边形颜色相同

5.六边形得响应鼠标点击事件

6. 六边形被选中时要突出一点,边线变粗,边线颜色变红。

7. 六边形相加后随即生成一个

8.六边形的值要有分布规律,不能过大或太小,让分布的期望在一个合理的范围。

9.如何产生一堆六边形(1.种子填充法,2.利用六边形围成的图形也是六边形的特性)

在这里插入图片描述

需要一个点集

class Point{
	double x,y;
	Point(double x,double y){
		this.x=x;
		this.y=y;
	}
	public void show(){
		System.out.println("<"+x+" "+y+"> ");
	}
}

六边形对象,图形属性, 六边形群的生成,事件的响应

class Hexagon{
	final int lenth=800,wide=800;
	public static int EDGE=50;//边长
	public static int char_s=8;//字符间距
	public static int sum=0;//数字和
	public static int RESEVERWIDE=2;//变宽
	public static int UPLINE=4;//选中时线宽
	public static int DEFLINE=1;//默认线宽
	public static int maxmi=5;//
	public static int minmi=1;//
	public static Stack<Integer> stackvalue = new Stack<Integer>(); 
	public int id;//id
	public int value;//
	public  double x,y;//坐标
	public String str;//字符串
	public Polygon shape;//形状
	public Text text;//文本框
	public Color downColor=Color.YELLOW;//颜色
	public Color upColor=Color.RED;//选中颜色
	public Color color=Color.GREEN;//
	
	public static final Color colors[]={			
			Color.ALICEBLUE,
			Color.BLANCHEDALMOND,
			Color.AQUA,
			Color.CHARTREUSE,
			Color.SEAGREEN,
			Color.PLUM,
			Color.PINK,
			Color.YELLOWGREEN,
			Color.BLUEVIOLET,
			Color.BLUE,
			Color.RED
	};//颜色表
	boolean isUp=false;
	static Hexagon oldH=null,newH=null;//
	Hexagon(double X,double Y){	
		id=sum;
		sum++;	
		x=X;
		y=Y;
		if(id==0)
			value=maxmi;
		else
	    	value=greateValues();
		shape=new Polygon();
		color=colors[value-minmi+1];
		shape.setFill(color);	
		shape.setStroke(downColor);
		shape.getPoints().addAll(new Double[]{
				X, Y-(EDGE-RESEVERWIDE),
		        X+Math.sqrt(3)*(EDGE-RESEVERWIDE)/2,Y-(EDGE-RESEVERWIDE)/2,
		        X+Math.sqrt(3)*(EDGE-RESEVERWIDE)/2,Y+(EDGE-RESEVERWIDE)/2,
		        X, Y+(EDGE-RESEVERWIDE),
		        X-Math.sqrt(3)*(EDGE-RESEVERWIDE)/2,Y+(EDGE-RESEVERWIDE)/2,
		        X-Math.sqrt(3)*(EDGE-RESEVERWIDE)/2,Y-(EDGE-RESEVERWIDE)/2,
		   });
		
		shape.setOnMousePressed(e->{
			if(oldH==null)
			{
				lightUp(this);
				oldH=this;
			}else if(oldH==this){
				lightDown(this);
				oldH=null;
			}else if(oldH.value==this.value&&adjacent(oldH,this)){
				if(oldH.id==0)
					addHexagon(oldH,this);
				else
					addHexagon(this,oldH);
				lightDown(oldH);
				oldH=null;
			}else{
				lightDown(oldH);
				lightUp(this);
				oldH=this;
			}
			System.out.println("id-> "+id);
		});
		str=""+(int)Math.pow(2,value);
		text=new Text(X-char_s*str.length()/2,Y+char_s/2,""+(int)Math.pow(2,value));
		//让文本也响应事件,因为文本覆盖了六边形一部分。
	/*	text.setOnMousePressed(e->{
			//lightUp();
			System.out.println("text.setOnMousePressed");});
		if(oldH==null){
			oldH=this;
			System.out.println("id--> "+id);
		}*/
	}
	public static int greateValues(){
		Random r = new Random();
		double x=3;
		do{
			x=Math.abs(r.nextGaussian());
		}while(x>2);
			return (int)(x*(maxmi-2)/2+minmi);
	}
	public static boolean lately(ArrayList<Hexagon> hs,Point p){//�า��
		for(Hexagon i:hs){
			if(Math.pow((i.x-p.x),2)+Math.pow((i.y-p.y),2)<EDGE*EDGE){
				return false;
			}
		}
		return true;
	}
	public static boolean lately(Hexagon h1,Hexagon h2){//�า��
		if(Math.pow((h1.x-h2.x),2)+Math.pow((h1.y-h2.y),2)<EDGE*EDGE){	
			return true;
		}
		return false;
	}
	public static boolean lately(Point h1,Point h2){//�า��
		if(Math.pow((h1.x-h2.x),2)+Math.pow((h1.y-h2.y),2)<EDGE*EDGE){	
			return true;
		}
		return false;
	}
	public static boolean adjacent(Hexagon h1,Hexagon h2){//������
		if(Math.pow((h1.x-h2.x),2)+Math.pow((h1.y-h2.y),2)<4*EDGE*EDGE){	
			return true;
		}
		return false;
	}
	public void lightUp(Hexagon hexagon)
	{
		hexagon.shape.setStroke(hexagon.upColor);
		hexagon.shape.setStrokeWidth(UPLINE);
		hexagon.isUp=true;
	}
	public void lightDown(Hexagon hexagon)
	{
		hexagon.shape.setStroke(hexagon.downColor);
		hexagon.shape.setStrokeWidth(DEFLINE);
		hexagon.isUp=false;
	}
	public void addHexagon(Hexagon h1,Hexagon h2){
		if(h1.value==maxmi||h2.id==0){
			maxmi+=1;
			if(maxmi==10)
				System.out.println("666666");

		}
		h1.value+=1;
		h2.value=greateValues();
		System.out.println(">>"+h1.value+ "   >"+h2.value);	
		h1.color=colors[h1.value-minmi+1];
		h2.color=colors[h2.value-minmi+1];
		h1.text.setText(""+(int)Math.pow(2,h1.value));
		h2.text.setText(""+(int)Math.pow(2,h2.value));
		refresh(h1);
		refresh(h2);
	}
	public void refresh(Hexagon hexagon){
		
		hexagon.shape.setFill(hexagon.color);
	}
	//利用六边形围成的图形也是六边形的特性
	public static Point[] findNode(double edge,int top,Point P){//top 
		Point[] six=new Point[7];
		Point[] poi=new Point[3*(top*top+top)+1];
		int e=0;
		poi[e]=P;
		for(int t=1;t<=top;t++){
			double a=Math.sqrt(3)*edge*t;//
	    	double h=a*Math.sqrt(3)/2;
	    	six[0]=new Point(P.x+a,P.y);
	    	six[1]=new Point(P.x+a/2,P.y+h);
	    	six[2]=new Point(P.x-a/2,P.y+h);
	    	six[3]=new Point(P.x-a,P.y);
	    	six[4]=new Point(P.x-a/2,P.y-h);
	    	six[5]=new Point(P.x+a/2,P.y-h);
	    	six[6]=new Point(P.x+a,P.y);
	    	for(int index=0;index<6;index++){
	    		poi[++e]=six[index];
	    		for(int cer=1;cer<t;cer++){
	    			int ndf=t;
	    			double ex=six[index+1].x-six[index].x;
	    			double ey=six[index+1].y-six[index].y;
	    			poi[++e]=new Point(six[index].x+ex*cer/ndf,
	    					six[index].y+ey*cer/ndf);
	    			six[index].show();
	    			poi[e].show();
	    			six[index+1].show();
	    		}
	    	}
	    	
		}
		return poi;
	}
	//种子填充法
	public static Point[] findNode2(double edge,int top,Point P){//top 
		int edges=3*(top*top+top);
		double FERROR=0.0001;//
		Point[] poi=new Point[edges+1];
		poi[0]=new Point(P.x+0,P.y+0);
		int e=0;
		for(int t=1;t<=top;t++){
			e=0;
			int tedegs=3*((t-1)*(t-1)+(t-1));
			double a=Math.sqrt(3)*edge*t;//��
			double b=3*edge/4*(2*t);
			double dt_radian=2*Math.PI/(t*6);//�Ƕ� /
			for(double radian=0;radian<=2*Math.PI/3+FERROR;radian+=dt_radian){
				if(radian<Math.PI/3-FERROR){
					double x=a/(1+Math.tan(radian)/Math.sqrt(3));
					double y=x*Math.tan(radian);
					poi[++e+tedegs]=new Point(P.x+x,P.y+y);	  			
				}else{
					int n=t+1;
					for(int i=0;i<n;i++){
						poi[++e+tedegs]=new Point(P.x+a/2-i*(a/(n-1)),P.y+b);
						System.out.println("x--> "+(P.x+a/2-i*(a/(n-1)))+"  y--> "+(P.y+b));
					}			
					for(int te =tedegs +e+1,index=tedegs +e-(t+1)
							;index>tedegs +1;
							te++,index--){			
						poi[te]=new Point(2*P.x-poi[index].x,poi[index].y);		
					}
					for(int i=tedegs+1;i<=tedegs+3*t;i++){
						poi[i+3*t]=new Point(2*P.x-poi[i].x, 2*P.y-poi[i].y);
					}
					break;					
				}
			}
		}
		return poi;
	}
}
使用种子填充法,将遍历所有已经生成的六边形中心,检查是否重复,复杂度将是O(N^2)
利用六边形围成的图形也是六边形的特性,等分边长点为新六边形的中心,复杂度降至是 O(N)
个人觉得这可能就是学过算法分析的好处吧!

注:个人劳动成果,不提供主类。(点一下玩一年!)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值