英文:
Search a positive gradient in a dynamic-sized 2d array
问题
我需要在一个二维数组中搜索正梯度和负梯度的值,例如:
正梯度:左下 > 中间 > 右上
负梯度:左上 > 中间 > 右下
我有两个函数。首先,在for循环中找到值,当找到值后,这两个函数都运行以搜索迷宫的正梯度和负梯度,并在找到获胜组合时返回true。
scanForNegativeGradientCombinations
函数可以工作,但 scanForPositiveGradientCombinations
函数会出现类型错误。我认为问题可能出在减小 rowIndex
时,可能使游戏超出边界,但我不确定。
以下是将调用以下两个函数的函数:
const isDiagonalWinner = (rowCellValues, winCountCondition, player) => {
for (let rowIndex = 0; rowIndex < rowCellValues.length; rowIndex++) {
for (let columnIndex = 0; columnIndex < rowCellValues[rowIndex].length; columnIndex++) {
const cellValue = rowCellValues[rowIndex][columnIndex];
if (cellValue === player.symbol) {
console.log('initiating player scan for ', player.symbol, 'at', [rowIndex, columnIndex]);
if (scanForNegativeGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
return true;
}
if (scanForPositiveGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
return true;
}
}
}
}
return null;
};
下面是这两个函数:
const scanForNegativeGradientCombinations = (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) => {
let counter = 0;
while (rowIndex < rowCellValues.length && columnIndex < rowCellValues[rowIndex].length) {
if (rowCellValues[rowIndex][columnIndex] === playerSymbol) {
counter++;
}
if (counter >= winCountCondition) {
return true;
}
rowIndex++;
columnIndex++;
}
return false;
}
const scanForPositiveGradientCombinations = (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) => {
let counter = 0;
while (rowIndex < rowCellValues.length && columnIndex < rowCellValues[rowIndex].length) {
if (rowCellValues[rowIndex][columnIndex] === playerSymbol) {
counter++;
}
if (counter >= winCountCondition) {
return true;
}
rowIndex--;
columnIndex++;
}
return false;
}
英文:
I need to search a positive & negative gradient in a 2d array for values, for example.
Positive gradient: bottom left > middle > top right
Negative gradient: top left > middle > bottom right
[
['x', 'x', 'o'],
[null, null, 'o'],
['x', 'x', null]
]
I have 2 functions. First the value is found in a for loop, when found both of these functions run to search the positive & negative gradient of the maze and return true if a winning combination is found.
scanForNegativeGradientCombinationsworks, scanForPositiveGradientCombinations does not and ends in a typeError. I think the problem may be when reducing the rowIndex, I am pushing the game out of bounds, but im not sure.
const isDiagonalWinner = (rowCellValues, winCountCondition, player) => {
for(let rowIndex = 0; rowIndex < rowCellValues.length; rowIndex++){
for(let columnIndex = 0; columnIndex < rowCellValues[rowIndex].length; columnIndex++){
const cellValue = rowCellValues[rowIndex][columnIndex];
if(cellValue === player.symbol) {
console.log('initiating player scan for ', player.symbol, 'at', [rowIndex, columnIndex]);
if(scanForNegativeGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
return true
}
if(scanForPositiveGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
return true
}
}
}
}
return null;
};
Above is the function that will call the following 2 functions.
const scanForNegativeGradientCombinations= (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) => {
let counter = 0;
while(rowIndex < rowCellValues.length && columnIndex < rowCellValues[rowIndex].length){
if(rowCellValues[rowIndex][columnIndex] === playerSymbol){
counter++;
}
if(counter >= winCountCondition){
return true;
}
rowIndex++;
columnIndex++;
}
return false;
}
const scanForPositiveGradientCombinations= (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) => {
let counter = 0;
while(rowIndex < rowCellValues.length && columnIndex < rowCellValues[rowIndex].length){
if(rowCellValues[rowIndex][columnIndex] === playerSymbol){
counter++;
}
if(counter >= winCountCondition){
return true;
}
rowIndex--;
columnIndex++;
}
return false;
}
答案1
得分: 0
function sequence(n) { return Array(n).fill().map((_,i)=>i) }
function diagonalWin(board) {
let d = board.length
let seq = sequence(d)
return board[0][0]!==null && seq.every(i=>board[i][i]===board[0][0]) ||
board[0][d-1]!==null && seq.every(i=>board[i][d-i-1]===board[0][d-1])
}
console.log(diagonalWin([
['x', 'x', 'o'],
[null, null, 'o'],
['x', 'x', null]
]))
console.log(diagonalWin([
['x', 'x', 'o'],
[null, 'x', 'o'],
['x', 'x', 'x']
]))
console.log(diagonalWin([
['x', 'x', 'o'],
[null, 'o', 'o'],
['o', 'x', 'x']
]))
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function sequence(n) { return Array(n).fill().map((_,i)=>i) }
function diagonalWin(board) {
let d = board.length
let seq = sequence(d)
return board[0][0]!==null && seq.every(i=>board[i][i]===board[0][0]) ||
board[0][d-1]!==null && seq.every(i=>board[i][d-i-1]===board[0][d-1])
}
console.log(diagonalWin([
['x', 'x', 'o'],
[null, null, 'o'],
['x', 'x', null]
]))
console.log(diagonalWin([
['x', 'x', 'o'],
[null, 'x', 'o'],
['x', 'x', 'x']
]))
console.log(diagonalWin([
['x', 'x', 'o'],
[null, 'o', 'o'],
['o', 'x', 'x']
]))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论