Advertisement
Guest User

Untitled

a guest
Aug 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. function(myself, grid, bots, gameInfo) {
  2. const W = grid.length, H = grid[0].length;
  3. const meIdx = bots.findIndex(b => b[0] == myself[0]);
  4. const meClr = bots[meIdx][0];
  5.  
  6. function copy2D(from, to) {
  7. // assumes both 2D arrays are the same size
  8. for (let i = 0; i < from.length; i++) {
  9. const fromI = from[i];
  10. const toI = to[i];
  11. for (let j = 0; j < fromI.length; j++) {
  12. toI[j] = fromI[j];
  13. }
  14. }
  15. }
  16.  
  17. function clone2D(a) {
  18. return a.map(l => l.slice());
  19. }
  20.  
  21. function paintValue(gr, x, y, clr) {
  22. if (gr[x][y] == 0) return clr;
  23. else return [clr, 0, gr[x][y]][Math.abs(clr - gr[x][y]) % 3];
  24. }
  25.  
  26. function paint(gr, x, y, clr) {
  27. gr[x][y] = paintValue(gr, x, y, clr);
  28. }
  29.  
  30. function randomStep(gr, bt, id) {
  31. let scoreDelta = 0;
  32.  
  33. for (let i = 0; i < bt.length; i++) {
  34. const b = bt[i];
  35.  
  36. while (Math.random() < 0.95) {
  37. const dir = Math.random() * 4 | 0;
  38.  
  39. switch (dir) {
  40. case 0: if (b[1] < W-1) b[1]++; else continue; break;
  41. case 1: if (b[2] < H-1) b[2]++; else continue; break;
  42. case 2: if (b[1] > 0) b[1]--; else continue; break;
  43. case 3: if (b[2] > 0) b[2]--; else continue; break;
  44. }
  45. break;
  46. }
  47.  
  48. const prevC = gr[b[1]][b[2]];
  49. const currC = paint(gr, b[1], b[2], b[0]);
  50. if (prevC == id && currC != id) {
  51. scoreDelta--;
  52. } else if (prevC != id && currC == id) {
  53. scoreDelta++;
  54. }
  55. }
  56.  
  57. return scoreDelta;
  58. }
  59.  
  60. function calcScore(gr, id) {
  61. let sc = 0;
  62. for (let x = 0; x < W; x++) {
  63. for (let y = 0; y < H; y++) {
  64. sc += gr[x][y] == id;
  65. }
  66. }
  67. return sc;
  68. }
  69.  
  70. const dxes = [1, 0, -1, 0, 0], dyes = [0, 1, 0, -1, 0];
  71. const outputs = ["right", "down", "left", "up", "wait"];
  72.  
  73. let maxscore = -1, maxat = -1;
  74.  
  75. let grid2 = clone2D(grid);
  76. let grid3 = clone2D(grid);
  77.  
  78. for (let i = 0; i < 5; i++) {
  79. const nx = bots[meIdx][1] + dxes[i];
  80. const ny = bots[meIdx][2] + dyes[i];
  81. if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
  82.  
  83. copy2D(grid, grid2);
  84. const bots2 = clone2D(bots);
  85. bots2[meIdx][1] = nx;
  86. bots2[meIdx][2] = ny;
  87. paint(grid2, nx, ny, meClr);
  88.  
  89. let score = 0;
  90.  
  91. let baseScore = calcScore(grid2, meClr);
  92.  
  93. for (let playouti = 0; playouti < 100; playouti++) {
  94. copy2D(grid2, grid3);
  95. const bots3 = clone2D(bots2);
  96.  
  97. let scoreDelta = 0;
  98. for (let si = 0; si < 5; si++) {
  99. scoreDelta += randomStep(grid3, bots3, meClr);
  100. }
  101.  
  102. score += baseScore + scoreDelta;
  103. }
  104.  
  105. if (score > maxscore) {
  106. maxscore = score;
  107. maxat = i;
  108. }
  109. }
  110.  
  111. if (maxscore == -1) return outputs[Math.random() * 5 | 0];
  112. else return outputs[maxat];
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement