previous

page 7

next
implementing the computer's move
function square_clicked(el) {
do_x(el.id);
if (check()) {
show_result();
} else {
setTimeout(computer,2000);
}
}

Notice the 2 second delay so the computer appears to be "thinking".
It's this that led me to include the show_result function in the computer function otherwise it all gets a bit tricky.

We still have to ensure the player can't click while the computer is thinking. We'll do that by using a global g_player which must be true for the player's click to be accepted.

globals.js
var g_squares=[];
var g_result;
var g_player;
setup function
function setup() {
for (var i=0;i<9;i++) g_squares[i]=0;
g_player=true;
}
previous
next