Advertisement
Guest User

Untitled

a guest
Aug 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 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) {
  31. const lastDir = new Array(bt.length).fill(-1);
  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. if (dir == lastDir[i]) continue;
  39.  
  40. switch (dir) {
  41. case 0: if (b[1] < W-1) b[1]++; else continue; break;
  42. case 1: if (b[2] < H-1) b[2]++; else continue; break;
  43. case 2: if (b[1] > 0) b[1]--; else continue; break;
  44. case 3: if (b[2] > 0) b[2]--; else continue; break;
  45. }
  46. break;
  47. }
  48.  
  49. paint(gr, b[1], b[2], b[0]);
  50. }
  51. }
  52.  
  53. function calcScore(gr, id) {
  54. let sc = 0;
  55. for (let x = 0; x < W; x++) {
  56. for (let y = 0; y < H; y++) {
  57. sc += gr[x][y] == id;
  58. }
  59. }
  60. return sc;
  61. }
  62.  
  63. const dxes = [1, 0, -1, 0, 0], dyes = [0, 1, 0, -1, 0];
  64. const outputs = ["right", "down", "left", "up", "wait"];
  65.  
  66. let maxscore = -1, maxat = -1;
  67.  
  68. let grid2 = clone2D(grid);
  69. let grid3 = clone2D(grid);
  70.  
  71. for (let i = 0; i < 5; i++) {
  72. const nx = bots[meIdx][1] + dxes[i];
  73. const ny = bots[meIdx][2] + dyes[i];
  74. if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
  75.  
  76. copy2D(grid, grid2);
  77. const bots2 = clone2D(bots);
  78. bots2[meIdx][1] = nx;
  79. bots2[meIdx][2] = ny;
  80. paint(grid2, nx, ny, meClr);
  81.  
  82. let score = 0;
  83.  
  84. for (let playouti = 0; playouti < 100; playouti++) {
  85. copy2D(grid2, grid3);
  86. const bots3 = clone2D(bots2);
  87. for (let si = 0; si < 5; si++) {
  88. randomStep(grid3, bots3);
  89. }
  90.  
  91. score += calcScore(grid3, meClr);
  92. }
  93.  
  94. if (score > maxscore) {
  95. maxscore = score;
  96. maxat = i;
  97. }
  98. }
  99.  
  100. if (maxscore == -1) return outputs[Math.random() * 5 | 0];
  101. else return outputs[maxat];
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement