Artificial intelligence tic tac toe help!
Posted: Wed Nov 03, 2010 4:24 pm
so for my science fair i am doing teaching the computer to play tic tac toe. i have done research and i know alot of stuff pretty well through trial and error. it seems like i get stuck at somthing and than i find it out later. so far when i bring up my code to firefox it works. i am towards the end of making the game part of it. i have been stuck at the (checking for the end of the game) section i pretty much have no idea what to put anywhere. i think me having two function squareclicked(square) s might be a problem. but pretty much what i am asking is, did i put somthing in wrong or in the wrong order. if you could help me get things back to order and correct it would be very helpful so i can know if things are right. heres my code. ps. so far everythings has been working like it should but im pretty sure im doing somthing wrong. im really stuck with this part of it
Code: Select all
<HTML>
<HEAD>
<title>
tic-tac-toe
</title>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
// Global Variables
var xTurn = true;
function squareclicked(square)
{
square.value = 'X';
}
function newgame()
{
var status = document.getElementById('status');
xTurn = true;
status.innerHTML = 'X\'s turn';
for(var x = 0; x < 3; x++)
{
for(var y = 0; y < 3; y++)
{
document.getElementById(x + '_' + y).value = ' ';
numMoves = 0;
gameOver = false;
}
}
}
function squareclicked(square)
{
var status = document.getElementById('status');
var value = square.value;
if(value != 'X' && value != 'O')
{
if(xTurn)
{
square.value = 'X';
xTurn = false;
status.innerHTML = 'O\'s turn';
}
else
{
square.value = 'O';
xTurn = true;
status.innerHTML = 'X\'s turn';
}
}
else
alert('That square has already been played.');
}
{
var value = square.value;
if(value != 'X' && value != 'O')
{
if(xTurn)
{
square.value = 'X';
xTurn = false;
}
else
{
square.value = 'O';
xTurn = true;
}
}
else
alert('That square has already been played.');
}
</script>
</HEAD>
<INPUT TYPE="BUTTON" ID="0_0" VALUE=" " onclick="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="1_0" VALUE=" " onclick="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="2_0" VALUE=" " onclick="squareclicked(this);">
<BR>
<INPUT TYPE="BUTTON" ID="0_1" VALUE=" " onclick="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="1_1" VALUE=" " onclick="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="2_1" VALUE=" " onclick="squareclicked(this);">
<BR>
<INPUT TYPE="BUTTON" ID="0_2" VALUE=" " onclick="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="1_2" VALUE=" " onclick="squareclicked(this);">
<INPUT TYPE="BUTTON" ID="2_2" VALUE=" " onclick="squareclicked(this);">
<BR>
</form>
<BR>
<INPUT TYPE="BUTTON" ID="NEWGAME" VALUE="New Game" ONCLICK="newgame();">
<P ID="status">X's turn</P>
</BODY>
</HTML>