📄 map.java
字号:
package kyodai.map;
import java.awt.*;
import java.util.Random;
import java.util.Vector;
import kyodai.*;
import javax.swing.BorderFactory;
import javax.swing.JButton;
/**
* 生成连连看方块的类
*/
public class Map{
private int level;
public int map[][];
int array[];
private int restBlock;
//private Vector vector;
AnimateDelete animate;
private boolean test;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
public int blocks=0,isdisplayFinding=0;
public JButton[] dots;
public void setBorder(int index,int blueBorder)
{
if(blueBorder!=0)
{
dots[index].setBorder(Kyodai.blueBorder);
}
else
dots[index].setBorder(Kyodai.unSelected);
}
public boolean findPath(int x,int y)
{
int[]mapTemp=convMapToArray();
int ip=0,adjust=0,nextAd;
int[] direct=new int[170];int path[]=new int[170];path[0]=x;
for(int i = (direct.length-1); i >=0;i--)
direct[i]=0;
W1: while(ip>=0)
{
switch(direct[ip])
{
case 0: adjust=1;break;
case 1: adjust=19;break;
case 2: adjust=-1;break;
case 3: adjust=-19;break;
case 4: if(isdisplayFinding!=0)
{setBorder(convIndex(path[ip]),0);
long l=1000000;while(l>0){l--;}
//try{Thread.sleep(100);}catch(Exception e){}
}
direct[ip]=0;
ip--;continue W1;
}
direct[ip]++;nextAd=path[ip]+adjust;
if(nextAd==y)
{ path[++ip]=y;
if(!test)
{
convPath(path,++ip); new AnimateDelete(path,ip);
}
return true;
}
if(mapTemp[nextAd]<0)
{
if(isdisplayFinding!=0)
{ int temp=convIndex(nextAd);
setBorder(temp,1);
long l=10000000;while(l>0){l--;}
}
path[++ip]=nextAd;mapTemp[nextAd]=0;
}
}
return false;
}
public int[] convMapToArray()
{
int array[]=new int[19*12];
for(int i = (array.length-1); i >=0;i--)
array[i]=0;
for(int i=0;i<10;i++)
for(int j=0;j<17;j++)
array[(i+1)*19+j+1]=map[i][j];
return array;
}
public void clearMap(int clearLayout)
{
for(int i=0;i<10;i++)
for(int j=0;j<17;j++)
if(map[i][j]==0)
{ if(clearLayout!=0)
map[i][j]=-1;
}else map[i][j]=-1;
}
public int convToIndex(Point a)
{
return ((a.x+1)*19+a.y+1);
}
public void convPath(int[]path,int pathLen)
{
for(int i=0;i<pathLen;i++)
{
path[i]=convIndex(path[i]);
}
}
public Point convToPoint(int index)
{ int row;int col;
row=Math.round(index/19);
col=index-row*19;
return new Point(row-1,col-1);
}
public int convIndex(int index )
{
int row,col;
row=Math.round(index/19);
col=index-row*19;
return ((row-1)*17+col-1);
}
public boolean test(Point a,Point b)
{
if(map[a.x][a.y]!= map[b.x][b.y])
return false;
return findPath(convToIndex(a),convToIndex(b));
}
public Line findNext(Point a)
{
int i=20,j;int[]mapTemp=convMapToArray();
w1: while(i<207)
{
while(mapTemp[i]<=0)
{
i++;
if(i>=207)
break w1;
}
j=i+1;
while(j<208)
{
if(mapTemp[i]!=mapTemp[j]||!findPath(i,j))
{ j++; }
else
return new Line(1,convToPoint(i),convToPoint(j));
}
i++;
}
return new Line(0,null,null);
}
public void earse(Point a, Point b){
map[a.x][a.y] =-1;
map[b.x][b.y] =-1;
restBlock -= 2;
}
public Map(){
level = 28;
map = new int[10][17];
array = new int[170];
restBlock = level * 4;
//vector = new Vector();
test = false;clearMap(1);
initMap();
}
public Map(int level){
this.level = 28;
map = new int[10][17];
array = new int[170];
restBlock = this.level * 4;
//vector = new Vector();
test = false;
this.level = level;
restBlock = level * 4;clearMap(1);
initMap();
}
public void setTest(boolean test){
this.test = test;
}
public void setLevel(int level){
this.level = level;
restBlock = level * 4;
clearMap(0);
initMap();
}
private void initMap(){
int num=170-blocks;
array=new int[num];int p=0;
for(int i = (array.length-1); i >=0;i--)
array[i]=-1;
for(int i = 0; i<level; i++){//&&i * 4<num
array[i * 4] = i + 1;
array[i * 4 + 1] = i + 1;
array[i * 4 + 2] = i + 1;
array[i * 4 + 3] = i + 1;
}
random(array);
for(int i = 0; i < 10; i++){
for(int j = 0; j < 17; j++)
if(map[i][j]!=0)map[i][j]=array[p++];
}
}
private void random(int array[]){
Random random = new Random();
for(int i = array.length; i > 0; i--){
int j = random.nextInt(i);
int temp = array[j];
array[j] = array[i - 1];
array[i - 1] = temp;
}
}
public int getCount(){
return restBlock<=0 ? 0 : restBlock;
}
public void refresh(){
int count = getCount();
if(count<=0)
return;
int temp[]=new int[count];
count = 0;
for(int row = 0; row < 10; row++){
for(int col = 0; col < 17; col++)
if(map[row][col] > 0){
temp[count] = map[row][col];
count++;
}
}
random(temp);
count = 0;
for(int row = 0; row < 10; row++){
for(int col = 0; col < 17; col++)
if(map[row][col] > 0){
map[row][col] = temp[count];
count++;
}
}
}
public int[][] getMap(){
return map;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -