I have an object representing a player and let us say 2 players
//create a player object
function player(name,state,score) {
this.name = name;
this.state = state;
this.score = score;
}
var players = [
new player('player1',true,0),
new player('player2',false,0)
];
so I want the players to make a move one after another. For that purpose I use true or false state. So when true, it counts (I count the right answers of player and display them) for player 1 and vs.
what I have for now is
$("#check").click(function(){
if(localCorrect == 2){
//counting number of correct cards and assigning it to the player
var i=0;
if (players[i].state == true){
var localscore = players[i].score;
localscore++;
score[i] = localscore;
//display the score
$("span#"+players[i].name).html(score[i]);
players[i].state = false;
i++;
players[i].state = true;
//if(i=2)
//{i=0}
}
}else{
//some other thing
}
});
I have a feedle with this http://jsfiddle.net/ycycghwq/
So I have 2 question:
I think I'm wrong with the declaration of var i (it represents an index of a player, I have 2 of them so it should count to 1 and then go back to 0 at this point. But I want to make it not dependant on a particular number, so that it could be 4 or 10 players.
I need to switch the state of the current player(i). player1 needs to be true and when switch to false. then the state of player 2 needs to be swithced from false to true. (So if I have more that 2 players I need to switch the state of the following one to true and the to false)
My brain of the beginner is boiling. Could someone please help?! Thanks in advance!