我试图创建单独的对象,因为我必须对它们执行类似的任务,我将它们推入一个数组。这样做似乎改变了对象的特定属性,我不知道为什么。我创建这样的对象:
var lo_ro = new PB.Edge(lo, ro);
var ro_ru = new PB.Edge(ro, ru);
代码段
console.log(lo_ro);
console.log("+");
console.log(ro_ru);
console.log("=");
console.log([lo_ro, ro_ru]);
返回了这个(在chrome中):
所以每个对象的所有者属性都变为“boardOwner” 这是我的Edge功能:
if (!window.PB)
window.PB = {};
...
PB.Edge = function (start, end) {
this.start = start.id; //Ids of vertecies
this.end = end.id;
this.owner = "";
this.id = "(" + start.id + ")>(" + end.id + ")";
};
PB.Edge.boardOwner = "boardOwner";
“PB.Edge.boardOwner”用于其他用途,我不明白为什么所有者属性设置为“boardOwner”。
PS。这是一些更多的代码(评论是德语):
(function () {
if (!window.PB)
window.PB = {};
PB.Item = function (typ) {
this.typ = typ;
//weitere Feler typspezifisch
};
PB.GoalItem = function (playerID) {
this.typ = "goal";
this.player = playerID;
};
PB.Vertex = function (x, y) {
this.x = x; //ganzzahlen auf karrierten papier!
this.y = y;
this.item = null;
if (arguments.length > 2)
this.item = arguments[2];
this.id = x+"|"+y;
};
PB.Vertex.getID = function (x, y) {
return x+"|"+y;
};
PB.Edge = function (start, end) {
this.start = start.id; //Ids of vertecies
this.end = end.id;
this.owner = ""; //kein owner heisst es edge ist initial edge einer zelle
//ansonsten ID vom spieler
this.id = "(" + start.id + ")>(" + end.id + ")";
};
PB.Edge.boardOwner = "boardOwner"; //Spielfeldrand
PB.Edge.getID = function (start, end) {
return start.id + ">" + end.id;
};
PB.Cell = function (corners) {
this.corners = corners; //Array von Ids von Vertecies
this.id = corners[0].id;
for (var i = 1; i < corners.length; i+=1)
this.id += ":"+corners[i].id;
};
PB.Cell.getID = function (corners) {
var id = corners[0].id;
for (var i = 1; i < corners.length; i+=1)
id += ":"+corners[i].id;
return id;
};
PB.Board = function (vertecies, edges, cells, start, player, width, height, type) {
this.vertecies = vertecies; //bin Suchbäume
this.edges = edges;
this.cells = cells;
this.start = start; //id zu startvertex
this.player = player; //array von ids von playern
this.width = width; //max x bzw y wert eines erhaltenen Vertex
this.height = height;
this.type = type;
};
})();
和
(function () {
PB.RectBoard = function(player, width, height) {
var vertecies = new BinarySearchTree();
var edges = new BinarySearchTree();
var cells = new BinarySearchTree();
var start = null;
var realh = height+2;
var realw = player.length > 2 ? (player.length > 3 ? width+2 : width+1 ) : width;
/**
* @param i Koordinate der Celle
* @param j K...
* @param b1 "l", "o", "r" oder "u" dort soll boardowner
* @param b2 ...
* @param g "lo", "ro", "ru" oder "lu" sezt item "item"
* @param item
*/
function create_cell(i, j, b1, b2, g1, g2, item1, item2) {
var lo = vertecies.get(i+"|"+j);
if (!lo) {
lo = new PB.Vertex(i, j);
vertecies.push(lo);
}
var ro = vertecies.get((i+1)+"|"+j);
if (!ro) {
ro = new PB.Vertex((i+1), j);
vertecies.push(ro);
}
var ru = vertecies.get((i+1)+"|"+(j+1));
if (!ru) {
ru = new PB.Vertex((i+1), (j+1));
vertecies.push(ru);
}
var lu = vertecies.get(i+"|"+(j+1));
if (!lu) {
lu = new PB.Vertex(i, (j+1));
vertecies.push(lu);
}
var cell = new PB.Cell([lo,ro,ru,lu]);
cells.push(cell);
var lo_ro = new PB.Edge(lo, ro);
var ro_ru = new PB.Edge(ro, ru);
var ru_lu = new PB.Edge(ru, lu);
var lu_lo = new PB.Edge(lu, lo);
console.log(lo_ro);
console.log("+");
console.log(ro_ru);
console.log("=");
console.log([lo_ro, ro_ru]);
var a = [lo_ro,ro_ru,ru_lu,lu_lo];
for (var z = 0; z < 4; z+=1) {
if (!edges.get( a[z].id )) {
edges.push(a[z]);
}
}
if (b1 == "l" || b2 == "l") {
lu_lo.owner = PB.Board.boardOwner;
}
if (b1 == "r" || b2 == "r") {
ro_ru.owner = PB.Board.boardOwner;
}
if (b1 == "o" || b2 == "o") {
lo_ro.owner = PB.Board.boardOwner;
}
if (b1 == "u" || b2 == "u") {
ru_lu.owner = PB.Board.boardOwner;
}
if (g1 == "lo") {
lo.item = item1;
}
if (g1 == "ro") {
ro.item = item1;
}
if (g1 == "lu") {
lu.item = item1;
}
if (g1 == "ru") {
ru.item = item1;
}
if (g2 == "lo") {
lo.item = item2;
}
if (g2 == "ro") {
ro.item = item2;
}
if (g2 == "lu") {
lu.item = item2;
}
if (g2 == "ru") {
ru.item = item2;
}
//startpunkt:
if (i == (width-1)/2 && j == (height-1)/2)
start = lo.id;
}
for (var i = 0; i < width-1; i+=1) {
for (var j = 0; j < height-1; j+=1) {
var b1 = null;
var b2 = null;
if (j == 0) {
if (!(i == (width-1)/2 || i == (width-1)/2-1))
b2 = "o";
}
if (j == height-2) {
if (!(i == (width-1)/2 || i == (width-1)/2-1))
b2 = "u";
}
if (i == 0) {
b1 = "l";
if (player.length > 2)
if (j == (height-1)/2 || j == (height-1)/2 -1)
b1 = null;
}
if (i == width-2) {
b1 = "r";
if (player.length > 3)
if (j == (height-1)/2 || j == (height-1)/2 -1)
b1 = null;
}
create_cell(i, j, b1, b2, null, null, null, null);
}
}
//Tore
var lgoal = (width -1)/2-1;
var ogoal = (height-1)/2-1;
//Player1
create_cell(lgoal, -1, "o", "l", "lo", "ro", new PB.GoalItem(player[0]), new PB.GoalItem(player[0]));
create_cell(lgoal+1, -1, "o", "r", "ro", null, new PB.GoalItem(player[0]), null);
//Player2
create_cell(lgoal, height-1, "u", "l", "lu", "ru", new PB.GoalItem(player[0]), new PB.GoalItem(player[0]));
create_cell(lgoal+1, height-1, "u", "r", "ru", null, new PB.GoalItem(player[0]), null);
//Player3
if (player.length>2){
create_cell(-1, ogoal, "o", "l", "lo", "lu", new PB.GoalItem(player[0]), new PB.GoalItem(player[0]));
create_cell(-1, ogoal+1, "u", "l", "lu", null, new PB.GoalItem(player[0]), null);
}
//Player4
if (player.length>3){
create_cell(width-1, ogoal, "o", "r", "ro", "ru", new PB.GoalItem(player[0]), new PB.GoalItem(player[0]));
create_cell(width-1, ogoal+1, "u", "r", "ru", null, new PB.GoalItem(player[0]), null);
}
//Damit keine koordinaten negativ sind
vertecies.forEach(function (e) {
e.y = e.y+1;
if (player.length>2) {
e.x = e.x+1;
}
});
return new PB.Board(vertecies, edges, cells, start, player, realw, realh, PB.RectBoard.type);
}
PB.RectBoard.type = "Rect";
})();