Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function(myself, grid, bots, gameInfo) {
- const W = grid.length, H = grid[0].length;
- const meIdx = bots.findIndex(b => b[0] == myself[0]);
- const meClr = bots[meIdx][0];
- function copy2D(from, to) {
- // assumes both 2D arrays are the same size
- for (let i = 0; i < from.length; i++) {
- const fromI = from[i];
- const toI = to[i];
- for (let j = 0; j < fromI.length; j++) {
- toI[j] = fromI[j];
- }
- }
- }
- function clone2D(a) {
- return a.map(l => l.slice());
- }
- function paintValue(gr, x, y, clr) {
- if (gr[x][y] == 0) return clr;
- else return [clr, 0, gr[x][y]][Math.abs(clr - gr[x][y]) % 3];
- }
- function paint(gr, x, y, clr) {
- gr[x][y] = paintValue(gr, x, y, clr);
- }
- function randomStep(gr, bt) {
- const lastDir = new Array(bt.length).fill(-1);
- for (let i = 0; i < bt.length; i++) {
- const b = bt[i];
- while (Math.random() < 0.95) {
- const dir = Math.random() * 4 | 0;
- if (dir == lastDir[i]) continue;
- switch (dir) {
- case 0: if (b[1] < W-1) b[1]++; else continue; break;
- case 1: if (b[2] < H-1) b[2]++; else continue; break;
- case 2: if (b[1] > 0) b[1]--; else continue; break;
- case 3: if (b[2] > 0) b[2]--; else continue; break;
- }
- break;
- }
- paint(gr, b[1], b[2], b[0]);
- }
- }
- function calcScore(gr, id) {
- let sc = 0;
- for (let x = 0; x < W; x++) {
- for (let y = 0; y < H; y++) {
- sc += gr[x][y] == id;
- }
- }
- return sc;
- }
- const dxes = [1, 0, -1, 0, 0], dyes = [0, 1, 0, -1, 0];
- const outputs = ["right", "down", "left", "up", "wait"];
- let maxscore = -1, maxat = -1;
- let grid2 = clone2D(grid);
- let grid3 = clone2D(grid);
- for (let i = 0; i < 5; i++) {
- const nx = bots[meIdx][1] + dxes[i];
- const ny = bots[meIdx][2] + dyes[i];
- if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
- copy2D(grid, grid2);
- const bots2 = clone2D(bots);
- bots2[meIdx][1] = nx;
- bots2[meIdx][2] = ny;
- paint(grid2, nx, ny, meClr);
- let score = 0;
- for (let playouti = 0; playouti < 100; playouti++) {
- copy2D(grid2, grid3);
- const bots3 = clone2D(bots2);
- for (let si = 0; si < 5; si++) {
- randomStep(grid3, bots3);
- }
- score += calcScore(grid3, meClr);
- }
- if (score > maxscore) {
- maxscore = score;
- maxat = i;
- }
- }
- if (maxscore == -1) return outputs[Math.random() * 5 | 0];
- else return outputs[maxat];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement