Your real problem is that "multiple functions can change the value of that variable at any time". That’s the real problem you have to solve.
After solving the problem of the variable being changed in several possible locations, you apply the Observer design standard.
The solution would be to create a code something like this:
var Turno = (function() {
var estado = {};
var tipo = "player";
var listeners = [];
estado.adicionarListener = function(listener) {
listeners.push(listener);
};
estado.getTipo = function() {
return tipo;
};
estado.vezDePlayer = function() {
tipo = "player";
for (var i in listeners) {
listeners[i]();
}
};
estado.vezDeBot = function() {
tipo = "bot";
for (var i in listeners) {
listeners[i]();
}
};
return estado;
)();
And then you must trade all the places that have it:
estado = "bot";
That’s why:
Turno.vezDeBot();
And also change that:
estado = "player";
That’s why:
Turno.vezDeBot();
And finally change that:
if (estado == "bot") {
That’s why:
if (Turno.getTipo() === "bot") {
When you want something to happen as soon as it’s the bot’s turn:
Turno.adicionarListener(function() {
if (Turno.getTipo() !== "bot") return;
// Fazer algo acontecer assim que o bot assumir a vez.
});
Give us a scenario where you need to use this, so it’s easier to give us an answer that fits what you want.
– Isac
That’s a XY problem. The solution you want to apply is not ideal for your problem. Try to be more specific and detailed, preferably by placing a code block where you want to use it.
– Isac
wish there was something like when (status == "bot") knife x
– Morais Vilson