# How would I go about updating my electronic circuit simulator's 'electricity'?

I have made an application which allows the user to place down wires, power sources, and inverters on a virtual circuit board. All connections between tiles are automatic, as shown here:

As you can see in the last image, the updating of power throughout this grid is not yet functioning. I think I understand how to do this updating conceptually, but getting it to work in my program is appearing to be much more difficult than I first imagined. My source code can be found here.

Getting tiles to pass on power to their neighbors is quite easy, but the tricky part is getting wires to "unpower" after the removal of a power source. The size of the grid is just 18x18, so efficiency really isn't a factor, for those wondering.

If you have any tips as to how to I might approach this task, please let me know.

• Are you trying to achieve any level of accuracy? Or just something that "works"? And please give more detail, how large is the total grid? – MickLH Jun 5 '14 at 23:35
• @MickLH Sorry it was unclear, I've updated the post and hopefully answered all of your questions. – AJ Weeks Jun 6 '14 at 2:16
• Hi, I'm an author of a logic simulator, email me if you want to talk =) – Jari Komppa Jun 6 '14 at 5:21
• @JariKomppa prefarrably answer things here, for the completeness of the site, please. – Gustavo Maciel Jun 6 '14 at 5:38
• @GustavoMaciel I hear you, and for single questions, sure, but circuit simulation is a bit too big topic for a single q/a. – Jari Komppa Jun 6 '14 at 6:12

The simple solution would be to just discard and recalculate the power distribution in the whole grid whenever you make a change. Considering that your grid is only 18x18, it shouldn't be too computationally intensive to do so.

Inverters might be problematic, though. What do you want to happen when the player connects the output of an inverter to its input? This would lead to a paradoxical situation. In a real-life circuit, the inverter might either oscillate (switch between on- and off as fast as it can) or just short-circuit and break.

When you want to go for the oscillating solution, you should go for a tick-based simulation where a fixed number of updates is calculated each second. In such a simulation, an inverter would not directly forward the state of its input. It would instead be modeled as an object which blocks power-flow, but which is also an independent power-source, just with the difference that the player can not switch it off- and on directly. The update-function which is executed in regular intervals would then look like this in pseudo-code:

 for each source
if source is inverter
if input wire is in "on"
set source to "off"
else
set source to "on"

for each wire
set wire to "off"

for each source
if source is "on"
flood-fill wires starting from output-wire and set all found wires to "on"


note that evaluation of the inverter-state and evaluation of the wire-state are two separate steps. That way you will not run into any infinite loops during the update.

When you instead want to go for a "breaking" solution, you would recursively flood-fill from all sources. The status of each wire would start out as "undefined". When you start flood-filling from a souce, you fill with the value "on". When you pass through an inverter, you toggle from "on" to "off" or vice versa and continue flood-filling with the new value. When you ever encounter an "off" wire while flooding with "on" or an "on" wire while flooding with "off", you found a paradoxon in the players circuit, revert the last move and tell the player they can't do that.

• Thanks for the advice, I think I will try to implement the tick-based oscillating method you mentioned. – AJ Weeks Jun 6 '14 at 16:43
• The only problem I'm running into is how to specifically implement the flood-filling of wires when a source is "on". – AJ Weeks Jun 6 '14 at 17:02
• @AJWeeks FloodFill is a stock algorithm. When you have problems with implementing it you could ask about it on stackoverflow. – Philipp Jun 6 '14 at 17:29
• I've attempted implementing the flood-fill algorithm after reading the WikiPedia page on it, and seem to have run into a problem. As you can see in this picture, the algorithm is only updating tiles which are above the source. I've tried to follow the code line by line using eclipse's debugging tools, but I feel stumped. Any ideas? As mentioned above, the source code can be seen here, if you want to read it, feel free. Thanks again for all the help! – AJ Weeks Jun 6 '14 at 23:08
• @AJWeeks Because you have a completely new problem (how to implement flood-fill) and this problem is too complex to describe and answer just with comments. So please create a new question where you paste the relevant code-section (not just a link to your git repository) and someone (maybe me) will give you an answer. – Philipp Jun 6 '14 at 23:19

This sounds strange, and high-level, but I would consider modelling electricity through cellular automata.

Practically, this means modelling electricity as a state (on/off) for each cell, and codifying a set of rules that tell you which states are on or off.

The rules might go something like:

• For every cell that's a power source, set the state to "on"
• Notify every adjacent cell that's connected that we're on