队列:当点击一个空白格子的时候,算法如下:
将当前格子入队;
循环:一个格子出队。将没有打开的空白格子进队(要没有打开过,且是空白)。将周围的格子置为“已打开”。不断重复,直至队列为空。为了方便,将一个格子周围8个格子的坐标差值放到数组中,循环使用。
void GameMap::SearchBlank(int x,int y)
{
int tempindex[16]={-1, -1, 0, -1, 1, -1,
-1 ,0, 1, 0,
-1, 1, 0, 1, 1, 1};
.......
}
队列: queue.h queue.cpp

#ifndef __QUEUE_H
#define __QUEUE_H
class GQUEUE
...{
public:
GQUEUE();
~GQUEUE();
void CreateQueue(int len);
void DeleteQueue();
void CreateLog(); 
int InQueue(POINT x);
POINT OutQueue(void);
int IsFull();
int IsEmpty();
private:
POINT pData[400];
int front;
int rear;
int length;
};
#endif

/**////////////// queue.cpp //////////////////
//队列,用来做空白格子的搜索
#include "stdafx.h"
#include "queue.h"
#include "stdio.h"
GQUEUE::GQUEUE()
...{
front=0;
rear=0;
length=0;
}
GQUEUE::~GQUEUE()
...{
}
void GQUEUE::CreateQueue(int len)
...{
front=0;
rear=0;
length=len;
memset(pData,-1,sizeof(pData));
}
void GQUEUE::DeleteQueue()
...{
}
int GQUEUE::InQueue(POINT x)
...{
pData[rear]=x;
rear=(rear+1)%length;
return 1;
}
POINT GQUEUE::OutQueue(void)
...{
int temp_index;
temp_index=front;
front=(front+1)%length;
return pData[temp_index];
}
int GQUEUE::IsFull()
...{
return ((rear+1)%length == front)?1:0;
}
int GQUEUE::IsEmpty()
...{
return (rear == front)?1:0;
}
void GQUEUE::CreateLog()
...{
// flog=fopen("queuelog.txt","w");
}
本文介绍了一种基于队列实现的空白格子搜索算法,该算法用于在游戏中寻找并标记空白格子。通过创建一个队列结构,将需要检查的格子坐标依次入队,并遍历每个格子周围的空白格子,直到队列为空。文章提供了详细的队列操作代码实现。
2755

被折叠的 条评论
为什么被折叠?



