Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function(myself, grid, bots, gameinfo) {
- my_id = myself[0];
- my_x = myself[1];
- my_y = myself[2];
- delta = [[0, -1], [0, 1], [-1, 0], [1, 0], [0, 0]];
- seen = Array(grid.length).fill().map(() => Array(grid.length).fill(false));
- scores = Array(bots.length + 2).fill(0);
- for (let i = 0; i < grid.length; i++) {
- for (let j = 0; j < grid.length; j++) {
- scores[grid[i][j]]++;
- }
- }
- let maxPossibleScoreAtLevel = 0.05 * (1 + 1e-6 + 8.1 * 1e-10);
- function maxPossibleScore(depth) {
- switch (depth) {
- case 11: return 0;
- case 10: return maxPossibleScoreAtLevel;
- default: return maxPossibleScoreAtLevel + 0.95 * maxPossibleScore(depth + 1);
- }
- }
- function search(x, y, depth) {
- if (depth == 11) {
- return [4, 0];
- }
- let max_score = 0;
- let best_move = 0;
- for (let i = 0; i < 4; i++) {
- let x1 = x + delta[i][0];
- let y1 = y + delta[i][1];
- if ((x1 < 0) || (x1 >= grid.length)) {
- continue;
- }
- if ((y1 < 0) || (y1 >= grid.length)) {
- continue;
- }
- if (seen[x1][y1]) {
- continue;
- }
- let n = 0;
- for (let dx = -1; dx <= 1; dx++) {
- let x2 = x1 + dx;
- if ((x2 < 0) || (x2 >= grid.length)) {
- continue;
- }
- for (let dy = -1; dy <= 1; dy++) {
- if ((dx == 0) && (dy == 0)) {
- continue;
- }
- let y2 = y1 + dy;
- if ((y2 < 0) || (y2 >= grid.length)) {
- continue;
- }
- if (grid[x2][y2] == my_id) {
- n++;
- }
- }
- }
- let prev = grid[x1][y1];
- if (prev == 0) {
- next = my_id;
- } else {
- next = [my_id, 0, prev][Math.abs(my_id - prev) % 3];
- }
- let score = 0;
- score += 1e-10 * (n + 0.1 * Math.random());
- if (next != prev) {
- if (next == my_id) {
- score += 1;
- }
- if (prev == 0 || scores[prev] > scores[my_id]) {
- score += 1e-6;
- }
- }
- score *= 0.05;
- if (score + 0.95 * maxPossibleScore(depth + 1) <= max_score) {
- continue;
- }
- grid[x1][y1] = next;
- seen[x1][y1] = true;
- let final_score = score + 0.95 * search(x1, y1, depth + 1)[1];
- seen[x1][y1] = false;
- grid[x1][y1] = prev;
- if (final_score > max_score) {
- max_score = final_score;
- best_move = i;
- }
- }
- return [best_move, max_score];
- }
- let best_move = search(my_x, my_y, 0)[0];
- return ["up", "down", "left", "right", "wait"][best_move];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement