Help with Match 3 code logic
Hey guys i've been racking my head through this for the past few hours, not really getting it. Hoping someone can help me out and point out where my mistake is.
Basically i'm trying to make a match 3 game. I've gotten to the point where I need to check the board if there are 3 or more tiles of the same value in a row. I've got a different array for each row and column of the board so now it's just up to looking through the arrays for 3 or more in a row.
Here's the code. It sometimes returns 3 in a row, almost always adds 2 in a row, and i've rare seen it add 4 in a row, although its rare to get four in a row when randomly generating my board in the first place.
List<Tile> ListOfSet = new List<Tile>();
int ValueToCompare = 0;
for (int i = 0; i < myList.Count - 1; i++)
{
ValueToCompare = myList[i].tile.value;
if (myList[i + 1].tile.value == ValueToCompare)
{
ListOfSet.Add(myList[i + 1].tile);
if (!ListOfSet.Contains(myList[i].tile))
{
ListOfSet.Add(myList[i].tile);
}
}
}
if(ListOfSet.Count <= 2)
{
ListOfSet.Clear();
}
else
{
MatchingSet.AddRange(ListOfSet);
ListOfMatchingSets.Add(ListOfSet);
print("There was a set found!!");
//print("There is a set starting from: " + MatchingSet[0].transform.localPosition + " Ending at: " + myList[MatchingSet.Count-1].transform.localPosition);
}
MatchingSet is just a debug set I've got so I can see all the different "Matches" my code brings out. I add the "ListOfSet" To a list of lists so I can have access to each separate case of a match.
Please help!! Thanks
Posted Feb 25, 2019, 10:39 AM UTC
On my current job, I have a test task to write a match 3 game.
https://github.com/nicloay/Match3TestTask/blob/master/Assets/Scripts/Logic/MatchFinder/LineMatcher.cs
http://nicloay.com/Match3TestTask/ here is a webgl demo with autoplay button (basically get hint and make the move)