-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer.cpp
More file actions
120 lines (107 loc) · 3.71 KB
/
Copy pathcomputer.cpp
File metadata and controls
120 lines (107 loc) · 3.71 KB
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
110
111
112
113
114
115
116
117
118
119
120
#include "computer.hpp"
Computer::Computer(unsigned int size) {
field_size = size;
used = new bool* [field_size + 2];
near_foxes = new int* [field_size + 2];
for (unsigned int i = 0; i < size + 2; i++) {
used[i] = new bool[field_size + 2];
near_foxes[i] = new int[field_size + 2];
}
for (unsigned int i = 0; i < size + 2; i++) {
for (unsigned int j = 0; j < size + 2; j++) {
used[i][j] = false;
near_foxes[i][j] = 0; // мы не знаем, есть ли рядом лисы - считаем, что их нет
}
}
// отбортовка
for (unsigned int i = 0; i < size + 2; i++) {
used[i][0] = true;
used[0][i] = true;
used[i][size + 1] = true;
used[size + 1][i] = true;
}
}
Computer::~Computer() {
for (unsigned int i = 0; i < field_size + 2; i++) {
delete[] used[i];
delete[] near_foxes[i];
}
delete[] used;
delete[] near_foxes;
}
Result Computer::make_move(Foxes& foxes, Move& move) {
Result result = foxes.step(move);
used[move.x][move.y] = true;
// если лисы пойманы
if (result.type) {
// для всех счётчков лис рядом, уменьшаем их на 1
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (near_foxes[move.x + i][move.y + j] > 0) {
near_foxes[move.x + i][move.y + j] -= result.foxes_count;
}
}
}
} else {
// если получили новый счётчик лис, присваиваем его
near_foxes[move.x][move.y] = result.foxes_count;
// и если лис рядом нет - нет смысла ходить туда, где их нет
if (result.foxes_count == 0) {
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
used[move.x + i][move.y + j] = true;
}
}
}
}
return result;
}
Move Computer::search() {
queue <Move> priority_moves;
// найти, если нам известна информация, что у какой-то ячейки есть рядом лисы
for (int i = 1; i <= field_size; i++) {
for (int j = 1; j <= field_size; j++) {
if (near_foxes[i][j] > 0) {
priority_moves.push(Move(i - 1, j - 1));
priority_moves.push(Move(i , j - 1));
priority_moves.push(Move(i + 1, j - 1));
priority_moves.push(Move(i - 1, j ));
priority_moves.push(Move(i + 1, j ));
priority_moves.push(Move(i - 1, j + 1));
priority_moves.push(Move(i , j + 1));
priority_moves.push(Move(i + 1, j + 1));
break;
}
}
}
while (!priority_moves.empty()) {
Move mov = priority_moves.front();
priority_moves.pop();
if (used[mov.x][mov.y]) {
continue;
}
return mov;
}
// нет ходов в приоритете, делаем случайный
int moves = 0;
for (int i = 1; i <= field_size; i++) {
for (int j = 1; j <= field_size; j++) {
if (!used[i][j]) {
moves++;
}
}
}
// выбор случайного
int rand_move = rand() % moves;
for (int i = 1; i <= field_size; i++) {
for (int j = 1; j <= field_size; j++) {
if (used[i][j]) {
continue;
}
if (rand_move == 0) {
return Move(i, j);
}
rand_move--;
}
}
}