javascript - Stop overwriting in Tic Tac Toe -
this in regards how create turns in tic tac toe game? wanted add option box should not overwritten once clicked. used code below , have gone through , through material online several times can't seem figure out answer. it's making boxes x instead of x , o , x. unfortunately, working on web based editor doesn't support jquery , can't edit html either.
var turn = "x"; function next() { turn = turn === "x" ? "o" : "x"; } function play() { if (this.innerhtml === "") { this.innerhtml == turn; next();} } function click() { if (this.id == "cell1") { document.getelementbyid("cell1").innerhtml= turn; play(); } else if (this.id == "cell2") { document.getelementbyid("cell2").innerhtml = turn; play(); } else if (this.id == "cell3") { document.getelementbyid("cell3").innerhtml = turn; play(); } else if (this.id == "cell4") { document.getelementbyid("cell4").innerhtml = turn; play(); } else if (this.id == "cell5") { document.getelementbyid("cell5").innerhtml = turn; play(); } else if (this.id == "cell6") { document.getelementbyid("cell6").innerhtml = turn; play(); } else if (this.id == "cell7") { document.getelementbyid("cell7").innerhtml = turn; play(); } else if (this.id == "cell8") { document.getelementbyid("cell8").innerhtml = turn; play(); } else if (this.id == "cell9") { document.getelementbyid("cell9").innerhtml =turn; play(); } } document.getelementbyid("cell1").onclick = click; document.getelementbyid("cell2").onclick = click; document.getelementbyid("cell3").onclick = click; document.getelementbyid("cell4").onclick = click; document.getelementbyid("cell5").onclick = click; document.getelementbyid("cell6").onclick = click; document.getelementbyid("cell7").onclick = click; document.getelementbyid("cell8").onclick = click; document.getelementbyid("cell9").onclick = click;
i know have been told not repeat code new @ this. maybe few months of practice make me confident enough - find clearer right now.
p.s: working fine before tried add function of stopping overwriting of cells. code before was:
var nextturn = 'x'; function changeturn(){ if(nextturn == 'x'){ nextturn = 'o'; } else { nextturn = 'x'; } }
for new option have changed nextturn turn , changeturn next - user earlier post.
i think meant this.innerhtml == turn;
this.innerhtml = turn;
the first form comparison whereas second assignment
Comments
Post a Comment