Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function State (p00, p01, p10, p11, turn) {
- if (p00 > 4) p00 = 0;
- if (p01 > 4) p01 = 0;
- if (p10 > 4) p10 = 0;
- if (p11 > 4) p11 = 0;
- this.p00 = p00;
- this.p01 = p01;
- this.p10 = p10;
- this.p11 = p11;
- this.turn = turn;
- this.id = "";
- if (p00 < p01) {
- this.id += p00;
- this.id += p01;
- } else {
- this.id += p01;
- this.id += p00;
- }
- if (p10 < p11) {
- this.id += p10;
- this.id += p11;
- } else {
- this.id += p11;
- this.id += p10;
- }
- this.id += +turn;
- }
- State.prototype.equals = function (that) {
- return this.id == that.id;
- };
- State.prototype.getNextStates = function () {
- var from0 = this.turn ? this.p10 : this.p00;
- var from1 = this.turn ? this.p11 : this.p01;
- var to0 = this.turn ? this.p00 : this.p10;
- var to1 = this.turn ? this.p01 : this.p11;
- var sum = from0 + from1;
- var states = [];
- if (this.turn) {
- if (to0) {
- states.push(new State(to0 + from0, to1, from0, from1, !this.turn));
- states.push(new State(to0 + from1, to1, from0, from1, !this.turn));
- }
- if (to1) {
- states.push(new State(to0, to1 + from0, from0, from1, !this.turn));
- states.push(new State(to0, to1 + from1, from0, from1, !this.turn));
- }
- for (var i = 0; i <= sum / 2; i ++) {
- if (from0 != i && from1 != i && i < 5 && sum - i < 5)
- states.push(new State(to0, to1, i, sum - i, !this.turn));
- }
- } else {
- if (to0) {
- states.push(new State(from0, from1, to0 + from0, to1, !this.turn));
- states.push(new State(from0, from1, to0 + from1, to1, !this.turn));
- }
- if (to1) {
- states.push(new State(from0, from1, to0, to1 + from0, !this.turn));
- states.push(new State(from0, from1, to0, to1 + from1, !this.turn));
- }
- for (var j = 0; j <= sum / 2; j ++) {
- if (from0 != j && from1 != j && j < 5 && sum - j < 5)
- states.push(new State(j, sum - j, to0, to1, !this.turn));
- }
- }
- for (var s = 0; s < states.length; s ++) {
- for (var t = 0; t < states.length; t ++) {
- if (t != s && states[s].equals(states[t]))
- states.splice(t, 1);
- }
- }
- return states;
- };
- var tree = {};
- function recurse (state) {
- var states = state.getNextStates().map(function (e) {
- return !e.p00 && !e.p01 ? "P2 wins" :
- !e.p10 && !e.p11 ? "P1 wins" : e;
- });
- tree[state.id] = states;
- for (var i = 0; i < states.length; i ++) {
- if (states[i] instanceof State && !tree[states[i].id]) {
- recurse(states[i]);
- }
- }
- }
- recurse(new State(1, 1, 1, 1, 0));
- console.log(tree);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement