Reputation: 35
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;
}
Upvotes: 1
Views: 479
Reputation: 37020
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 equals 3
, we found a matching consecutive set and can return true
. If it doesn't match, then reset the itemToMatch
to the current column's value and reset the counter back to 1
. If we finish the last row and still haven't returned true
, then we didn't find 3 consecutive matches, so we can return 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;
Upvotes: 2