Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can you help me with an array?

Status
Not open for further replies.

rabidlemming

Technical User
Sep 12, 2003
14
GB
hi,

I have included the java script below. I am trying to make an array 100 blocks big. (See in the code where it say’s "this is where I need help"). I need to put the values of 0 to 99 into the array blocks. I then need to check that when a user clicks an image it checks the image index number again the values in the array. If the number is in the array it will do an image swap. Then it will remove the number that matched the image index number from the array. This is so that once an image has been click once it can not be click again. I need an alert message to say something like "this has already been clicked on". I then need it so when the array is empty or when all the images have been click on, if the value life is still = or > 1 then alert you win. Then reload the page. It’s for a java game version of mind sweeper. There are a total of 100 images that swap over in a table. Can you please help me?
My javascript is:
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
var life = 3;
var b = 0;
var a = 9;
var v = 1;
var rNum;
var SwapImg = new Image;
SwapImg.src=&quot;BOXCORRECT.gif&quot;;
var bang = new Image;
bang.src=&quot;BANG.gif&quot;;
var numArray = new Array(a);
function makebomb() {
if (v == 1){
name = &quot;normal&quot;;
alert (&quot;You have &quot; + life + &quot; lives. You must find &quot; + (a + 1) + &quot; Mines. This is set to &quot; + name + &quot; difficulty. To change the difficulty setting click on the difficulty settings below.&quot;);
v = v + 1;
}
for(i=0;i<=a;i++) {
rNum = Math.round(Math.random()*99);
for(k=0;k<=a;k++) {
if(rNum==numArray[k]) {
rNum = Math.round(Math.random()*99);
i=0;
}
}
}
numArray=rNum;
}
function add(userschoice) {
// THIS IS WHERE I AM STUCK
THIS IS WHERE I AM STUCK
{
function easy() {
name = &quot;easy&quot;
life = 3;
a = 4;
alert (&quot;You have &quot; + life + &quot; lives. You must find &quot; + (a + 1) + &quot; Mines. This is set to &quot; + name + &quot; difficulty. To change the difficulty setting click on the difficulty settings below.&quot;);
for(j=0;j<=99;j++) {
b = b + 1;
document.images[j].src=&quot;BOX.gif&quot;;
}
makebomb();
}
function normal() {
name = &quot;normal&quot;
life = 3;
a = 9;
alert (&quot;You have &quot; + life + &quot; lives. You must find &quot; + (a + 1) + &quot; Mines. This is set to &quot; + name + &quot; difficulty. To change the difficulty setting click on the difficulty settings below.&quot;);
for(l=0;l<=99;l++) {
document.images[l].src=&quot;BOX.gif&quot;;
}
makebomb();
}
function hard() {
name = &quot;hard&quot;
life = 3;
a = 19;
alert (&quot;You have &quot; + life + &quot; lives. You must find &quot; + (a + 1) + &quot; Mines. This is set to &quot; + name + &quot; difficulty. To change the difficulty setting click on the difficulty settings below.&quot;);
for(m=0;m<=99;m++) {
document.images[m].src=&quot;BOX.gif&quot;;
}
makebomb();
}
function message1() {
alert (&quot;You have &quot; + life + &quot; life(s) left&quot;);
}
function message() {
if (v == 1);
name = &quot;notmal&quot;;
alert (&quot;You have &quot; + life + &quot; lives. You must find &quot; + (a + 1) + &quot; Mines. This is set to &quot; + name + &quot; difficulty. To change the difficulty setting click on the difficulty settings below.&quot;);
v == v + 1;
}
function swap(userschoice) {
var found = 0;
for(i=0;i<=a;i++){
if (numArray==userschoice)
found++;
}
if(found >0){
life = life - 1;
document.images[userschoice].src=bang.src;
end();
}
else
{
document.images[userschoice].src=SwapImg.src;
alert (&quot;Thew! That was close.&quot;);
}
function end() {
if (life == 0){
alert (&quot;Sorry you died Game Over&quot;);
window.location.reload();
}
else
{
message1();
}
}}}}</script>
</head>
<body onLoad=&quot;makebomb();&quot;>
<div align=&quot;center&quot;></div>
<table width=&quot;38%&quot; align=&quot;center&quot; height=&quot;339&quot; border=&quot;1&quot; cellpadding=&quot;1&quot; cellspacing=&quot;1&quot; bordercolor=&quot;#000000&quot; bgcolor=&quot;#000000&quot;>
<tr>
<td><a href=&quot;JavaScript:add(0)&quot; onMouseDown=&quot;add(0)&quot;src=&quot;BOXCORRECT.gif&quot; width=&quot;70&quot; height=&quot;70&quot;><img src=&quot;BOX.gif&quot; width=&quot;70&quot; height=&quot;70&quot;></a></td>
<td><a href=&quot;JavaScript:add(1)&quot; onMouseDown=&quot;add(1)&quot; src=&quot;BOXCORRECT.gif&quot; width=&quot;70&quot; height=&quot;70&quot;><img src=&quot;BOX.gif&quot; width=&quot;70&quot; height=&quot;70&quot;></a></td>
<td>&quot; DITTO DITTO&quot;</a></td>
<td>&quot;DITTO DITTO&quot;</a></td>
</tr>
<tr>

cheers
 
not sure i understood all your code, but couldn't you just use another array of 100 elements that has a value of 0 or 1 to show if the image has been clicked.

If it has then display the error message. If not then flip the value, swap the image, and increment a counter. Check the counter to see if it is 100.

Let me know how close I got.

Randall2nd
 
hi,

I think I could do that, but I am new to JavaScript could you possibly give me an example of the code I would need to do what you suggested as I am unsure how to do it my self.

Thank you.
 
to ensure the button doesn't get clicked again why not just set the button's disabled property to true after it's been clicked?

-kaht

banghead.gif
 
hi,

Well the button is an image, when the user click it swaps to another image. You see I am making a simple minesweeper game, and the image can swap to either a bomb or to a flag. Can I still use the set button disabled property to true after it's been clicked? If so how do i do that in java script as I don’t know the code to do that?

Thanks
 
<input type=button onclick='this.disabled=true'>

to change the picture on the button I'm sure you'd just use the style element, but I've not done it before.

-kaht

banghead.gif
 
hi,

thanks for your help I got it working now:

<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
var life = 3;
var a = 9;
var winner = 90;
var rNum;
var num = 3;
var SwapImg = new Image;
SwapImg.src=&quot;BOXCORRECT.gif&quot;;
var bang = new Image;
bang.src=&quot;BANG.gif&quot;;
var numArray = new Array(a);
function makebomb() {
alert (&quot;You have &quot; + life + &quot; lives. You must find &quot; + (a + 1) + &quot; Mines. This is set to normal difficulty. To change the difficulty setting click on the difficulty settings below.&quot;);
for(i=0;i<=a;i++) {
rNum = Math.round(Math.random()*99);
for(k=0;k<=a;k++) {
if(rNum==numArray[k]) {
rNum = Math.round(Math.random()*99);
i=0;
}
}
numArray=rNum;
}
}
function easy() {
life = 3;
a = 4;
for(j=0;j<=99;j++) {
document.images[j].src=&quot;BOX.gif&quot;;
}
makebomb();
}
function normal() {
life = 3;
a = 9;
for(l=0;l<=99;l++) {
document.images[l].src=&quot;BOX.gif&quot;;
}
makebomb();
}
function hard() {
life = 3;
a = 19;
for(m=0;m<=99;m++) {
document.images[m].src=&quot;BOX.gif&quot;;
}
makebomb();
}
var unclickedIndexes = new Array(99);
for (var i=0; i <= 99; i++){
unclickedIndexes = i;
}
function add(userschoice){
//a switch that will be set to true if image index is found
var imageIndexRemoved = false;
for (var i in unclickedIndexes){
if (userschoice == unclickedIndexes){
swap(userschoice);
unclickedIndexes.splice(i, 1);
imageIndexRemoved = true;
break;
}
}

//if we didn't find the index, show alert
if (imageIndexRemoved == false){
alert(&quot;This has already been clicked on&quot;);
}
}

function message() {
if (life >= 1);
alert (&quot;You have &quot; + life + &quot; life(s) left&quot;);
}
function swap(userschoice) {
var found = 0;
for(i=0;i<=a;i++){
if (numArray==userschoice)
found++;
}
if(found >0){
{
life = life - 1;
document.images[userschoice].src=bang.src;
end();
}
}
else
{
winner = winner - 1;
document.images[userschoice].src=SwapImg.src;
haveiwon();
}
function haveiwon() {
if (winner == 0){
alert (&quot;Congradulations! You Have Won! Well Done!&quot;);
window.location.reload();
}
else
{
alert (&quot;Thew! That was close. Only &quot; + winner + &quot; more to go.&quot;);
}
}
function end() {
if (life == 0){
alert (&quot;Sorry you died Game Over&quot;);
window.location.reload();
}
else
{
message();
}
}}
</script>
</head>
<body onLoad=&quot;makebomb()&quot;>

<div align=&quot;center&quot;></div>
<table width=&quot;38%&quot; align=&quot;center&quot; height=&quot;339&quot; border=&quot;1&quot; cellpadding=&quot;1&quot; cellspacing=&quot;1&quot; bordercolor=&quot;#000000&quot; bgcolor=&quot;#000000&quot;>
<tr>
<td><a href=&quot;JavaScript:add(0)&quot; onMouseDown=&quot;add(0)&quot;src=&quot;BOXCORRECT.gif&quot; width=&quot;70&quot; height=&quot;70&quot;><img src=&quot;BOX.gif&quot; width=&quot;70&quot; height=&quot;70&quot;></a></td>
<td><a href=&quot;JavaScript:add(1)&quot; onMouseDown=&quot;add(1)&quot; src=&quot;BOXCORRECT.gif&quot; width=&quot;70&quot; height=&quot;70&quot;><img src=&quot;BOX.gif&quot; width=&quot;70&quot; height=&quot;70&quot;></a></td>

ditto ditto
then at the end of the page

<td><div align=&quot;center&quot;><a href=&quot;javascript:scroll(0,0);easy()&quot;><img src=&quot;Easy.GIF&quot; width=&quot;100&quot; height=&quot;50&quot; border=&quot;0&quot;></a></div></td>
<td><div align=&quot;center&quot;><a href=&quot;javascript:scroll(0,0);normal()&quot;><img src=&quot;Normal.GIF&quot; width=&quot;100&quot; height=&quot;50&quot; border=&quot;0&quot;></a></div></td>
<td><div align=&quot;center&quot;><a href=&quot;javascript:scroll(0,0);hard()&quot;><img src=&quot;Hard.gif&quot; width=&quot;100&quot; height=&quot;50&quot; border=&quot;0&quot;></a></a></div></td>

thank you for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top