473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Unable to get functions & alerts to work with imgs

Hey I am making a tic tac toe game, im new to javascript, and when i make the game with 'buttons' and 'x' and 'o' it works perfectly, but i also made some pics and i can get the pics to show up, but i am unable to get the alerts to come up, and i cannot figure out how to make it so it wont allow you to click on the same block more then once.
this is my complete code (with the gifs i am trying to use) .. any help at all would be appreaciated

[HTML]<html>
<title>new x-o's </title>
<head>

<!-- starts making X and O's on the IMAGEs / change player turn -->
<SCRIPT TYPE='TEXT/JAVASCRIPT'>
var xTurn = true;
function squareclicked(square)

// squareclicked is a function that is called whenever a IMAGE is clicked.
{
var status = document.getElementById('status');
var src = square.src;
{
if(src != 'boxx.gif' && src != 'boxo.gif')
{
if(xTurn)
{
numMoves++;
square.src = 'boxx.gif';
xTurn = false;
status.innerHTML = 'O\'s turn';
}
else
{
numMoves++;
square.src = 'boxo.gif';
xTurn = true;
status.innerHTML = 'X\'s turn';
}
}
else
alert('That square has already been played.');
if(gameOver)
{
alert('The game is already over.');
return;
}
}
var winner = checkWin();
if(!winner)
{
//check to see if there is a tie
if(numMoves == 9)
status.innerHTML = 'Tie Game!';
}
else
gameOver = true;
}

</script>
<!-- END making X and O's on IMAGEs / change player turn / GAMEOVER CHECK / TIE GAME CHECK-->
<!-- START CHECK WINNER FUNCTION -->
<SCRIPT TYPE='TEXT/JAVASCRIPT'>
var gameOver = false;
var 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).src;
val1 = document.getElementById('1_'+y).src;
val2 = document.getElementById('2_'+y).src;
if(val0 == 'boxx.gif' && val1 == 'boxx.gif' && val2 == 'boxx.gif')
{
status.innerHTML = 'X WINS!';
return true;
}
else if(val0 == 'boxo.gif' && val1 == 'boxo.gif' && val2 == 'boxo.gif')
{
status.innerHTML = 'O WINS!';
return true;
}
}

// check rows
for(var x = 0; x < 3; x++)
{
val0 = document.getElementById(x + '_0').src;
val1 = document.getElementById(x + '_1').src;
val2 = document.getElementById(x + '_2').src;
if(val0 == 'boxx.gif' && val1 == 'boxx.gif' && val2 == 'boxx.gif')
{
status.innerHTML = 'X WINS!';
return true;
}
else if(val0 == 'boxo.gif' && val1 == 'boxo.gif' && val2 == 'boxo.gif')
{
status.innerHTML = 'O WINS!';
return true;
}
}

// check top left to lower right diagonal
val0 = document.getElementById('0_0').src;
val1 = document.getElementById('1_1').src;
val2 = document.getElementById('2_2').src;
if(val0 == 'boxx.gif' && val1 == 'boxx.gif' && val2 == 'boxx.gif')
{
status.innerHTML = 'X WINS!';
return true;
}
else if(val0 == 'boxo.gif' && val1 == 'boxo.gif' && val2 == 'boxo.gif')
{
status.innerHTML = 'O WINS!';
return true;
}

// check lower left to top right diagonal
val0 = document.getElementById('2_0').src;
val1 = document.getElementById('1_1').src;
val2 = document.getElementById('0_2').src;
if(val0 == 'boxx.gif' && val1 == 'boxx.gif' && val2 == 'boxx.gif')
{
status.innerHTML = 'X WINS!';
return true;
}
else if(val0 == 'boxo.gif' && val1 == 'boxo.gif' && val2 == 'boxo.gif')
{
status.innerHTML = 'O WINS!';
return true;
}

// no winner yet return false;
}
</script>
<!-- END CHECK WINNER FUNCTION -->
<!-- NEW GAME FUNCTION -->
<SCRIPT TYPE='TEXT/JAVASCRIPT'>
function newgame()
{
var status = document.getElementById('status');
numMoves = 0;
gameOver = false;
xTurn = true;
status.innerHTML = 'Player 1 [X] GO!';

for(var x = 0; x < 3; x++)
{
for(var y = 0; y < 3; y++)
{
document.getElementById(x + '_' + y).src = 'box.gif';
}
}
}
</script>
<!-- END NEW GAME FUNCTION -->

</head>

<body bgcolor='#FFFFFF' link='#' alink='#' vlink='#'>

<h1 align='center'>Tic Tac Toe</h1>


<CENTER>
<P ID='status'>Player 1 [X] GO!</P>
<INPUT TYPE='IMAGE' ID='0_0' src='box.gif' ONCLICK='squareclicked(this);'>
<INPUT TYPE='IMAGE' ID='1_0' src='box.gif' ONCLICK='squareclicked(this);'>
<INPUT TYPE='IMAGE' ID='2_0' src='box.gif' ONCLICK='squareclicked(this);'>
<BR>
<INPUT TYPE='IMAGE' ID='0_1' src='box.gif' ONCLICK='squareclicked(this);'>
<INPUT TYPE='IMAGE' ID='1_1' src='box.gif' ONCLICK='squareclicked(this);'>
<INPUT TYPE='IMAGE' ID='2_1' src='box.gif' ONCLICK='squareclicked(this);'>
<BR>
<INPUT TYPE='IMAGE' ID='0_2' src='box.gif' ONCLICK='squareclicked(this);'>
<INPUT TYPE='IMAGE' ID='1_2' src='box.gif' ONCLICK='squareclicked(this);'>
<INPUT TYPE='IMAGE' ID='2_2' src='box.gif' ONCLICK='squareclicked(this);'>
<BR>
<INPUT TYPE='BUTTON' ID='NEWGAME' value='New Game' ONCLICK='newgame();'>
</CENTER>
</body>
</html>[/HTML]
Dec 18 '07 #1
3 1476
acoder
16,027 Expert Mod 8TB
If you alert the src, you'll see that it's the full source.

Change the check on line 15 to something like:
Expand|Select|Wrap|Line Numbers
  1. if(!src.match('boxx.gif') && !src.match('boxo.gif'))
You will also have to make changes in your checkWin function when checking the sources of the images.
Dec 21 '07 #2
Hey thanks acoder! that was making me mad i was going over it and over it and it was only that little piece i was missing =)
Dec 25 '07 #3
acoder
16,027 Expert Mod 8TB
Glad you got it working. Post again if you have any more questions.
Dec 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Nancy | last post by:
Hi, When I start my PC(winXP Pro), it always says: Unknown(): Unable toload dynamic library './php_msql.dll' - The specified module could not be found. Then my Apache servre starts, load php...
0
by: ryan baldwin | last post by:
Hi, I posted this in the GotDotNet .NET Alerts forum, but that was 3 days ago and still no reply so I'll try my luck here. My apologies if there's a better group to post this question in, but at...
2
by: Koh | last post by:
My rollover image effect work fine in Netscape but not in Internet Explorer. Is there any important element or tag that I have miss out so it is not working in Internet Explorer? Below is my code,...
6
by: Schraalhans Keukenmeester | last post by:
I want to achieve the following: A small image in each of the corners of a box, like below: +-------------------------------+ | + | + | + | + | + | +
8
by: Brad Isaacs | last post by:
Good morning friends, I am working with Visual Studio 2005, ASP.NET 2.0 I am working with the Login controls provided my .NET 2.0, trying to make the Login1 control UserName textbox obtain...
5
by: Jang | last post by:
I noticed something interesting while I was doing some JavaScript. Lets say I have the code below (simplified greatly from what I have but it's just about the same). var objs = {'foo' : {},...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.