r/howdidtheycodeit • u/DeltaMike1010 • Jul 30 '24
Question Water flow connection mechanic implementation. Any ideas how?
https://play.google.com/store/apps/details?id=com.gma.water.connect.flowYou guys know those kind of games (like the one I've attached here in the post) where you tap on a cell and they rotate and you have to make the water flow through the whole level to complete the puzzle?! I always wondered how do they determine if two adjacent cells are connected to each other. Like each cell has edges. Would really appreciate the help!🙌
9
Upvotes
8
u/Substantial_Marzipan Jul 30 '24
Think of it like a grid, so if you have a cell with water on the right side you simply need to check if the cell to its right has water on the left side
5
u/HostisHumaniGeneris Jul 30 '24
Huh, one of the first games I played was a connecting water canals game called Jungle Jack, released in 1990 for DOS. I hadn't realized that this game concept was still a popular genre.
https://www.myabandonware.com/game/jungle-jack-ko4
Sorry for the useless anecdote, but I'm quite bemused right now.
2
u/marioferpa Jul 31 '24
Exactly that, cells has edges. You need a grid of cells, each cell should have four edges that are either openings or walls, and you need a way to rotate each cell. Then the water can start in one cell and move towards all cells that match using pathfinding (the flood fill algorigthm seems perfect for that).
1
u/[deleted] Aug 01 '24
With a flood fill algorithm! Badoom, tish!Â
Seriously though, each segment is a square in a grid, you can just start at the beginning where the water starts, and travel along the path until a square is missing and the water spills, or until the water gets to the end. The tricky part is that at each point on the grid the piece can either be empty, a straight piece, 90 degree curve, or T shaped, so you have to know what rotation it is in and do for each possible direction to find out whether it continues to another piece or spills. You'll also want to mark each visited square so that you don't go in a circle or back to the start. If you want to get fancy you can also us A*star or similar, but it is overkill in this specific situation.