1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
function _sasite_node(parent, sasite) { if (!Array.isArray(sasite)) sasite = [sasite]; this.parent = parent; this.sasite = sasite; } function _sasite_tree(root) { if (root == undefined) root = new _sasite_node(null, _sasite.get_empty_sasite()); this.node = root; this.pos = 0; this.b_tsume = false; } _sasite_tree.prototype.set_tsume = function(b) { this.b_tsume = b; } _sasite_tree.prototype.is_empty = function() { return this.get_root().sasite.length == 1; } _sasite_tree.prototype.get_root = function() { var node = this.node; while (node.parent != null) node = node.parent; return node; } _sasite_tree.prototype.move_to_start = function() { this.node = this.get_root(); this.pos = 0; } _sasite_tree.prototype.get_next_sasite = function() { if (this.pos == this.node.sasite.length - 1) return null; var next_sasite = this.node.sasite[this.pos + 1]; if (Array.isArray(next_sasite)) { // 分岐なら var a = new Array(); for (var i = 0; i < next_sasite.length; i++) if (!this.b_tsume || i == 0 || next_sasite[i].sasite.length != 1) a.push(next_sasite[i].sasite[0]); return a; } return [next_sasite]; } _sasite_tree.prototype.move_sasite = function(sasite) { this.pos++; var node = this.node; if (this.pos == node.sasite.length) { node.sasite.push(sasite); return sasite; } var next_sasite = node.sasite[this.pos]; if (Array.isArray(next_sasite)) { // 分岐なら var i; for (i = 0; i < next_sasite.length; i++) if (sasite.equals(next_sasite[i].sasite[0])) break; if (i == next_sasite.length) // 新しい手 next_sasite.push(new _sasite_node(node, sasite)); this.node = next_sasite[i]; // 子ノードに移動 this.pos = 0; return this.node.sasite[0]; } else if (!sasite.equals(next_sasite)) { // 分岐でなく、新しい手 var a = [new _sasite_node(node, node.sasite.slice(this.pos)), new _sasite_node(node, sasite)]; node.sasite.splice(this.pos); node.sasite.push(a); this.node = a[1]; // 子ノードに移動 this.pos = 0; return sasite; } else { return next_sasite; } } _sasite_tree.prototype.can_back = function() { return (this.pos != 0) || (this.node.parent != null); } _sasite_tree.prototype.back_sasite = function() { if (!this.can_back()) return; if (this.pos == 0) { this.node = this.node.parent; this.pos = this.node.sasite.length - 2; } else { this.pos--; } } _sasite_tree.prototype.remove_after = function() { if (!this.can_back()) return; if (this.pos == 0) { var node = this.node; this.node = node.parent; var sasite = this.node.sasite; this.pos = sasite.length - 1; var a = sasite[this.pos]; a.splice(a.indexOf(node), 1); if (a.length == 0) sasite.splice(this.pos); this.pos--; } else { this.node.sasite.splice(this.pos--); } } _sasite_tree.prototype.remove_before = function() { if (!this.can_back()) return; var node = this.node; node.parent = null; node.sasite.splice(0, this.pos); this.pos = 0; }