Advertisement
Guest User

Untitled

a guest
Aug 24th, 2018
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. function(myself, grid, bots, gameinfo) {
  2. my_id = myself[0];
  3. my_x = myself[1];
  4. my_y = myself[2];
  5. delta = [[0, -1], [0, 1], [-1, 0], [1, 0], [0, 0]];
  6. seen = Array(grid.length).fill().map(() => Array(grid.length).fill(false));
  7. scores = Array(bots.length + 2).fill(0);
  8. for (let i = 0; i < grid.length; i++) {
  9. for (let j = 0; j < grid.length; j++) {
  10. scores[grid[i][j]]++;
  11. }
  12. }
  13. let maxPossibleScoreAtLevel = 0.05 * (1 + 1e-6 + 8.1 * 1e-10);
  14. function maxPossibleScore(depth) {
  15. switch (depth) {
  16. case 11: return 0;
  17. case 10: return maxPossibleScoreAtLevel;
  18. default: return maxPossibleScoreAtLevel + 0.95 * maxPossibleScore(depth + 1);
  19. }
  20. }
  21. function search(x, y, depth) {
  22. if (depth == 11) {
  23. return [4, 0];
  24. }
  25. let max_score = 0;
  26. let best_move = 0;
  27. for (let i = 0; i < 4; i++) {
  28. let x1 = x + delta[i][0];
  29. let y1 = y + delta[i][1];
  30. if ((x1 < 0) || (x1 >= grid.length)) {
  31. continue;
  32. }
  33. if ((y1 < 0) || (y1 >= grid.length)) {
  34. continue;
  35. }
  36. if (seen[x1][y1]) {
  37. continue;
  38. }
  39. let n = 0;
  40. for (let dx = -1; dx <= 1; dx++) {
  41. let x2 = x1 + dx;
  42. if ((x2 < 0) || (x2 >= grid.length)) {
  43. continue;
  44. }
  45. for (let dy = -1; dy <= 1; dy++) {
  46. if ((dx == 0) && (dy == 0)) {
  47. continue;
  48. }
  49. let y2 = y1 + dy;
  50. if ((y2 < 0) || (y2 >= grid.length)) {
  51. continue;
  52. }
  53. if (grid[x2][y2] == my_id) {
  54. n++;
  55. }
  56. }
  57. }
  58. let prev = grid[x1][y1];
  59. if (prev == 0) {
  60. next = my_id;
  61. } else {
  62. next = [my_id, 0, prev][Math.abs(my_id - prev) % 3];
  63. }
  64. let score = 0;
  65. score += 1e-10 * (n + 0.1 * Math.random());
  66. if (next != prev) {
  67. if (next == my_id) {
  68. score += 1;
  69. }
  70. if (prev == 0 || scores[prev] > scores[my_id]) {
  71. score += 1e-6;
  72. }
  73. }
  74. score *= 0.05;
  75. if (score + 0.95 * maxPossibleScore(depth + 1) <= max_score) {
  76. continue;
  77. }
  78. grid[x1][y1] = next;
  79. seen[x1][y1] = true;
  80. let final_score = score + 0.95 * search(x1, y1, depth + 1)[1];
  81. seen[x1][y1] = false;
  82. grid[x1][y1] = prev;
  83. if (final_score > max_score) {
  84. max_score = final_score;
  85. best_move = i;
  86. }
  87. }
  88. return [best_move, max_score];
  89. }
  90.  
  91. let best_move = search(my_x, my_y, 0)[0];
  92. return ["up", "down", "left", "right", "wait"][best_move];
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement