I want to check if a two dimensional array has a row with 3 matching, consecutive columns:
bool ScoreRijAanwezig(RegularCandies[,] speelveld)
Here's what I have so far:
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;
}
Here's one way to do it.
For each row, store the first column value in a variable and set a counter variable to
1. Then loop through the rest of the columns, and if the value matches, increment the counter, and if the counter equals3, we found a matching consecutive set and can returntrue. If it doesn't match, then reset theitemToMatchto the current column's value and reset the counter back to1. If we finish the last row and still haven't returnedtrue, then we didn't find 3 consecutive matches, so we can returnfalse: