[英]Problem with recursion. How to solve that problem?
I have a little problem with my recursion.我的递归有点问题。 I have a function with checking matching directions of clicked box
我有一个检查点击框匹配方向的功能
const checkMatchingDirections = (board, r, c) => {
const top = board[r - 1] !== undefined && { row: r - 1, column: c };
const bottom = board[r + 1] !== undefined && { row: r + 1, column: c };
const left = board[r][c - 1] !== undefined && { row: r, column: c - 1 };
const right = board[r][c + 1] !== undefined && { row: r, column: c + 1 };
// filter for edge blocks and finding match color
const directionsWithMatches = [top, bottom, left, right]
.filter(dir => dir instanceof Object)
.filter(({ row, column }) => board[row][column].color === board[r][c].color);
return directionsWithMatches;
};
That function returns array of matching color of clicked box.该函数返回点击框匹配颜色的数组。
My problem is that I want to recall that function checkMatchingDirections on results of previous returned array from that function.我的问题是我想在该函数之前返回的数组的结果上调用该函数 checkMatchingDirections 。
Actually I'm creating like this其实我是这样创造的
const matches = checkMatchingDirections(blocks, y, x);
matches.map(({ row, column }) => {
const restMatches = checkMatchingDirections(blocks, row, column);
allMatchingBlocks = [...matches, ...allMatchingBlocks, ...restMatches];
});
But's it's hardcoded to recall that function twice by maping results of checkMatchingDirection in first call.但是通过在第一次调用中映射 checkMatchingDirection 的结果来两次调用该函数是硬编码的。
How to create function which gonna recall checkMatchingDirection on results array of checkMathingDirection?如何在checkMathingDirection的结果数组上创建调用checkMatchingDirection的函数?
For example.例如。
If I have clicked one green box and then there's 4 box on the left and one on top.如果我点击了一个绿色框,然后左边有 4 个框,顶部有一个框。 There's all selected.
都选好了
A flood fill would work like this (pseudocode):洪水填充将像这样工作(伪代码):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.