Advertisement
Guest User

chopstick variant exploration

a guest
Aug 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function State (p00, p01, p10, p11, turn) {
  2.     if (p00 > 4) p00 = 0;
  3.     if (p01 > 4) p01 = 0;
  4.     if (p10 > 4) p10 = 0;
  5.     if (p11 > 4) p11 = 0;
  6.    
  7.     this.p00 = p00;
  8.     this.p01 = p01;
  9.     this.p10 = p10;
  10.     this.p11 = p11;
  11.     this.turn = turn;
  12.    
  13.     this.id = "";
  14.    
  15.     if (p00 < p01) {
  16.         this.id += p00;
  17.         this.id += p01;
  18.     } else {
  19.         this.id += p01;
  20.         this.id += p00;
  21.     }
  22.    
  23.     if (p10 < p11) {
  24.         this.id += p10;
  25.         this.id += p11;
  26.     } else {
  27.         this.id += p11;
  28.         this.id += p10;
  29.     }
  30.    
  31.     this.id += +turn;
  32. }
  33.  
  34. State.prototype.equals = function (that) {
  35.     return this.id == that.id;
  36. };
  37.  
  38. State.prototype.getNextStates = function () {
  39.     var from0 = this.turn ? this.p10 : this.p00;
  40.     var from1 = this.turn ? this.p11 : this.p01;
  41.     var to0 = this.turn ? this.p00 : this.p10;
  42.     var to1 = this.turn ? this.p01 : this.p11;
  43.     var sum = from0 + from1;
  44.    
  45.     var states = [];
  46.    
  47.     if (this.turn) {
  48.         if (to0) {
  49.             states.push(new State(to0 + from0, to1, from0, from1, !this.turn));
  50.             states.push(new State(to0 + from1, to1, from0, from1, !this.turn));
  51.         }
  52.         if (to1) {
  53.             states.push(new State(to0, to1 + from0, from0, from1, !this.turn));
  54.             states.push(new State(to0, to1 + from1, from0, from1, !this.turn));
  55.         }
  56.        
  57.         for (var i = 0; i <= sum / 2; i ++) {
  58.             if (from0 != i && from1 != i && i < 5 && sum - i < 5)
  59.                 states.push(new State(to0, to1, i, sum - i, !this.turn));
  60.         }
  61.     } else {
  62.         if (to0) {
  63.             states.push(new State(from0, from1, to0 + from0, to1, !this.turn));
  64.             states.push(new State(from0, from1, to0 + from1, to1, !this.turn));
  65.         }
  66.         if (to1) {
  67.             states.push(new State(from0, from1, to0, to1 + from0, !this.turn));
  68.             states.push(new State(from0, from1, to0, to1 + from1, !this.turn));
  69.         }
  70.        
  71.         for (var j = 0; j <= sum / 2; j ++) {
  72.             if (from0 != j && from1 != j && j < 5 && sum - j < 5)
  73.                 states.push(new State(j, sum - j, to0, to1, !this.turn));
  74.         }
  75.     }
  76.    
  77.     for (var s = 0; s < states.length; s ++) {
  78.         for (var t = 0; t < states.length; t ++) {
  79.             if (t != s && states[s].equals(states[t]))
  80.                 states.splice(t, 1);
  81.         }
  82.     }
  83.    
  84.     return states;
  85. };
  86.  
  87. var tree = {};
  88.  
  89. function recurse (state) {
  90.     var states = state.getNextStates().map(function (e) {
  91.         return !e.p00 && !e.p01 ? "P2 wins" :
  92.             !e.p10 && !e.p11 ? "P1 wins" : e;
  93.     });
  94.    
  95.     tree[state.id] = states;
  96.    
  97.     for (var i = 0; i < states.length; i ++) {
  98.         if (states[i] instanceof State && !tree[states[i].id]) {
  99.             recurse(states[i]);
  100.         }
  101.     }
  102. }
  103.  
  104. recurse(new State(1, 1, 1, 1, 0));
  105.  
  106. console.log(tree);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement