Select any position then Switch Player in JavaScript -
i trying write function allows me select position on board switch players. had attempted write loop either nothing appeared or positions filled "x". believe need stop loop iterating once position selected , created loop addeventlister. (js newbie- thanks!)
function player1(){ for(var = 0; i<boardslots.length;i++) //iterate through positions if(boardslots[i].classlist !== "x" || "o"){ //check see if space occupied boardslots[i].classlist.add("x"); //add x player2(); //switch player 2 } } //player 2 selects o function player2(){ for(var = 0; i<boardslots.length;i++) if(boardslots[1].classlist !== "x" || "o"){ boardslots[1].classlist.add("o"); player1(); } } boardslots[i].addeventlistener("click",player1); //select position on board
var count = 0; function move1(){ if(count % 2 === 0 && !this.classlist.contains("x") && !this.classlist.contains("o")){ this.classlist.add("x"); count++; // this.removeeventlistener('click', move1); } else if(!count % 2 === 0) { this.classlist.add("o"); count++; } } // add event listener each position boardslots[0].addeventlistener("click",move1); boardslots[1].addeventlistener("click",move1); boardslots[2].addeventlistener("click",move1); boardslots[3].addeventlistener("click",move1); boardslots[4].addeventlistener("click",move1); boardslots[5].addeventlistener("click",move1); boardslots[6].addeventlistener("click",move1); boardslots[7].addeventlistener("click",move1); boardslots[8].addeventlistener("click",move1); boardslots[9].addeventlistener("click",move1); };
Comments
Post a Comment