// maze_v0.2.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <cstdlib> #include <conio.h> using namespace std; #define WIDTH 10 #define LENGTH 20 //函数声明// void draw(void); void moveUp(void); void moveDown(void); void moveLeft(void); void moveRight(void); void change(void); int map[WIDTH][LENGTH] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 2, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 3, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; int x = 1;//初始x坐标 int y = 1;//初始y坐标 int xx = 0; int yy = 0; bool victory = false; int main() { char select; draw(); while (!victory) { select = _getch(); switch(select) { case 'w': moveUp(); change(); system("cls"); draw(); break; case 's': moveDown(); change(); system("cls"); draw(); break; case 'a': moveLeft(); change(); system("cls"); draw(); break; case 'd': moveRight(); change(); system("cls"); draw(); break; } } cout << "win!\n"; return 0; } void draw()//draw a map// { int val_1 = 0; int val_2 = 0; for (val_1=0; val_1<WIDTH; ++val_1)//<>--------------遍历数组// { for (val_2=0; val_2<LENGTH; ++val_2) { if (1 == map[val_1][val_2])//<>-----------------画墙// { cout << "# "; } else if (0 == map[val_1][val_2])//<>-----------------画路// { cout << " "; } else if (2 == map[val_1][val_2])//<>-----------------画人// { cout << "·"; } else if (3 == map[val_1][val_2])//<>-----------------画出口// { cout << "* "; } } cout << endl; } } void moveUp()//上移// { if (1 != map[x-1][y]) { xx = x; yy = y; x = x - 1; } } void moveDown()//下移// { if (1 != map[x+1][y]) { xx = x; yy = y; x = x + 1; } } void moveLeft()//左移// { if (1 != map[x][y-1]) { xx = x; yy = y; y = y - 1; } } void moveRight()//右移// { if (1 != map[x][y+1]) { xx = x; yy = y; y = y + 1; } } void change() { if (3 == map[x][y]) { victory = true; } map[x][y] = 2; map[xx][yy] = 0; }