Skip to content

Commit bbb214e

Browse files
committed
Update libblokusduo
1 parent 045b84f commit bbb214e

9 files changed

+45
-41
lines changed

docs/blokus.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/hm5move.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/hm5move.wasm

-21.2 KB
Binary file not shown.

src/backend.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class WorkerBackend {
77
this.worker = new Worker('hm5move.js');
88
this.worker.addEventListener('message', (e) => {
99
let move = new Move(e.data.move);
10-
console.log(e.data.nps + ' nps');
10+
console.log(`${move.fourcc()} (${e.data.nps} nps)`);
1111
this.handler(move);
1212
});
1313
}

src/move.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Move {
99
this.m = x << 4 | y! | piece_id! << 8;
1010
else
1111
this.m = x;
12-
} else if (x == '----') {
12+
} else if (x == '0000') {
1313
this.m = 0xffff;
1414
} else {
1515
let xy = parseInt(x.substring(0, 2), 16);
@@ -28,7 +28,7 @@ export class Move {
2828

2929
fourcc() {
3030
if (this.isPass())
31-
return '----';
31+
return '0000';
3232
return ((this.m & 0xff) + 0x11).toString(16) +
3333
String.fromCharCode(117 - this.blockId()) +
3434
this.direction();

test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
test("Move", function(){
33
ok(Move.PASS.isPass());
4-
ok((new Move("----")).isPass());
4+
ok((new Move("0000")).isPass());
55
var m = new Move(3, 5, 0x12);
66
same(m.x(), 3);
77
same(m.y(), 5);
@@ -39,7 +39,7 @@ test("Board constructor", function(){
3939

4040
test("first move", function(){
4141
var b = new Board();
42-
ok(b.isValidMove(new Move("----")));
42+
ok(b.isValidMove(new Move("0000")));
4343
ok(b.isValidMove(new Move("66t0")));
4444
ok(b.isValidMove(new Move("55t0")));
4545
ok(!b.isValidMove(new Move("44t0")));
@@ -120,8 +120,8 @@ test("end game", function(){
120120
checkMove(new Move("B6g0"));
121121
checkMove(new Move("8Ej2"));
122122
checkMove(new Move("81f6"));
123-
checkMove(new Move("----"));
123+
checkMove(new Move("0000"));
124124
checkMove(new Move("63a0"));
125-
checkMove(new Move("----"));
125+
checkMove(new Move("0000"));
126126
ok(!b.canMove());
127127
});

wasm/hm5move.cpp

+31-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
1+
#include <emscripten.h>
12
#include <stdlib.h>
23
#include <time.h>
34

5+
#include <string_view>
6+
47
#include "blokusduo.h"
58
using namespace blokusduo;
69

7-
extern "C" const char* hm5move(const char* pathstr, int max_depth,
8-
int time_limit);
9-
extern "C" int getVisitedNodes();
10-
11-
Move com_move(const Board& b, int max_depth, int time_ms) {
12-
Move move;
13-
int score = 100;
14-
15-
move = search::opening_move(b);
16-
if (move == Move::invalid()) {
17-
search::SearchResult r;
18-
if (b.turn() < 25)
19-
r = search::negascout(b, max_depth, time_ms / 2, time_ms);
20-
else if (b.turn() < 27)
21-
r = search::wld(b, 1000);
22-
else
23-
r = search::perfect(b);
24-
move = r.first;
25-
score = r.second;
10+
EM_JS(unsigned, clock_ms, (), { return performance.now(); });
11+
12+
Move com_move(const Board& b, int max_depth, int limit_ms) {
13+
Move move = search::opening_move(b);
14+
if (move.is_valid()) return move;
15+
16+
unsigned start_ms = clock_ms();
17+
search::SearchResult r;
18+
if (b.turn() < 25) {
19+
r = search::negascout(
20+
b, max_depth, [start_ms, limit_ms](int depth, search::SearchResult r) {
21+
unsigned now = clock_ms();
22+
printf("%d> %s %d nodes %d ms\n", depth, r.first.code().c_str(),
23+
search::visited_nodes, now - start_ms);
24+
return now < start_ms + limit_ms;
25+
});
26+
} else if (b.turn() < 27) {
27+
r = search::wld(b);
28+
} else {
29+
r = search::perfect(b);
2630
}
27-
28-
return move;
31+
return r.first;
2932
}
3033

31-
const char* hm5move(const char* path, int max_depth, int time_limit) {
34+
extern "C"
35+
const char* hm5move(const char* path, int max_depth, int limit_ms) {
3236
static std::string fourcc;
3337
static bool initialized = false;
3438
if (!initialized) {
@@ -38,17 +42,17 @@ const char* hm5move(const char* path, int max_depth, int time_limit) {
3842

3943
Board b;
4044
while (*path) {
41-
Move m(path);
45+
Move m(std::string_view(path, 4));
4246
path += 4;
4347
if (*path == '/') path++;
44-
if (m == Move::invalid() || !b.is_valid_move(m))
45-
return "XXXX invalid move ";
48+
if (!m.is_valid() || !b.is_valid_move(m)) return "XXXX invalid move ";
4649
b.play_move(m);
4750
}
4851
search::visited_nodes = 0;
49-
Move m = com_move(b, max_depth, time_limit);
50-
fourcc = m.fourcc();
52+
Move m = com_move(b, max_depth, limit_ms);
53+
fourcc = m.code();
5154
return fourcc.c_str();
5255
}
5356

57+
extern "C"
5458
int getVisitedNodes() { return search::visited_nodes; }

wasm/hm5move_post.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var hm5move = Module.cwrap('hm5move', 'string', ['string', 'number'])
22
var getVisitedNodes = Module.cwrap('getVisitedNodes', 'number')
33

44
function isValidPath(path) {
5-
return path.search(/^((----|[1-9a-e]{2}[a-u][0-7])\/?)*$/) >= 0;
5+
return path.search(/^((0000|[1-9a-e]{2}[a-u][0-7])\/?)*$/) >= 0;
66
}
77

88
function depth(level) {
@@ -21,11 +21,11 @@ function limit(level, player) {
2121

2222
switch (level) {
2323
case 2:
24-
return 1000 * mult;
24+
return 300 * mult;
2525
case 3:
26-
return 10000 * mult;
26+
return 3000 * mult;
2727
}
28-
return 1000 * mult;
28+
return 300 * mult;
2929
}
3030

3131
function handle(data) {

0 commit comments

Comments
 (0)