-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaas.cpp
266 lines (231 loc) · 6.13 KB
/
taas.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <memory>
#include <cassert>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netdb.h>
#include <sstream>
#include <regex>
#include "taas.h"
#include "picojson.h"
static taas::board nullboard{{{{0,0,0,0}}, {{0,0,0,0}}, {{0,0,0,0}}, {{0,0,0,0}}}};
using namespace taas;
void rotC0(const board& b, board& ret) {
ret = b;
}
void rotC1(const board& b, board& ret) {
for(int y = 0; y < 4; y++) {
for(int x = 0; x < 4; x++) {
ret[x][3-y] = b[y][x];
}
}
}
void rotC2(const board& b, board& ret) {
for(int y = 0; y < 4; y++) {
for(int x = 0; x < 4; x++) {
ret[3-y][3-x] = b[y][x];
}
}
}
void rotC3(const board& b, board& ret) {
for(int y = 0; y < 4; y++) {
for(int x = 0; x < 4; x++) {
ret[3-x][y] = b[y][x];
}
}
}
void taas::rot(const board& b, board &ret, const int d) {
switch(d) {
case 0: return rotC0(b, ret);
case 1: return rotC1(b, ret);
case 2: return rotC2(b, ret);
case 3: return rotC3(b, ret);
default:
assert(0 <= d and d <= 3);
}
}
int movL(const board& b, board& out) {
int score_inc = 0;
for(int y=0; y < 4; y++) {
int ml = 0;
for(int x = 0; x < 4; x++) {
if(b[y][x] == 0) {
// n.t.d
} else if(out[y][ml] == 0) {
out[y][ml] = b[y][x];
} else if(out[y][ml] == b[y][x]) {
out[y][ml] = b[y][x] * 2;
score_inc += out[y][ml];
ml++;
} else {
out[y][ml+1] = b[y][x];
ml++;
}
}
}
return score_inc;
}
int taas::mov(const board& b, board& ret, const int d) {
int l,r;
auto b2(nullboard);
auto b3(nullboard);
switch(d) {
case 0: l = 1; r = 3; break;
case 1: l = 2; r = 2; break;
case 2: l = 3; r = 1; break;
case 3: l = 0; r = 0; break;
default:
assert(0 <= d and d <= 3);
l = -1; r = -1;
}
rot(b, b2, r);
int score_inc = movL(b2, b3);
rot(b3, ret, l);
return score_inc;
}
std::vector<std::pair<int, int>> taas::hole(const board& b) {
std::vector<std::pair<int, int>> ret;
for(int y=0; y < 4; y++) {
for(int x = 0; x < 4; x++) {
if(b[y][x] == 0) {
ret.push_back(std::make_pair(y, x));
}
}
}
return ret;
}
template<class Gen>
void taas::inp(board &b, const std::vector<int>& data, const std::vector<double>& ps, Gen& g) {
auto h = taas::hole(b);
if(h.empty()) return;
int mi = std::rand() % h.size();
int vi = taas::rwc(data, ps, g);
auto i = h[mi];
b[i.first][i.second] = vi;
}
int taas::score(const board& b) {
int ret = 0;
for(const auto& l:b) {
for(const auto& x:l) {
ret += x * std::log(x) / std::log(2);
}
}
return ret;
}
bool taas::over(const board& b) {
int yb[] = {0,0,0,0};
for(const auto& l:b) {
int xb = 0, i = 0;
for(const auto& x:l) {
if(x == 0) return false;
if(xb == x) return false;
if(yb[i] == x) return false;
yb[i] = x;
xb = x;
i++;
}
}
return true;
}
void taas::pb(const board& b) {
for(const auto& l:b) {
for(const auto& x:l) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
}
taas::Local::Local()
:over(false), moved(false), score(0), b(nullboard) {
std::random_device sg;
this->g.seed(sg());
auto v = {2, 4};
auto p = {0.9, 0.1};
inp(this->b, v, p, this->g);
inp(this->b, v, p, this->g);
}
bool taas::Local::move(int d) {
board bb = this->b;
auto v = {2, 4};
auto p = {0.9, 0.1};
this->score += taas::mov(this->b, this->b, d);
this->moved = (this->b != bb);
inp(this->b, v, p, this->g);
this->over = taas::over(this->b);
return this->moved;
}
taas::API::API(std::string serv, int port)
:over(false), moved(false), score(0), b(nullboard), serv(serv), port(port) {
auto json = js_get(std::string("/hi/start/json"));
const auto& obj = json.get<picojson::object>();
this->session = obj.at("session_id").get<std::string>();
auto jb = obj.at("grid").get<picojson::array>();
for(int y = 0; y < 4; y++) {
for(int x = 0; x < 4; x++) {
this->b[y][x] = jb[y].get<picojson::array>()[x].get<double>();
}
}
}
bool taas::API::move(int d) {
std::stringstream ss;
ss << "/hi/state/" << this->session << "/move/" << d << "/json";
auto json = this->js_get(ss.str());
const auto& obj = json.get<picojson::object>();
const auto& jb = obj.at("grid").get<picojson::array>();
for(int y = 0; y < 4; y++) {
for(int x = 0; x < 4; x++) {
this->b[y][x] = jb[y].get<picojson::array>()[x].get<double>();
}
}
this->score = obj.at("score").get<double>();
this->session = obj.at("session_id").get<std::string>();
this->moved = obj.at("moved").get<bool>();
this->over = obj.at("over").get<bool>();
return this->moved;
}
picojson::value taas::API::js_get(std::string path) {
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock < 0){
throw std::runtime_error(std::strerror(errno));
}
struct sockaddr_in addr;
struct hostent *servhost = gethostbyname(this->serv.c_str());
memset(static_cast<void *>(&addr), 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
bcopy(servhost->h_addr, &addr.sin_addr, servhost->h_length);
addr.sin_port = htons(this->port == 0 ? 8080 : this->port);
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1 ) {
throw std::runtime_error(std::strerror(errno));
}
std::string data;
std::stringstream ss;
ss << "GET " << path << " HTTP/1.0" << "\r\n\r\n";
// ss << "HOST " << serv << ":" << port << "\r\n\r\n";
std::string msg = ss.str();
write(sock, msg.c_str(), msg.size()+1);
while(true) {
char buf[1024];
int size = read(sock, buf, 1024);
if(size <= 0) break;
data += std::string(buf, buf + size);
}
close(sock);
if(data.find("Location: ") != std::string::npos) {
auto ia = data.find("Location: ");
auto ib = data.find("\r\n", ia);
std::string cont = data.substr(ia + 10, ib - (ia + 10));
std::cout << "moved: " << cont << std::endl;
return this->js_get(cont);
} else{
picojson::value v;
std::string err;
std::string cont = data.substr(data.find("\r\n\r\n")+4);
picojson::parse(v, cont.begin(), cont.end(), &err);
if(not err.empty()) throw std::runtime_error(err);
return v;
}
}