Tree algorithm

Today I was thinking about the idea of ​​a small game and came across its implementation. The idea is that the player can make a series of moves that cause a small effect, but if it is done in a certain sequence, it will lead to a greater effect. So far so good, I know how to do it. Obviously, I had to complicate the task (because we like to complicate it), so I thought that there could be several possible paths for the sequence, which would have a greater effect, although different. Furthermore, part of some sequences may be the start of other sequences, or even whole sequences may be contained by other larger sequences. Now I don’t know for sure the best way to implement this. I had some ideas.

1) I could implement a circular n-linked list. But since the list of steps never ends, I am afraid that this may cause stack overflow ™. The idea is that each node will have n children and after receiving a command it can lead you to one of its children or, if none of the children are available for such a team, it will lead you to the beginning. Upon arrival, a pair of functions will be performed on any children, causing a small and large effect. This can, however, lead to a large number of duplicated nodes in the tree to cope with all possible sequences ending in this particular move with various effects, which can be a pain to maintain, but I'm not sure. I have never tried something so complex in code, only theoretically. Does this algorithm exist and have a name? Is that a good idea?

2) I could implement a state machine. Then, instead of wandering around the linked list, I will have a giant nested switch that will call functions and update the state of the machine accordingly. It seems easier to implement, but ... well ... it doesn't seem funny ... and not ellegant. Gigantic switches always seem ugly to me, but will this work better?

3) Suggestions? I am good, but I am far inexperienced. The good thing about coding is that no matter how strange your problem is, someone solved it in the past, but you need to know where to look. Someone may have a better idea than mine, and I really wanted to hear suggestions.

+3
source share
7 answers

, , , , , , - . "117" - , "468" - , "411799" - ( ).

, :

55468411799

" " * s:

55468 * 4117 * 99 *

- , ? , , - (, Java):

MagicSequence fireworks = new MagicSequence(new FireworksAction(), 1, 1, 7);
MagicSequence playMusic = new MagicSequence(new MusicAction(), 4, 6, 8);
MagicSequence fixUserADrink = new MagicSequence(new ManhattanAction(), 4, 1, 1, 7, 9, 9);

Collection<MagicSequence> sequences = ... all of the above ...;

while (true) {
  int num = readNumberFromUser();
  for (MagicSequence seq : sequences) {
    seq.handleNumber(num);
  }
}

MagicSequence - :

Action action = ... populated from constructor ...;
int[] sequence = ... populated from constructor ...;
int position = 0;

public void handleNumber(int num) {
  if (num == sequence[position]) {
    // They've entered the next number in the sequence
    position++;
    if (position == sequence.length) {
       // They've got it all!
       action.fire();
       position = 0; // Or disable this Sequence from accepting more numbers if it a once-off
    }
  } else {
    position = 0; // missed a number, start again!
  }
}
+3

, , .
, , .
, , .

: node :
--
- ,   :
 --
 -recondition, , , .

+2

, , .

, Civ, "" - "", re , .

, , , .

, [] .

+1

. , , .

+1

, , . .

+1

, . , "" . , , , . , .

, , , : node , node. node.

edit: , Cowan , , . , .

+1

@Cowan, @Javier: , ?

MagicSequence , () . , , MagicSequence . , .

Optimize this by only giving input to the MagicSequences that await him. There are two ways to do this:

  • You have an object that allows all MagicSequences to connect to events that correspond to the numbers in their templates. MagicSequence (1,1,7) will add itself to got1 and got7, for example:

    UserInput.got1 + = MagicSequnece [i] .SendMeInput;

  • You can optimize this so that after each entry MagicSequences unregisters from invalid events and logs in with valid ones.

+1
source

Source: https://habr.com/ru/post/1698091/


All Articles