-
Notifications
You must be signed in to change notification settings - Fork 0
/
person.cpp
109 lines (100 loc) · 1.85 KB
/
person.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "person.h"
person::person()
{
x = 4;
y = 2;
direction = 1;
step = 0;
}
person::~person()
{
}
void person::move(map m) {
switch (direction)
{
case 1:x--;break;
case 2:y++;break;
case 3:x++;break;
case 4:y--;break;
}
m.print(x, y);
step++;
Sleep(100);//每走一步歇半秒
}
void person::start(map m)
{
while (1) {
changeDirection(m);
move(m);
if (x == 0 && y == 7) {//终点坐标
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << "成功啦!" << endl;
cout << endl;
cout << endl;
cout << "总用步数:" << step << endl;
break;
}
system("cls");
}
}
char person::getRight(map m)
{
switch (direction)
{
case 1: return m.getMap(x, y + 1);
case 2: return m.getMap(x + 1, y);
case 3: return m.getMap(x, y - 1);
case 4: return m.getMap(x - 1, y);
default: cout << "获取右边错误,终止程序!" << endl;exit(0);
}
}
char person::getLeft(map m)
{
switch (direction)
{
case 1: return m.getMap(x, y - 1);
case 2: return m.getMap(x - 1, y);
case 3: return m.getMap(x, y + 1);
case 4: return m.getMap(x + 1, y);
default: cout << "获取左边错误,终止程序!" << endl;exit(0);
}
}
char person::getFront(map m)
{
switch (direction)
{
case 1: return m.getMap(x - 1, y);
case 2: return m.getMap(x, y + 1);
case 3: return m.getMap(x + 1, y);
case 4: return m.getMap(x, y - 1);
default: cout << "获取前面错误,终止程序!" << endl;exit(0);
}
}
char person::getBehind(map m)
{
switch (direction)
{
case 1: return m.getMap(x + 1, y);
case 2: return m.getMap(x, y - 1);
case 3: return m.getMap(x - 1, y);
case 4: return m.getMap(x, y + 1);
default: cout << "获取后面错误,终止程序!" << endl;exit(0);
}
}
void person::changeDirection(map m)
{
if (getRight(m) == k) {
direction++;
if (direction > 4)
direction = 1;
}
else
while (getFront(m) == f) {
direction--;
if (direction < 1)
direction = 4;
}
}