java 实现 连连看的算法

来源:互联网 发布:前端性能优化的方式 编辑:程序博客网 时间:2024/05/17 07:54

前几天要写一程序 连连看 。上网查了很多。总结一下。

算法大致分为2种。

(1)  图的广度搜索算法(这边就不讲了)

 (2)当连接两张图片的线条拐点不超过 2个的时候,此时分为3种情况。没有拐点,只有一个拐点,有两个拐点。

 

 

      我们可以把连连看中的图片坐标看做是一个2维坐标,就用(x,y)坐标。红色的字体就是放图片的位置。黑色的就相当于那个边框。

 

 (1)当图片之间没有拐点的时候,只要检测之间有咩有其他图片

  (2)当有1个拐点的时候,假设两张图片的位置是(x01,y01)(x02,y02),只要找出那个拐点的坐标就可以了 (x01,y02)或(x02,y01)看看这个点是否存在

  (3)当有2个拐点的时候,只要找出两个拐点的坐标就可以了。以(x01,y01)为标准,分别以这个点的左右上下方向寻找 。下面是用java 实现的代码,这个是我从程序中摘的,可能里面会有与算法无关的东西。

package com.play;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

public class alg {

 /**
  * @param args
  */
 
 public static int[][]  map;
 private  int numCol,numRow;
 
 //路线图
 private Point   point1,point2,point3,point4;
 private int pointX=0,pointY=0;
 
 Point push01=new Point();
 Point push02=new Point();
 
 private  int countPoint=-1;
 alg(int col,int row)
 {
  numCol=col+2;
  numRow=row+2;
  map=new int[numRow][numCol];
  int i,j;
  for(i=0;i<numRow;i++)
   for(j=0;j<numCol;j++)
    map[i][j]=1;
  for(i=0;i<numCol;i++)
  { map[0][i]=0;
     map[numRow-1][i]=0;
  }
  
  for(i=0;i<numRow;i++)
  {
   map[i][0]=0;
   map[i][numCol-1]=0;
  }
  
  point1=new Point();
  point2=new Point();
  point3=new Point();
  point4=new Point();
 }
 public  void  getMark(int[][] _map)   //当游戏是第一关的时候
 {
  
  
  for(int i=0;i<numRow;i++)
   for(int j=0;j<numCol;j++)
    map[i][j]=_map[i][j];
 
 }
 
 
.//是否垂直
 public  boolean  ver(Point p1,Point p2)
 {int i,j;
  if(p1.y!=p2.y)
   return false;
  i=p1.x>p2.x?p1.x:p2.x;
  j=p1.x>p2.x?p2.x:p1.x;
  for(int lo=j;lo<=i;lo++)
  {
   if(map[lo][p1.y]!=0)
    return false;
  }
  
  point1.x=pointX+(p1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
  point1.y=pointY+(p1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
  point2.x=pointX+(p2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
  point2.y=pointY+(p2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
  
  
  return true;
 }
 
public  boolean  hor(Point p1,Point p2)
 {int i,j;
 if(p1.x!=p2.x)
  return false;
  i=p1.y>p2.y?p1.y:p2.y;
  j=p1.y>p2.y?p2.y:p1.y;
  for(int lo=j;lo<=i;lo++)
   if(map[p1.x][lo]!=0)
    return false;
  
  point1.x=pointX+(p1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
  point1.y=pointY+(p1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
  point2.x=pointX+(p2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
  point2.y=pointY+(p2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
  
  
  return true;
 }
 public  boolean  oneCon(Point p1,Point p2)
 {
  Point p=new Point();
  p.x=p1.x;
  p.y=p2.y;
  if(hor(p,p1)==true &&ver(p,p2)==true)
  {
   
   point1.x=pointX+(p1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
   point1.y=pointY+(p1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
   
   point2.x=pointX+(p.y-1)*PlayGame.frameW+PlayGame.frameW/2;
   point2.y=pointY+(p.x-1)*PlayGame.frameH+PlayGame.frameH/2;
   
   point3.x=pointX+(p2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
   point3.y=pointY+(p2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
  
   return true;
  }
  p.x=p2.x;
  p.y=p1.y;
  if(hor(p,p2)==true && ver(p,p1)==true)
  {
   
   point1.x=pointX+(p1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
   point1.y=pointY+(p1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
   
   point2.x=pointX+(p.y-1)*PlayGame.frameW+PlayGame.frameW/2;
   point2.y=pointY+(p.x-1)*PlayGame.frameH+PlayGame.frameH/2;
   
   point3.x=pointX+(p2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
   point3.y=pointY+(p2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
   
   return true;
  }
  
  
  return false;
 }
 
 public  boolean  twoCor(Point p1,Point p2)
 {int i,j;
  Point m1=new Point();
  Point m2=new Point();
  
  //point的右侧
     for(i=p1.y+1;i<numCol;i++)
     {
      m1.x=p1.x;
      m1.y=i;
      m2.x=p2.x;
      m2.y=i;
      if(hor(m1,p1) ==true && ver(m1,m2)==true  && hor(m2,p2)==true)
      {
       point1.x=pointX+(p1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
    point1.y=pointY+(p1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
    
    point2.x=pointX+(m1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
    point2.y=pointY+(m1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
    
    point3.x=pointX+(m2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
    point3.y=pointY+(m2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
    
    point4.x=pointX+(p2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
    point4.y=pointY+(p2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
       
       
       
       
       return true;
      }
      
     }
     //point 的左侧
     {
      for(i=p1.y-1;i>=0;i--)
      {
       m1.x=p1.x;
       m1.y=i;
       m2.x=p2.x;
       m2.y=i;
       if(hor(m1,p1) ==true && ver(m1,m2)  && hor(m2,p2)==true)
       {
        point1.x=pointX+(p1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point1.y=pointY+(p1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point2.x=pointX+(m1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point2.y=pointY+(m1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point3.x=pointX+(m2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point3.y=pointY+(m2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point4.x=pointX+(p2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point4.y=pointY+(p2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
        return true;
       }
       
      }
     }
     //point的上侧
     {
      for(j=p1.x-1;j>=0;j--)
      {
       m1.x=j;
       m1.y=p1.y;
       m2.x=j;
       m2.y=p2.y;
       if(ver(p1,m1)==true && hor(m1,m2)==true && ver (m2,p2)==true)
       {
        point1.x=pointX+(p1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point1.y=pointY+(p1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point2.x=pointX+(m1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point2.y=pointY+(m1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point3.x=pointX+(m2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point3.y=pointY+(m2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point4.x=pointX+(p2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point4.y=pointY+(p2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
        return true;
      
       }
      }
      
     }
     //point的下侧
     {
      for(j=p1.x+1;j<numRow;j++)
      {
       m1.x=j;
       m1.y=p1.y;
       m2.x=j;
       m2.y=p2.y;
       if(ver(m1,p1)==true && hor(m1,m2)==true && ver(m2,p2)==true)
       {
        point1.x=pointX+(p1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point1.y=pointY+(p1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point2.x=pointX+(m1.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point2.y=pointY+(m1.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point3.x=pointX+(m2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point3.y=pointY+(m2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
     
     point4.x=pointX+(p2.y-1)*PlayGame.frameW+PlayGame.frameW/2;
     point4.y=pointY+(p2.x-1)*PlayGame.frameH+PlayGame.frameH/2;
        return true;
       }
      }
      
      
     }
  
  
  
  return false;
 }
 

}

 

 

 

 

原创粉丝点击