迷宫自动寻路的小程序,比较有意思,它会记录自己所走过的步数,往出口的方向做尝试,从而达到走出迷宫的目的!
话不多说,先上图:
这是一个奔跑在寻找出口小路上的小.......五角星( ^_^ )/~~哈哈
怎么样,是不是感觉挺有意思的,下面贴代码:
#include
#include
#include
#include
#include
using namespace std;
class node
{
public:
int x, y;
int lastOpt;
};
deque sta;
int x, y;
int endx, endy;
int mapW, mapH;
int steps;
int xopt[5] = { 0,0,1,0,-1 };
int yopt[5] = { 0,1,0,-1,0 };
int map[100][100] =
{
};
void init()
{
x = 1;
y = 1;
endx = 1;
endy = 9;
mapH = 10;
mapW = 10;
for (int i = 0; i
for (int j = 0; j
{
if (i == 0 || j == 0 || i == mapH || j == mapW)
map[i][j] = -1;
}
steps = 0;
map[1][2] = -1;
map[2][2] = -1;
map[3][2] = -1;
map[4][2] = -1;
map[6][2] = -1;
map[7][2] = -1;
map[8][2] = -1;
map[9][2] = -1;
map[9][3] = -1;
map[8][3] = -1;
map[1][4] = -1;
map[3][4] = -1;
map[4][4] = -1;
map[5][4] = -1;
map[6][4] = -1;
map[7][4] = -1;
map[1][6] = -1;
map[2][6] = -1;
map[3][6] = -1;
map[4][6] = -1;
map[5][6] = -1;
map[6][6] = -1;
map[7][6] = -1;
map[8][6] = -1;
map[8][7] = -1;
map[8][8] = -1;
map[7][8] = -1;
map[6][8] = -1;
map[5][8] = -1;
map[4][8] = -1;
map[3][8] = -1;
map[2][8] = -1;
map[1][8] = -1;
map[endx][endy] = 5;
}
void dis()
{
system("cls");
int ori = map[x][y];
map[x][y] = 1;
for (int i = 0; i
{
for (int j = 0; j
{
if (map[i][j] == 0)
printf(" ");
else if (map[i][j] == -1)
printf("");
else if (map[i][j] == 1)
printf("★");
else if (map[i][j] == 2)
printf(" .");
else if (map[i][j] == 5)
printf("※");
}
cout
}
for (int j = 0; j
cout
printf("\n\n 移动步数:%d 出口坐标:(%d,%d)\n", steps, endx, endy);
map[x][y] = ori;
}
int can(int n)
{
if (map[x + xopt[n]][y + yopt[n]] == 0 || map[x + xopt[n]][y + yopt[n]] == 5)
return 1;
return 0;
}
void visit(int n)
{
map[x][y] = 2;
x += xopt[n];
y += yopt[n];
node tem;
tem.x = x;
tem.y = y;
tem.lastOpt = n;
sta.push_back(tem);
steps++;
}
int main()
{
init();
node tem;
while (x != endx || y != endy)
{
int cans = 0;
for (int i = 1; i
{
if (can(i))
{
cans = 1;
visit(i);
break;
}
}
if (!cans)
{
if (!sta.empty())
{
tem = sta.back();
map[tem.x][tem.y] = 0;
sta.pop_back();
}
else
{
map[x][y] = 2;
x += xopt[tem.lastOpt];
x += yopt[tem.lastOpt];
dis();
break;
}
}
dis();
Sleep(500);
}
if (x == endx && y == endy)
printf("\n 我找到终点啦!\n");
else
printf("\n \n 我走完了,但是没找到终点!\n");
getchar();
return 0;
}
开心,跑出迷宫了~~~
哈哈 找到终点啦
C语言制作表白神器-LOVE
#include
#include
#include
#include
//3维数组,存放LOVE字符
char LETTER[][5][5] = {
{
0, 1, 0, 0, 0,
0, 1, 0, 0, 0,
0, 1, 0, 0, 0,
0, 1, 0, 0, 0,
0, 1, 1, 1, 0
},
{
0, 0, 1, 0, 0,
0, 1, 0, 1, 0,
0, 1, 0, 1, 0,
0, 1, 0, 1, 0,
0, 0, 1, 0, 0
},
{
0, 1, 0, 1, 0,
0, 1, 0, 1, 0,
0, 1, 0, 1, 0,
0, 1, 0, 1, 0,
0, 0, 1, 0, 0
},
{
0, 1, 1, 1, 0,
0, 1, 0, 0, 0,
0, 1, 1, 1, 0,
0, 1, 0, 0, 0,
0, 1, 1, 1, 0
}
};
int status = 1;//控制线程函数的运行状态
int speed = 500;//字符变换的速度
HANDLE hout;//控制台标准输出句柄
CONSOLE_SCREEN_BUFFER_INFO csbi;//控制台屏幕缓冲区信息结构体
char *symbol[] = {"☆", "★", "", "※", "", ""};// 用这些特殊字符组成LOVE中的各个字母
WORD attr[] = {
FOREGROUND_RED | FOREGROUND_INTENSITY, //红色
FOREGROUND_GREEN | FOREGROUND_INTENSITY,//绿色
FOREGROUND_BLUE | FOREGROUND_INTENSITY,//蓝色
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY//白色
};
//在屏幕上画出LOVE
void DrawLove()
{
int i, j, c;
COORD pos;//光标的坐标
DWORD dwLen;
for(c = 0; c
{
for(i = 0; i
{
pos.X = c * 5 * 2;
pos.Y = i;
SetConsoleCursorPosition(hout, pos);
for(j = 0; j
{
if(LETTER[c][i][j])
{
//获取当前屏幕缓冲区信息
GetConsoleScreenBufferInfo(hout, &csbi);
printf("%s", symbol[rand() %
(sizeof symbol / sizeof symbol[0])]);
//用指定的颜色填充字符
FillConsoleOutputAttribute(hout, attr[rand() %
(sizeof attr / sizeof attr[0])], 2,
csbi.dwCursorPosition, &dwLen);
}
else
{
printf(" ");//打印两个空格
//线程的处理函数
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
while(1)
{
if(status)
{
DrawLove();
Sleep(speed);
}
}
return 0;
}
int main()
{
HANDLE hThread;
int inChar;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
srand((unsigned)time(0));
hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
CloseHandle(hThread);
while(1)
{
inChar = getch();
if('q' == inChar)
{
break;
}
else if('w' == inChar && speed > 50)
speed -= 50;
else if('s' == inChar && speed
speed += 50;
else if('p' == inChar)
status = 0;
else if('c' == inChar)
status = 1;
}
return 0;
}