首页 C/C++修行正文

C++二维数组走迷宫游戏

欲儿 C/C++修行 2019-04-29 431 0
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define N 15

//生成地图数组 
int map[N][N] = {
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
    {0,2,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,1,1,1,1,1,1,1,1,1,1,1,1,3,0},
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};


//申明全局变量 
int i;
int j;


//打印地图程序 
void print()
{
	int x=0,y = 0;
	for(x = 0;x<N;x++)
	{
    for( y =0;y<N;y++)
	   {
	   	if(map[x][y] ==0)
	   	{
		printf("■");
		}
		else if(map[x][y] == 1)
		{
		printf("  ");
		}
		else if (map[x][y] == 2)
		{
		printf("♂");
		}
		else if (map[x][y] == 3) 
		{
		printf("☆");
		}
	   }
    printf("\n");
	}
}

//找到移动的正 
void find()
{
    int x=0,y = 0;
	for(x = 0;x<N;x++)
	{
	for(y = 0;y<N;y++)
	{
		if(map[x][y] == 2)
		{
		i =x;
		j = y;
		break;
		}
	}	
	}
}



void down()
{
    if(map[i+1][j] == 0)
    {
	}
	else
	{
    map[i+1][j] = 2;
	map[i][j] = 1;
	system("cls");
    print();
    }
}
void up()
{
	if(map[i-1][j] == 0)
    {
	}
	else
	{
    map[i-1][j] = 2;
	map[i][j] = 1;
	system("cls");
    print();
    }
}
void left()
{
	if(map[i][j-1] == 0)
	{
	}
	else
	{
	map[i][j-1] = 2;
	map[i][j]	= 1;
	system("cls");
	print();
	}
}
void right()
{
	if(map[i][j+1] == 0)
	{
	}
	else if(map[i][j+1] == 3)
	{
	system("cls");
	printf("你已经获胜") ; 
	}
	else
	{
	map[i][j+1] = 2;
	map[i][j]	= 1;
	system("cls");
	print();
	}
}


int main()
{
	print();

	char choice;//移动	
    while(1)
	{
    choice=getch();
    find();
    switch(choice)
	{
    case 'w':  up(); 
    break;
    case 's':  down(); 
    break;
    case 'a':  left();
    break;
    case 'd':  right();
    break;
    }
    }
	
	
	
	while(1);
	return 0;
}

blob.png

blob.png

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论