04
2012
04

用C#编写QQ连连看助手

这几天闲着陪MM去玩QQ连连看,不知道别人是不是都用了外挂,速度好快,自己手动实在是跟不上。上网下载了一流的外挂,可惜要注册 干脆自己动手写个吧。。。程序原理

通过FindWindow 获得QQ连连看的窗口handle,再捕捉QQ连连看的窗口,然后分释每个格的颜色,判断数据,然后存在一个two dimensional array里。这样程序就有了一份QQ连连看的数据了。

数据都有了,接下来就要找出两点之间是否能连得通。。这个算法也简单。先判断是否两点之间的位置,如果直线则只有一种连接的可能,比如两点的X相同,那么只能是上下的连接等。。

路茎的代码如下
程序代码
using System;
using System.Drawing;
using System.Windows.Forms;

namespace LianLianKan
{
///
/// Summary description for FindPath.
/// This class is used to find a existing path of two point.
/// Author: DX,
/// email: webmaster@51home.org
///

public class FindPath
{
public FindPath()
{
}

#region GetTop() GetBottom, GetLeft, GetRight
private int GetTop(int [,] map, Point p)
{
if(p.X <= 0)
return 0;
else
{
for(int i=p.X-1; i>=0; i--)
if(map[i,p.Y] != 0)
return (i+1);
return 0;
}
}
private int GetBottom(int [,] map, Point p)
{
int maxLen = map.GetLength(0);
if(p.X >= maxLen )
return maxLen-1;
else
{
for(int i=p.X+1; iif( map[i, p.Y] != 0)
return (i-1);
return maxLen-1;
}
}
private int GetLeft(int [,] map, Point p)
{
if(p.Y <= 0)
return 0;
else
{
for(int i = p.Y-1; i>=0; i--)
if( map[p.X, i] != 0)
return (i+1);
return 0;
}
}
private int GetRight(int [,] map, Point p)
{
int maxLen = map.GetLength(1);
if(p.Y >= maxLen)
return maxLen-1;
else
{
for(int i=p.Y+1; iif(map[p.X, i] != 0)
return (i-1);
return maxLen-1;
}
}
#endregion

private bool CheckHorizontalPath(int [,] map, Rectangle r)
{
bool check;
for(int i= r.Y; i<= r.Y + r.Height; i++)
{
check = true;
for(int j=r.X + 1; j< r.X + r.Width; j++)
if( map[i,j] != 0 )
{
check = false;
break;
}

if(check)
{
// MessageBox.Show(string.Format("Rectangle x:{0}, y:{1}, Width:{2}, Height:{3}",r.X,r.Y,r.Width,r.Height));
return true;
}
}
return false;
}

private bool CheckVerticalPath(int [,] map, Rectangle r)
{
bool check;
for(int i = r.X; i<= r.X + r.Width; i++)
{
check = true;
for(int j = r.Y + 1; j < r.Y + r.Height; j++)
if(map[j,i] != 0)
{
check = false;
break;
}

if(check)
{
// MessageBox.Show(string.Format("Rectangle x:{0}, y:{1}, Width:{2}, Height:{3}",r.X,r.Y,r.Width,r.Height));
return true;
}
}
return false;
}

private bool HorizontalConnection(int [,] map, Point p1,Point p2)
{
int upper,lower;
int top1,top2,bottom1,bottom2;

top1 = GetTop(map,p1);
bottom1 = GetBottom(map, p1);

top2 = GetTop(map,p2);
bottom2 = GetBottom(map, p2);

upper = top1 > top2 ? top1 : top2;
lower = bottom1 < bottom2 ? bottom1 : bottom2;

if(upper > lower)
return false;
else
{
int x = p1.Y < p2.Y ? p1.Y : p2.Y;
Rectangle r = new Rectangle(x, upper, Math.Abs(p2.Y - p1.Y), lower - upper);

return CheckHorizontalPath(map,r);
}
}
private bool VerticalConnection(int [,] map, Point p1, Point p2)
{
int left, right;
int left1, left2, right1, right2;

left1 = GetLeft(map, p1);
right1 = GetRight(map, p1);
left2 = GetLeft(map,p2);
right2 = GetRight(map, p2);

left = left1 > left2 ? left1 : left2;
right = right1 < right2 ? right1 : right2;

if(left > right)
return false;
else
{
int y = p1.X < p2.X ? p1.X : p2.X;
Rectangle r = new Rectangle(left, y, right - left, Math.Abs(p1.X - p2.X));

return CheckVerticalPath(map, r);
}
}
public bool IsThereAPath(int [,] map, Point p1, Point p2)
{
if(p1.X == p2.X) // if two block is Vertical then only check vertical connection
return HorizontalConnection(map, p1, p2);
else if(p1.Y == p2.Y) // if two block on is horizontal, then only check for horizontal connection
return VerticalConnection(map, p1, p2);
else if( HorizontalConnection(map, p1, p2) ) // otherwise check for both
return true;
else
return VerticalConnection(map, p1, p2);
}
}
}
« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。