javascript - 尝试将更改作为一个整体保存为包含自定义对象的数组中的数组
问题描述
我正在尝试做一个 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)])
即使您在函数 mouseclicked 中修改 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]
推荐阅读
- java - Docker 容器只能使用内部 IP/端口访问在另一个容器中运行的 MSSQL
- angular - Angular 使用 AOT 时不导入 JS
- bash - 在 Raspberry Pi 上使用 CUPS 打印不一致
- windows - 使用 Pandoc/PowerShell 选项来避免 �? 代替结束双引号
- visual-studio - 在 STOCK Visual Studio 2012 MFC 应用程序中调整停靠窗格的大小会在 Windows 10 下留下工件,为什么?
- c# - ASP.net,c#,如何在 sql 命令中使用 cookie 中的全局变量
- haskell - 如何使用 haskell 堆栈创建 nix 包
- node.js - 更新 vuetify 后 vuetify/src/stylus/components/_app.styl 中的“$material.text 没有属性 .primary”
- node.js - 使用 node.js iconv-lite 和 iconv 模块创建 ISO-8859-1 XML 时失败
- asp.net - 无法从服务器调用客户端方法