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!
<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;
}
if(value != 'X' && value != 'O')
{
if(turnX)
{
moveCount++;
square.value="X";
turnX = false;
AImove = true;
status.innerHTML = 'O\'s turn';
moveTicker.innerHTML = moveCount;
}
if(AImove)
{
makeMove()
status.innerHTML = 'X\'s turn';
turnX = true;
}
}
else
alert('You cannot play that square.');
var winner = checkWin();
if(!winner)
{
if(moveCount == 9)
status.innerHTML = 'Tie Game!';
}
else
gameOver = true;
}
// 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;
//tie check
if(moveCount == 9)
{
gameCount++;
tieCount++;
tieDisplay.innerHTML = tieCount;
noGames.innerHTML = gameCount;
gameOver = true;
}
else
gameOver = false;
//columns
{
val1 = document.getElementById('0_0').value;
val2 = document.getElementById('1_0').value;
val3 = document.getElementById('2_0').value;
{
if(val1 == 'X' && val2 == 'X' && val3 == 'X')
{
winsX++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'X WINS!';
movecount = 0
return true;
}
else if(val1 == 'O' && val2 == 'O' && val3 == 'O')
{
winsO++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'O WINS!';
movecount = 0
return true;
}
}
//row 2
{
val1 = document.getElementById('0_1').value;
val2 = document.getElementById('1_1').value;
val3 = document.getElementById('2_1').value;
if(val1 == 'X' && val2 == 'X' && val3 == 'X')
{
winsX++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'X WINS!';
movecount = 0
return true;
}
else if(val1 == 'O' && val2 == 'O' && val3 == 'O')
{
winsO++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'O WINS!';
movecount = 0
return true;
}
}
//row 3
{
val1 = document.getElementById('0_2').value;
val2 = document.getElementById('1_2').value;
val3 = document.getElementById('2_2').value;
if(val1 == 'X' && val2 == 'X' && val3 == 'X')
{
winsX++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'X WINS!';
movecount = 0
return true;
}
else if(val1 == 'O' && val2 == 'O' && val3 == 'O')
{
winsO++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'O WINS!';
movecount = 0
return true;
}
}
// column 1
{
val1 = document.getElementById('0_0').value;
val2 = document.getElementById('0_1').value;
val3 = document.getElementById('0_2').value;
if(val1 == 'X' && val2 == 'X' && val3 == 'X')
{
winsX++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'X WINS!';
movecount = 0
return true;
}
else if(val1 == 'O' && val2 == 'O' && val3 == 'O')
{
winsO++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'O WINS!';
movecount = 0
return true;
}
}
//column 2
{
val1 = document.getElementById('1_0').value;
val2 = document.getElementById('1_1').value;
val3 = document.getElementById('1_2').value;
if(val1 == 'X' && val2 == 'X' && val3 == 'X')
{
winsX++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'X WINS!';
movecount = 0
return true;
}
else if(val1 == 'O' && val2 == 'O' && val3 == 'O')
{
winsO++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'O WINS!';
movecount = 0
return true;
}
}
//column 3
{
val1 = document.getElementById('2_0').value;
val2 = document.getElementById('2_1').value;
val3 = document.getElementById('2_2').value;
if(val1 == 'X' && val2 == 'X' && val3 == 'X')
{
winsX++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'X WINS!';
movecount = 0
return true;
}
else if(val1 == 'O' && val2 == 'O' && val3 == 'O')
{
winsO++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'O WINS!';
movecount = 0
return true;
}
}
// diagonals
val1 = document.getElementById('0_0').value;
val2 = document.getElementById('1_1').value;
val3 = document.getElementById('2_2').value;
if(val1 == 'X' && val2 == 'X' && val3 == 'X')
{
winsX++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'X WINS!';
movecount = 0
return true;
}
else if(val1 == 'O' && val2 == 'O' && val3 == 'O')
{
winsO++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'O WINS!';
movecount = 0
return true;
}
}
val1 = document.getElementById('2_0').value;
val2 = document.getElementById('1_1').value;
val3 = document.getElementById('0_2').value;
if(val1 == 'X' && val2 == 'X' && val3 == 'X')
{
winsX++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'X WINS!';
movecount = 0
return true;
}
else if(val1 == 'O' && val2 == 'O' && val3 == 'O')
{
winsO++
checkXwin.innerHTML = winsX
checkOwin.innerHTML = winsO
gameCount++
noGames.innerHTML = gameCount;
status.innerHTML = 'O WINS!';
movecount = 0
return true;
}
return false;
}
//AI Function
//Points note: +10 block win, +15 block guaranteed opponent win, +20 guaranteed win/3-in-a-row
//Save this: document.getElementById(x + '_' + y).value = ' ';
function makeMove()
{
sq1 = 3;
sq2 = 2;
sq3 = 3;
sq4 = 2;
sq5 = 4;
sq6 = 2;
sq7 = 3;
sq8 = 2;
sq9 = 3;
var checkSq1;
var checkSq2;
var checkSq3;
var checkSq4;
var checkSq5;
var checkSq6;
var checkSq7;
var checkSq8;
var checkSq9;
{
checkSq1 = document.getElementById('0_0').value;
checkSq2 = document.getElementById('1_0').value;
checkSq3 = document.getElementById('2_0').value;
checkSq4 = document.getElementById('0_1').value;
checkSq5 = document.getElementById('1_1').value;
checkSq6 = document.getElementById('2_1').value;
checkSq7 = document.getElementById('0_2').value;
checkSq8 = document.getElementById('1_2').value;
checkSq9 = document.getElementById('2_2').value;
var status = document.getElementById('status');
if(checkSq1 != ' ')
{
sq1 = sq1 - 500
}
else if(checkSq2 != ' ')
{
sq2 = sq2 - 500
}
else if(checkSq3 != ' ')
{
sq3 = sq3 - 500
}
else if(checkSq4 != ' ')
{
sq4 = sq4 - 500
}
else if(checkSq5 != ' ')
{
sq5 = sq5 - 500
}
else if(checkSq6 != ' ')
{
sq6 = sq6 - 500
}
else if(checkSq7 != ' ')
{
sq7 = sq7 - 500
}
else if(checkSq8 != ' ')
{
sq8 = sq8 - 500
}
else if(checkSq9 != ' ')
{
sq9 = sq9 - 500
}
//Sneaky
if(checkSq5 == 'X' && checkSq9 == 'X' && checkSq3 == ' ')
{
sq3 = sq3 + 20
}
if(checkSq7 == 'X' && checkSq3 == 'X' && checkSq5 == 'O' && checkSq2 == ' ')
{
sq2 = sq2 + 20
}
if(checkSq8 == 'X' && checkSq6 == 'X' && checkSq5 == 'O' && checkSq9 == ' ')
{
sq9 = sq9 + 20
}
if(checkSq1 == 'O' && checkSq3 == 'X' && checkSq4 == 'X' && checkSq5 == 'O' && checkSq6 == 'O' && checkSq7 == 'X' && checkSq9 == 'X' && checkSq8 == ' ')
{
sq8 = sq8 + 20
}
//Row 1 (squares 1,2,3)
if(checkSq1 == 'X' && checkSq2 == 'X' && checkSq3 == ' ')
{
sq3 = sq3 + 10
}
else if(checkSq1 == 'O' && checkSq2 == 'O' && checkSq3 == ' ')
{
sq3 = sq3 + 20
}
else if(checkSq1 == 'X' && checkSq3 == 'X' && checkSq2 == ' ')
{
sq2 = sq2 + 10
}
else if(checkSq1 == 'O' && checkSq3 == 'O' && checkSq2 == ' ')
{
sq2 = sq2 + 20
}
else if(checkSq2 == 'X' && checkSq3 == 'X' && checkSq1 == ' ')
{
sq1 = sq1 + 10
}
else if(checkSq2 == 'O' && checkSq3 == 'O' && checkSq1 == ' ')
{
sq1 = sq1 + 10
}
// Row 2(squares 4,5,6)
else if(checkSq4 == 'X' && checkSq5 == 'X' && checkSq6 == ' ')
{
sq6 = sq6 + 10
}
else if(checkSq4 == 'O' && checkSq5 == 'O' && checkSq6 == ' ')
{
sq6 = sq6 + 20
}
else if(checkSq4 == 'X' && checkSq6 == 'X' && checkSq5 == ' ')
{
sq5 = sq5 + 10
}
else if(checkSq4 == 'O' && checkSq6 == 'O' && checkSq5 == ' ')
{
sq5 = sq5 + 20
}
else if(checkSq5 == 'X' && checkSq6 == 'X' && checkSq4 == ' ')
{
sq4 = sq4 + 10
}
else if(checkSq5 == 'O' && checkSq6 == 'O' && checkSq4 == ' ')
{
sq4 = sq4 + 10
}
//Row 3(squares 7,8,9)
else if(checkSq7 == 'X' && checkSq8 == 'X' && checkSq9 == ' ')
{
sq9 = sq9 + 10
}
else if(checkSq7 == 'O' && checkSq8 == 'O' && checkSq9 == ' ')
{
sq9 = sq9 + 20
}
else if(checkSq7 == 'X' && checkSq9 == 'X' && checkSq8 == ' ')
{
sq8 = sq8 + 10
}
else if(checkSq7 == 'O' && checkSq9 == 'O' && checkSq8 == ' ')
{
sq8 = sq8 + 20
}
else if(checkSq8 == 'X' && checkSq9 == 'X' && checkSq7 == ' ')
{
sq7 = sq7 + 10
}
else if(checkSq8 == 'O' && checkSq9 == 'O' && checkSq7 == ' ')
{
sq7 = sq7 + 10
}
// Column 1 (squares 1, 4, 7)
else if(checkSq1 == 'X' && checkSq4 == 'X' && checkSq7 == ' ')
{
sq7 = sq7 + 10
}
else if(checkSq1 == 'O' && checkSq4 == 'O' && checkSq7 == ' ')
{
sq7 = sq7 + 20
}
else if(checkSq1 == 'X' && checkSq6 == 'X' && checkSq4 == ' ')
{
sq4 = sq4 + 10
}
else if(checkSq1 == 'O' && checkSq7 == 'O' && checkSq4 == ' ')
{
sq4 = sq4 + 20
}
else if(checkSq4 == 'X' && checkSq7 == 'X' && checkSq4 == ' ')
{
sq1 = sq1 + 10
}
else if(checkSq4 == 'O' && checkSq7 == 'O' && checkSq4 == ' ')
{
sq1 = sq1 + 10
}
//Column 2 (squares 2,5,8)
else if(checkSq2 == 'X' && checkSq5 == 'X' && checkSq8 == ' ')
{
sq8 = sq8 + 10
}
else if(checkSq2 == 'O' && checkSq5 == 'O' && checkSq8 == ' ')
{
sq8 = sq8 + 20
}
else if(checkSq2 == 'X' && checkSq8 == 'X' && checkSq5 == ' ')
{
sq5 = sq5 + 10
}
else if(checkSq2 == 'O' && checkSq8 == 'O' && checkSq5 == ' ')
{
sq5 = sq5 + 20
}
else if(checkSq5 == 'X' && checkSq8 == 'X' && checkSq2 == ' ')
{
sq2 = sq2 + 10
}
else if(checkSq5 == 'O' && checkSq8 == 'O' && checkSq2 == ' ')
{
sq2 = sq2 + 10
}
//Column 3 (squares 3,6,9)
else if(checkSq3 == 'X' && checkSq6 == 'X' && checkSq9 == ' ')
{
sq9 = sq9 + 10
}
else if(checkSq3 == 'O' && checkSq6 == 'O' && checkSq9 == ' ')
{
sq9 = sq9 + 20
}
else if(checkSq3 == 'X' && checkSq9 == 'X' && checkSq6 == ' ')
{
sq6 = sq6 + 10
}
else if(checkSq3 == 'O' && checkSq9 == 'O' && checkSq6 == ' ')
{
sq6 = sq6 + 20
}
else if(checkSq6 == 'X' && checkSq9 == 'X' && checkSq3 == ' ')
{
sq3 = sq3 + 10
}
else if(checkSq6 == 'O' && checkSq9 == 'O' && checkSq3 == ' ')
{
sq3 = sq3 + 10
}
//Diagonal 1(squares 1,5,9)
else if(checkSq1 == 'X' && checkSq5 == 'X' && checkSq9 == ' ')
{
sq9 = sq9 + 10
}
else if(checkSq1 == 'O' && checkSq5 == 'O' && checkSq9 == ' ')
{
sq9 = sq9 + 20
}
else if(checkSq1 == 'X' && checkSq9 == 'X' && checkSq5 == ' ')
{
sq5 = sq2 + 10
}
else if(checkSq1 == 'O' && checkSq9 == 'O' && checkSq5 == ' ')
{
sq5 = sq2 + 20
}
else if(checkSq5 == 'X' && checkSq9 == 'X' && checkSq1 == ' ')
{
sq1 = sq1 + 10
}
else if(checkSq5 == 'O' && checkSq9 == 'O' && checkSq1 == ' ')
{
sq1 = sq1 + 10
}
//Diagonal 2(squares 3,5,7)
else if(checkSq3 == 'X' && checkSq5 == 'X' && checkSq7 == ' ')
{
sq7 = sq7 + 10
}
else if(checkSq3 == 'O' && checkSq5 == 'O' && checkSq7 == ' ')
{
sq7 = sq7 + 20
}
else if(checkSq3 == 'X' && checkSq7 == 'X' && checkSq5 == ' ')
{
sq5 = sq2 + 10
}
else if(checkSq3 == 'O' && checkSq7 == 'O' && checkSq5 == ' ')
{
sq5 = sq2 + 20
}
else if(checkSq5 == 'X' && checkSq7 == 'X' && checkSq3 == ' ')
{
sq3 = sq3 + 10
}
else if(checkSq5 == 'O' && checkSq7 == 'O' && checkSq3 == ' ')
{
sq3 = sq3 + 10
}
}
if(sq1 >= sq2 && sq1 >= sq3 && sq1 >= sq4 && sq1 >= sq5 && sq1 >= sq6 && sq1 >= sq7 && sq1 >= sq8 && sq1 >= sq9 && checkSq1 == ' ')
{
document.getElementById('0_0').value = 'O';
AImove = !AImove
moveCount++;
}
else if(sq2 >= sq1 && sq2 >= sq3 && sq2 >= sq4 && sq2 >= sq5 && sq2 >= sq6 && sq2 >= sq7 && sq2 >= sq8 && sq2 >= sq9 && checkSq2 == ' ')
{
document.getElementById('1_0').value = 'O';
AImove = !AImove
moveCount++;
}
else if(sq3 >= sq1 && sq3 >= sq2 && sq3 >= sq4 && sq3 >= sq5 && sq3 >= sq6 && sq3 >= sq7 && sq3 >= sq8 && sq3 >= sq9 && checkSq3 == ' ')
{
document.getElementById('2_0').value = 'O';
AImove = !AImove
moveCount++;
}
else if(sq4 >= sq2 && sq4 >= sq3 && sq4 >= sq1 && sq4 >= sq5 && sq4 >= sq6 && sq4 >= sq7 && sq4 >= sq8 && sq4 >= sq9 && checkSq4 == ' ')
{
document.getElementById('0_1').value = 'O';
AImove = !AImove
moveCount++;
}
else if(sq5 >= sq2 && sq5 >= sq3 && sq5 >= sq4 && sq5 >= sq1 && sq5 >= sq6 && sq5 >= sq7 && sq5 >= sq8 && sq5 >= sq9 && checkSq5 == ' ')
{
document.getElementById('1_1').value = 'O';
AImove = !AImove
moveCount++;
}
else if(sq6 >= sq2 && sq6 >= sq3 && sq6 >= sq4 && sq6 >= sq5 && sq6 >= sq1 && sq6 >= sq7 && sq6 >= sq8 && sq6 >= sq9 && checkSq6 == ' ')
{
document.getElementById('2_1').value = 'O';
AImove = !AImove
moveCount++;
}
else if(sq7 >= sq2 && sq7 >= sq3 && sq7 >= sq4 && sq7 >= sq5 && sq7 >= sq6 && sq7 >= sq1 && sq7 >= sq8 && sq7 >= sq9 && checkSq7 == ' ')
{
document.getElementById('0_2').value = 'O';
AImove = !AImove
moveCount++;
}
else if(sq8 >= sq2 && sq8 >= sq3 && sq8 >= sq4 && sq8 >= sq5 && sq8 >= sq6 && sq8 >= sq7 && sq8 >= sq1 && sq8 >= sq9 && checkSq8 == ' ')
{
document.getElementById('1_2').value = 'O';
AImove = !AImove
moveCount++;
}
else if(sq9 >= sq2 && sq9 >= sq3 && sq9 >= sq4 && sq9 >= sq5 && sq9 >= sq6 && sq9 >= sq7 && sq9 >= sq8 && sq9 >= sq1 && checkSq9 == ' ')
{
document.getElementById('2_2').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq1 == ' ')
{
document.getElementById('0_0').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq2 == ' ')
{
document.getElementById('1_0').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq3 == ' ')
{
document.getElementById('2_0').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq4 == ' ')
{
document.getElementById('0_1').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq5 == ' ')
{
document.getElementById('1_1').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq6 == ' ')
{
document.getElementById('2_1').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq7 == ' ')
{
document.getElementById('0_2').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq8 == ' ')
{
document.getElementById('1_2').value = 'O';
AImove = !AImove
moveCount++;
}
else if(checkSq9 == ' ')
{
document.getElementById('2_2').value = 'O';
AImove = !AImove
moveCount++;
}
}
</script>
<body>
<p align="center">
<input type="button" id="0_0" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<input type="button" id="1_0" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<input type="button" id="2_0" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<BR>
<input type="button" id="0_1" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<input type="button" id="1_1" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<input type="button" id="2_1" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<BR>
<input type="button" id="0_2" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<input type="button" id="1_2" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<input type="button" id="2_2" value=" " style=" width: 45; height: 45; " ONCLICK="clicked(this);" />
<BR><BR><BR>
<input type="button" id="newgame" value="New Game" style=" width: 150; height: 30; " ONCLICK="newgame();" />
<BR><BR>
<table width="200" height="49" background="sqbgs.png" cellpadding="10">
<tr align = "bottom">
<td>
<p id="status" align="center">
X's turn
</p>
</td>
</tr></table>
<br><br>
<table width="50" height="57" background="mTickerBG.png" cellpadding="10">
<tr align = "bottom">
<td>
<p id="moveTicker" align="center">
0
</p>
</td></tr></table>
<table width = "400" height="50" background = "scoreBG.png">
<tr>
<td>
<p id="winsX" align="center">
0
</p>
</td>
<td>
<p id="winsO" align="center">
0
</p>
</td>
<td>
<p id="gameCount" align="center">
0
</p>
</td>
<td>
<p id="tieDisplay" align="center">
0
</p>
</td></tr></table>
<br><br>
</body>
</html>
//Line count: 831
//Byte count: 20599
