«

»

Jul 27

Print this Post

TicTacToe

This is a small Tic-Tac-Toe game that I wrote. Feel free to copy and use it if you like it.

<script language="JavaScript" type="text/javascript">
<!--
var player = new String("X");
var winner = new String("");

function playThis(sender)
{
if(player=="X"){
sender.value="X";
player = "O";
document.frmBoard.txtStatus.value = "O's Turn";
}
else {
sender.value = "O";
player = "X";
document.frmBoard.txtStatus.value = "X's Turn";
}

if(document.frmBoard.btn11.value=="X"&&document.frmBoard.btn21.value=="X"&&document.frmBoard.btn31.value=="X") winner = "X";
if(document.frmBoard.btn12.value=="X"&&document.frmBoard.btn22.value=="X"&&document.frmBoard.btn32.value=="X") winner = "X";
if(document.frmBoard.btn13.value=="X"&&document.frmBoard.btn23.value=="X"&&document.frmBoard.btn33.value=="X") winner = "X";

if(document.frmBoard.btn11.value=="X"&&document.frmBoard.btn12.value=="X"&&document.frmBoard.btn13.value=="X") winner = "X";
if(document.frmBoard.btn21.value=="X"&&document.frmBoard.btn22.value=="X"&&document.frmBoard.btn23.value=="X") winner = "X";
if(document.frmBoard.btn31.value=="X"&&document.frmBoard.btn32.value=="X"&&document.frmBoard.btn33.value=="X") winner = "X";

if(document.frmBoard.btn11.value=="X"&&document.frmBoard.btn22.value=="X"&&document.frmBoard.btn33.value=="X") winner = "X";
if(document.frmBoard.btn13.value=="X"&&document.frmBoard.btn22.value=="X"&&document.frmBoard.btn31.value=="X") winner = "X";

if(document.frmBoard.btn11.value=="O"&&document.frmBoard.btn21.value=="O"&&document.frmBoard.btn31.value=="O") winner = "O";
if(document.frmBoard.btn12.value=="O"&&document.frmBoard.btn22.value=="O"&&document.frmBoard.btn32.value=="O") winner = "O";
if(document.frmBoard.btn13.value=="O"&&document.frmBoard.btn23.value=="O"&&document.frmBoard.btn33.value=="O") winner = "O";

if(document.frmBoard.btn11.value=="O"&&document.frmBoard.btn12.value=="O"&&document.frmBoard.btn13.value=="O") winner = "O";
if(document.frmBoard.btn21.value=="O"&&document.frmBoard.btn22.value=="O"&&document.frmBoard.btn23.value=="O") winner = "O";
if(document.frmBoard.btn31.value=="O"&&document.frmBoard.btn32.value=="O"&&document.frmBoard.btn33.value=="O") winner = "O";

if(document.frmBoard.btn11.value=="O"&&document.frmBoard.btn22.value=="O"&&document.frmBoard.btn33.value=="O") winner = "O";
if(document.frmBoard.btn13.value=="O"&&document.frmBoard.btn22.value=="O"&&document.frmBoard.btn31.value=="O") winner = "O";

if(winner=="X")
{
alert("X Wins!");
}
if(winner=="O")
{
alert("O Wins!");
}
if(winner!="")
{
winner = "";
player = "X";
document.frmBoard.txtStatus.value = "X's Turn";
history.go(0);
}
}
//-->
</script>

<form name="frmBoard">
<table cellpadding="5" cellspacing="0">
<tr>
<td style="border-right:1px solid black;border-bottom:1px solid black;"><input type="button" name="btn13" value=" " style="width:25px;height:25px;background-color:transparent;" onclick="playThis(this)"></td>
<td style="border-right:1px solid black;border-bottom:1px solid black;"><input type="button" name="btn23" value=" " style="width:25px;height:25px;background-color:transparent;" onclick="playThis(this)"></td>
<td style="border-bottom:1px solid black;"><input type="button" name="btn33" value=" " style="width:25px;height:25px;background-color:transparent;" onclick="playThis(this)"></td>
</tr>
<tr>
<td style="border-right:1px solid black;border-bottom:1px solid black;"><input type="button" name="btn12" value=" " style="width:25px;height:25px;background-color:transparent;" onclick="playThis(this)"></td>
<td style="border-right:1px solid black;border-bottom:1px solid black;"><input type="button" name="btn22" value=" " style="width:25px;height:25px;background-color:transparent;" onclick="playThis(this)"></td>
<td style="border-bottom:1px solid black;"><input type="button" name="btn32" value=" " style="width:25px;height:25px;background-color:transparent;" oncli

Permanent link to this article: https://www1.wperry.net/code/tictactoe/

Leave a Reply

Your email address will not be published. Required fields are marked *