javascript - Basic tic tac toe JS -
i want create simple tic-tac-toe game in using html table. able add "x" , "o" values onto html table changing variable either true or false. unable add function take turns. when user clicks on of table cell should start "x" "o". have tried nested loops (for loops & while) can't seem work.
here code:
function x() { this.innerhtml = "x"; } function o() { this.innerhtml = "o"; } function xdo() { (i = 1 ; <=9;i++) { document.getelementbyid("cell"+i).onclick = x; } } function odo() { (i = 1 ; <=9;i++) { document.getelementbyid("cell"+i).onclick = o; } } var turn = true; if(turn==true) { xdo(); } else if(turn==false) { odo();
you should add onclick
event handler cells set value of cell either x or o depending on global flag and invert flag (provided want game played on same computer).
like this:
function oncellclick() { if (turn) this.innerhtml = "x"; else this.innerhtml = "o"; turn = !turn; }
Comments
Post a Comment