//头文件
#define w 10
#define h 10
#define s (w*h)
#define KD 0 //□
#define ZA 1 //■
#define KOU 2//口
#define MU 3 //木
#define KUN 4//困
void Draw();
bool Run(int dir);
//-------------------------cpp其一
#include "txz.h"
#include <iostream>
int map[s] =
{
ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,
ZA,KD,KD,KD,KD,KD,KD,KD,KD,ZA,
ZA,KD,MU,KD,KD,MU,KD,KD,KD,ZA,
ZA,KD,KOU,KD,KD,KOU,KD,KD,KD,ZA,
ZA,KD,KUN,KD,KD,KD,KD,KD,KD,ZA,
ZA,KD,KD,KD,KD,KD,KD,KD,KD,ZA,
ZA,KD,KD,KD,KD,KD,KD,KD,KD,ZA,
ZA,KD,KD,KD,KUN,KD,KD,KD,KD,ZA,
ZA,KD,KD,KD,KD,KD,KD,KD,KD,ZA,
ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,ZA,
};
//英雄的坐标
int px = 3;
int py = 3;
void Draw()
{
char* ss[5] = {"□","■","口","木","困"};
for (int y = 0; y <h; ++y)
{
for (int x = 0; x < w; ++x)
{
if (x == px && y == py)
{
if (map[x+y*w] == KD)
std::cout<<"人";
else if (map[x+y*w] == KOU)
std::cout<<"囚";
}
else
std::cout<< ss[map[x+y*w]];
}
std::cout<<"\n";
}
}
//true胜利 false 没胜利
bool Run(int dir)
{
//胜负判断
bool win = true;
for (int i = 0;i < s; ++i)
{
if (map[i] == KOU || map[i] == MU)
{
win = false;
break;
}
}
if (win)
return true;
if (dir == -1)
return false;
int xx[4] = {0,0,-1,1};
int yy[4] = {-1,1,0,0};
//和英雄相邻的位置
int nearx = px + xx[dir];
int neary = py + yy[dir];
int near = nearx + neary * w;
//和英雄离得较远的位置
int farx = px + xx[dir] * 2;
int fary = py + yy[dir] * 2;
int far = farx + fary * w;
switch (map[near])
{
case KD ://□
case KOU://口
{
px = nearx; py = neary;
break;
}
case MU ://木
{
switch (map[far])
{
case KD:
{
map[far] = MU;
map[near] = KD;
px = nearx; py = neary;
break;
}
case KOU:
{
map[far] = KUN;
map[near] = KD;
px = nearx; py = neary;
break;
}
}
break;
}
case KUN://困
{
switch (map[far])
{
case KD:
{
map[far] = MU;
map[near] = KOU;
px = nearx; py = neary;
break;
}
case KOU:
{
map[far] = KUN;
map[near] = KOU;
px = nearx; py = neary;
break;
}
}
break;
}
}
return false;
}
//--------------------cpp main
#include <iostream>
#include <conio.h>
#include "txz.h"
void main()
{
while (1)
{
system("cls");
Draw();
int dir = -1;
int a = _getch();
if (a == 'w' || a == 'W')
dir = 0;
if (a == 's' || a == 'S')
dir = 1;
if (a == 'a' || a == 'A')
dir = 2;
if (a == 'd' || a == 'D')
dir = 3;
if (Run(dir))
{
std::cout<<"胜利!";
system("pause");
break;
}
}
}