I am solving a particular problem in an ugly, brute force way and I feel like there should be a better way to solve it.
I have a two dimensional tile map I need to send over the network in rows. The rows can arrive at the other end in any order, and might even be sent twice. I need to know if all the rows have been sent.
My current solution, after receiving each row and storing the row locally, is to loop through the first tile of each local row and check if it’s null. It works, but it’s so darn ugly.
What’s a better way to test if each possible event of a set of events has happened at least once?
I’d say to add your tiles into a list/array and use either a for loop or a foreach loop to accomplish what you need. However, this is very difficult to determine blindly.
How bout you give your rows a unique identifier. You create a Dict<int, bool> on the client, add all the possible IDs with a false flag and turn them to true as they come in. DoStuff() when all is true.
You could assign a bit value for each row
row 1 → 1
row 2 → 2
row 3 → 4
row 4 → 8
etc.
Keep track of loaded rows with a single int “loadedRows” which has an inital value of 0.
When a row is received, it sets its bit value:
loadedRows |= rowBitValue
If you have 8 rows, you would check if loadedRows equals 255 to make sure all rows were received.
No looping or dictionaries necessary. Also, sending rows twice isn’t an issue with this approach.
A dict/map is always my first go to for such a thing.
And I have to assume you have a unique identifier of some sort so you can sort the rows into their appropriate position since you said you may retrieve them out of order (probably their index).
Of course there’s an even more compact way of doing this if you’re looking to avoid more memory as well as not looping constantly.
And that’s just an int counter. Every time you receive a new row, check if it’s slot (index) is filled… if it’s not, place it in its slot, and incremement a counter. Once that counter reaches the total number of rows, well, you have all your rows. (just don’t accidentally increment if you received a duplicate row, which you know because its slot isn’t null)
…
All of the options though of course rely on knowing how many rows you’re going to receive in total (dict, count, etc). But I mean… if you don’t know how many you’re going to receive there’s honestly know way to know when you’ve received them all unless a “complete” message is sent by the remote client. In which case, that’s how you’d know it’s done.