-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobo_find_way.cpp
104 lines (94 loc) · 1.92 KB
/
robo_find_way.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
#include<iostream>
#include<windows.h>
#include<string>
using namespace std;
int **map;
int n;
typedef struct {
int x;
int y;
}absolute;
absolute robot;
void createEmptyMap(){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
map[i][j] = 0;
}
}
}
void print(){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(map[i][j] == 0) cout << " .";
if(map[i][j] == 1) cout << " R";
if(map[i][j] == 2) cout << " B";
}
cout << endl;
cout << endl;
}
}
void process(char c){
if(c == '1'){
if(robot.x-1 >= 0 && map[robot.x-1][robot.y] != 2){
cout << c;
map[robot.x][robot.y] = 0;
robot.x = robot.x - 1;
}
}
if(c == '2'){
if(map[robot.x][robot.y+1] != 2 && robot.y +1 < n){
map[robot.x][robot.y] = 0;
robot.y = robot.y + 1;
}
}
if(c == '3'){
if(map[robot.x+1][robot.y] != 2 && robot.x+1 < n){
map[robot.x][robot.y] = 0;
robot.x = robot.x + 1;
}
}
if(c == '4'){
if(robot.y-1 >= 0 && map[robot.x][robot.y-1] != 2){
map[robot.x][robot.y] = 0;
robot.y = robot.y - 1;
}
}
map[robot.x][robot.y] = 1;
}
int main(){
robot.x = 0;
robot.y = 0;
cout << "Input map size: "; cin >> n;
map = new int*[n];
for(int i=0; i<n; i++)
map[i] = new int[n];
createEmptyMap();
while(1){
int x = 0;
int y = 0;
cout << "Input absolute of barrier: " << endl;
cout << "x = "; fflush(stdin); cin >> x;
cout << "y = "; fflush(stdin); cin >> y;
if(x > -1 && y > -1 && x < n && y < n) map[x][y] = 2;
cout << "Have another barrier? <y/n> ";
char c; fflush(stdin); cin >> c;
if(c == 'n') break;
}
map[robot.x][robot.y] = 1;
system("cls");
cout << "Map: " <<endl;
print();
while(1){
string way;
cout << "\n1.Up 2.Right 3.Down 4.Left \nWay:"; fflush(stdin); getline(cin,way);
for(int i=0; i<way.length(); i++){
process(way[i]);
system("cls");
string sub = way.substr(i+1,way.length());
cout << endl << sub << endl;
print();
Sleep(1000);
}
}
return 0;
}