走迷宫
/*
* 程序的版权和版本声明部分:
* Copyright (c) 2012, 烟台大学计算机学院
* All rights reserved.
* 文件名称:【数组】迷宫.cpp
* 作 者:毛通
* 完成日期:2012 年 12月 03日
* 版 本 号:v1.0
*/
[cpp]
#include<iostream>
#include<conio.h>
using namespace std;
void draw(void); //函数声明
void up(void);
void down(void);
void left(void);
void right(void);
void change(void);
int map[10][20]=
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,2,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1},
{1,0,1,0,1,1,0,0,0,1,0,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,0,0,0,1,0,1,1,0,1,0,1},
{1,0,1,1,0,1,0,0,0,1,0,0,0,0,0,1,1,0,0,1},
{1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1},
{1,0,1,0,1,1,0,0,0,1,0,0,1,0,0,1,0,0,0,1},
{1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,0,1},
{1,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,3,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
}; //数组初始化的模式
int x=1,y=1; //x y的值需要改变 定义全局变量
int x1=0,y1=0; //x1 y1的值需要改变 定义全局变量
bool victory=false; //bool变量 开始时为假 用来结束
int main()
{
draw(); //先画原图
char (ch);
while(!victory) //victory为假时 执行
{
char (ch); //字符变量
ch=getch(); //得到字符
switch (ch)
{
case 'w': up(); change();system("cls");draw();break;
case 'a': left(); change();system("cls");draw();break;
case 's': down(); change();system("cls");draw();break;
case 'd': right(); change();system("cls");draw();break;
}
}
cout<<"恭喜你 你赢啦!"<<endl;
return 0;
}
void up() //改变数组行列
{
if(1!=map[x-1][y])
{
x1=x;
y1=y;
x=x-1;
}
}
void down()
{
if(1!=map[x+1][y])
{
x1=x;
y1=y;
x=x+1;
}
}
void right()
{
if(1!=map[x][y+1])
{
x1=x;
y1=y;
y=y+1;
}
}
void left()
{
if(1!=map[x][y-1])
{
x1=x;
y1=y;
y=y-1;
}
}
void draw() //画迷宫
{
for (int val = 0; val < 10; ++val) //行
{
for (int flag = 0; flag < 20; ++flag) //列
{
if (1 == map[val][flag]) //1 为 墙
{
cout << "# ";
}
else
if (0 == map[val][flag]) //0 为 路
{
cout << " ";
}
&nbs
补充:软件开发 , C++ ,