搜索动态大小的二维数组中的正梯度

huangapple go评论71阅读模式
英文:

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

[
	[&#39;x&#39;,  &#39;x&#39;,  &#39;o&#39;],
	[null, null, &#39;o&#39;],
	[&#39;x&#39;, &#39;x&#39;,   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) =&gt; {

for(let rowIndex = 0; rowIndex &lt; rowCellValues.length; rowIndex++){
	for(let columnIndex = 0; columnIndex &lt; rowCellValues[rowIndex].length; columnIndex++){
		const cellValue = rowCellValues[rowIndex][columnIndex];
		if(cellValue === player.symbol) {
			console.log(&#39;initiating player scan for &#39;, player.symbol, &#39;at&#39;, [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) =&gt; {
let counter = 0;
while(rowIndex &lt; rowCellValues.length &amp;&amp; columnIndex &lt; rowCellValues[rowIndex].length){
	if(rowCellValues[rowIndex][columnIndex] === playerSymbol){
		counter++;
	}
	if(counter &gt;= winCountCondition){
		return true;
	}
	rowIndex++;
	columnIndex++;
}
return false;
}


const scanForPositiveGradientCombinations= (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) =&gt; {
let counter = 0;
while(rowIndex &lt; rowCellValues.length &amp;&amp; columnIndex &lt; rowCellValues[rowIndex].length){
	if(rowCellValues[rowIndex][columnIndex] === playerSymbol){
		counter++;
	}
	if(counter &gt;= 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)=&gt;i) }

function diagonalWin(board) {
  let d = board.length
  let seq = sequence(d)
  return board[0][0]!==null &amp;&amp; seq.every(i=&gt;board[i][i]===board[0][0]) ||
  board[0][d-1]!==null &amp;&amp; seq.every(i=&gt;board[i][d-i-1]===board[0][d-1])
}

console.log(diagonalWin([
  [&#39;x&#39;,  &#39;x&#39;,  &#39;o&#39;],
  [null, null, &#39;o&#39;],
  [&#39;x&#39;, &#39;x&#39;,   null]
]))

console.log(diagonalWin([
  [&#39;x&#39;,  &#39;x&#39;,  &#39;o&#39;],
  [null, &#39;x&#39;, &#39;o&#39;],
  [&#39;x&#39;, &#39;x&#39;,  &#39;x&#39;]
]))

console.log(diagonalWin([
  [&#39;x&#39;,  &#39;x&#39;,  &#39;o&#39;],
  [null, &#39;o&#39;, &#39;o&#39;],
  [&#39;o&#39;, &#39;x&#39;,  &#39;x&#39;]
]))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 08:26:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定