-
Notifications
You must be signed in to change notification settings - Fork 0
/
wybe.cpp
171 lines (145 loc) · 4.3 KB
/
wybe.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include"wybe.h"
#include<queue>
#include<stdlib.h>
using namespace std;
player notThisOne(player thisOne) {
switch(thisOne) {
case playerOne:
return playerTwo;
case playerTwo:
return playerOne;
default:
return empty;
}
}
void wybe::buildGraph(player now) {
const unsigned long size = (this->game)->getSize();
this->graph = vector<vector<node> >(size, vector<node>(size));
player enemy = notThisOne(now);
srand(time(NULL));
for(unsigned long x = 0; x < size; x++) {
for(unsigned long y = 0; y < size; y++) {
this->graph[x][y] = node(x, y);
// neighbor
if(x > 0 && this->status(x, y) != enemy) {
this->graph[x][y].neigbor.push_back(pair<int, int>(x - 1, y));
}
if(x + 1 < size && this->status(x + 1, y) != enemy) {
this->graph[x][y].neigbor.push_back(pair<int, int>(x + 1, y));
}
if(x > 0 && y + 1 < size && this->status(x - 1, y + 1) != enemy) {
this->graph[x][y].neigbor.push_back(pair<int, int>(x - 1, y + 1));
}
if(y > 0 && this->status(x, y - 1) != enemy) {
this->graph[x][y].neigbor.push_back(pair<int, int>(x, y - 1));
}
if(y + 1 < size && this->status(x, y + 1) != enemy) {
this->graph[x][y].neigbor.push_back(pair<int, int>(x, y + 1));
}
if(x + 1 < size && y > 0 && this->status(x + 1, y - 1) != enemy) {
this->graph[x][y].neigbor.push_back(pair<int, int>(x + 1, y - 1));
}
// weight
if(this->status(x, y) == now) {
this->graph[x][y].weight = this->myWeight;
} else if(this->status(x, y) == empty) {
this->graph[x][y].weight = rand()
% (this->emptyWeight.second - this->emptyWeight.first)
+ this->emptyWeight.first;
} else {
this->graph[x][y].weight = this->emptyWeight.second * 100;
}
}
}
}
player wybe::status(int x, int y) {
return this->game->getField(x, y)->getStatus();
}
vector<pair<int, int> > wybe::dijkstra(player now) {
priority_queue<priorityNode, vector<priorityNode>, compare> queue;
pair<int, int> last (0, 0);
priorityNode cuNode;
node *nodeNow;
for(unsigned long i = 0; i < this->graph.size(); i++) {
switch(now) {
case playerOne:
nodeNow = &this->graph[i][0];
break;
case playerTwo:
nodeNow = &this->graph[0][i];
break;
default:
nodeNow = &this->graph[0][i];
}
cuNode.cNode = nodeNow;
cuNode.predecessor = nodeNow->self;
cuNode.priority = nodeNow->weight;
queue.push(cuNode);
}
while(!queue.empty()) {
cuNode = queue.top();
queue.pop();
if(cuNode.cNode->checked) {
continue;
}
cuNode.cNode->parent = cuNode.predecessor;
cuNode.cNode->checked = true;
if(cuNode.cNode->checkDestiny(now, this->graph.size())) {
last = cuNode.cNode->self;
break;
}
vector<pair<int, int> > neigbors = cuNode.cNode->neigbor;
priorityNode nextNeigbor;
for(unsigned long i = 0; i < neigbors.size(); i++) {
node *nowNeigbor = &this->graph[neigbors[i].first][neigbors[i].second];
if(!nowNeigbor->checked) {
nextNeigbor.cNode = nowNeigbor;
nextNeigbor.priority = nowNeigbor->weight;
nextNeigbor.priority += cuNode.priority;
nextNeigbor.predecessor = cuNode.cNode->self;
queue.push(nextNeigbor);
}
}
}
vector<pair<int, int> > path;
path.push_back(last);
if(last == pair<int, int>(0, 0)) {
return path;
}
// just needs to be something other than 0
int significantCoordinate = 5;
pair<int, int> parent;
while(significantCoordinate != 0) {
parent = this->graph[path.back().first][path.back().second].parent;
path.push_back(parent);
switch(now) {
case playerOne:
significantCoordinate = path.back().second;
break;
case playerTwo:
significantCoordinate = path.back().first;
break;
default:
significantCoordinate = path.back().second;
}
}
return path;
}
pair<int, int> wybe::crossPoint(vector<pair<int, int> > myPath, vector<pair<int, int> > enemyPath) {
for(unsigned long i = 0; i < myPath.size(); i++) {
for(unsigned long j = 0; j < enemyPath.size(); j++) {
if(myPath[i] == enemyPath[j]) {
return myPath[i];
}
}
}
return myPath[myPath.size() / 2];
}
void wybe::turn() {
this->buildGraph(this->me);
vector<pair<int, int> > myPath = dijkstra(this->me);
this->buildGraph(notThisOne(this->me));
vector<pair<int, int> > enemyPath = dijkstra(notThisOne(this->me));
pair<int, int> desiredField = crossPoint(myPath, enemyPath);
this->game->setzen(desiredField, this->me);
}