函数内更新数组的问题



我一直在编写一个被蒙住眼睛的魔方备忘录生成器,如果你不知道那是什么,你基本上给它一个魔方的混乱,它会给你所有你需要记住的东西来解决魔方蒙住眼睛。目前,它只是用于求解边。蒙住眼睛的方法是这样的你有一个方块来交换边,这是红白边,你总是拿着绿色中心面向你的方块,白色中心在上面。如果你想要更详细的解释,请观看:https://www.youtube.com/watch?v=ZZ41gWvltT8.

我的问题是在控制台你得到正确的备忘录,但边缘数组似乎没有改变,这没有意义,我添加了大量的评论到这个jsfiddle,请帮助,如果你可以,提前感谢!

这是完整的js代码(这是一个控制台的事情):

//NOTE: THIS IS ONLY FOR THE EDGES PART OF THE MEMO, SO FAR
//list of all the edge pairs on the rubiks cube
var edges = [
["a", "q"],
["b", "m"],
["c", "i"],
["d", "e"],
["e", "d"],
["f", "l"],
["g", "x"],
["h", "r"],
["i", "c"],
["j", "p"],
["k", "u"],
["l", "f"],
["m", "b"],
["n", "t"],
["o", "v"],
["p", "j"],
["q", "a"],
["r", "h"],
["s", "w"],
["t", "n"],
["u", "k"],
["v", "o"],
["w", "s"],
["x", "g"],
];
//a copy of the edges array, in its original state, 
//this is used to check where the edges need to be placed on the cube
const edgesOriginal = [
["a", "q"],
["b", "m"],
["c", "i"],
["d", "e"],
["e", "d"],
["f", "l"],
["g", "x"],
["h", "r"],
["i", "c"],
["j", "p"],
["k", "u"],
["l", "f"],
["m", "b"],
["n", "t"],
["o", "v"],
["p", "j"],
["q", "a"],
["r", "h"],
["s", "w"],
["t", "n"],
["u", "k"],
["v", "o"],
["w", "s"],
["x", "g"],
];
//swaps a given edge with the buffer edge(b)
function swapEdges(edge1, edge2) {
var buffer2, swap2;
//in the regular blindfolded method, the buffer piece that you
//swap with is always the b/q edge, 12 represents the q side of it here
buffer2 = 12
//loop through the edges to find the number for the other side of the edge1 edge
for (var i = 0; i < edges.length; i++) {
if (edges[i][1] == edges[edge1][0]) {
swap2 = i
}
}
//a = 0, b = 1, c = 3 etc.
console.log(buffer2, swap2, edge1, edge2)
//swap both sides of the edge pieces using a temporary variable, 
//[a, b] = [b, a] method was finnicky for some reason
var temp = edges[edge1];
edges[edge1] = edges[edge2];
edges[edge2] = temp;
temp = edges[buffer2];
edges[buffer2] = edges[swap2];
edges[swap2] = temp;
}
//takes a value from R, U, F, B, L, and D. You can add a ' or 2 to the end to make the move
//go the opposite direction or make it turn twice. Also I am too lazy to loop for the ' (this is called a prime move) moves
//all possible turns, note this is only for edges right now, I am almost certain this is working
function turn(side) {
switch(side) {
case "R":
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
break;
case "L":
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
break;
case "F":
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
break;
case "U":
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
break;
case "D":
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
break;
case "B":
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
break;
case "R'":
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
break;
case "L'":
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
break;
case "F'":
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
break;
case "U'":
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
break;
case "D'":
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
break;
case "B'":
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
break;
case "R2":
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
break;
case "L2":
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
break;
case "F2":
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
break;
case "U2":
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
break;
case "D2":
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
break;
case "B2":
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
break;

}
}
function doTurns(turns) {
//remove whitespace
var turnsFormated = turns.split(/s/g);
//loop through the turns to do
for (var i = 0; i < turnsFormated.length; i++) {
turn(turnsFormated[i])
//console.log(turnsFormated[i])
}
}
//literally just cylces four values in the edge array
function cycleFour(a, b, c, d) {
[edges[a], edges[b], edges[c], edges[d]] = [edges[d], edges[a], edges[b], edges[c]];
}
//this is the weird part, when I look at the console, the memo is correct for what I am trying to do
//but the logged edge array doesnt seem to change :( please help
function generateMemo(scramble) {
//scramble the cube
doTurns(scramble);
//this is the variable for the piece we need to swap with
var currentBuffer;
var memo = [];
//in 3x3 blindfolded you go until you find the buffer in the lineup
//and then you pick a random piece that you haven't already hit, and start on that
//NOTE: currently this just goes until it finds the buffer piece (b/m)
while (currentBuffer !== 1) {
//search for where the piece that is in the 1/12 position, this is the position that you will swap all edges with
//eventually solving all the edges into the correct position
for (var i = 0; i < edgesOriginal.length; i++) {
if (edgesOriginal[i][0] == edges[1][0]) {
currentBuffer = i;
}
}
//then swap the buffer with where it needs to go, putting the edge in the right spot
swapEdges(currentBuffer, 1);
console.log(edges, edgesOriginal);
//this never triggers however the above console log shows them identical, please help :)
if (edges == edgesOriginal) {
console.log("yes")
}
// add the letter to the memo
memo.push(getLetter(currentBuffer));
}
console.log(memo);
}
//converts a letter to the correct number to match the edgesOriginal array
function getLetter(num) {
var letter = String.fromCharCode(num + 1 + 64);
return letter;
}
//does exactly the opposite, converting the number to a letter I have no clue how this works btw, I just found it online
function getNumber(letter) {
var number = letter.charCodeAt(0) - 65 - 32;
return number;
}

简而言之,我想知道的是为什么edges变量似乎没有从控制台日志判断变化,为什么它不打印&;yes"即使控制台日志看起来是一样的。

我制作了下面的代码片段,它显示了数组之间的变化。你看不到变化的原因是,当涉及到大数组的文本时,人眼很难捕捉到这样的东西。而且,变化很小,大部分保持不变,但变化确实存在。

就像@ChrisHamilton说的,"edges == edgesOriginal永远不会为真,除非它们引用同一个数组。">

//NOTE: THIS IS ONLY FOR THE EDGES PART OF THE MEMO, SO FAR
var outputArray = [];
var outputThis="";
function outputAr() {
var result="";
for (let i=0; i<arguments.length;i++) {
if (i==0) {
result+="[";
}
result+=arguments[i];
if (i==(arguments.length-1)) {
result+="]";
}
}
outputArray.push(result);
}
//list of all the edge pairs on the rubiks cube
var edges = [
["a", "q"],
["b", "m"],
["c", "i"],
["d", "e"],
["e", "d"],
["f", "l"],
["g", "x"],
["h", "r"],
["i", "c"],
["j", "p"],
["k", "u"],
["l", "f"],
["m", "b"],
["n", "t"],
["o", "v"],
["p", "j"],
["q", "a"],
["r", "h"],
["s", "w"],
["t", "n"],
["u", "k"],
["v", "o"],
["w", "s"],
["x", "g"],
];
//a copy of the edges array, in its original state, 
//this is used to check where the edges need to be placed on the cube
const edgesOriginal = [
["a", "q"],
["b", "m"],
["c", "i"],
["d", "e"],
["e", "d"],
["f", "l"],
["g", "x"],
["h", "r"],
["i", "c"],
["j", "p"],
["k", "u"],
["l", "f"],
["m", "b"],
["n", "t"],
["o", "v"],
["p", "j"],
["q", "a"],
["r", "h"],
["s", "w"],
["t", "n"],
["u", "k"],
["v", "o"],
["w", "s"],
["x", "g"],
];
//swaps a given edge with the buffer edge(b)
function swapEdges(edge1, edge2) {
var buffer2, swap2;
//in the regular blindfolded method, the buffer piece that you
//swap with is always the b/q edge, 12 represents the q side of it here
buffer2 = 12
//loop through the edges to find the number for the other side of the edge1 edge
for (var i = 0; i < edges.length; i++) {
if (edges[i][1] == edges[edge1][0]) {
swap2 = i
}
}
//a = 0, b = 1, c = 3 etc.
console.log(buffer2, swap2, edge1, edge2)
outputAr(buffer2, swap2, edge1, edge2);
//swap both sides of the edge pieces using a temporary variable, 
//[a, b] = [b, a] method was finnicky for some reason
var temp = edges[edge1];
edges[edge1] = edges[edge2];
edges[edge2] = temp;
temp = edges[buffer2];
edges[buffer2] = edges[swap2];
edges[swap2] = temp;
}
//takes a value from R, U, F, B, L, and D. You can add a ' or 2 to the end to make the move
//go the opposite direction or make it turn twice. Also I am too lazy to loop for the ' (this is called a prime move) moves
//all possible turns, note this is only for edges right now, I am almost certain this is working
function turn(side) {
switch(side) {
case "R":
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
break;
case "L":
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
break;
case "F":
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
break;
case "U":
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
break;
case "D":
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
break;
case "B":
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
break;
case "R'":
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
break;
case "L'":
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
break;
case "F'":
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
break;
case "U'":
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
break;
case "D'":
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
break;
case "B'":
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
break;
case "R2":
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
cycleFour(12, 13, 14, 15);
cycleFour(1, 19, 21, 9)
break;
case "L2":
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
cycleFour(4, 5, 6, 7);
cycleFour(3, 11, 23, 17);
break;
case "F2":
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
cycleFour(8, 9, 10, 11);
cycleFour(2, 15, 20, 5);
break;
case "U2":
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
cycleFour(0, 1, 2, 3);
cycleFour(8, 4, 16, 12);
break;
case "D2":
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
cycleFour(20, 21, 22, 23);
cycleFour(10, 14, 18, 6);
break;
case "B2":
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
cycleFour(16, 17, 18, 19);
cycleFour(0, 7, 22, 13)
break;

}
}
function doTurns(turns) {
//remove whitespace
var turnsFormated = turns.split(/s/g);
//loop through the turns to do
for (var i = 0; i < turnsFormated.length; i++) {
turn(turnsFormated[i])
//console.log(turnsFormated[i])
}
}
//literally just cylces four values in the edge array
function cycleFour(a, b, c, d) {
[edges[a], edges[b], edges[c], edges[d]] = [edges[d], edges[a], edges[b], edges[c]];
}
//this is the weird part, when I look at the console, the memo is correct for what I am trying to do
//but the logged edge array doesnt seem to change :( please help
function generateMemo(scramble) {
//scramble the cube
doTurns(scramble);
//this is the variable for the piece we need to swap with
var currentBuffer;
var memo = [];
//in 3x3 blindfolded you go until you find the buffer in the lineup
//and then you pick a random piece that you haven't already hit, and start on that
//NOTE: currently this just goes until it finds the buffer piece (b/m)
while (currentBuffer !== 1) {
//search for where the piece that is in the 1/12 position, this is the position that you will swap all edges with
//eventually solving all the edges into the correct position
for (var i = 0; i < edgesOriginal.length; i++) {
if (edgesOriginal[i][0] == edges[1][0]) {
currentBuffer = i;
}
}
//then swap the buffer with where it needs to go, putting the edge in the right spot
swapEdges(currentBuffer, 1);
console.log(edges, edgesOriginal);
outputAr(edges, edgesOriginal);
//this never triggers however the above console log shows them identical, please help :)
if (edges == edgesOriginal) {
console.log("yes")
}
// add the letter to the memo
memo.push(getLetter(currentBuffer));
}
console.log(memo);
if (edges==edgesOriginal) {console.warn("yes");outputAr("yes");} else {console.error("no");outputAr("no");}
document.getElementById("output").innerHTML=outputArray;
for (let mnd=0;mnd<outputArray[1].length;mnd++) {
outputThis+="<p>"+"<span>"+outputArray[1][mnd]+"</span>"+"<span>"+outputArray[3][mnd]+"</span>"+"<span>"+outputArray[5][mnd]+"</span>"+"<span>"+outputArray[7][mnd]+"</span>"+"<span>"+outputArray[9][mnd]+"</span>"+"</p>";
}
document.getElementById("output2").innerHTML=outputThis;
}
//converts a letter to the correct number to match the edgesOriginal array
function getLetter(num) {
var letter = String.fromCharCode(num + 1 + 64);
return letter;
}
//does exactly the opposite, converting the number to a letter I have no clue how this works btw, I just found it online
function getNumber(letter) {
var number = letter.charCodeAt(0) - 65 - 32;
return number;
}
generateMemo(" R U F2 L' ");
<div id="output"></div>
<div id="output2"></div>

最新更新