Tic tac toe code URGENT

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
maxl123
Posts: 7
Joined: Mon Dec 14, 2015 8:06 pm
Occupation: Student

Tic tac toe code URGENT

Post by maxl123 »

I dont know whats wrong my code, somedy please help me quickly! randomMove calls upon

Code: Select all

	
Last edited by maxl123 on Wed Dec 16, 2015 8:29 am, edited 1 time in total.
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Tic tac toe code URGENT

Post by HowardE »

I see a few things I'll point out, but what is it doing wrong? Can you describe how it fails?
maxl123
Posts: 7
Joined: Mon Dec 14, 2015 8:06 pm
Occupation: Student

Re: Tic tac toe code URGENT

Post by maxl123 »

The computer doesn't move at all, the user can place an x but nothing else will happen.
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Tic tac toe code URGENT

Post by HowardE »

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.
maxl123
Posts: 7
Joined: Mon Dec 14, 2015 8:06 pm
Occupation: Student

Re: Tic tac toe code URGENT

Post by maxl123 »

The checkwin function executes this

Code: Select all


Last edited by maxl123 on Wed Dec 16, 2015 8:29 am, edited 1 time in total.
maxl123
Posts: 7
Joined: Mon Dec 14, 2015 8:06 pm
Occupation: Student

Re: Tic tac toe code URGENT

Post by maxl123 »

I'm going to post the whole program later today, I don't have access to my computer right now.
maxl123
Posts: 7
Joined: Mon Dec 14, 2015 8:06 pm
Occupation: Student

Re: Tic tac toe code URGENT

Post by maxl123 »

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.

Code: Select all


Last edited by maxl123 on Wed Dec 16, 2015 8:30 am, edited 1 time in total.
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Tic tac toe code URGENT

Post by HowardE »

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.
maxl123
Posts: 7
Joined: Mon Dec 14, 2015 8:06 pm
Occupation: Student

Re: Tic tac toe code URGENT

Post by maxl123 »

*Facepalm*
I haven't checked it yet but I'm sure that was the problem
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Tic tac toe code URGENT

Post by HowardE »

Bummer, eh? :) Yes, sometimes it's that simple.

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.

Howard
maxl123
Posts: 7
Joined: Mon Dec 14, 2015 8:06 pm
Occupation: Student

Re: Tic tac toe code URGENT

Post by maxl123 »

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. :D
HowardE
Posts: 496
Joined: Thu Nov 20, 2014 1:35 pm
Occupation: Science Buddies content developer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Re: Tic tac toe code URGENT

Post by HowardE »

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. :)

Howard
Locked

Return to “Grades 6-8: Math and Computer Science”