What you describe sounds as if your solution is almost complete. If your code works all the time, not just in the cases you want, that’s perfect because all you need is a restriction.
The computer is not intelligent. It cannot read our minds, it does not know anything about our ideas. If something happens that is not supposed to happen, we usually add an if-statement to our code which prevents that “something” from happening.
And if we want something to happen only once, the default solution is usually a flag (= bool variable). We change the value of the flag where the “something” happened the first time in our code, and our if-condition checks the value of the bool.
If necessary, make a sketch of your idea and define the “rules” of your game exactly. Then the implementation will become much easier. In programming, we rarely write a complete solution from scratch. We usually break the problem down into subproblems and solve the subproblems. Not getting the perfect result at the first onset is fine as long as you are making a step forward to your goal.