#include<iostream> #include<cstdio> #include"conio.h" #include"windows.h" using namespace std; struct point { int x, y; }; const point PlayerStart = {10, 2}; const point PlayerEnd = {2, 10}; const int MapLenX = 11, MapLenY = 10; const char EndSymbol = '#', PlayerSymbol = '+', WallSymbol = '1', RoadSymbol = '0'; char Map[MapLenX][MapLenY]; const int MoveX[4] = {-1, 1, 0, 0}, MoveY[4] = {0, 0, -1, 1}; const int _UP = 0, _DOWN = 1, _LEFT = 2, _RIGHT = 3; class PlayerActor { public: point m_Location; bool m_IfWin; PlayerActor(); ~PlayerActor(); void PlayerMove(int _Direc); void Refresh(void); void CheckIfWin(void); }; PlayerActor::PlayerActor() { m_IfWin = false; this-> m_Location.x = PlayerStart.x; this-> m_Location.y = PlayerStart.y; Map[this-> m_Location.x][this-> m_Location.y] = PlayerSymbol; Map[PlayerEnd.x][PlayerEnd.y] = EndSymbol; PlayerActor::Refresh(); } PlayerActor::~PlayerActor() {

} void PlayerActor::PlayerMove(int _Direct) { if ( Map[this-> m_Location.x+MoveX[_Direct]][this-> m_Location.y+MoveY[_Direct]] == RoadSymbol || Map[this-> m_Location.x+MoveX[_Direct]][this-> m_Location.y+MoveY[_Direct]] == EndSymbol )/////// JUDGE IF CAN MOVE { Map[this-> m_Location.x][this-> m_Location.y] = RoadSymbol; this-> m_Location.x += MoveX[_Direct]; this-> m_Location.y += MoveY[_Direct]; Map[this-> m_Location.x][this-> m_Location.y] = PlayerSymbol; PlayerActor::Refresh(); PlayerActor::CheckIfWin(); } }

void PlayerActor::Refresh(void) { system("cls"); for (int i=1; i<=MapLenX; i++) { for (int j=1; j<=MapLenY; j++) { if (Map[i][j] == RoadSymbol) printf(" "); else if (Map[i][j] == WallSymbol) printf("* "); else if (Map[i][j] == '+') printf("%c ", PlayerSymbol); else if (Map[i][j] == EndSymbol) printf("%c",EndSymbol); } printf("\n"); } }

void PlayerActor::CheckIfWin(void) { if (this-> m_Location.x == PlayerEnd.x && this-> m_Location.y == PlayerEnd.y) m_IfWin = true; } void PlayerControl(PlayerActor* Player, int _KEY) { switch (_KEY) { case 119 : Player->PlayerMove(_UP); break; case 115 : Player->PlayerMove(_DOWN); break; case 97 : Player->PlayerMove(_LEFT); break; case 100 : Player->PlayerMove(_RIGHT); break; default: break; } } int main() { for (int i=1; i<=MapLenX; i++) { for (int j=1; j<=MapLenY; j++) { cin >> Map[i][j]; } } PlayerActor* Player = new PlayerActor; while (!Player->m_IfWin) { PlayerControl(Player, _getch()); } system("cls"); MessageBox(NULL, "You Win!", "Congratulations!", MB_OK); delete Player; return 0; } /* 地图代码 1111111111 1000000001 1011111111 1010000001 1011111101 1000000101 1111110101 1000010101 1011110101 1000000001 1111111111*/