Java 如何在二维阵列中检查3的水平集?

Java 如何在二维阵列中检查3的水平集?,java,arrays,Java,Arrays,我目前正在为一个班级做一个项目,我被困在这里的时间最长。基本上,我要做的是检查2D 8x8数组中的水平3个相同字符集。如果有一组3(例如&&&&),它应该将它们全部删除,从上到下删除项目,并使用我创建的单独数组中的随机字符填充数组最顶层的空插槽。我不明白的主要问题是如何在这个2D数组中水平检查3个字符的相同集合 任何帮助都将非常感谢 基本上,您需要遍历矩阵中的每一行,从索引等于1的元素到上一个元素到最后一个元素,并检查当前元素是否等于其上一个和下一个元素: for (int i = 0; i &

我目前正在为一个班级做一个项目,我被困在这里的时间最长。基本上,我要做的是检查2D 8x8数组中的水平3个相同字符集。如果有一组3(例如&&&&),它应该将它们全部删除,从上到下删除项目,并使用我创建的单独数组中的随机字符填充数组最顶层的空插槽。我不明白的主要问题是如何在这个2D数组中水平检查3个字符的相同集合


任何帮助都将非常感谢

基本上,您需要遍历矩阵中的每一行,从索引等于1的元素到上一个元素到最后一个元素,并检查当前元素是否等于其上一个和下一个元素:

for (int i = 0; i < matrix.length; i++) {
    for (int j = 1; j < matrix[i].length - 1; j++) {
        if (matrix[i][j-1] == matrix[i][j] && matrix[i][j] == matrix[i][j+1]) {
            // do something here
        }
    }
}
for(int i=0;i
基本上,您需要遍历矩阵中的每一行,从索引等于1的元素到上一个元素到最后一个元素,并检查当前元素是否等于其上一个和下一个元素:

for (int i = 0; i < matrix.length; i++) {
    for (int j = 1; j < matrix[i].length - 1; j++) {
        if (matrix[i][j-1] == matrix[i][j] && matrix[i][j] == matrix[i][j+1]) {
            // do something here
        }
    }
}
for(int i=0;i
假设变量i表示特定行的索引,例如,对于第三行,索引为2,对于第五行,索引为4。 因此,对于矩阵[8][8]

for (int j = 0; j < 8; j++) {
if (matrix[i][j-1] == matrix[i][j] && matrix[i][j] == matrix[i][j+1]) {
            // do your stuff
        }
}
for(int j=0;j<8;j++){
if(矩阵[i][j-1]==矩阵[i][j]&矩阵[i][j]==矩阵[i][j+1]){
//做你的事
}
}

将i更新为第二行索引并再次检查。

假设变量i表示特定行的索引,例如,对于第三行,索引为2,对于第五行,索引为4。 因此,对于矩阵[8][8]

for (int j = 0; j < 8; j++) {
if (matrix[i][j-1] == matrix[i][j] && matrix[i][j] == matrix[i][j+1]) {
            // do your stuff
        }
}
for(int j=0;j<8;j++){
if(矩阵[i][j-1]==矩阵[i][j]&矩阵[i][j]==矩阵[i][j+1]){
//做你的事
}
}

更新i到第二行索引并再次检查。

将有助于了解您使用的语言,但本质上您需要的是一个循环,用于在每个步骤上执行环视

这都是psudocode,因此您必须适应所使用的语言

// your array looks something like this but I will assume that all the numbers are characters
Array myArray = [ [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,];

// set up a variable to store how many we need together to get a match
int required = 3; // lets go with 3

// loop through the nested arrays
for(int i = 0; i < myArray.length; i++){

    // loop through the elements in the nested array
    for(int j = 0; myArray[i].length; i++){

        // get the character we are looking for
        char c = myArray[i][j];

        // we will need to know where we are coming from first
        Point p = {x:j,y:i};// setting p.x = j and p.y = i

        // now lets perform the look-around (I will assume no diagonals)
        Array result = [p];// array with the original point

        result = lookAround(result, myArray);

        // if we got a result then you can proccess it in your game how you like and exit the loop.
        if(result != null){
           // do game logic here and you can remove the points in the result array
        }
    }
}
//您的数组看起来像这样,但我假设所有的数字都是字符
数组myArray=[[1,2,3,4,5,6,7,8],
[1,2,3,4,5,6,7,8] ,
[1,2,3,4,5,6,7,8] ,
[1,2,3,4,5,6,7,8] ,
[1,2,3,4,5,6,7,8] ,
[1,2,3,4,5,6,7,8] ,
[1,2,3,4,5,6,7,8] ,];
//设置一个变量来存储我们需要一起获得匹配的数量
int required=3;//我们一起去吧
//循环遍历嵌套数组
for(int i=0;i
现在来看一下环顾法:

// this method returns an array of points if match is found or null otherwise.
// a is our array, from is the last point checked or null if it's the first point, to is the point to check next, count is the current itteration or how many have been found
Array lookAround( Array resultArray, Array gameArray){

    // get char of first point from gameArray
    Char c = gameArray[resultArray[0].y][resultArray[0].x]; // this is the char we are looking for

    // grab last point in the array
    Point lastPoint = resultArray[resultArray.length - 1];

    // try get second last point in array or null
    Point secondLastPoint = resultArray.length <= 1 ? null : resultArray[resultArray.length - 2];
    // now we find all the points we need to check
    Point up = { x:lastPoint.x , y:lastPoint.y - 1 )
    Point down = { x:lastPoint.x , y:lastPoint.y + 1 )
    Point left = { x:lastPoint.x - 1 , y:lastPoint.y )
    Point right = { x:lastPoint.x + 1 , y:lastPoint.y )

    Array pointsToCheck = [up,down,left,right];

    foreach(Point p in pointsToCheck){
        // check each of these points characters.
        // we have to check bounds first
        // the validatePoint method will help us
        if(validatePoint(p, secondLastPoint)){

            // grab the character that we want to check now
            Char t = gameArray[p.y][p.x];

            // now we finally check for the character match                
            if( c == t ){
                // if we are here then we have another match
                // lets make a copy of the result array and append the new point
                Array newResult = resultArray.copy();
                newResult.push(p);

                // if we have enough points we can return the result as final.
                if( newResult.length >= required){
                    return newResult;
                }else{
                    return lookAround( newResult, gameArray);
                }
            }




        }
    }
    // if the code reaches this point then we havn't been able to find a sequence for this point... lets return null.
    return null;
}
//如果找到匹配项,则此方法返回一个点数组,否则返回null。
//a是我们的数组,from是最后一个检查的点,如果是第一个点,则为null,to是下一个要检查的点,count是当前的计数或找到的数量
数组查找(数组结果数组、数组游戏数组){
//从gameArray获取第一个点的字符
Char c=gameArray[resultArray[0].y][resultArray[0].x];//这就是我们要找的字符
//抓住阵列中的最后一点
Point lastPoint=resultArray[resultArray.length-1];
//尝试获取数组中的第二个最后一个点或null
Point secondLastPoint=resultArray.length=必需){
返回新结果;
}否则{
返回lookAround(newResult,gameArray);
}
}
}
}
//如果代码达到这一点,那么我们就无法找到这一点的序列。。。让我们返回null。
返回null;
}
此方法是确保我们的点有效的实用方法,即在数组边界内,也不是我们检查的最后一个点。。。注意,如果我们需要检查3个以上的点,我们应该传递和点数组(resultArray)并检查所有点,而不是最后一个点。 //此方法将检查该点是否在游戏数组中,并检查它是否与我们已检查的最后一个点不同

Boolean validatePoint( Point p, Point last, Array gameArray){

    //check array bounds
    if(p.x < 0 || p.y < 0 || p.x >= gameArray[0].length || p.y >= gameArray.length){
        return false;
    }

    // check if the point is the same as the last one
    if(p.x == last.x && p.y == last.y){
        return false;
    }

    // if we got here then the point is valid
    return true;

}
Boolean validatePoint(点p、点last、数组gameArray){
//检查数组边界
如果(p.x<0 | | p.y<0 | | p.x>=gameArray[0]。长度| | p.y>=gameArray.length){
返回false;
}
//检查该点是否与上一点相同
如果(p.x==last.x&&p.y==last.y){
返回false;
}
//如果我们到了这里,那么这一点是正确的
返回true;
}

将有助于了解您正在使用的语言,但本质上您需要的是一个循环,该循环在每个步骤上执行环视

这都是psudocode,因此您必须适应所使用的语言

// your array looks something like this but I will assume that all the numbers are characters
Array myArray = [ [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,
            [1,2,3,4,5,6,7,8] ,];

// set up a variable to store how many we need together to get a match
int required = 3; // lets go with 3

// loop through the nested arrays
for(int i = 0; i < myArray.length; i++){

    // loop through the elements in the nested array
    for(int j = 0; myArray[i].length; i++){

        // get the character we are looking for
        char c = myArray[i][j];

        // we will need to know where we are coming from first
        Point p = {x:j,y:i};// setting p.x = j and p.y = i

        // now lets perform the look-around (I will assume no diagonals)
        Array result = [p];// array with the original point

        result = lookAround(result, myArray);

        // if we got a result then you can proccess it in your game how you like and exit the loop.
        if(result != null){
           // do game logic here and you can remove the points in the result array
        }
    }
}
//您的数组看起来像这样,但我假设所有的数字都是字符
数组myArray=[[1,2,3,4,5,6,7,8],