Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1
A match 5 could also happen! I would suggest a different approach. Say you swapped piece `a` with piece `b` below it. Really you only need to check the row and column where `a` is for a match 3/4/5, same for `b`. The rest of the board doesn't need to be checked, since if there wasn't a match 3 before, there won't be now. Write a function checkMatches that takes a piece (color, place id) as a parameter. Find the square 2 places to the left of it, and read the next 5 squares to the right. You are looking for consecutive stretches with the same color, so you need to count how many you saw. Ideally this is a for loop, but it can be a tricky loop to write (doesn't have to be, your approach is fine too). It helps to realize that you are checking at most 5 squares and in this stretch there is at most one match 3/4/5. Then you do the same thing, starting two pieces from the top and reading downward. Then you can run this function for both pieces that were swapped.
12th Jan 2022, 12:23 AM
Schindlabua
Schindlabua - avatar