That's where my program is currently. I just wanted to drop by and say thanks to science buddies because without your tutorial I wouldnt have a project! I was able to figure out a way to make the AI and now it's (hypothetically) unbeatable. Here's the code, welcome for reference for anybody else doing the project and thank you once again! (note that the pictures are not in there, I made them myself..)
<html>
<head>
Tic-Tac-Toe V.1.0 - finished 8:09 pm, 1/17/07 <br><br>
Note: This is the first working version of a one player tic-tac-toe game. It recognizes wins, losses and ties and records them. Moves are counted and recorded but resets once the new game button is clicked. It features a status bar where it says whose turn it is and who wins the game (or if it is a tie), and there is also a scorecard where it keeps track of wins, losses, ties and games played for the player. The opponent is the computer, and though it is hypothetical, I dont believe that it could be beaten.
<title>Tic-Tac-Toe V.1.0</title>
<script type="TEXT/JAVASCRIPT">
// Global Variables
var turnX = true;
var gameOver = false;
var moveCount = 0;
var gameCount = 0;
var tieCount = 0;
var winsX = 0;
var winsO = 0;
var AImove = false;
var sq1 = 3;
var sq2 = 2;
var sq3 = 3;
var sq4 = 2;
var sq5 = 4;
var sq6 = 2;
var sq7 = 3;
var sq8 = 2;
var sq9 = 3;
function clicked(square)
{
var status = document.getElementById('status');
var moveTicker = document.getElementById('moveTicker');
var value = square.value;
var move1 = document.getElementById('2_0').value;
if(gameOver)
{
AImove = false;
alert('The game is already over.')
return;
}
// starts new game
function newgame()
{
var tieDisplay = document.getElementById('tieDisplay')
var status = document.getElementById('status');
turnX = 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 = ' ';
moveCount = 0;
moveTicker.innerHTML = moveCount;
gameOver = false;
}
}
}
function checkWin()
{
//variables
var noGames = document.getElementById('gameCount');
var status = document.getElementById('status')
var checkXwin = document.getElementById('winsX')
var checkOwin = document.getElementById('winsO')
var val1;
var val2;
var val3;
Before, you declare victory, I encourage you to exhaustively test your program (go through all the cases), which can be done for tic-tac-toe. The test process could be part of your display.
Hey!! I need some help with that!!! Could you look at the following script and see where I went wrong? Thanks!
<html>
<head>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
var aimove = false;
var xTurn = true;
var gameOver = false;
var numMoves = 0;
var sq1 = 3;
var sq2 = 2;
var sq3 = 3;
var sq4 = 2;
var sq5 = 4;
var sq6 = 2;
var sq7 = 3;
var sq8 = 2;
var sq9 = 3;
function squareclicked(square)
//square clicked is a function that is called whenever a button is clicked.
{
var value = square.value;
var status = document.getElementById('status');
if(gameOver)
{
computerTurn = false;
alert("This game is OVER!!");
return;
}
if(value != 'X' && value != 'O')
{
if(xTurn)
{
numMoves++;
square.value = "X";
xTurn = false;
aiMove = true;
}
if(aiMove)
{
makeMove()
status.innerHTML = 'X\'s turn';
xturn = true;
}
}
else
alert('That square is already being used: DUH!');
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';
for(var x = 0; x < 3; x++)
{
for(var y = 0; y < 3; y++)
{
document.getElementById(x + '_' + y).value = ' ';
gameOver = false;
numMoves = 0;
}
}
}
function checkWin()
{
var status = document.getElementById('status');
var val0;
var val1;
var val2;
I am sorry but that advise wasn't helpful at all. The problem is that the AI won't work. I know this becuase THE AI WON'T WORK. Having said that, how can you help me?
Mago wrote:I am sorry but that advise wasn't helpful at all. The problem is that the AI won't work. I know this becuase THE AI WON'T WORK. Having said that, how can you help me?
Hi Mago,
What do you mean by saying "The AI won't work?" I'm not going to debug your program; I will help you debug it. Please look at the question in this posting and the two questions in the previous posting. Thanks.
Mago wrote:I am sorry but that advise wasn't helpful at all. The problem is that the AI won't work. I know this becuase THE AI WON'T WORK. Having said that, how can you help me?
Chris and David are right... those questions are the key. They gave excellent advice. Let me give you three examples:
1) the 'ai' plays but makes moves so bad that it always loses
2) the ai won't make a move; the program crashes
3) the ai over-writes your token- it places its mark in the square you already took.
All of these could be describes as the "ai won't work", but they are very different problems and would be fixed in very different ways. [The first point isn't a coding problem, but does fit in to "AI doesn't work"] To debug code, you need to find out what conditions produce what behavoir. Then, you go to the part of the code that produces that result, and look for the problem. [I am assuming the code actually compiles and that you didn't miss a comma or semi-colon or something or pasted the javascript code in to a fortran compiler or something like that.]
One other point... I'm not a good programmer. I find it _much_ easier to write my own code, rather than try to decipher someone else's code. To me, the problem of trying to debug code is such a hard problem that I don't want to add a second problem of trying to understand someone else's code. If you do want to use someone else's code, then the thing to do is to walk through it with pen and paper and make sure you understand what each part of the program is doing. Sez posted his code as reference, which was nice, but there is no guarrantee that it works.
Alright, heres the facts. When I add my AI, I can't play any of the squares, and the computer won't play either. My page says that an object is expected under the error report, but everytime I try to debug it, nothing changes! Do you have any ideas that would help?
Mago wrote:Alright, heres the facts. When I add my AI, I can't play any of the squares, and the computer won't play either. My page says that an object is expected under the error report, but everytime I try to debug it, nothing changes! Do you have any ideas that would help?
This still isn't very clear to me- the way to debug is to step through every input, and make sure you get the correct output. You are giving us very little detail. Are you 'x' or 'o'? Do you go first, or does the computer? These details may be critical for figuring out where things went wrong. If you go first, and you cannot play any squares, then the problem cannot be with the AI. If you remove the AI part and you play for both players, does it work? What does "cannot play" mean? How far in the game do you get? Does a board draw, does it welcome you to the game? There is stuff to this before you pick a square... does that execute correctly or is it hanging up before you make your pick? Throw in some output lines- have it print to the screen "I am in this section" "I am in that section".
If your getting an error report, it usually tells you the variable name that is the problem, or the line of code that is flaky, or some sort of detail that will help you track down the problem. "My page says that an object is expected under the error report" is not very informative.
So, using the suggestions I've given, can you isolate the problem?
Okay, I will clarify my information. I go first. I am x. The computer is o. The script works fine in two player. What I mean when I say that I can't play a square is that when I click on the square, nothing happens; no x is written. In the error report, it sent me to a line where there was only a } symbol, and said that "an object was expected". I am not sure what that means, but I am sure I will figure it out. Thank you!
Mago wrote:Okay, I will clarify my information. I go first. I am x. The computer is o. The script works fine in two player. What I mean when I say that I can't play a square is that when I click on the square, nothing happens; no x is written. In the error report, it sent me to a line where there was only a } symbol, and said that "an object was expected". I am not sure what that means, but I am sure I will figure it out. Thank you!
Okay, I have two lines of thought...
First, if you are going first, and the two player code works fine, it seems odd that adding the AI causes problems at this point (before it enters AI). So I would check very carefully your code prior to the computer making its move. Are you defining both players properly? Is there some new functionality you've added?
Second,
This error is javascript has to do with not being able to find a file or directory. The line it takes you to should be the one that contains the file call. Since you end up on a line with just }, I would look immediately prior to that line.
I'm not sure if you get this error for variables or functions that are missing, or just external files. Otherwise, go through and make sure every variable and function are properly defined (and defined before you call them). This language is case sensitive, so watch for caps...
There are also numerous javascript debugging tools. I've never used any of them, but some browsers contain them (like firefox- called the 'error console'). Supposedly they are more likely to give you detailed information (like the name of the object it is looking for).
This is a general reference on debugging javascript. I thought it might be useful.