迷宫1.1

发一下上次的代码(作者良心吧?)

cpp 复制代码
#include<bits/stdc++.h>
#include <conio.h>
using namespace std;
char a[1005][1005]={
"         ",
"#########",
"#       #",
"#       #",
"#I      #",
"#########",
};
int main()
{
	for(int i=1;i<=5;i++)
	{
		puts(a[i]);
	}
	return 0;
}

现在,终于来到激动人心的时刻------移动功能

移动得用getch();

用#include <conio.h>(因为它还是#include <conio.h>中的函数)

cpp 复制代码
char c;
c=getch();

加上之前的代码

cpp 复制代码
#include<bits/stdc++.h>
#include <conio.h>
using namespace std;
char a[1005][1005]={
"################",
"#      #      *#",
"#    # #       #",
"#I   #         #",
"################",
};
int main()
{
	for(int i=1;i<=5;i++)
	{
		puts(a[i]);
	}
	char c;
	c=getch();
	return 0;
}

再定义两个变量i=4,j=1;

cpp 复制代码
int i=4,j=1;

再用一个while(1)无限循环

cpp 复制代码
while(1)
{
c=getch();
}

接下来是向上移动的代码

system("cls")是#include<windows.h>中的函数

cpp 复制代码
if(c=='w')
    {
    	if(a[i-1][j]=='#')
    	{
    		system("cls");
    for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
	system("cls");
    		
		}
		else
		{
    	a[i][j]=' ';
    	a[i-1][j]='I';
    	i=i-1;
    	system("cls");
    }
	}

接着,就可以做出其他三个件的移动了

cpp 复制代码
if(c=='s')
    {
    	if(a[i+1][j]=='#')
    	{
    		system("cls");
    for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
	system("cls");
    		
		}
		else
		{
    	a[i][j]=' ';
    	a[i+1][j]='I';
    	i=i+1;
    	system("cls");
    }
	}
	if(c=='a')
    {
    	if(a[i][j-1]=='#')
    	{
    		system("cls");
    for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
	system("cls");
    		
		}
		else
		{
    	a[i][j]=' ';
    	a[i][j-1]='I';
    	j=j-1;
    	system("cls");
    }
	}
	if(c=='d')
    {
    	if(a[i][j+1]=='#')
    	{
    		system("cls");
    for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
	system("cls");
    		
		}
		else
		{
    	a[i][j]=' ';
    	a[i][j+1]='I';
    	j=j+1;
    	system("cls");
    }
	}
	if('I'==a[2][14])
	{
		system("cls");
		cout<<"Win!";
		return 0; 
	}

最后发一下代码

cpp 复制代码
#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
using namespace std;
char a[1005][1005]={
"                ",
"################",
"#      #      *#",
"#    # #       #",
"#I   #         #",
"################",
};
int main()
{
	int i=4,j=1;
	for(int i=1;i<=5;i++)
	{
		puts(a[i]);
	}
	char c;
	while(1)
	{ 
	c=getch();
	if(c=='w')
    {
    	if(a[i-1][j]=='#')
    	{
    		system("cls");
    for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
	system("cls");
    		
		}
		else
		{
    	a[i][j]=' ';
    	a[i-1][j]='I';
    	i=i-1;
    	system("cls");
    }
	}
	if(c=='s')
    {
    	if(a[i+1][j]=='#')
    	{
    		system("cls");
    for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
	system("cls");
    		
		}
		else
		{
    	a[i][j]=' ';
    	a[i+1][j]='I';
    	i=i+1;
    	system("cls");
    }
	}
	if(c=='a')
    {
    	if(a[i][j-1]=='#')
    	{
    		system("cls");
    for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
	system("cls");
    		
		}
		else
		{
    	a[i][j]=' ';
    	a[i][j-1]='I';
    	j=j-1;
    	system("cls");
    }
	}
	if(c=='d')
    {
    	if(a[i][j+1]=='#')
    	{
    		system("cls");
    for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
	system("cls");
    		
		}
		else
		{
    	a[i][j]=' ';
    	a[i][j+1]='I';
    	j=j+1;
    	system("cls");
    }
	}
	if('I'==a[2][14])
	{
		system("cls");
		cout<<"Win!";
		return 0; 
	}
	for(int i=1;i<=6;i++)
	{
		puts(a[i]);
	}
    }
	return 0;
}

下次来做第二关(逃!)

相关推荐
。。。9043 天前
mit6s081 lab8 locks
操作系统·c
CAU界编程小白4 天前
数据结构系列之堆
数据结构·c
煤球王子6 天前
学而时习之:C语言中文件操作Error处理
c
煤球王子6 天前
学而时习之:C语言中的Exception处理
c
BlackQid8 天前
深入理解指针Part3——指针与数组
c
要做朋鱼燕9 天前
【AES加密专题】1.AES的原理详解和加密过程
运维·网络·密码学·c·加密·aes·嵌入式工具
煤球王子10 天前
学而时习之:C语言中的Error处理
c
qq_4378964314 天前
unsigned 是等于 unsigned int
开发语言·c++·算法·c
Lonble15 天前
C语言篇:预处理
c语言·c
BlackQid16 天前
深入理解指针Part1——C语言
c++·c