The biggest immediate problem is that you have some code missing. After a move you have the code call a function named checkWin like this:
var winner = checkWin();
if(!winner)
{
...
There is no function by that name, so the code won't execute at all.
In your random square picker you pick a random number from 0 to 1 and break it up into 9 ranges - assigning one range to each square:
if(0 < p < 0.11)
{
var r = '0_0';
}
else if(0.11 < p < 0.22)
...
What happens if p is exactly 0.11? If the first condition is true the code won't ever look at the second condition, so you don't have to check (in the second case) for 0.11 < p. You know it is because the first condition wasn't satisfied. You also can't check that a number is in a range by saying (0 < p < 0.11). You would code this as ((0 < p) && (p < 0.11)).
I didn't see any other glaring errors that would stop your code from running but as I said, you didn't post the whole program so I can't test it.
Howard
UPDATE: code snippet removed 12/16/15
Last edited by HowardE on Wed Dec 16, 2015 7:06 am, edited 1 time in total.
Here is the whole program with your adjustments to the randomMove() function. Unfortunately, the same problem occurs.
My guess is that it has something to do with the if(value != 'X' && value != 'O') statement but i don't know how to fix it.
So here's one of those things about computer languages. Spelling and letter case matters.
In randomMove() you had:
function randomMove()
{
var p = math.random();
if(p < 0.11) /* There's no need to check against 0 */
and what you needed was:
function randomMove()
{
var p = Math.random(); /* The Math library must be referred to as 'Math' and not 'math' */
if(p < 0.11) /* There's no need to check against 0 */
The difference is that the word 'math' has to be in uppercase. The way you had it, the javascript interpreter couldn't find the random number generator. You may still have things to fix with it detecting the end of a game, but It looks nice otherwise. Make that change and if you still have problems, write back.
I did see it misbehave a bit when I tried it. Sometimes it didn't make a move at all and it didn't seem to recognize when a tie game was over. You still have some tweaks to do but it looks good.
When you have a chance, it would be nice if you could go back and edit the messages to remove the code that you wrote. Now that you have it working, I wouldn't want another student to use your work as their project.
Thank you very much, Howard. You helped be out quite a bit and i would be stuck with out you. I appreciate your help and hope that you can make other people as happy as you made me.
It was my pleasure to help you, maxl123. Good luck with your project. If you come up with a version that lets two people play over the internet, post a link and you can win a few games against me. I know your computer playing function was entirely random and it managed to win a few times when i was testing it.