Thursday, September 14, 2017

java - Electronic circuit simulator four-way flood-filling issues


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)


half-updating grid


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:



  1. the board gets iterated in a loop from up to down switching off all wires

  2. the loop reaches the source in the middle of the board

  3. a flood-fill is performed from the source and all wires on the board get switched on

  4. 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

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...