I've made an electronic circuit board simulator which has simply 3 types of tiles: wires, power sources, and inverters. Wires connect to anything they touch, other than the sides of inverters; inverters have one input side and one output side; and finally power tiles connect in a similar manner as wires. In the case of an infinite loop, caused by the output of the inverter feeding into its input, I want inverters to oscillate (quickly turn on/off).
I've attempted to implement a FloodFill algorithm to spread the power throughout the grid, but seem to have gotten something wrong, as only the tiles above the power source get powered (as seen below)
I've attempted to debug the program, but have had no luck thus far. My code concerning the updating of power can be seen here.
Answer
Your flood-fill algorithm seems to works correctly, but you are overwriting the results.
You have a loop in which you update every single tile. When it is a wire, you switch it off. When you reach a power-source, you flood-fill from it.
What happens in your code is:
- the board gets iterated in a loop from up to down switching off all wires
- the loop reaches the source in the middle of the board
- a flood-fill is performed from the source and all wires on the board get switched on
- the loop continues updating and switches off all not yet processed wires which were just switched on during the flood-fill
To fix this code you need two separate loops. First a loop over all wires to reset their state to off and then, when the board is properly cleared, a second loop to flood-fill from the power sources.
No comments:
Post a Comment