## AI help for Tic-Tac-Toe!!!!

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

Moderators: MelissaB, kgudger, Ray Trent, Moderators

### AI help for Tic-Tac-Toe!!!!

HELP!!! I have don't know how to do AI! I am having some difficulties, so help me as soon as you can!! My teacher hasn't told me when the project is due, so I want to have it done just in case. ANY helpful hints would be nice!!!!!!!!!!!!!!!!
Mago

Posts: 11
Joined: Sun Sep 23, 2007 5:52 pm

### Re: AI help for Tic-Tac-Toe!!!!

Hi Mago,

There's another thread on this Math & Computer Science section of the board with subject "My Tic-Tac-Toe Problem." This thread appears to address the AI function.

Do any of the posts there help?
Cheers!

Dave
davidkallman
Former Expert

Posts: 675
Joined: Thu Feb 03, 2005 3:38 pm

Thank you!! I didn't see those posts. Anyways, I think that I may have had a breakthrough myself!

Thanks for the advice

_________________________
Mago
Mago

Posts: 11
Joined: Sun Sep 23, 2007 5:52 pm

Dang. I can't get the AI to work. I have it writen out so correctly, but I can't get it to post!!!!! I have already checked the other forums, and they don't help me, so could you please help???????? My project is due thursday, so I am in a rush!!!!!! Here, I will post my script that doesn't work:

<html>
<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;

// 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 = "Computer 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 = "Computer 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 = "Computer 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 = "Computer WINS!";
return true;
}

// no winner yet return false;
}
Function makeMove()
{
box1 = 3;
box2 = 2;
box3 = 3;
box4 = 2;
box5 = 4;
box6 = 2;
box7 = 3;
box8 = 2;
box9 = 3;
var sq1;
var sq2;
var sq3;
var sq4;
var sq5;
var sq6;
var sq7;
var sq8;
var sq9;
{
sq1 = document.getElementById('0_0').value;
sq2 = document.getElementById('1_0').value;
sq3 = document.getElementById('2_0').value;
sq4 = document.getElementById('0_1').value;
sq5 = document.getElementById('1_1').value;
sq6 = document.getElementById('2_1').value;
sq7 = document.getElementById('0_2').value;
sq8 = document.getElementById('1_2').value;
sq9 = document.getElementById('2_2').value;
var status = document.getElementById('status');
// check top row
if(sq1 == 'X' && sq2 == 'X' && sq3 == ' ')
{
box3 = box3+10}
if(sq2 == 'X' && sq3 == 'X' && sq1 == ' ')
{
box1 = box1+10}
if(sq1 == 'X' && sq3 == 'X' && sq2 == ' ')
{box2 = box2 + 10}
// checking top row for win
if(sq1 == 'O' && sq3 == 'O' && sq2 == ' ')
{
box2 = box2 + 15}
if(sq1 == ' ' && sq3 == 'O' && sq2 == 'O')
{
box1 = box1 + 15}
if(sq1 == 'O' && sq3 == ' ' && sq2 == 'O')
{box3 = box3 + 15};
// now check diagonal upper left to lower right
if(sq1 == 'X' && sq5 == 'X' && sq9 == ' ')
{
box9 = box9 + 10}
if(sq1 == ' ' && sq5 == 'X' && sq9 == 'X')
{
box1 = box1 + 10}
if(sq1 == 'X' && sq5 == ' ' && sq9 == 'X')
{
box5 = box5 + 10}
if(sq1 == 'O' && sq5 == 'O' && sq9 == ' ')
{
box9 = box9 + 15}
if(sq1 == ' ' && sq5 == 'O' && sq9 == 'O')
{
box1 = box1 + 15}
if(sq1 == 'O' && sq5 == ' ' && sq9 == 'O')
{
box5 = box5 + 15};
// now check upper right to lower left
if(sq3 == 'X' && sq5 == 'X' && sq7 == ' ')
{
box7 = box7 + 10}
if(sq3 == ' ' && sq5 == 'X' && sq7 == 'X')
{
box3 = box3 + 10}
if(sq3 == 'X' && sq5 == ' ' && sq7 == 'X')
{
box5 = box5 + 10}
if(sq3 == 'O' && sq5 == 'O' && sq7 == ' ')
{
box7 = box7 + 15}
if(sq3 == ' ' && sq5 == 'O' && sq7 == 'O')
{
box3 = box3 + 15}
if(sq3 == 'O' && sq5 == ' ' && sq7 == 'O')
{
box5 = box5 + 15};
// check row two
if(sq4 == ' ' && sq5 == 'X' && sq6 == 'X')
{
box4 = box4 + 10}
if(sq4 == 'X' && sq5 == 'X' && sq6 == ' ')
{
box6 = box6 + 10}
if(sq4 == 'X' && sq5 == ' ' && sq6 == 'X')
{
box5 = box5 + 10}
if(sq4 == 'O' && sq5 == 'O' && sq6 == ' ')
{
box6 = box6 + 15}
if(sq4 == ' ' && sq5 == 'O' && sq6 == 'O')
{
box4 = box4 + 15}
if(sq4 == 'O' && sq5 == ' ' && sq6 == 'O')
{
box5 = box5 + 15};
//check bottom row
if(sq7 == 'X' && sq8 == 'X' && sq9 == ' ')
{
box9 = box9 + 10}
if(sq8 == 'X' && sq9 == 'X' && sq7 == ' ')
{
box7 = box7 + 10}
if(sq7 == 'X' && sq9 == 'X' && sq8 == ' ')
{box8 = box8 + 10}
if(sq7 == 'O' && sq8 == 'O' && sq9 == ' ')
{box9 = box9+ 15}
if(sq7 == ' ' && sq8 == 'O' && sq9 == 'O')
{box7 = box7 + 15}
if(sq7 == 'O' && sq8 == ' ' && sq9 == 'O')
{box8 = box8 + 15}
//check left down row
if(sq1 == 'X' && sq4 == 'X' && sq7 == ' ')
{box7 = box7 +10}
if(sq1 == ' ' && sq4 == 'X' && sq7 == 'X')
{box1 = box1 + 10}
if(sq1 == 'X' && sq4 == ' ' && sq7 == 'X')
{box4 = box4 + 10}
if(sq1 == 'O' && sq4 == 'O' && sq7 == ' ')
{box7 = box7 +15}
if(sq1 == ' ' && sq4 == 'O' && sq7 == 'O')
{box1 = box1 + 15}
if(sq1 == 'O' && sq4 == ' ' && sq7 == 'O')
{box4 = box4 + 15}
//check middle vertical row
if(sq2 == 'X' && sq5 == 'X' && sq8 == ' ')
{box8 = box8 +10}
if(sq2 == ' ' && sq5 == 'X' && sq8 == 'X')
{box2 = box2 + 10}
if(sq2 == 'X' && sq5 == ' ' && sq8 == 'X')
{box5 = box5 + 10}
if(sq2 == 'O' && sq5 == 'O' && sq8 == ' ')
{box8 = box8 +15}
if(sq2 == ' ' && sq5 == 'O' && sq8 == 'O')
{box2 = box2 + 15}
if(sq2 == 'O' && sq5 == ' ' && sq8 == 'O')
{box5 = box5 + 15}
//check last row
if(sq3 == 'X' && sq6 == 'X' && sq9 == ' ')
{box9 = box9 +10}
if(sq3 == ' ' && sq6 == 'X' && sq9 == 'X')
{box3 = box3 + 10}
if(sq3 == 'X' && sq6 == ' ' && sq9 == 'X')
{box6 = box6 + 10}
if(sq3 == 'O' && sq6 == 'O' && sq9 == ' ')
{box9 = box9 +15}
if(sq3 == ' ' && sq6 == 'O' && sq9 == 'O')
{box3 = box3 + 15}
if(sq3 == 'O' && sq6 == ' ' && sq9 == 'O')
{box6 = box6 + 15}
}
if(box1>= box2 && box1 >= box3 && box1 >= box4 && box1 >= box5 && box1 >= box6 && box1 >= box7 && box1 >= box8 && box1 >=

box9 && sq1 == ' ')
{
document.getElementById('0_0').value = 'O';
aiMove = !aiMove
numMoves++;
}
else if(box2 >= box1 && box2 >= box3 && box2 >= box4 && box2 >= box5 && box2 >= box6 && box2 >= box7 && box2 >= box8 && box2

>= box9 && sq2 == ' ')
{document.getElementById('1_0').value = 'O';
aiMove = !aiMove
numMoves++;
}
else if(box3 >= box1 && box3 >= box2 && box3 >= box4 && box3 >= box5 && box3 >= box6 && box3 >= box7 && box3 >= box8 && box3

>= box9 && sq3 == ' ')
{
document.getElementById('2_0').value = 'O';
aiMove = !aiMove
numMoves++;}
else if(box4 >= box1 && box4 >= box2 && box4 >= box3 && box4 >= box5 && box4 >= box6 && box4 >= box7 && box4 >= box8 && box4

>= box9 && sq4 == 'O')
{
document.getElementById('0_1').value = 'O';
aiMove = !aiMove
numMoves++;}
else if(box5 >= box1 && box5 >= box2 && box5 >= box3 && box5 >= box4 && box5 >= box6 && box5 >= box7 && box5 >= box8 && box5

>= box9 && sq5 == 'O')
{
document.getElementById('1_1').value = 'O';
aiMove = !aiMove
numMoves++;}
else if(box6 >= box1 && box6 >= box2 && box6 >= box3 && box6 >= box4 && box6 >= box5 && box6 >= box7 && box6 >= box8 && box6

>= box9 && sq6 == 'O')
{
document.getElementById('2_1').value = 'O';
aiMove = !aiMove
numMoves++;}
else if(box7 >= box1 && box7 >= box2 && box7 >= box3 && box7 >= box4 && box7 >= box5 && box7 >= box6 && box7 >= box8 && box7

>= box9 && sq7 == 'O')
{
document.getElementById('0_2').value = 'O';
aiMove = !aiMove
numMoves++;}
else if(box8 >= box1 && box8 >= box2 && box8 >= box3 && box8 >= box4 && box8 >= box5 && box8 >= box6 && box8 >= box7 && box8

>= box9 && sq8 == 'O')
{
document.getElementById('1_2').value = 'O';
aiMove = !aiMove
nuMoves++;}
aiMove = !aiMove
numMoves++;}
else if(box9 >= box1 && box9 >= box2 && box9 >= box3 && box9 >= box4 && box9 >= box5 && box9 >= box6 && box9 >= box7 && box9

>= box8 && sq9 == 'O')
{
document.getElementById('2_2').value = 'O';
aiMove = !aiMove
numMoves++;}
else if(sq1 == ' ')
{
document.getElementById('0_0').value = 'O';
aiMove = !aiMove
numMove++;}
else if(sq2 == ' ')
{
document.getElementById('1_0').value = 'O';
aiMove = !aiMove
numMove++;}
else if(sq3 == ' ')
{
document.getElementById('2_0').value = 'O';
aiMove = !aiMove
numMove++;}
else if(sq4 == ' ')
{
document.getElementById('0_1').value = 'O';
aiMove = !aiMove
numMove++;}
else if(sq5 == ' ')
{
document.getElementById('1_1').value = 'O';
aiMove = !aiMove
numMove++;}
else if(sq6 == ' ')
{
document.getElementById('2_1').value = 'O';
aiMove = !aiMove
numMove++;}
else if(sq7 == ' ')
{
document.getElementById('0_2').value = 'O';
aiMove = !aiMove
numMove++;}
else if(sq8 == ' ')
{
document.getElementById('1_2').value = 'O';
aiMove = !aiMove
numMove++;}
else if(sq9 == ' ')
{
document.getElementById('2_2').value = 'O';
aiMove = !aiMove
numMove++;}
}
</SCRIPT>
<body bgcolor='lightblue' text='green'>
<center>
<input type="button" id="0_0" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<input type="button" id="1_0" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<input type="button" id="2_0" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<br>
<input type="button" id="0_1" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<input type="button" id="1_1" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<input type="button" id="2_1" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<br>
<input type="button" id="0_2" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<input type="button" id="1_2" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<input type="button" id="2_2" style=" width = 50; height = 50;" value=" " onClick="squareclicked(this)">
<br>
<br>
<br>
<br>
<br>
<br>
<input type="button" id="NEWGAME" Value="Click here for a new game" onClick="newgame();">
<P ID="status">X's turn</P>
<form name = "blah">
<input type = "text" value ="Type your name here." Id = "numming" name = "numing">
<br>
<input type = "button" value = " Player's Name goes here" name = "hello" style = "position: absolute; left: 100; top : 100;

width: 100; height: 75;">
<input type = "button" value = " Computer " name = "heyllo" style = "position: absolute; left: 800; top : 100; width: 100;

height: 75;">
<input type = "button" value = "Player's name." onClick = "document.blah.hello.value = document.blah.numing.value">
</form>
</center>
</body>
</html>
Mago

Posts: 11
Joined: Sun Sep 23, 2007 5:52 pm

Mago wrote:Dang. I can't get the AI to work. I have it writen out so correctly, but I can't get it to post!!!!! I have already checked the other forums, and they don't help me, so could you please help???????? My project is due thursday, so I am in a rush!!!!!! Here, I will post my script that doesn't work:

Hi, Mago!

You have an excellent opportunity to learn how to debug programs. The experts watching this forum aren't signed up to debug your code for you. I can offer three simple suggestions that have worked well for me in the past:

1) Insert extra statements in the code that print out intermediate values along the way. That way you can watch the execution of the program and spot where things have gone wrong.

2) Step through the code yourself by hand. That is, you play the role of the computer and step through the code one line at a time. Sometimes using a print out of the code and writing values as you go along works well.

3) Form a better question or problem description. Right now, all we know is that the AI doesn't work. In what way doesn't it work? How does the problem show itself? How is what it is doing different than what you expect? IF you post a well-formed question, the experts watching this forum may be able to suggest additional techniques that you can use to try to debug this problem yourself.

Thanks!
Brian Castelli (OneBriiguy)
Engineering Specialist
OneBriiguy
Former Expert

Posts: 159
Joined: Fri Sep 30, 2005 6:31 am
Occupation: Engineer
Project Question: N/A
Project Due Date: N/A
Project Status: Not applicable

Thanks for the help, but I rewrote my code and it works!!!
The only problem is for my science fair project, I wanted to see if I could make an invincible tic-tac-toe program, and now that I have accomplished that, I don't know what data I am going to collect!!!!!!! Do you have any suggestions?
___________________________

Mago
Mago

Posts: 11
Joined: Sun Sep 23, 2007 5:52 pm

### Re: tic-tac-toe

Hi mago,

Congratulations!!!

How do you know the program is invincible? Proving the invincibility, presumably by looking at classes of moves (i.e., corners and others), could make an interesting project as would connecting the proof to the program's logic. Thoughts?
Cheers!

Dave
davidkallman
Former Expert

Posts: 675
Joined: Thu Feb 03, 2005 3:38 pm