📄 lianliankan.java
字号:
/*@Description: A leisure game LianLiankan,by java..
With the function of Refeshing/Level Setup/ShowYesNo
*@Program LianLianKan.java
*@author Eric Ban && Andy Wei(班留想**韦哲)
*@Date April 10th 2007
*@version 1.03
*/
import javax.swing.*;//BorderFactory,JOptionPane,JProgressBar,Timer,ImageIcon
import javax.swing.Timer;
import javax.swing.event.*;//MenuListener
import java.awt.event.*;//ActionListener
import java.net.*;//URLClassLoader,URL
import javax.swing.border.*;//Border
import java.awt.*;//Menu,Point,Color,ToolKit
import java.util.*;//Vector,Random
import java.applet.*;//Applet
class Music
{
public URL u;
public AudioClip a;
public Music(String str)
{
try
{
u=new URL("file:"+"/D:/My Java Code/java_lianliankan/sound/"+str);
a =Applet.newAudioClip(u);
}catch(Exception e ){}
}
public void start()
{a.play();}
public void loop()
{a.loop();}
}
class Line
{
public Point a;
public Point b;
public int direct;
public Line(int direct,Point a ,Point b)
{
this.a =a;
this.b = b;
this.direct = direct;
}
}
class Setting
{
public static int ROW = 12;//共有10行
public static int COLUMN = 19;//共有17列
public static int Level = 28;
public Setting(){Level = 28;}//secondary level dafault..
public void setTestLevel(){Level = 3;}
public void setPrimaryLevel(){Level = 15;}//primary level= 15
public void setSecondaryLevel(){Level = 28;}//second level =28
public void setHighLevel(){Level = 39;}//high level = 39
}
class Map
{
public int array [];
public int map[][];
public int restBlock;//the rest blocks ,present moment.
public Vector vector;//keep the line found when scanning...
//deminished the dots choosed..
public int level = Setting.Level;//the level is secondary level by default..
public Map()
{
map =new int[12][19];
array =new int[170];
restBlock = level*4;//initialized the map with 80 blocks..
vector = new Vector();
initMap();
}
public void initMap()
{
for(int i =0;i<level;i++)
{
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 < Setting.ROW; i++)
{
for(int j = 0; j < Setting.COLUMN; j++)
{
if(i==0||i==11||j==0||j==18)
map[i][j] = 0;
else
map[i][j] = array[(i-1)* 17 + (j-1)];
}
}
}
public void random(int array[])
{
Random r = new Random();
for(int i=array.length;i>0;i--)
{
int index = r.nextInt(i);
int temp =array[index];
array[index]=array[i-1];
array[i-1] = temp;
}
}
public void erase(Point a,Point b)
{
if(getCount() == 0)
return;
map[a.x][a.y] = 0;
map[b.x][b.y] = 0;
restBlock -= 2;
}
//see how many blocks rest..
public int getCount()
{
int count = 0;
for(int i=0;i<Setting.ROW;i++)
for(int j=0;j<Setting.COLUMN;j++)
if(map[i][j] >0)
count++;
return count;
}
public boolean isGameOver()
{
return (getCount() == 0);
}
public void refresh()
{
int count = getCount();
if(count <= 0)
return;
int temp[]=new int[count];
count = 0;
//先借助一个数组,将当前的状态数组保存起来。
for(int row=1;row<Setting.ROW-1;row++)
{
for(int col=1;col<Setting.COLUMN-1;col++)
{
if(map[row][col] > 0)
{
temp[count]=map[row][col];
count++;
}
}
}
random(temp);
count=0;
for(int row=1;row<Setting.ROW-1;row++)
{
for(int col=1;col<Setting.COLUMN-1;col++)
{
if(map[row][col] > 0)//??????????
{
map[row][col]=temp[count];
count++;
}
}
}
}
public boolean horizonMatch(Point a,Point b)
{
if(a.x==b.x&&a.y==b.y)
return false;//相同的点,不能算。。
int start = a.y<b.y?a.y:b.y;
int end = a.y>b.y?a.y:b.y;
for(int x=start+1;x<end;x++)
if(map[a.x][x] != 0)
return false;
return true;
}
public boolean verticalMatch(Point a,Point b)
{
if(a.x==b.x&&a.y==b.y)
return false;
int start = a.x<b.x?a.x:b.x;
int end = a.x>b.x?a.x:b.x;
for(int y=start+1;y<end;y++)
if(map[y][a.y] != 0)
return false;
return true;
}
public boolean oneCorner(Point a,Point b)
{
Point c= new Point(a.x,b.y);
Point d = new Point(b.x,a.y);
if(map[c.x][c.y] == 0)
{
return horizonMatch(a,c)&&verticalMatch(b,c);
}
if(map[d.x][d.y] == 0)
{
return horizonMatch(b,d)&&verticalMatch(a,d);
}
return false;
}
private Vector scan(Point a, Point b)
{
Vector<Line> v = new Vector<Line>();
Point c = new Point(a.x, b.y);
Point d = new Point(b.x, a.y);
//first,scan from the left zone of the Point a and b..
for(int y = a.y; y >= 0; y--)
if(map[a.x][y] == 0 && map[b.x][y] == 0 && verticalMatch(new Point(a.x, y), new Point(b.x, y)))
v.addElement(new Line(0, new Point(a.x, y), new Point(b.x, y)));
for(int y = a.y; y < 19; y++)
if(map[a.x][y] == 0 && map[b.x][y] == 0 && verticalMatch(new Point(a.x, y), new Point(b.x, y)))
v.addElement(new Line(0, new Point(a.x, y), new Point(b.x, y)));
for(int x = a.x; x >= 0; x--)
if(map[x][a.y] == 0 && map[x][b.y] == 0 && horizonMatch(new Point(x, a.y), new Point(x, b.y)))
v.add(new Line(1, new Point(x, a.y), new Point(x, b.y)));
for(int x = a.x; x < 12; x++)
if(map[x][a.y] == 0 && map[x][b.y] == 0 && horizonMatch(new Point(x, a.y), new Point(x, b.y)))
v.addElement(new Line(1, new Point(x, a.y), new Point(x, b.y)));
return v;
}
private boolean twoCorner(Point a, Point b)
{
vector = scan(a, b);
if(vector.isEmpty())
return false;
for(int index = 0; index < vector.size(); index++)
{
Line line = (Line)vector.elementAt(index);
if(line.direct == 1)
{
if(verticalMatch(a, line.a) && verticalMatch(b, line.b))
{
return true;
}
}else if(horizonMatch(a, line.a) && horizonMatch(b, line.b))
{
return true;
}
}
return false;
}
//judge whether these two points can be erasd...
public boolean test(Point a, Point b)
{
if(map[a.x][a.y] != map[b.x][b.y])
return false;
if((a.x == b.x) && horizonMatch(a, b))
return true;
if((a.y == b.y) && verticalMatch(a, b))
return true;
if(oneCorner(a, b))
return true;
else
return twoCorner(a, b);
}
public int [][] getMap()
{
return map;
}
}
class MapUI extends JPanel implements ActionListener,Runnable
{
public Map map;
public JButton[] dots;
public Music erasSound;//music for erasing...
public Music selSound;//music for selecting
public Music freshSound;//music for refreshing..
private Point lastPoint = new Point(0,0);//用户上次选定的点
private boolean isSelected = false;//当前是否已经选中了一个点。。
public int limiteTime = 0;//限定每一步的时间
public boolean isPlaying = false;
public MapUI(){}
public MapUI(Map map,JButton[] dots)
{
this.map = map;
this.dots = dots;
selSound= new Music("select.wav");
erasSound=new Music("erase.wav");
freshSound=new Music("refresh.wav");
this.setLayout(new GridLayout(Setting.ROW,Setting.COLUMN,2,2));
this.setBackground(new Color(0,0,0));
////////////////////////////////////////////////////
for (int row = 0; row < Setting.ROW; row++)
{
for (int col = 0; col < Setting.COLUMN; col++)
{
int index = row * Setting.COLUMN + col;
dots[index].addActionListener(this);
this.add(dots[index]);
}
}
}
public void setMap(Map map)
{
this.map = map;
}
public void paint()
{
for (int row = 0; row < Setting.ROW; row++)
{
for (int col = 0; col < Setting.COLUMN; col++)
{
int index = row * Setting.COLUMN + col;
if (map.getMap()[row][col] > 0)
{
dots[index].setIcon(LianLianKan.BlocksIcon[map.getMap()[row][col] - 1]);
dots[index].setEnabled(true);
}
else
{
dots[index].setIcon(null);
dots[index].setEnabled(false);
}
}
}
}
public void clearMap()
{
for(int i=0;i<dots.length;i++)
dots[i].setIcon(null);
}
/**
* 刷新当前的排列方式
*/
public void refresh()
{
if (!isPlaying)
{ //不在游戏中,返回
return;
}
freshSound.start();
map.refresh();
paint();
}
public boolean isGameOver()
{
return map.isGameOver();
}
void erase(Point a ,Point b)
{
if (map.getCount() == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -