URGENT!!!!! AI Tic-Tac-Toe

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

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
kubekua
Posts: 3
Joined: Sun Jan 12, 2014 9:23 pm
Occupation: Student
Project Question: URGENT!!!!! ARTIFICIAL INTELLIGENCE: TIC TAC TOE
I have finished making the tic tac toe game where two players can play the game. The game runs perfectly fine when two people are playing it. I'm stuck on the part where you create the AI and what you put into the makeComputerMove function. If someone can give me some examples and/or give me help on this project I would really appreciate
Project Due Date: January 14, 2014
Project Status: I am conducting my experiment

URGENT!!!!! AI Tic-Tac-Toe

Post by kubekua »

I'm creating the artificial intelligence tic tac toe game from https://www.sciencebuddies.org and i am stuck on the part where you create the artificial intelligence. I have made the game to perfectly run while two players are playing it. I have read other questions and answers to this topic but none seem to help. If anyone can help me create the AI by examples of the text or just plain HELP I would really appreciate it. I

Here is my program so far(works perfectly fine):

<HTML>
<HEAD>
<TITLE>Tic Tac Toe!</TITLE>

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

function squareclicked(square)

{
var value = square.value;
var status = document.getElementById('status');
var computerTurn = false; // true/false variable to keep track of the turn

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)
{

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;


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


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


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


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


}

</SCRIPT>
</HEAD>

<BODY>

<div align="center">
<div id="outline">
<br>
<br>
<INPUT TYPE="BUTTON" ID="NEWGAME" VALUE="New Game" ONCLICK="newgame();">
<br>
<br>
<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>

<div align="center">
<div id="stat">
<P ID="status">X's turn</P>
</div>
</div>
</div>
</div>


</BODY>
</HTML>
hhemken
Former Expert
Posts: 266
Joined: Mon Oct 03, 2005 3:16 pm

Re: URGENT!!!!! AI Tic-Tac-Toe

Post by hhemken »

kubekua,

The point of the project is for you to read the javascript and figure it out. For example, use a pencil and paper to draw what the computer is doing step-by-step. That way you'll find where in the code the program switches from waiting for someone to input an X to waiting for someone to input an O. Instead of waiting for someone to input an O, replace that part of the program with some javascript code that chooses the best spot and marks it with an O. You might want to write a separate javascript function that gets called, and put your AI strategies in there. Have you read this post?:

https://www.sciencebuddies.org/science- ... 30&t=12130

There are some pretty strong hints in there. It's no fun if we tell you exactly what to do!

Good luck!
Heinz Hemken
Mentor
Science Buddies Expert Forum
kubekua
Posts: 3
Joined: Sun Jan 12, 2014 9:23 pm
Occupation: Student
Project Question: URGENT!!!!! ARTIFICIAL INTELLIGENCE: TIC TAC TOE
I have finished making the tic tac toe game where two players can play the game. The game runs perfectly fine when two people are playing it. I'm stuck on the part where you create the AI and what you put into the makeComputerMove function. If someone can give me some examples and/or give me help on this project I would really appreciate
Project Due Date: January 14, 2014
Project Status: I am conducting my experiment

Re: URGENT!!!!! AI Tic-Tac-Toe

Post by kubekua »

Hi but I don't know what functions to write that will model of the tic tac toe strategy moves and I don't have a lot of time left too my project got held back to Thursday :|
kubekua
Posts: 3
Joined: Sun Jan 12, 2014 9:23 pm
Occupation: Student
Project Question: URGENT!!!!! ARTIFICIAL INTELLIGENCE: TIC TAC TOE
I have finished making the tic tac toe game where two players can play the game. The game runs perfectly fine when two people are playing it. I'm stuck on the part where you create the AI and what you put into the makeComputerMove function. If someone can give me some examples and/or give me help on this project I would really appreciate
Project Due Date: January 14, 2014
Project Status: I am conducting my experiment

Re: URGENT!!!!! AI Tic-Tac-Toe

Post by kubekua »

Hi again,

I read the link you gave and i don't know if that will work, I haven't had the chance. I'm also a beginner at coding, I did do the simple adder project on science buddies, so I don't know how to put the tic Tac Toe strategy into the coding language so the computer will understand it.
hhemken
Former Expert
Posts: 266
Joined: Mon Oct 03, 2005 3:16 pm

Re: URGENT!!!!! AI Tic-Tac-Toe

Post by hhemken »

kubekua,

One day isn't a lot of time, but if you are patient and focused you can do it. Look at the code snippets in the link I sent. Print them out and read them carefully. Read the comments in that thread, and mark the printouts with a red pen with the remarks in the comments.

The point of the project is for you to figure out how to do it, and learn something cool, interesting, and useful in the process. Here are some things that are not goals for this project:

1) becoming a programming expert
2) making a super powerful Tic Tac Toe AI
3) doing something that no human has ever done before

Here are some goals that are part of the project:

1) get enough familiarity with javascript to do some basic stuff, such as modifying the tic tac toe program and keep it working
2) setting the game up to make moves on its own
3) making at least one rule for it to play, such as always taking the middle spot if it is available. At very least, it should randomly choose a square from those that do not yet have an O or an X.

As you can see, it can be boiled down to only a few things. The link I sent has enough information for you do meet these three goals. If you are still confused, ask someone to sit with you and work through it. I suspect you need a live person to help you figure out the few things that are blocking you.

Good luck!
Heinz Hemken
Mentor
Science Buddies Expert Forum
Locked

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