Your real problem is that " various functions can change the value of this variable at any time ". This is the real problem you have to solve.
Once you have resolved the issue of changing the variable in several possible places, you apply the Observer project pattern .
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 should swap everywhere you have this:
estado = "bot";
So:
Turno.vezDeBot();
And also change this:
estado = "player";
So:
Turno.vezDeBot();
And finally change that:
if (estado == "bot") {
So:
if (Turno.getTipo() === "bot") {
When you want something to happen as soon as it is the bot:
Turno.adicionarListener(function() {
if (Turno.getTipo() !== "bot") return;
// Fazer algo acontecer assim que o bot assumir a vez.
});