最近两天用HGE写了个对对碰...
感觉HGE写2D非常方便...以前我都打算自己封装一个2D的..
不过HGE的存在已经使我的想法没有太多意思了..
好了.以下代码就是交换两个点以后检查可消对象的方法...目前不是最佳的.
数据解释:
iObject[w][h] : 就是格子内的对象数据.1->N(N<100) 即是该格是什么. N / 100(N>=100) 即是该格的状态,可能是可消,放大,缩小,等状态.由自定义的.
W_DelFocus : 是一个vector<int> 容器,装有检查出来的可消对象表,横向的.
H_DelFocus : 是一个vector<int> 容器,装有检查出来的可消对象表,纵向的.
MakeFocus(): 便是将一个点生成为指定数据.如 w = 2, h = 3 则生成为 2 * 10 + 3 得 23;
如一个4位数的iObject的值分段为: xxxx 前2个为状态,第3个为横,第4个为纵.
NUM_LEAST: 是最少多少个可以消.通常为3;
bool
tMagicFloor::InspectPosition(
int
w1,
int
h1,
int
w2,
int
h2)
{
W_DelFocus.clear();
H_DelFocus.clear();
int i;
std::vector < int > _tempFocus;
if (h1 == h2) // 横向交换 : -|-|- : 横向两个不相等且abs(w1 - w2) == 1,纵向两条,横向两条
{
// 查找第一纵
for (i = h1; i < W_FLOOR; ++ i)
{
if (iObject[w1][i] != iObject[w1][h1])
break ;
_tempFocus.push_back(MakeFocus(w1, i));
}
for (i = h1 - 1 ; i >= 0 ; -- i)
{
if (iObject[w1][i] != iObject[w1][h1])
break ;
_tempFocus.push_back(MakeFocus(w1, i));
}
if (_tempFocus.size() >= NUM_LEAST)
H_DelFocus.insert(H_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 查找第二纵
for (i = h1; i < W_FLOOR; ++ i)
{
if (iObject[w2][i] != iObject[w2][h1])
break ;
_tempFocus.push_back(MakeFocus(w2, i));
}
for (i = h1 - 1 ; i >= 0 ; -- i)
{
if (iObject[w2][i] != iObject[w2][h1])
break ;
_tempFocus.push_back(MakeFocus(w2, i));
}
if (_tempFocus.size() >= NUM_LEAST)
H_DelFocus.insert(H_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 横向查找
int _left, _right;
_left = w1 > w2 ? w2 : w1;
_right = _left == w1 ? w2 : w1;
// 查找左横
for (i = _left; i >= 0 ; -- i)
{
if (iObject[i][h1] != iObject[_left][h1])
break ;
_tempFocus.push_back(MakeFocus(i, h1));
}
if (_tempFocus.size() >= NUM_LEAST)
W_DelFocus.insert(W_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 查找右横
for (i = _right; i < W_FLOOR; ++ i)
{
if (iObject[i][h1] != iObject[_right][h1])
break ;
_tempFocus.push_back(MakeFocus(i, h1));
}
if (_tempFocus.size() >= NUM_LEAST)
W_DelFocus.insert(W_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
}
else // 纵向两个交换(w1 == w2) : ==|== : 纵向两个不相等且abs(h1 - h2) == 1,纵向两条,横向两条
{
// 查找第一横
for (i = w1; i < W_FLOOR; ++ i)
{
if (iObject[i][h1] != iObject[w1][h1])
break ;
_tempFocus.push_back(MakeFocus(i, h1));
}
for (i = w1 - 1 ; i >= 0 ; -- i)
{
if (iObject[i][h1] != iObject[w1][h1])
break ;
_tempFocus.push_back(MakeFocus(i, h1));
}
if (_tempFocus.size() >= NUM_LEAST)
W_DelFocus.insert(W_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 查找第二横
for (i = w1; i < W_FLOOR; ++ i)
{
if (iObject[i][h2] != iObject[w1][h2])
break ;
_tempFocus.push_back(MakeFocus(i, h2));
}
for (i = w1 - 1 ; i >= 0 ; -- i)
{
if (iObject[i][h2] != iObject[w1][h2])
break ;
_tempFocus.push_back(MakeFocus(i, h2));
}
if (_tempFocus.size() >= NUM_LEAST)
W_DelFocus.insert(W_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 纵向查找
int _up, _down;
_up = h1 > h2 ? h2 : h1;
_down = _up == h1 ? h2 : h1;
// 查找上纵
for (i = _up; i >= 0 ; -- i)
{
if (iObject[w1][i] != iObject[w1][_up])
break ;
_tempFocus.push_back(MakeFocus(w1, i));
}
if (_tempFocus.size() >= NUM_LEAST)
H_DelFocus.insert(H_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 查找下纵
for (i = _down; i < W_FLOOR; ++ i)
{
if (iObject[w1][i] != iObject[w1][_down])
break ;
_tempFocus.push_back(MakeFocus(w1, i));
}
if (_tempFocus.size() >= NUM_LEAST)
H_DelFocus.insert(H_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
}
if (W_DelFocus.size() < NUM_LEAST) W_DelFocus.clear();
if (H_DelFocus.size() < NUM_LEAST) H_DelFocus.clear();
return (W_DelFocus.size() > 0 || H_DelFocus.size() > 0 );
}
{
W_DelFocus.clear();
H_DelFocus.clear();
int i;
std::vector < int > _tempFocus;
if (h1 == h2) // 横向交换 : -|-|- : 横向两个不相等且abs(w1 - w2) == 1,纵向两条,横向两条
{
// 查找第一纵
for (i = h1; i < W_FLOOR; ++ i)
{
if (iObject[w1][i] != iObject[w1][h1])
break ;
_tempFocus.push_back(MakeFocus(w1, i));
}
for (i = h1 - 1 ; i >= 0 ; -- i)
{
if (iObject[w1][i] != iObject[w1][h1])
break ;
_tempFocus.push_back(MakeFocus(w1, i));
}
if (_tempFocus.size() >= NUM_LEAST)
H_DelFocus.insert(H_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 查找第二纵
for (i = h1; i < W_FLOOR; ++ i)
{
if (iObject[w2][i] != iObject[w2][h1])
break ;
_tempFocus.push_back(MakeFocus(w2, i));
}
for (i = h1 - 1 ; i >= 0 ; -- i)
{
if (iObject[w2][i] != iObject[w2][h1])
break ;
_tempFocus.push_back(MakeFocus(w2, i));
}
if (_tempFocus.size() >= NUM_LEAST)
H_DelFocus.insert(H_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 横向查找
int _left, _right;
_left = w1 > w2 ? w2 : w1;
_right = _left == w1 ? w2 : w1;
// 查找左横
for (i = _left; i >= 0 ; -- i)
{
if (iObject[i][h1] != iObject[_left][h1])
break ;
_tempFocus.push_back(MakeFocus(i, h1));
}
if (_tempFocus.size() >= NUM_LEAST)
W_DelFocus.insert(W_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 查找右横
for (i = _right; i < W_FLOOR; ++ i)
{
if (iObject[i][h1] != iObject[_right][h1])
break ;
_tempFocus.push_back(MakeFocus(i, h1));
}
if (_tempFocus.size() >= NUM_LEAST)
W_DelFocus.insert(W_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
}
else // 纵向两个交换(w1 == w2) : ==|== : 纵向两个不相等且abs(h1 - h2) == 1,纵向两条,横向两条
{
// 查找第一横
for (i = w1; i < W_FLOOR; ++ i)
{
if (iObject[i][h1] != iObject[w1][h1])
break ;
_tempFocus.push_back(MakeFocus(i, h1));
}
for (i = w1 - 1 ; i >= 0 ; -- i)
{
if (iObject[i][h1] != iObject[w1][h1])
break ;
_tempFocus.push_back(MakeFocus(i, h1));
}
if (_tempFocus.size() >= NUM_LEAST)
W_DelFocus.insert(W_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 查找第二横
for (i = w1; i < W_FLOOR; ++ i)
{
if (iObject[i][h2] != iObject[w1][h2])
break ;
_tempFocus.push_back(MakeFocus(i, h2));
}
for (i = w1 - 1 ; i >= 0 ; -- i)
{
if (iObject[i][h2] != iObject[w1][h2])
break ;
_tempFocus.push_back(MakeFocus(i, h2));
}
if (_tempFocus.size() >= NUM_LEAST)
W_DelFocus.insert(W_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 纵向查找
int _up, _down;
_up = h1 > h2 ? h2 : h1;
_down = _up == h1 ? h2 : h1;
// 查找上纵
for (i = _up; i >= 0 ; -- i)
{
if (iObject[w1][i] != iObject[w1][_up])
break ;
_tempFocus.push_back(MakeFocus(w1, i));
}
if (_tempFocus.size() >= NUM_LEAST)
H_DelFocus.insert(H_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
// 查找下纵
for (i = _down; i < W_FLOOR; ++ i)
{
if (iObject[w1][i] != iObject[w1][_down])
break ;
_tempFocus.push_back(MakeFocus(w1, i));
}
if (_tempFocus.size() >= NUM_LEAST)
H_DelFocus.insert(H_DelFocus.end(), _tempFocus.begin(), _tempFocus.end());
_tempFocus.clear();
}
if (W_DelFocus.size() < NUM_LEAST) W_DelFocus.clear();
if (H_DelFocus.size() < NUM_LEAST) H_DelFocus.clear();
return (W_DelFocus.size() > 0 || H_DelFocus.size() > 0 );
}
可任意复制代码.转载请注明出处:原作BLOG(ShowLong);
以上只是一时想到的基本办法...
如有遇到更好的办法来与我交流噢...QQ:29774874