Page 1 of 1

Help with Javascript tic tac toe

Posted: Tue Dec 26, 2006 2:21 pm
by thom
I have gotten the board to work--including adding the language for the check win function. However, I can't get the check win function to work. I have made the necessary modifications to the squareclicked function (I think), but my board will still not register when a game is ended--and/or who has won (or tied). I have tried to rearrange the script to make it work, but it still doesn't seem to do it.

Please help! Any tips that you can give would be greatly appreciated.

Thank you very much.

Re: Help with Javascript tic tac toe

Posted: Tue Dec 26, 2006 2:54 pm
by deleted-2574
Hi thom!

I don't know javascript, so I can't comment directly on your code.

The game has ended if nine turns have been taken or someone has three in a row.

The three in row can come in one of eight was vertically (3), horizontally (3), or diagonally (2).

Think I got that right right. Does that help?

Posted: Tue Dec 26, 2006 3:11 pm
by thom
Dave:

Thanks for your response.

I already understood the 8 ways that a game could be won and how it could be tied, and had tried to write the script as per your site's instructions (http://www.sciencebuddies.com/mentoring ... ?from=Home).

However, my issue is with how to write the javascript code to make it recognize and execute the game end and check win functions.


This is what I have been able to make it do:
1) Display the board.
2) Display the "X" and "O" markers
3) Alternate turns, and show that in the status bar.
4) Indicate when a square is clicked for a second time, and is no longer available.
5) Reset the board for a new game.

However, what I haven't been able to do is:
1) Get the program to recognize the end of the game.
2) Get the program to display the winner and/or indicate a tie.

Any other tips you have would be appreciated.

Thanks.

Re: Help with Javascript tic tac toe

Posted: Tue Dec 26, 2006 10:06 pm
by deleted-2574
thom:

The javascript at the URL you mentioned :

http://www.sciencebuddies.com/mentoring ... ?from=Home

has a solution. It sounds like you're using this program.

It now becomes an issue of debugging your program. I think that's easiest done by inputting select board states into your program, and seeing if you get the right output (i.e., WON/LOST, GAME OVER, etc.

I would also encourage looking at the javascript tutorial mentioned in the bibliography.

Posted: Wed Dec 27, 2006 7:57 am
by thom
Dave-
Thanks for the tips. I had already gone through the tutorial before I even started this, and didn't see anything refering to this problem. Is there a specific chapter that you are refering to, or is it just the tutorial in general? Also, what do you mean by "Input select board states"? If you mean playing the game so that 'X' wins, or they tie, or something like that, thanks, but we have been trying that for a while.



Thanks Again!! :D :D

HIP HIP HOORAY!!!!!

Posted: Wed Dec 27, 2006 8:29 am
by thom
:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D
YAAAAAYYYYY!!!!!
I finally got it to recognize the end of a game!!!!
:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D
Now I am working on making it recognize ties. The work is going well.
:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D
THANKS TO EVERYONE WHO HELPED!!!
:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D

Posted: Wed Dec 27, 2006 10:39 am
by thom
I can't get the program to register a tie!!!
Here is my code, so if you know Javascript, please take a look and tell me anything that might be wrong.

Code: Select all

<HTML>
<HEAD>
<SCRIPT type=TEXT/JAVASCRIPT>
 var xTurn = true;
 var gameOver = false;
 var numMoves = 0;
function squareclicked(square)
// squareclicked is a function that is called whenever a button is clicked.
{
if(gameOver)
{
   alert("The game is already over.");
   return;
 }
 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 winner = checkWin();
 if(!winner)
{
   //check to see if there is a tie
   if(numMoves == 9)
      status.innerHTML = 'Tie Game!';
 }
 else
   gameOver = true;
}
</SCRIPT>
<SCRIPT type=TEXT/JAVASCRIPT>
function newgame()
{ 
 numMoves = 0;
 gameOver = false;
   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 = ' ';
      }
   }
}
</SCRIPT>

<SCRIPT type=TEXT/JAVASCRIPT>
function checkWin()
{
 var gameOver = false;
 var numMoves = 0;
 var status = document.getElementById('status');
 var val0;
 var val1;
 var val2;
 // check columns
 for(var y = 0; y < 3; y++)
 {
   val0 = document.getElementById('0_'+y).value;
   val1 = document.getElementById('1_'+y).value;
   val2 = document.getElementById('2_'+y).value;
   if(val0 == 'X' && val1 == 'X' && val2 == 'X')
   {
      status.innerHTML = "X WINS!";
      return true;
   }
   else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
   {
      status.innerHTML = "O WINS!";
      return true;
   }
 }
 // check rows
 for(var x = 0; x < 3; x++)
 {
   val0 = document.getElementById(x + '_0').value;
   val1 = document.getElementById(x + '_1').value;
   val2 = document.getElementById(x + '_2').value;
   if(val0 == 'X' && val1 == 'X' && val2 == 'X')
   {
      status.innerHTML = "X WINS!";
      return true;
   }
   else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
   {
      status.innerHTML = "O WINS!";
      return true;
   }
 }
 // check top left to lower right diagonal
 val0 = document.getElementById('0_0').value;
 val1 = document.getElementById('1_1').value;
 val2 = document.getElementById('2_2').value;
 if(val0 == 'X' && val1 == 'X' && val2 == 'X')
 {
   status.innerHTML = "X WINS!";
   return true;
 }
 else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
 {
   status.innerHTML = "O WINS!";
   return true;
 }
 // check lower left to top right diagonal
 val0 = document.getElementById('2_0').value;
 val1 = document.getElementById('1_1').value;
 val2 = document.getElementById('0_2').value;
 if(val0 == 'X' && val1 == 'X' && val2 == 'X')
 {
   status.innerHTML = "X WINS!";
 return true;
 }
 else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
 {
   status.innerHTML = "O WINS!";
   return true;
 }
 // no winner yet  return false;
}
</SCRIPT>
</HEAD>
<BODY>
<INPUT id=0_0 onclick=squareclicked(this); type=button value=" "> <INPUT id=1_0 onclick=squareclicked

(this); type=button value=" "> <INPUT id=2_0 onclick=squareclicked(this); type=button value=" "> 

<BR><INPUT id=0_1 onclick=squareclicked(this); type=button value=" "> <INPUT id=1_1 

onclick=squareclicked(this); type=button value=" "> <INPUT id=2_1 onclick=squareclicked(this); 

type=button value=" "> <BR><INPUT id=0_2 onclick=squareclicked(this); type=button value=" "> <INPUT 

id=1_2 onclick=squareclicked(this); type=button value=" "> <INPUT id=2_2 onclick=squareclicked(this); 

type=button value=" "> 
<P id=status>X's turn</P><INPUT id=NEWGAME onclick=newgame(); type=button value="New Game"> 
</BODY>
</HTML>
Thanks!

Re: Help with Javascript tic tac toe

Posted: Wed Dec 27, 2006 1:44 pm
by deleted-2574
thom:

I don't know javascript, so I can't comment in full. Based on the languages I know, right after:
status.HTML = 'Tie Game';
and before the "}"
there needs to be a:
gameOver = true;

In other words, once the number of moves is 9; the is over and a tie

Posted: Wed Dec 27, 2006 5:14 pm
by thom
Dave:
Thanks, but I tried it and it actually messed up my program.
I really do appreciate all the help that you have given me.


Thanks,

Posted: Wed Dec 27, 2006 6:54 pm
by deleted-2574
thom:

Sorry the progam hasn't worked for you. As I mentioned previously, I don't know javascript, so I can't give you a definitive answer.

The thought is, however, to mark the game over and a tie when 9 moves have been made AND there is no winner. (A winner would take priority.)

Posted: Thu Dec 28, 2006 8:57 am
by thom
Okay, I've gotten the "tie game" to work, and it registers the end of the game. However, when "new game" button is clicked, it will reset the board, but does not reset the status bar, i.e., if "x" won the last game, the status bar still says "X wins", even after the "new game" button is clicked, instead of saying "X's turn", which is the default for starting a new game. I've tried going back through the code lots and lots of times, but haven't figured out exactly what needs to be changed to make this happen. Any ideas/tips? I've included my code so far below, in case that helps you at all. Thank you VERY much.

Code: Select all

<HTML>
<HEAD>
<SCRIPT type=TEXT/JAVASCRIPT>
 var xTurn = true;
 var gameOver = false;
 var numMoves = 0;
function squareclicked(square)
// squareclicked is a function that is called whenever a button is clicked.
{
if(gameOver)
{
   alert("The game is already over.");
   return;
 }
 var status = document.getElementById('status');
 var value = square.value;
 if(value != 'X' && value != 'O')
{
if(xTurn)
   {
      numMoves++;
      square.value = 'X';
      xTurn = false;
      status.innerHTML = 'O\'s turn';
   }
   else
      {
         numMoves++;
         square.value = 'O';
         xTurn = true;
         status.innerHTML = 'X\'s turn';
      }
 }
   else
     alert('That square has already been played.');

var winner = checkWin();
 if(!winner)
 {
   //check to see if there is a tie
   if(numMoves == 9)
   status.innerHTML = 'Tie Game!';
 }
 else
   gameOver = true;
}
</SCRIPT>
<SCRIPT type=TEXT/JAVASCRIPT>
function newgame()
{
   var winner = checkWin(); 
   numMoves = 0;
   gameOver = false;
   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 = ' ';
      }
   }
}
</SCRIPT>
<SCRIPT type=TEXT/JAVASCRIPT>
function checkWin()
{
 var gameOver = false;
 var numMoves = 0;
 var status = document.getElementById('status');
 var val0;
 var val1;
 var val2;
 // check columns
 for(var y = 0; y < 3; y++)
 {
   val0 = document.getElementById('0_'+y).value;
   val1 = document.getElementById('1_'+y).value;
   val2 = document.getElementById('2_'+y).value;
   if(val0 == 'X' && val1 == 'X' && val2 == 'X')
   {
      status.innerHTML = "X WINS!";
      return true;
   }
   else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
   {
      status.innerHTML = "O WINS!";
      return true;
   }
 }
 // check rows
 for(var x = 0; x < 3; x++)
 {
   val0 = document.getElementById(x + '_0').value;
   val1 = document.getElementById(x + '_1').value;
   val2 = document.getElementById(x + '_2').value;
   if(val0 == 'X' && val1 == 'X' && val2 == 'X')
   {
      status.innerHTML = "X WINS!";
      return true;
   }
   else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
   {
      status.innerHTML = "O WINS!";
      return true;
   }
 }
 // check top left to lower right diagonal
 val0 = document.getElementById('0_0').value;
 val1 = document.getElementById('1_1').value;
 val2 = document.getElementById('2_2').value;
 if(val0 == 'X' && val1 == 'X' && val2 == 'X')
 {
   status.innerHTML = "X WINS!";
   return true;
 }
 else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
 {
   status.innerHTML = "O WINS!";
   return true;
 }
 // check lower left to top right diagonal
 val0 = document.getElementById('2_0').value;
 val1 = document.getElementById('1_1').value;
 val2 = document.getElementById('0_2').value;
 if(val0 == 'X' && val1 == 'X' && val2 == 'X')
 {
   status.innerHTML = "X WINS!";
 return true;
 }
 else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
 {
   status.innerHTML = "O WINS!";
   return true;
 }
 // no winner yet  return false;
}
</SCRIPT>
</HEAD>
<BODY bgColor="hotpink">
<INPUT id=0_0 onclick=squareclicked(this); type=button value=" "> <INPUT id=1_0 onclick=squareclicked(this); type=button 

value=" "> <INPUT id=2_0 onclick=squareclicked(this); type=button value=" "> <BR><INPUT id=0_1 onclick=squareclicked(this); 

type=button value=" "> <INPUT id=1_1 onclick=squareclicked(this); type=button value=" "> <INPUT id=2_1 onclick=squareclicked

(this); type=button value=" "> <BR><INPUT id=0_2 onclick=squareclicked(this); type=button value=" "> <INPUT id=1_2 

onclick=squareclicked(this); type=button value=" "> <INPUT id=2_2 onclick=squareclicked(this); type=button value=" "> 
<P id=status>X's turn</P>
<INPUT id=NEWGAME onclick=newgame(); type=button value="New Game"> 
</BODY></HTML>

Posted: Thu Dec 28, 2006 9:36 am
by thom
SOMEBODY PLEASE HELP!!! PPPPPPPPPLLLLLLLLLLLLLLEEEEEEEEEEEAAAAAAAAAAAAAASSSSSSSSSSSSSEEEEEEEEEEEEE!!!!!!!!!!!!!!!!!!!!!!!!!!!

Re: Help with Javascript tic tac toe

Posted: Fri Dec 29, 2006 10:03 am
by deleted-2574
thom:

I think there's a missing statement in the program: "gameOver = true;"

Please see below:

   if(numMoves == 9) {
status.innerHTML = 'Tie Game!';
gameOver = true;
}

Please let me know if this works. It would indicate an error in the posted program.

Like I said, I don't know javascript, so please adjust to be valid javascript code.

Thanks.

Re: Help with Javascript tic tac toe

Posted: Fri Dec 29, 2006 10:46 am
by deleted-2574
thom:

I think there's a missing statement in the program: "gameOver = true;"

Please see below:

   if(numMoves == 9) {
status.innerHTML = 'Tie Game!';
gameOver = true;
}

Please let me know if this works. It would indicate an error in the posted program.

Like I said, I don't know javascript, so please adjust to be valid javascript code.

Thanks.

Posted: Sat Dec 30, 2006 8:04 am
by thom
Dave-
Thanks, and I really appreciate you helping me so much. Unfortunately, by the time that I saw your latest post, one of my neighbors who programs had helped me fix my problem. Also, here is the corrected code, with all the errors that were on the site fixed:

Code: Select all

<HTML>
<HEAD>
<SCRIPT TYPE=TEXT/JAVASCRIPT>
 var xTurn = true;
 var gameOver = false;
 var numMoves = 0;
 var status = document.getElementById('status');

function squareclicked(square)
// squareclicked is a function that is called whenever a button is clicked.
{
if(gameOver)
{
   alert("The game is already over.");
   return;
 }
 var status = document.getElementById('status');
 var value = square.value;
 if(value != 'X' && value != 'O')
{
if(xTurn)
   {
      numMoves++;
      square.value = 'X';
      xTurn = false;
      status.innerHTML = 'O\'s turn';
   }
   else
      {
         numMoves++;
         square.value = 'O';
         xTurn = true;
         status.innerHTML = 'X\'s turn';
      }
 }
   else
     alert('That square has already been played.');

var winner = checkWin();
 if(!winner)
 {
   //check to see if there is a tie
   if(numMoves == 9)
   status.innerHTML = 'Tie Game!';
 }
 else
   gameOver = true;
}
</SCRIPT>
<SCRIPT type=TEXT/JAVASCRIPT>
function newgame()
{
   xTurn = true;
   gameOver = false;
   numMoves = 0;
   status = document.getElementById('status');
   status.innerHTML = 'X\'s turn';
   for(var x = 0; x < 3; x++)
   {
      for(var y = 0; y < 3; y++)
      {
         document.getElementById(x + '_' + y).value = ' ';
      }
   }
}
</SCRIPT>
<SCRIPT type=TEXT/JAVASCRIPT>
function checkWin()
{
 var gameOver = false;
 var numMoves = 0;
 var status = document.getElementById('status');
 var val0;
 var val1;
 var val2;
 // check columns
 for(var y = 0; y < 3; y++)
 {
   val0 = document.getElementById('0_'+y).value;
   val1 = document.getElementById('1_'+y).value;
   val2 = document.getElementById('2_'+y).value;
   if(val0 == 'X' && val1 == 'X' && val2 == 'X')
   {
      status.innerHTML = "X WINS!";
      return true;
   }
   else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
   {
      status.innerHTML = "O WINS!";
      return true;
   }
 }
 // check rows
 for(var x = 0; x < 3; x++)
 {
   val0 = document.getElementById(x + '_0').value;
   val1 = document.getElementById(x + '_1').value;
   val2 = document.getElementById(x + '_2').value;
   if(val0 == 'X' && val1 == 'X' && val2 == 'X')
   {
      status.innerHTML = "X WINS!";
      return true;
   }
   else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
   {
      status.innerHTML = "O WINS!";
      return true;
   }
 }
 // check top left to lower right diagonal
 val0 = document.getElementById('0_0').value;
 val1 = document.getElementById('1_1').value;
 val2 = document.getElementById('2_2').value;
 if(val0 == 'X' && val1 == 'X' && val2 == 'X')
 {
   status.innerHTML = "X WINS!";
   return true;
 }
 else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
 {
   status.innerHTML = "O WINS!";
   return true;
 }
 // check lower left to top right diagonal
 val0 = document.getElementById('2_0').value;
 val1 = document.getElementById('1_1').value;
 val2 = document.getElementById('0_2').value;
 if(val0 == 'X' && val1 == 'X' && val2 == 'X')
 {
   status.innerHTML = "X WINS!";
 return true;
 }
 else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
 {
   status.innerHTML = "O WINS!";
   return true;
 }
 // no winner yet  return false;
}
</SCRIPT>
</HEAD>
<BODY>
<INPUT id=0_0 onclick=squareclicked(this); type=button value=" "> <INPUT id=1_0 onclick=squareclicked(this); type=button value=" "> <INPUT id=2_0 onclick=squareclicked(this); type=button value=" "> <BR><INPUT id=0_1 onclick=squareclicked(this); type=button value=" "> <INPUT id=1_1 onclick=squareclicked(this); type=button value=" "> <INPUT id=2_1 onclick=squareclicked(this); type=button value=" "> <BR><INPUT id=0_2 onclick=squareclicked(this); type=button value=" "> <INPUT id=1_2 onclick=squareclicked(this); type=button value=" "> <INPUT id=2_2 onclick=squareclicked(this); type=button value=" "> 
<P id=status>X's turn</P>
<INPUT id=NEWGAME onclick=newgame(); type=button value="New Game"> 
</BODY></HTML>
Thanks again,

Posted: Sat Dec 30, 2006 8:56 am
by thom
By the way: The code that I have posted above has absolutely NO AI in it, but anybody who wants to can use it to help with programming their project. :D :D :D

Posted: Sat Dec 30, 2006 11:22 am
by thom
Okay, I've hit a minor block. Could someone please tell me how I make the computer select a square? I have been able to make it check all of the squares for empty ones, but can't make it actually select one.


Thanks,

Re: Help with Javascript tic tac toe

Posted: Sat Dec 30, 2006 4:16 pm
by deleted-2574
thom-

Congratulations on a job well done! Congratulations also on your stamina.

What specific change did you need you need to make to the program that's posted online? I'll need this information to report the suspected bug to the science buddies staff.

Posted: Sat Dec 30, 2006 6:45 pm
by thom
As far as I can tell, the only changes are that a global variable (var status = document.getElementById('status');) needs to be added, and a line in the newgame() function (status = document.getElementById('status');) needs to be added. I'm in a hurry right now though, so I might have missed something.


Also, again, if anybody can help me with the AI, please do.


Thanks,

Re: Help with Javascript tic tac toe

Posted: Mon Jan 01, 2007 11:50 am
by deleted-2574
thom-

Thanks. I'll pass the solution and the change description along to the science buddies staff.

Happy New Year!

Posted: Mon Jan 01, 2007 3:04 pm
by thom
CAN ANYBODY HELP!!! I CAN'T GET THE AI TO WORK!!!!!!! :shock :shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock:
:? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :?
Anyway, I have been able to make the program cycle through all the squares, but can't make it select a random empty one!!
:? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :? :?

PLEASE HELP IF YOU CAN!!!!!
:shock: :shock: :shock: :shock: :shock: :shock: :shock: :shock:

Re: Help with Javascript tic tac toe

Posted: Mon Jan 01, 2007 8:02 pm
by deleted-2574
thom-

Just a hunch. Did you try the code exactly as posted on the site? I can't believe science buddies would post code with errors.

Posted: Tue Jan 02, 2007 11:13 am
by pgordon
I wrote some AI strategy for this project on another thread.


Writing the AI that gets the computer to make a good move will be the hardest part. You will need to write code that will do 2 major things:

1) Create all possible board combinations that the computer can create on the next move. Since tic-tac-toe is a rather simple game you have the advantage of not having to create a perfect system. Computers are fast enough that you can compare all possible board combinations without bogging the computer down. The number of possible boards you would need to compare would be at most 9 (the very first move) and at least 1 (the very last move).

2) Rank these possible board combinations based on a value system to determine which move is the best move. This wikipedia page has a good ranking system:

1. Complete three in a row (WIN).
2. Block their opponent from completing three in a row.
3. Threaten a win with two possible completions in two rows.
4. Avoid a configuration in which the opponent can force the win.
5. Threaten a win with a possible completion (two in a row).
6. Prevent the opponent from getting two in a row.
7. Play in the middle box.
8. Play in a corner box.
9. Play on a side box.

I would rank a board in which the next move wins as 10,000, a board in which the next move loses as -1, and rank the rest of the boards using values you give to the list above. After you have ranked all the boards and found the best move (highest value), you need to make the computer take that move. You can then toss away the temporary boards you created since we got that information we wanted for that round (the best move).

Posted: Tue Jan 02, 2007 11:40 am
by scibudadmin
Dear Thom and Dan,

Thanks for bringing the problem with the TicTacToe code to my attention. There was one omission in the code posted on the site, and Thom found it (nice going, Thom!). The newgame() function did not declare the 'status' variable. I've added the necessary line in the code on the site.

Thom: there is no need for a global status variable, as long as a local 'status' variable is declared in each function that uses it (i.e., squareClicked, newGame and checkWin).

Below is a test HTML page that I put together using the code snippets from the Science Buddies site. It seems to work just fine.

Good luck with your project, Thom! Seems like you're off to a good start!
Andrew Olson

Senior Scientist
Science Buddies


<HTML>
<HEAD>

<!-- test of TicTacToe code posted on site -->
<TITLE>TicTacToe Code Test</TITLE>

<SCRIPT TYPE="TEXT/JAVASCRIPT">
var xTurn = true;
var gameOver = false;
var numMoves = 0;

function squareclicked(square)
// squareclicked is a function that is called whenever a button is clicked.
{
var value = square.value;
var status = document.getElementById('status');

if(gameOver)
{
alert("The game is already over.");
return;
}

if(value != 'X' && value != 'O')
{
if(xTurn)
{
numMoves++;
square.value = 'X';
xTurn = false;
status.innerHTML = 'O\'s turn';
}
else
{
numMoves++;
square.value = 'O';
xTurn = true;
status.innerHTML = 'X\'s turn';
}
}
else
alert('That square has already been played.');

var winner = checkWin();
if(!winner)
{
//check to see if there is a tie
if(numMoves == 9)
status.innerHTML = 'Tie Game!';
}
else
gameOver = true;

}

function newgame()
{
var status = document.getElementById('status');

xTurn = true;
status.innerHTML = 'X\'s turn';
gameOver = false;
numMoves = 0;

for(var x = 0; x < 3; x++)
{
for(var y = 0; y < 3; y++)
{
document.getElementById(x + '_' + y).value = ' ';
}
}
}

function checkWin()
{
var status = document.getElementById('status');
var val0;
var val1;
var val2;

// check columns
for(var y = 0; y < 3; y++)
{
val0 = document.getElementById('0_'+y).value;
val1 = document.getElementById('1_'+y).value;
val2 = document.getElementById('2_'+y).value;
if(val0 == 'X' && val1 == 'X' && val2 == 'X')
{
status.innerHTML = "X WINS!";
return true;
}
else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
{
status.innerHTML = "O WINS!";
return true;
}
}

// check rows
for(var x = 0; x < 3; x++)
{
val0 = document.getElementById(x + '_0').value;
val1 = document.getElementById(x + '_1').value;
val2 = document.getElementById(x + '_2').value;
if(val0 == 'X' && val1 == 'X' && val2 == 'X')
{
status.innerHTML = "X WINS!";
return true;
}
else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
{
status.innerHTML = "O WINS!";
return true;
}
}

// check top left to lower right diagonal
val0 = document.getElementById('0_0').value;
val1 = document.getElementById('1_1').value;
val2 = document.getElementById('2_2').value;
if(val0 == 'X' && val1 == 'X' && val2 == 'X')
{
status.innerHTML = "X WINS!";
return true;
}
else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
{
status.innerHTML = "O WINS!";
return true;
}

// check lower left to top right diagonal
val0 = document.getElementById('2_0').value;
val1 = document.getElementById('1_1').value;
val2 = document.getElementById('0_2').value;
if(val0 == 'X' && val1 == 'X' && val2 == 'X')
{
status.innerHTML = "X WINS!";
return true;
}
else if(val0 == 'O' && val1 == 'O' && val2 == 'O')
{
status.innerHTML = "O WINS!";
return true;
}

// no winner yet return false;
}

</SCRIPT>

</HEAD>

<BODY>

<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>
<BR>
<INPUT TYPE="BUTTON" ID="NEWGAME" VALUE="New Game" ONCLICK="newgame();">

<P ID="status">X's turn</P>

</BODY>
</HTML>

Posted: Sat Jan 06, 2007 2:08 pm
by thom
Thanks For the thanks and encouragement!!
one of my neighbor's sons has been helping me get the AI into the game and we are almost done!! But anyway, glad I could help, and wish me luck with getting the AI in!