L
L
levedun2015-01-11 12:38:46
Android
levedun, 2015-01-11 12:38:46

How to implement game logic using the example of this application?

56793b9e6babdaa268c4d19fdb1db40b.jar.jpg
Light a light bulb game. The point is that the battery transfers energy to the wires. And all this operates rationally from the point of view of physics, i.e. if there is a break, then the energy does not go further. I tried different options and all unsuccessful ones: such as setting the script initially. Agree it ties hands and kills AI. The second option is to pass the position of the activity through a bunch of conditions, if it is turned this way, then do it, etc. All this is also limited ... Maybe there are ideas?
I found an example on visual basic - there according to the scenario - it does not roll. I connected a battery there with a light bulb directly did not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Espleth, 2015-01-11
@Espleth

I don't quite get it, what's so hard about it? Something like a simple task on graphs. Each field cell has properties: bool - whether there is a wire in it (or string / int, if there can be not only a wire, but something else, such as a lamp), and 4 more bools corresponding to the sides (up, down, left , right), which makes it clear where the wire goes. with each update of the game (an update in terms of turning the wire or something like that), go directly through all the power sources in turn and see if the wires from it go to the lamp. If they come out, light it up. On the move, it reminded me of Prim's algorithm. Although it’s completely different here, they definitely have something in common, namely, it seems to me that a recursive function for going through all the options here will be a function for checking for loops in that algorithm

S
Sergey Lerg, 2015-01-11
@Lerg

Everything is simple. You have a 2d array - a field map. Each cell stores an object - a lamp, a battery, a wire. Wires have a property - four conductance variables: left, right, up, down. Which of them is true means there is a connection in that direction.
Next, create a second 2d array - an electricity map, the size is the same, but in each cell the value is true / false - there is electricity or no electricity.
Now your task is to fill the electricity map according to the wires, starting from the batteries.
Once that's done, look at your lamps, see where they're making contact, if that cell has electricity, and if there's a connection to that cell.
To do this, process each battery in a loop: start from the position of the battery, assign true to the electricity map, go along the map in the direction of the wires, where there is a branching - use recursion. As soon as there are no moves left, move on to the next battery. If the cell already has electricity, ignore it.
When all the batteries are processed, go to the lamps and light them if everything is in order.
As soon as there is a change in the field (wire rotation / movement), then clear the electricity map, turn off all the lamps and run the algorithm again. This happens in a fraction of a second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question