使用二维数组描绘一副地图,并描绘一个人物。通过键盘控制该人物在地图行走(不能穿墙)
如下图
1111111111
1000000001
1 1
1 * 1
1 1
1 1
1 1
1111111111;
#include<iostream>
using namespace std;
void main()
{
/*
使用二维数组描绘一副地图,并描绘一个人物。通过键盘控制该人物在地图行走(不能穿墙)
如下图
1111111111
1000000001
1 1
1 * 1
1 1
1 1
1 1
1111111111;
*/
//实现
int conset[10][10] = {
{ 1,1,1,1,1,1,1,1,1,1, },
{ 1,0,0,0,0,0,0,0,0,1, },
{ 1,0,0,0,0,0,0,0,0,1, },
{ 1,0,0,0,0,0,0,0,0,1, },
{ 1,0,0,0,0,0,0,0,0,1, },
{ 1,0,0,0,5,0,0,0,0,1, },
{ 1,0,0,0,0,0,0,0,0,1, },
{ 1,0,0,0,0,0,0,0,0,1, },
{ 1,0,0,0,0,0,0,0,0,1, },
{ 1,1,1,1,1,1,1,1,1,1, },
};
int num = 0;
while (num < 1) //死循环 ,可以重复移动
{
char quii;
cin >> quii;
switch (quii)
{
case 'a': //左移动一格
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
if (conset[i][k] == 5 && conset[i][k - 1] != 1) //没碰到壁就交换
{
conset[i][k - 1] = 5;
conset[i][k] = 0;
break;
}
}
for (int j = 0; j < 10; j++)
{
cout << conset[i][j];
}
cout << endl;
}
break;
case 's':
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
if (conset[i][k] == 5 && conset[i + 1][k] != 1) //没碰到壁就交换,运行结果不理想
{
conset[i+1][k] = 5;
conset[i][k] = 0;
break;
}
}
for (int j = 0; j < 10; j++)
{
cout << conset[i][j];
}
cout << endl;
}
break;
case 'd':
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
if (conset[i][k] == 5 && conset[i][k + 1] != 1) //没碰到壁就交换,运行结果不理想
{
conset[i][k + 1] = 5;
conset[i][k] = 0;
break;
}
}
for (int j = 0; j < 10; j++)
{
cout << conset[i][j];
}
cout << endl;
}
break;
case 'w':
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 10; k++)
{
if (conset[i][k] == 5 && conset[i - 1][k] != 1 && i - 1 >= 0) //没碰到壁就交换
{
conset[i - 1][k] = 5;
conset[i][k] = 0;
break;
}
}
for (int j = 0; j < 10; j++)
{
cout << conset[j][i];
}
cout << endl;
}
break;
//default:
}
}
system("pause");
}