迷宫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;
}

下次来做第二关(逃!)

相关推荐
努力努力再努力wz3 天前
【Linux实践系列】:用c/c++制作一个简易的进程池
linux·运维·数据库·c++·c
易保山3 天前
MIT6.S081 - Lab7 Locks(锁优化 | 并发安全)
linux·操作系统·c
Mr_Chenph4 天前
Visual Studio Code 开发 树莓派 pico
vscode·c·micropython·pico
dami_king8 天前
用C++手搓一个贪吃蛇?
c++·游戏·c
ShiinaKaze8 天前
VSCode、clangd、mingw 配置与使用
vscode·c·mingw·clangd
易保山8 天前
MIT6.S081 - Lab7 Multithreading(进程调度)
linux·操作系统·c
charlie11451419113 天前
从0开始的构建的天气预报小时钟(基于STM32F407ZGT6,ESP8266 + SSD1309)——第2章——构建简单的ESP8266驱动
stm32·单片机·物联网·学习·c·esp8266
易保山16 天前
MIT6.S081 - Lab6 Copy-on-Write(写时复制)
linux·操作系统·c
Ronin-Lotus17 天前
嵌入式硬件篇---USB&UART串口
嵌入式硬件·c·uart·usb
易保山18 天前
MIT6.S081 - Lab5 Lazy(延迟分配)
linux·操作系统·c