如何判断一个二维数组是否有一行有3列相匹配的连续数列

问题描述 投票:-1回答:1

我想检查一个二维数组是否有一行有3个匹配的连续列。

bool ScoreRijAanwezig(RegularCandies[,] speelveld)

这是我目前掌握的情况

public enum RegularCandies
{
    Jellybean,
    Lozenge,
    LemonDrop,
    GumSquare,
    LollipopHead,
    JujubeCluser
};

static void Main(string[] args)
{
    Program myProgram = new Program();
    myProgram.Start();
    Console.ReadKey();
}

void Start()
{
    RegularCandies[,] speelveld = new RegularCandies[10, 5];
    InitCandies(speelveld);
}

void InitCandies(RegularCandies[,] speelveld)
{
    Random rnd = new Random();

    for (int i = 0; i < speelveld.GetLength(0); i++)
    {
        for (int j = 0; j < speelveld.GetLength(1); j++)
        {
            speelveld[i, j] = (RegularCandies)rnd.Next(0, 6);
        }
    }
    PrintCandies(speelveld);
}

void PrintCandies(RegularCandies[,] speelveld)
{
    for (int i = 0; i < speelveld.GetLength(0); i++)
    {
        for (int j = 0; j < speelveld.GetLength(1); j++)
        {

            if (speelveld[i, j] == RegularCandies.Jellybean)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("#");
            }
            else if (speelveld[i, j] == RegularCandies.Lozenge)
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.Write("#");
            }
            else if (speelveld[i, j] == RegularCandies.LemonDrop)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("#");
            }
            else if (speelveld[i, j] == RegularCandies.GumSquare)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("#");
            }
            else if (speelveld[i, j] == RegularCandies.LollipopHead)
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("#");
            }
            else if (speelveld[i, j] == RegularCandies.JujubeCluser)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write("#");
            }

            Console.ResetColor();
            Console.Write(" ");
        } Console.WriteLine();

    }
    ScoreRijAanwezig(speelveld);
}

bool ScoreRijAanwezig(RegularCandies[,] speelveld)
{
    int count = 1;

    for (int i = 0; i < speelveld.GetLength(0); i++)
    { // check which enum regularCandies shows up 3times in a row  next to   eachother
        // like this below; 
        //
        // row i--->   red    [blue   blue   blue]
        //             green   red    yellow green
        //             blue    green  green  purple
        //             purple  yellow purple yellow
        //
        //              ^
        //              |
        //
        //             col j
        // blue shows up 3 times a a row now break the loop and return true 

        for (int j = 0; j < speelveld.GetLength(1); j++)
        {

            if (speelveld[i, j] == RegularCandies.Jellybean)
            {
                count++;
                if (speelveld[i - 1, j] == RegularCandies.Jellybean)

                    if (count >= 3)
                    {
                        Console.WriteLine("Speelvelde SCORE 3X hetzelfde getal rood (JellyBean)");
                        return true;
                    }
            }
            else
            {
                count = 1;
            }
            if (speelveld[i, j] == RegularCandies.Lozenge)
            {
                count++;
                if (count >= 3)
                {
                    Console.WriteLine("speelvelde SCORE 3X hetzelfde getal oranje ");
                    return true;
                }
            }
            else
            {
                count = 1;
            }
        }

    }
    return false;
}
c# multidimensional-array type-2-dimension
1个回答
1
投票

这是一种方法。

对于每一行,将第一列的值存储在一个变量中,并将一个计数器变量设置为 1. 然后循环执行其余的列,如果数值匹配,则递增计数器,如果计数器等于 3,我们找到了一个匹配的连续集,可以返回 true. 如果不匹配,则重新设置一下。itemToMatch当前 列的值,并将计数器重置为 1. 如果我们完成了最后一排,但仍然没有返回。true,那么我们就没有找到连续的3个匹配,所以我们可以回到 false:

for (var row = 0; row < speelveld.GetLength(0); row++)
{
    // Store the value of the first column and start our counter at 1
    var itemToMatch = speelveld[row, 0];
    var matchCount = 1;

    // Loop through the rest of the columns in the row
    for (var col = 1; col < speelveld.GetLength(1); col++)
    {
        // If this column matches, increment our counter
        if (speelveld[row, col] == itemToMatch)
        {
            matchCount++;

            // If we've found three matches, notify the user and return true
            if (matchCount == 3)
            {
                Console.WriteLine(
                    $"Row #{row + 1} has three matching items ({itemToMatch})");

                return true;
            }
        }
        // If the column didn't match, then store *this* column and reset the counter
        else
        {
            itemToMatch = speelveld[row, col];
            matchCount = 1;
        }
    }
}

// If we get this far, we never found three matches so notify the user and return false
Console.WriteLine("No rows have three consecutive matching columns");
return false;
© www.soinside.com 2019 - 2024. All rights reserved.