我在做数独游戏,下面的代码可以工作,但是很容易看出有很多重复的代码。我该如何优化它?谢谢
问题:
getSection
:这个函数应该接受三个参数:一个数独网格,以及一个拼图的 3x3 子网格之一的 x 和 y 坐标。该函数应返回一个数组,其中包含指定子网格中的所有数字。
输入范例:
var puzzle = [[ 8,9,5, 7,4,2, 1,3,6 ],
[ 2,7,1, 9,6,3, 4,8,5 ],
[ 4,6,3, 5,8,1, 7,9,2 ],
[ 9,3,4, 6,1,7, 2,5,8 ],
[ 5,1,7, 2,3,8, 9,6,4 ],
[ 6,8,2, 4,5,9, 3,7,1 ],
[ 1,5,9, 8,7,4, 6,2,3 ],
[ 7,4,6, 3,2,5, 8,1,9 ],
[ 3,2,8, 1,9,6, 5,4,7 ]];
输出:
getSection(puzzle, 0, 0);
// -> [ 8,9,5,2,7,1,4,6,3 ]
解决方案:
function getSection(arr, x, y) {
var section = [];
if (y === 0) {
arr = arr.slice(0, 3);
if (x === 0) {
arr.forEach(function (element) {
section.push(element.slice(0, 3));
})
} else if (x === 1) {
arr.forEach(function (element) {
section.push(element.slice(3, 6));
})
} else {
arr.forEach(function (element) {
section.push(element.slice(6, 9));
})
}
}
if (y === 1) {
arr = arr.slice(4, 7);
if (x === 0) {
arr.forEach(function (element) {
section.push(element.slice(0, 3));
})
} else if (x === 1) {
arr.forEach(function (element) {
section.push(element.slice(3, 6));
})
} else {
arr.forEach(function (element) {
section.push(element.slice(6, 9));
})
}
}
if (y === 2) {
arr = arr.slice(6, 9);
if (x === 0) {
arr.forEach(function (element) {
section.push(element.slice(0, 3));
})
} else if (x === 1) {
arr.forEach(function (element) {
section.push(element.slice(3, 6));
})
} else {
arr.forEach(function (element) {
section.push(elemet.slice(6, 9));
})
}
}
var subgrid = section.reduce(function (a, b) {
return a.concat(b);
},
[]
);
return subgrid;
}
console.log(getSection(puzzle, 0, 0));
// // -> [ 8,9,5,2,7,1,4,6,3 ]
console.log(getSection(puzzle, 1, 0));
// -> [ 7,4,2,9,6,3,5,8,1 ]
这是我使用 ES6 的感受
const puzzle = [
[8, 9, 5, 7, 4, 2, 1, 3, 6],
[2, 7, 1, 9, 6, 3, 4, 8, 5],
[4, 6, 3, 5, 8, 1, 7, 9, 2],
[9, 3, 4, 6, 1, 7, 2, 5, 8],
[5, 1, 7, 2, 3, 8, 9, 6, 4],
[6, 8, 2, 4, 5, 9, 3, 7, 1],
[1, 5, 9, 8, 7, 4, 6, 2, 3],
[7, 4, 6, 3, 2, 5, 8, 1, 9],
[3, 2, 8, 1, 9, 6, 5, 4, 7]
];
const GRID_SIZE = 3;
function getOffset(coordinate) {
const start = coordinate * GRID_SIZE;
const end = start + GRID_SIZE;
return [start, end];
}
function getSection(arr, x, y) {
const yOffset = getOffset(y);
const xOffset = getOffset(x);
const elements = arr.slice(...yOffset);
return elements
.map(element => element.slice(...xOffset))
.reduce((subgrid, grid) => [...subgrid, ...grid], []);
}
console.log(getSection(puzzle, 0, 0));
// // -> [ 8,9,5,2,7,1,4,6,3 ]
console.log(getSection(puzzle, 1, 0));
// -> [ 7,4,2,9,6,3,5,8,1 ]
我假设你的
x
和 y
不会超过你的数组长度。这是实现解决方案的最简单方法。
function getSection(arr, x, y) {
var GRID_SIZE = 3;
var indexX = x*GRID_SIZE;
var indexY = y*GRID_SIZE;
var results = [];
for(var i = indexY; i< indexY+GRID_SIZE; i++){
results = results.concat(puzzle[i].slice(indexX, indexX+GRID_SIZE));
}
return results;
}
console.log(getSection(puzzle, 0, 0));
// // -> [ 8,9,5,2,7,1,4,6,3 ]
console.log(getSection(puzzle, 1, 0));
// -> [ 7,4,2,9,6,3,5,8,1 ]
不像@nutboltu那么优雅,但几乎一样简洁。
function getSection(arr, x, y) {
var section = [];
z = (y===0?0:y+y+2);
arr = arr.slice(z, z+3);
arr.forEach(function (element) {
section.push(element.slice(z, z+3));
})
var subgrid = section.reduce(function (a, b) {
return a.concat(b);
},
[]
);
return subgrid;
}
另一种方法:
function getSection(puzzle, x, y) {
var x;
var y;
var section = [];
let firstRow = puzzle[0].slice(0, 3);
let secondRow = puzzle[1].slice(0, 3);
let thirdRow = puzzle[2].slice(0, 3);
let firstRow1 = puzzle[0].slice(3, 6);
let secondRow1 = puzzle[1].slice(3, 6);
let thirdRow1 = puzzle[2].slice(3, 6);
let firstRow2 = puzzle[0].slice(6, 9);
let secondRow2 = puzzle[1].slice(6, 9);
let thirdRow2 = puzzle[2].slice(6, 9);
let fourthRow = puzzle[3].slice(0, 3);
let fifthRow = puzzle[4].slice(0, 3);
let sixthRow = puzzle[5].slice(0, 3);
let fourthRow1 = puzzle[3].slice(3, 6);
let fifthRow1 = puzzle[4].slice(3, 6);
let sixthRow1 = puzzle[5].slice(3, 6);
let fourthRow2 = puzzle[3].slice(6, 9);
let fifthRow2 = puzzle[4].slice(6, 9);
let sixthRow2 = puzzle[5].slice(6, 9);
let seventhRow = puzzle[6].slice(0, 3);
let eigthRow = puzzle[7].slice(0, 3);
let ninethRow = puzzle[8].slice(0, 3);
let seventhRow1 = puzzle[6].slice(3, 6);
let eigthRow1 = puzzle[7].slice(3, 6);
let ninethRow1 = puzzle[8].slice(3, 6);
let seventhRow2 = puzzle[6].slice(6, 9);
let eigthRow2 = puzzle[7].slice(6, 9);
let ninethRow2 = puzzle[8].slice(6, 9);
if (x === 0 && y === 0) {
return firstRow.concat(secondRow, thirdRow)
} else if (x === 1 && y === 0) {
return firstRow1.concat(secondRow1, thirdRow1)
} else if (x === 2 && y === 0) {
return firstRow2.concat(secondRow2, thirdRow2)
} else if (x === 0 && y === 1) {
return fourthRow.concat(fifthRow, sixthRow)
} else if (x === 1 && y === 1) {
return fourthRow1.concat(fifthRow1, sixthRow1)
} else if (x === 2 && y === 1) {
return fourthRow2.concat(fifthRow2, sixthRow2)
} else if (x === 0 && y === 2) {
return seventhRow.concat(eigthRow, ninethRow)
} else if (x === 1 && y === 2) {
return seventhRow1.concat(eigthRow1, ninethRow1)
} else if (x === 2 & y === 2) {
return seventhRow2.concat(eigthRow2, ninethRow2)
}
};