[英]Trying to save changes as a whole as array inside an array that contains a custom object
我正在嘗試做出if聲明,如果陣列板與2之前的更改相同,它將通過說“不合法的非法比賽”來警告我。 但是每個數組在3圈后都會變得一樣(因為if(boardHistory.length >= 3){
),但是它總是警告我,而我卻沒有理由警告我,因為它總是在變化並且永遠不會變成相同的值。
我怎么了
var turn;
var boardSize;
var board;
var boardHistory;
var changer;
function setup() {
createCanvas(400, 400);
boardSize = 8;
turn = false;
board = [];
boardHistory = [];
changer = 0;
for (var y = 0; y < boardSize; y++) {
for (var x = 0; x < boardSize; x++) {
board.push(new Piece(x, y, 0));
}
}
boardHistory.push(board);
}
function mouseClicked() {
for (var y = 0; y < boardSize; y++) {
for (var x = 0; x < boardSize; x++) {
board[y * boardSize + x].colour = changer;
}
}
var play = true;
if (boardHistory.length >= 3) {
if (board == boardHistory[(boardHistory.length - 3)]) {
play = false;
}
}
if (play) {
turn = !turn;
boardHistory.push(board);
console.log("played");
} else {
console.log("undid illegal play, " + (boardHistory.length - 3));
console.log(boardHistory[(boardHistory.length - 3)]);
}
console.log(board);
changer++;
}
function Piece(x, y, colour) {
this.x = x;
this.y = y;
this.colour = colour;
this.arrayNum = this.y * boardSize + this.x;
}
var turn; var boardSize; var board; var boardHistory; var changer; function setup() { createCanvas(400, 400); boardSize = 8; turn = false; board = []; boardHistory = []; changer = 0; for(var y = 0; y < boardSize; y++){ for(var x = 0; x < boardSize; x++){ board.push(new Piece(x,y,0)); } } boardHistory.push(board); } function mouseClicked(){ for(var y = 0; y < boardSize; y++){ for(var x = 0; x < boardSize; x++){ board[y*boardSize+x].colour = changer; } } var play = true; if(boardHistory.length >= 3){ if(board == boardHistory[(boardHistory.length-3)]){ play = false; } } if(play){ turn = !turn; boardHistory.push(board); console.log("played"); } else { console.log("undid illegal play, "); } changer++; } function Piece(x, y, colour) { this.x = x; this.y = y; this.colour = colour; this.arrayNum = this.y*boardSize+this.x; }
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.sound.min.js"></script> <link rel="stylesheet" type="text/css" href="style.css"> <meta charset="utf-8" /> </head> <body> <script src="sketch.js"></script> </body> </html>
我認為您面臨的問題在兩個不同的地方:
boardHistory.push(board);
和
if (board == boardHistory[(boardHistory.length - 3)])
即使您在函數中用鼠標單擊了board的值進行修改,該變量也包含對象的引用,而不是對象的副本。 因此,如果您在一個地方進行更改,則會在另一地方看到它。
我想舉一個例子會更清楚:
var board = [1,2,3]
var boardHist = []
boardHist[0] = board; //This is similar of what you are doing
console.log(board == boardHist[0]); //Obviously we are getting true
board[0] = 5; //Now we change the first value of board
console.log(board == boardHist[0]); //This is still true
console.log(boardHist[0]); //We will get [5,2,3]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.