Artificial intelligence tic tac toe help!

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

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
JoshCocomise
Posts: 2
Joined: Wed Oct 27, 2010 3:33 pm
Occupation: Student: 8th grade
Project Question: Artificial intelligence : teaching the computer to play tic tac toe
Project Due Date: 10/15/2010
Project Status: I am conducting my experiment

Artificial intelligence tic tac toe help!

Post by JoshCocomise »

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>
WJClancey
Former Expert
Posts: 64
Joined: Fri Sep 02, 2005 6:43 pm
Occupation: NASA Computer & Cognitive Scientist
Project Question: n/a
Project Due Date: n/a
Project Status: Not applicable

Re: Artificial intelligence tic tac toe help!

Post by WJClancey »

Hi Josh,

I'm an AI professional at NASA with a PhD in computer science and have been asked to look at your question.

I am impressed by what you have so far. I don't know if you've made more progress since you posted your question, but I'll tell you what I see in case it might still be useful. I have never programmed a script in HTML, but I can see a few syntax issues in the code you posted.

First, you are almost certainly correct that you shouldn't have two definitions with the same function name. So I would start by deleting the short version, which looks like this --

function squareclicked(square)
{
square.value = 'X';
}

Next, you might want to use indentation in the definition of squareclicked so you can be sure you have matched up the curly brackets.

I notice that in this squareclicked where the first alert appears, the function seems to be missing a curly bracket to mark where the definition ends.

    else
      alert('That square has already been played.');
}

This bracket that completes the If/then/else, but then another fragment begins with a variable declaration. So you need another "}" to close off the squareclicked definition and then delete the fragment that comes after it (there's a variable declaration but no function header).

So in summary, taking it from the top -- after "var xTurn = true;" you'd have the function definitions for newgame and for squareclicked, and that would be the end of the script you've written here (i.e., </script> would appear after squareclicked's definition).

Please post another question if you're up to the part of checking for the end of the game and tell me how you are thinking of doing that.

Bill
WJClancey
Former Expert
Posts: 64
Joined: Fri Sep 02, 2005 6:43 pm
Occupation: NASA Computer & Cognitive Scientist
Project Question: n/a
Project Due Date: n/a
Project Status: Not applicable

Re: Artificial intelligence tic tac toe help!

Post by WJClancey »

Hi Josh,

I'm new to this forum, and I now see after posting a response to you, there are several related posts about Tic Tac Toe from previous years. You might want to take at look at those for suggestions, too.

I'm guessing you are following the Science Buddies guide -- https://www.sciencebuddies.org/science- ... p014.shtml

Bill
JoshCocomise
Posts: 2
Joined: Wed Oct 27, 2010 3:33 pm
Occupation: Student: 8th grade
Project Question: Artificial intelligence : teaching the computer to play tic tac toe
Project Due Date: 10/15/2010
Project Status: I am conducting my experiment

Re: Artificial intelligence tic tac toe help!

Post by JoshCocomise »

Thanks i have gotten farther from when i last posted it. i got all the way through the making the whole 2 player part but the part where u make the computer responds i dont have time to do. my projects due tommorow but ive already did my project on- if starting first in tic tac toe effects ur chances of winning. so i dont have to worry about that. but if i was to attempt to make the AI part just for whatever reason. i am curious how to come up with some algorithm to make it do so. for the parts it helps you with i can do but i dont know how to just come up with somthing. thanks for everything!
Locked

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