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

下次来做第二关(逃!)

相关推荐
moringlightyn13 小时前
Linux---进程状态
linux·运维·服务器·笔记·操作系统·c·进程状态
树在风中摇曳1 天前
数据结构与算法基础入门 —— 从概念到复杂度理解
开发语言·c
lucky_dog2 天前
c语言——while循环续,for循环
c
树在风中摇曳4 天前
每日小练——宏的运用
c
huangyuchi.11 天前
【Linux网络】Socket编程实战,基于UDP协议的Dict Server
linux·网络·c++·udp·c·socket
moringlightyn13 天前
进度条+ 基础开发工具----版本控制器git 调试器gdb/cgdb
笔记·git·其他·c·调试器·gdb/cgdb·进度条 倒计时
l1t16 天前
利用DeepSeek采用hugeint转字符串函数完善luadbi-duckdb的decimal处理
数据库·lua·c·duckdb·deepseek
l1t17 天前
luadbi和luasql两种lua duckdb驱动的性能对比
开发语言·单元测试·lua·c·csv·duckdb
moringlightyn18 天前
基础开发工具--编译器g++/gcc 自动化构建make/Makefile
linux·运维·笔记·自动化·c·编译器·make/makefile
howard200521 天前
CentOS 7上编写C程序访问MySQL数据表
c·centos 7·mysql 8.0.43