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

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

Search a positive gradient in a dynamic-sized 2d array

问题

我需要在一个二维数组中搜索正梯度和负梯度的值,例如:

正梯度:左下 > 中间 > 右上

负梯度:左上 > 中间 > 右下

我有两个函数。首先,在for循环中找到值,当找到值后,这两个函数都运行以搜索迷宫的正梯度和负梯度,并在找到获胜组合时返回true。

scanForNegativeGradientCombinations 函数可以工作,但 scanForPositiveGradientCombinations 函数会出现类型错误。我认为问题可能出在减小 rowIndex 时,可能使游戏超出边界,但我不确定。

以下是将调用以下两个函数的函数:

  1. const isDiagonalWinner = (rowCellValues, winCountCondition, player) => {
  2. for (let rowIndex = 0; rowIndex < rowCellValues.length; rowIndex++) {
  3. for (let columnIndex = 0; columnIndex < rowCellValues[rowIndex].length; columnIndex++) {
  4. const cellValue = rowCellValues[rowIndex][columnIndex];
  5. if (cellValue === player.symbol) {
  6. console.log('initiating player scan for ', player.symbol, 'at', [rowIndex, columnIndex]);
  7. if (scanForNegativeGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
  8. return true;
  9. }
  10. if (scanForPositiveGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
  11. return true;
  12. }
  13. }
  14. }
  15. }
  16. return null;
  17. };

下面是这两个函数:

  1. const scanForNegativeGradientCombinations = (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) => {
  2. let counter = 0;
  3. while (rowIndex < rowCellValues.length && columnIndex < rowCellValues[rowIndex].length) {
  4. if (rowCellValues[rowIndex][columnIndex] === playerSymbol) {
  5. counter++;
  6. }
  7. if (counter >= winCountCondition) {
  8. return true;
  9. }
  10. rowIndex++;
  11. columnIndex++;
  12. }
  13. return false;
  14. }
  15. const scanForPositiveGradientCombinations = (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) => {
  16. let counter = 0;
  17. while (rowIndex < rowCellValues.length && columnIndex < rowCellValues[rowIndex].length) {
  18. if (rowCellValues[rowIndex][columnIndex] === playerSymbol) {
  19. counter++;
  20. }
  21. if (counter >= winCountCondition) {
  22. return true;
  23. }
  24. rowIndex--;
  25. columnIndex++;
  26. }
  27. return false;
  28. }
英文:

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

  1. [
  2. [&#39;x&#39;, &#39;x&#39;, &#39;o&#39;],
  3. [null, null, &#39;o&#39;],
  4. [&#39;x&#39;, &#39;x&#39;, null]
  5. ]

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.

  1. const isDiagonalWinner = (rowCellValues, winCountCondition, player) =&gt; {
  2. for(let rowIndex = 0; rowIndex &lt; rowCellValues.length; rowIndex++){
  3. for(let columnIndex = 0; columnIndex &lt; rowCellValues[rowIndex].length; columnIndex++){
  4. const cellValue = rowCellValues[rowIndex][columnIndex];
  5. if(cellValue === player.symbol) {
  6. console.log(&#39;initiating player scan for &#39;, player.symbol, &#39;at&#39;, [rowIndex, columnIndex]);
  7. if(scanForNegativeGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
  8. return true
  9. }
  10. if(scanForPositiveGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
  11. return true
  12. }
  13. }
  14. }
  15. }
  16. return null;
  17. };

Above is the function that will call the following 2 functions.

  1. const scanForNegativeGradientCombinations= (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) =&gt; {
  2. let counter = 0;
  3. while(rowIndex &lt; rowCellValues.length &amp;&amp; columnIndex &lt; rowCellValues[rowIndex].length){
  4. if(rowCellValues[rowIndex][columnIndex] === playerSymbol){
  5. counter++;
  6. }
  7. if(counter &gt;= winCountCondition){
  8. return true;
  9. }
  10. rowIndex++;
  11. columnIndex++;
  12. }
  13. return false;
  14. }
  15. const scanForPositiveGradientCombinations= (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) =&gt; {
  16. let counter = 0;
  17. while(rowIndex &lt; rowCellValues.length &amp;&amp; columnIndex &lt; rowCellValues[rowIndex].length){
  18. if(rowCellValues[rowIndex][columnIndex] === playerSymbol){
  19. counter++;
  20. }
  21. if(counter &gt;= winCountCondition){
  22. return true;
  23. }
  24. rowIndex--;
  25. columnIndex++;
  26. }
  27. return false;
  28. }

答案1

得分: 0

  1. function sequence(n) { return Array(n).fill().map((_,i)=>i) }
  2. function diagonalWin(board) {
  3. let d = board.length
  4. let seq = sequence(d)
  5. return board[0][0]!==null && seq.every(i=>board[i][i]===board[0][0]) ||
  6. board[0][d-1]!==null && seq.every(i=>board[i][d-i-1]===board[0][d-1])
  7. }
  8. console.log(diagonalWin([
  9. ['x', 'x', 'o'],
  10. [null, null, 'o'],
  11. ['x', 'x', null]
  12. ]))
  13. console.log(diagonalWin([
  14. ['x', 'x', 'o'],
  15. [null, 'x', 'o'],
  16. ['x', 'x', 'x']
  17. ]))
  18. console.log(diagonalWin([
  19. ['x', 'x', 'o'],
  20. [null, 'o', 'o'],
  21. ['o', 'x', 'x']
  22. ]))
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. function sequence(n) { return Array(n).fill().map((_,i)=&gt;i) }
  2. function diagonalWin(board) {
  3. let d = board.length
  4. let seq = sequence(d)
  5. return board[0][0]!==null &amp;&amp; seq.every(i=&gt;board[i][i]===board[0][0]) ||
  6. board[0][d-1]!==null &amp;&amp; seq.every(i=&gt;board[i][d-i-1]===board[0][d-1])
  7. }
  8. console.log(diagonalWin([
  9. [&#39;x&#39;, &#39;x&#39;, &#39;o&#39;],
  10. [null, null, &#39;o&#39;],
  11. [&#39;x&#39;, &#39;x&#39;, null]
  12. ]))
  13. console.log(diagonalWin([
  14. [&#39;x&#39;, &#39;x&#39;, &#39;o&#39;],
  15. [null, &#39;x&#39;, &#39;o&#39;],
  16. [&#39;x&#39;, &#39;x&#39;, &#39;x&#39;]
  17. ]))
  18. console.log(diagonalWin([
  19. [&#39;x&#39;, &#39;x&#39;, &#39;o&#39;],
  20. [null, &#39;o&#39;, &#39;o&#39;],
  21. [&#39;o&#39;, &#39;x&#39;, &#39;x&#39;]
  22. ]))

<!-- 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:

确定