-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.cpp
76 lines (74 loc) · 1.44 KB
/
random.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
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<unistd.h>
using namespace std;
int main() {
printf("RANDOM\n");
fflush(stdout);
srand(time(NULL)*getpid());
char opp[21];
scanf("%s",opp);
int w, h, x1, x2, y1, y2;
bool** board;
scanf("%d%d",&w,&h);
board = new bool*[w];
for(int i = 0; i<w; i++) {
board[i] = new bool[h];
for(int j = 0; j<h; j++) board[i][j] = 0;
}
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
board[x1][y1] = board[x2][y2] = 1;
while(1) {
bool done = false;
for(int i = 0; i<100 && !done; i++) {
int rd = rand()%4;
if(rd == 0) {
if(y1 < h-1 && !board[x1][y1+1]) {
printf("N\n");
y1++;
board[x1][y1]=1;
done = true;
}
} else if(rd == 1) {
if(x1 < w-1 && !board[x1+1][y1]) {
printf("E\n");
x1++;
board[x1][y1]=1;
done = true;
}
} else if(rd == 2) {
if(x1 > 0 && !board[x1-1][y1]) {
printf("W\n");
x1--;
board[x1][y1]=1;
done = true;
}
} else if(rd == 3) {
if(y1 > 0 && !board[x1][y1-1]) {
printf("S\n");
y1--;
board[x1][y1]=1;
done = true;
}
}
}
if(!done) printf("N\n");
fflush(stdout);
char oppm[5];
scanf("%s",oppm);
if(!strcmp(oppm,"O") || !strcmp(oppm,"X") || !strcmp(oppm,"T")) return 0;
if(!strcmp(oppm,"N")) {
y2++;
} else if(!strcmp(oppm,"E")) {
x2++;
} else if(!strcmp(oppm,"W")) {
x2--;
} else {
y2--;
}
board[x2][y2]=1;
}
return 0;
}