作为菜鸟想编一个“连连看”小游戏,求指教
各位大虾们,小弟刚开始学C,最近想编一个连连看的游戏,判断路径的算法已经完成并写在下面,请大家看看,并不吝赐教。。另外我在做界面的时候遇到了一点困难想请教,如何在VC++中实现鼠标操作的读取,还有如何导入一些图片,因为我在写代码的时候是以矩阵中的值来区分图片的不同的(比如0是没有图片,1是1号图片,等等)如何导入这些图片并实现与这些值的关联啊?我研究了好多源代码都没搞懂。。。
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define XXX 10
#define YYY 10
#define nb_figures 10
typedef struct
{
int b;
COORD corner[2];
}link;
link link_simple1(int val[XXX][YYY],COORD c1,COORD c2)//查看c1和c2是否水平相联
{
int max,min,i;
link link;
if ((c1.X != c2.X)&&(c1.Y == c2.Y))
{
max = max(c1.X,c2.X);
min = min(c1.X,c2.X);
i = min + 1;
link.b = 1;
while ((link.b == 1)&&(i < max))
{
if (val[i][c1.Y] != 0)
link.b = 0;
i++;
}
}else
link.b = 0;
if (link.b == 1)
link.corner[0] = c1;
link.corner[1] = c2;
return link;
}
link link_simple2(int val[XXX][YYY],COORD c1,COORD c2)//查看竖直方向
{
int i,max,min;
link link;
if ((c1.X == c2.X)&&(c1.Y != c2.Y))
{
max = max(c1.Y,c2.Y);
min = min(c1.Y,c2.Y);
i = min + 1;
link.b = 1;
while ((link.b == 1)&&(i < max))
{
if (val[c1.X][i] != 0)
link.b = 0;
i++;
}
}else
link.b = 0;
if (link.b == 1)
link.corner[0] = c1;
link.corner[1] = c2;
return link;
}
link link_1_corner(int val[XXX][YYY],COORD c1,COORD c2)//是否能用一个拐点相连
{
COORD c3,c4;
int b1,b2;
link link;
if ((c1.X == c2.X)||(c1.Y == c2.Y))
link.b = 0;
else
{
c3.X = c1.X;
c3.Y = c2.Y;
c4.X = c2.X;
c4.Y = c1.Y;
b1 = ((link_simple1(val,c1,c3).b)&&(link_simple2(val,c3,c2).b)&&(val[c3.X][c3.Y] == 0));
b2 = ((link_simple2(val,c1,c4).b)&&(link_simple1(val,c4,c2).b)&&(val[c4.X][c4.Y] == 0));
}
if (b1 == 1)
{
link.corner[0] = c1;
link.corner[1] = c3;
}else if (b2 == 1)
{
link.corner[0] = c1;
link.corner[1] = c4;
}else
link.b = 0;
return link;
}
link link_2_corner(int val[XXX][YYY],COORD c1,COORD c2)//2个拐点
{
COORD c3,c4;
int b,i,j;
link link;
b = 0;
i = 0;
j = 0;
while ((b = 0)&&(j < YYY))
{
if ((j != c1.Y)&&(j != c2.Y))
{
c3.X = c1.X;
c3.Y = j;
c4.X = c2.X;
c4.Y = j;
b = (link_simple2(val,c1,c3).b)&&(link_simple1(val,c3,c4).b)&&(link_simple2(val,c4,c2).b)&&(val[c3.X][c3.Y] == 0)&&(val[c4.X][c4.Y] == 0);
}
j++;
}
while ((b = 0)&&(i < XXX))
{
if ((i != c1.X)&&(i != c2.X))
{
c3.Y = c1.Y;
c3.X = i;
c4.Y = c2.Y;
c4.X = i;
b = (link_simple1(val,c1,c3).b)&&(link_simple2(val,c3,c4).b)&&(link_simple1(val,c4,c2).b)&&(val[c3.X][c3.Y] == 0)&&(val[c4.X][c4.Y] == 0);
}
i++;
}
if (b == 1)
{
link.corner[0] = c3;
link.corner[1] = c4;
}else
link.b = 0;
return link;
}
link is_link(int val[XXX][YYY],COORD c1,COORD c2)//是否相连,并给出拐点坐标
{
link link;
if (link_simple1(val,c1,c2).b == 1)
link = link_simple1(val,c1,c2);
else if (link_simple2(val,c1,c2).b == 1)
link = link_simple2(val,c1,c2);
else if (link_1_corner(val,c1,c2).b == 1)
link = link_1_corner(val,c1,c2);
else if (link_2_corner(val,c1,c2).b == 1)
link = link_2_corner(val,c1,c2);
else
link.b = 0;
return link;
}
void dis(int val[XXX][YYY],COORD c1,COORD c2)//消去两个相连的“图”
{
val[c1.X][c1.Y] = 0;
val[c2.X][c2.Y] = 0;
}
void init_background()//初始化背景
{
int i,n;
COORD c1,c2;
int background[XXX][YYY] = {0};
for (i = 1;i < (XXX-2) * (YYY-2) / 2;i++)
{
srand(time(NULL));
n = rand() % nb_figures + 1;
do
{
c1.X = rand() % (XXX - 2) + 1;
c1.Y = rand() % (YYY - 2) + 1;
}while (background[c1.X][c1.Y] != 0);
background[c1.X][c1.Y] = n;
do
{
c2.X = rand() % (XXX - 2) + 1;
c2.Y = rand() % (YYY - 2) + 1;
}while (background[c2.X][c2.Y] != 0);
background[c2.X][c2.Y] = n;
}
}
就只写了这么多了。。
[ 本帖最后由 szy200414 于 2011-4-10 13:11 编辑 ]