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

下次来做第二关(逃!)

相关推荐
hie988941 天前
C语言中的输入输出函数:构建程序交互的基石
c
布多2 天前
内存对齐:程序员必知的性能优化秘籍
性能优化·c
鑫宇吖15 天前
【工具使用-VScode】VScode如何设置空格和tab键显示
vscode·c
GodKK老神灭17 天前
STM32 实现PID
stm32·单片机·算法·c
莱茵不哈哈19 天前
操作系统八股文
c++·操作系统·c·八股文·进程线程
向上的车轮20 天前
语言特性适用的场景:卫星、火箭控制系统用什么开发语言?
java·开发语言·c++·c#·c·ada
Once_day22 天前
代码训练LeetCode(34)文本左右对齐
算法·leetcode·c
Once_day22 天前
代码训练LeetCode(33)字符串首次匹配
算法·leetcode·c
凉、介23 天前
Linux 下 pcie 初始化设备枚举流程代码分析
linux·运维·服务器·学习·嵌入式·c·pcie
Once_day24 天前
代码训练LeetCode(29)最后一个单词的长度
算法·leetcode·c