import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
//单JFrame实现连连看V1.0版
public class LLKFrame extends JFrame implements MouseListener{
private int W = 50; //动物方块图案的宽度
private int GameSize=10; //布局大小即行列数
private boolean Select_first = false; //是否已经选中第一块
private int x1, y1; //被选中第一块的地图坐标
private int x2, y2; //被选中第二块的地图坐标
private Point z1=new Point(0,0);
private Point z2=new Point(0,0); //折点棋盘坐标
private int m_nCol = 10;
private int m_nRow = 10;
private int[] m_map = new int[10*10];
private int BLANK_STATE = -1;
public enum LinkType {LineType,OneCornerType,TwoCornerType};
LinkType LType; //连通方式
public void init() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 100, 640, 660); // 500 450
this.setTitle("连连看游戏");
this.setVisible(true);
this.addMouseListener(this);//否则鼠标单击无反应
this.setFocusable(true);
//pack();
}
private void StartNewGame()
{
//初始化地图,将地图中所有方块区域位置置为空方块状态
for(int iNum=0;iNum<(m_nCol*m_nRow);iNum++)
{
m_map[iNum] = BLANK_STATE;
}
Random r = new Random();
//生成随机地图
//将所有匹配成对的动物物种放进一个临时的地图中
ArrayList tmpMap=new ArrayList ();
for(int i=0;i<(m_nCol*m_nRow)/4;i++)
for(int j=0;j<4;j++)
tmpMap.add(i);
//每次从上面的临时地图中取走(获取后并在临时地图删除)
//一个动物放到地图的空方块上
for (int i = 0; i < m_nRow * m_nCol; i++)
{
//随机挑选一个位置
int nIndex = r.nextInt(tmpMap.size()) ;
//获取该选定物件放到地图的空方块
m_map[i]=(Integer)tmpMap.get(nIndex);
//在临时地图除去该动物
tmpMap.remove(nIndex);
}
}
private void Init_Graphic()//生成游戏开始的界面
{
Graphics g = this.getGraphics(); //生成Graphics对象
for (int i = 0; i< 10 * 10; i++)
{
g.drawImage(create_image(m_map[i]), W * (i % GameSize)+W,
W * (i / GameSize)+W, W, W,this);
}
}
//create_image()方法实现按标号n从所有动物图案的图片中截图。
private Image create_image(int n) //按标号n截图
{
int x=0;
int y=n*39 ;
int w=39;
int h=39;
BufferedImage src=null;
BufferedImage newpic=null;
try{
src = ImageIO.read(new File("pic\\animal2.bmp"));
}
catch(Exception e)
{
System.out.println(e);
}
newpic=src.getSubimage(x, y, w, h);//截取原图中矩形区域的图形
return newpic;
}
///
/// 检测是否已经赢得了游戏
///
boolean IsWin()
{
//检测所有是否尚有非未被消除的方块
// (非BLANK_STATE状态)
for(int i=0;i<m_nRow*m_nCol;i++)
{
if(m_map[i] != BLANK_STATE)
{
return false;
}
}
return true;
}
private boolean IsSame(int x1, int y1,int x2, int y2)
{
if (m_map[y1 * m_nCol + x1] == m_map[y2 * m_nCol + x2])
return true;
else
return false;
}
//
//X直接连通即垂直方向连通
//
boolean X_Link(int x, int y1,int y2)
{
//保证y1的值小于y2
if(y1>y2)
{
//数据交换
int n=y1;
y1=y2;
y2=n;
}
//直通
for(int i=y1+1;i<=y2;i++)
{
if(i==y2)
return true;
if(m_map[i*m_nCol+x]!=BLANK_STATE)
break;
}
return false;
}
//
//Y直接连通即水平方向连通
//
boolean Y_Link(int x1,int x2,int y)
{
if(x1>x2)
{
int x=x1;
x1=x2;
x2=x;
}
//直通
for(int i=x1+1;i<=x2;i++)
{
if(i==x2)
return true;
if(m_map[y*m_nCol+i]!=BLANK_STATE)
break;
}
return false;
}
//
// 1直角接口连通
//
boolean OneCornerLink(int x1, int y1,int x2, int y2)
{
if (x1 > x2) //目标点(x1,y1),(x2,y2)两点交换
{
int n=x1;
x1=x2;
x2=n;
n=y1;
y1=y2;
y2=n;
}
if (y2 < y1) //(x1,y1)为矩形左下顶点,(x2,y2)点为矩形右上顶点
{
//判断矩形右下角折点(x2,y1)是否空
if (m_map[y1 * m_nCol + x2] == BLANK_STATE)
{
if (Y_Link(x1, x2, y1) && X_Link(x2, y1, y2))
//判断折点(x2,y1)与两个目标点是否直通
{
z1.x= x2; z1.y = y1; //保存折点坐标到z1
return true;
}
}
//判断矩形左上角折点(x1,y2)是否空
if (m_map[y2 * m_nCol + x1] == BLANK_STATE)
{
if (Y_Link(x2 , x1, y2) && X_Link(x1, y2, y1))
//判断折点 (x1,y2)与两个目标点是否直通
{
z1.x = x1; z1.y = y2; //保存折点坐标到z1
return true;
}
}
return false;
}
else //(x1,y1)为矩形左上顶点,(x2,y2)点为矩形右下顶点
{
//判断矩形左下角折点(x1,y2)是否空
if (m_map[y2 * m_nCol + x1] == BLANK_STATE)
{
if (Y_Link(x1, x2, y2) && X_Link(x1, y1, y2 ))
//判断折点 (x1,y2)与两个目标点是否直通
{
z1.x = x1; z1.y = y2; //保存折点坐标到z1
return true;
}
}
//判断矩形右上角折点(x2,y1)是否空
if (m_map[y1 * m_nCol + x2] == BLANK_STATE)
{
if (Y_Link(x1 , x2, y1) && X_Link(x2, y1, y2))
//判断折点(x2,y1)与两个目标点是否直通
{
z1.x = x2; z1.y = y1; //保存折点坐标到z1
return true;
}
}
return false;
}
}
///
/// 2直角接口连通
///
boolean TwoCornerLink(int x1, int y1, int x2, int y2)
{
if (x1 > x2)
{
int n = x1;
x1 = x2;
x2 = n;
n = y1;
y1 = y2;
y2 = n;
}
//右
int x, y;
for (x = x1 + 1; x <= m_nCol; x++)
{
if (x == m_nCol)
//两个折点在选中方块的右侧,且两个折点在图案区域之外
if (XThrough(x2 + 1, y2, true))
//Y_Link(x2 + 1, m_nCol-1, y2)&&m_map[y1 * (m_nCol-1) + x]== BLANK_STATE
{
z2.x = m_nCol; z2.y = y1;
z1.x = m_nCol; z1.y = y2;
return true;
}
else
break ;
if (m_map[y1 * m_nCol + x] != BLANK_STATE)
break;
if (OneCornerLink(x, y1, x2, y2))
{
z2.x = x; z2.y = y1;
return true;
}
}
//左
for (x = x1 - 1; x >=-1; x--)
{
if (x == -1)
//两个折点在选中方块的左侧,且两个折点在图案区
项目花园范德彪
- 粉丝: 7534
- 资源: 219