-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.ts
90 lines (76 loc) · 2.65 KB
/
agent.ts
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
// @ts-ignore
const [BOARDSIZE, EMPTY, BLACK, WHITE] = [15, 0, 1, 2];
/**
* 選擇下棋位置
* (空: 0, 黑: 1, 白: 2)
* @param board 15x15 的棋盤
* @param stone 我方棋子顏色
* @param timer 該局我方剩餘時間
* @returns [x, y] 該局下棋位置
*/
// @ts-ignore
function main(board: number[][], stone: number, timer: number): [number, number] {
let [x, y] = [0, 0];
while (board[y][x] !== 0) {
x = Math.floor(Math.random() * BOARDSIZE);
y = Math.floor(Math.random() * BOARDSIZE);
}
return [x, y];
}
process.stdin.on("data", (data) => {
const raw = data.toString();
const parts = raw.split(/, /g);
const board = Array.from({ length: BOARDSIZE }, () => Array.from({ length: BOARDSIZE }, () => 0));
for (let i = 0; i < BOARDSIZE; i++) {
for (let j = 0; j < BOARDSIZE; j++) {
board[i][j] = parseInt(parts[i * BOARDSIZE + j]);
}
}
const stone = parseInt(parts[225]);
const timer = parseFloat(parts[226]);
const [x, y] = main(board, stone, timer);
console.log(y, x);
});
/**
* 從 board[i][j] 往八個方向看,看此方向有連續幾顆 stone 顏色的棋,以及在這些棋子之後是否有空位
* @param board 15x15 的棋盤
* @param i 觀看位置的列
* @param j 觀看位置的行
* @param stone 觀看方棋子顏色
* @returns [conti, empty][] 該方連續棋數及該方在這些棋之後是否有空位
* @example
* board =
* [[0, 0, 0, 0, 2],
* [0, 0, 1, 1, 2],
* [0, 1, 1, 1, 1],
* [0, 1, 1, 1, 2],
* [0, 1, 1, 1, 2]]
* peek(board, 2, 2, 1) -> [[1, true], [1, false], [2, false], [1, false], [2, false], [1, true], [1, true], [0, true]]
*/
// @ts-ignore
function peek(board: number[][], i: number, j: number, stone: number): [number, boolean][] {
if (i < 0 || i >= BOARDSIZE || j < 0 || j >= BOARDSIZE) {
throw new Error("Invalid index to peek!");
}
const ret: [number, boolean][] = Array.from({ length: 8 }, () => [0, false]);
let [delX, delY] = [0, 0];
for (let k = 0; k < 8; k++) {
let [y, x] = [i, j];
if (k === 2 || k === 6) delY = 0;
else delY = k > 2 && k < 6 ? 1 : -1;
if (k === 0 || k === 4) delX = 0;
else delX = k > 0 && k < 4 ? 1 : -1;
while (true) {
y += delY;
x += delX;
if (y < 0 || y >= BOARDSIZE) break;
if (x < 0 || x >= BOARDSIZE) break;
if (board[y][x] !== stone) break;
ret[k][0] += 1;
}
if (y < 0 || y >= BOARDSIZE) continue;
if (x < 0 || x >= BOARDSIZE) continue;
ret[k][1] = board[y][x] === EMPTY;
}
return ret;
}