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

下次来做第二关(逃!)

相关推荐
空山新雨(大队长)9 小时前
C 语言第一课:hello word c
c++·c·exe
饭碗的彼岸one11 小时前
C++ 并发编程:异步任务
c语言·开发语言·c++·后端·c·异步
EleganceJiaBao2 天前
我的创作纪念日
c
梁辰兴4 天前
数据结构:排序
数据结构·算法·排序算法·c·插入排序·排序·交换排序
charlie1145141915 天前
Windows 编程——字符串处理
windows·学习·c·字符串处理·windows编程
BlackQid6 天前
基于C的扫雷小游戏
游戏·c
牟同學7 天前
从竞态到原子:pread/pwrite 如何重塑高效文件 I/O?
linux·网络编程·c·多线程
studytosky8 天前
C语言数据结构之双向链表
c语言·数据结构·c++·算法·链表·c
BlackQid9 天前
基于C的二分查找和查月份天数小程序
算法·c
小牛历险记11 天前
手表--带屏幕音响-时间制切换12/24小时
c语言·开发语言·c·学习方法