# Thread: Problem with snippet of game code?

1. Registered User
Join Date
Jun 2014
Posts
9

## Problem with snippet of game code?

Hey all,

I have a code for a 1-player Tic Tac Toe, and the algorithm that I have for the CPU behavior is a little off. But I can't tell what the problem is. Could someone help me out and tell me what the problem is? I'm new to JS.

The part commented out is the one causing the problem, I know that much.

Code:
```		function CPUmove()
{
/* for (var z = 0; z < 8; z++) {
if (filled[winCombo[z][1]] == true && filled[winCombo[z][2]] == true
&& content[winCombo[z][1]] == content[winCombo[z][2]] && filled[content[winCombo[z][0]] == false)
return boxNumber = content[winCombo[z][0]];
if (filled[winCombo[z][0]] == true && filled[winCombo[z][2]] == true
&& content[winCombo[z][0]] == content[winCombo[z][2]] && filled[content[winCombo[z][1]] == false)
return boxNumber = content[winCombo[z][1]];
if (filled[winCombo[z][0]] == true && filled[winCombo[z][1]] == true
&& content[winCombo[z][0]] == content[winCombo[z][1]] && filled[content[winCombo[z][2]] == false)
return boxNumber = content[winCombo[z][2]];
} */

if (filled[1] == false)	return boxNumber = 1;
else if (filled[3] == false) return boxNumber = 3;
else if (filled[7] == false) return boxNumber = 7;
else if (filled[9] == false) return boxNumber = 9;
else if (filled[5] == false) return boxNumber = 5;
else return boxNumber = Math.floor((Math.random() * 9) + 1);
}```

2. You might get some ideas about how to fix the code here.
See: http://javascriptkit.com/script/script2/tactoe.shtml
or: http://ostermiller.org/calc/tictactoe.html
Last edited by JMRKER; 06-05-2014 at 10:15 PM.