473,324 Members | 2,567 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,324 software developers and data experts.

Javascript problem: Memory game

42
Hey all. I'm doing a memory game with Javascript using innerhtml to generate table where memory "cards" are depending on difficulty. The work for the code is still in progress as I have to make functions for gameplay as well. Though there's no sense to proceed if I can't get making the table to work.

Currently I've made tablecode for easy difficulty (helppo) however, when I click into image which should generate the code into div (id = pelialue), nothing happens. I'm not very experienced in javascript so there might be some simple thing I haven't noticed. Hope some of you can help me. Variables and function names are in my native language (finnish).

(PHP or Flash ain't option)

Here's the unfinished code entirely:

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Muistipeli
</title>
<link rel="stylesheet" type="text/css"
href="tyyli.css" />
<script language=">
function Pala(nimi, kuva, monta){
this.nimi = nimi;
this.kuva = kuva;
this.monta = monta;
}
function valittu(nimi){

}
function kokoa(paloja){
var pelipalat = "";
var kerta = 0;
var valittuja = 0;
while (paloja > 0){
random = Math.ceil(Math.random() * (paloja/2));
if(palat[random-1].monta==2){valittuja++;}
if(!palat[random-1].monta===0 && !(valittuja>=(paloja/2))){
pelipalat = pelipalat + "<td><img src='Tausta.bmp' name='"+palat[random-1].nimi+"' onclick='valittu("+palat[random-1].nimi+");'></td>";
palat[random-1].monta--;
paloja--;
kerta++;
if(kerta==4){
pelipalat= pelipalat + "</tr><tr>";
kerta=0;
}
}
}
return pelipalat;
}

var palat = [];
palat[0] = new Pala("Kakku","Kakku.bmp", 2);
palat[1] = new Pala("Kummitus","Kummitus.bmp", 2);
palat[2] = new Pala("Kyna","Kyna.bmp", 2);
palat[3] = new Pala("Kirja","Kirja.bmp", 2);
palat[4] = new Pala("Kukka","Kukka.bmp", 2);
palat[5] = new Pala("Talo","Talo.bmp", 2);
function helppo(){
var helppous = "<table><tr>";
helppous = helppous + kokoa(12);
helppous = helppous + "</tr></table>";
document.getElementById('pelialue').innerHTML = helppous;
}
</script>
</head>
<body>
<table class="vaikeustaso">
<tr>
<th colspan="3">VALITSE UUSI PELI</th>
</tr>
<tr>
<td class="vaikeus" width="113px"><img class="vaikea" src="Helppo.bmp" alt="Aloita helppo peli" onMouseUp="document.helpponappi.src='Helppo.bmp'; helppo();" onMouseDown="document.helpponappi.src='Helppopain. bmp'; " onMouseOut="document.helpponappi.src='Helppo.bmp'; " name="helpponappi"></td>
<td class="vaikeus" width="142px"><img class="vaikea" src="Normaali.bmp" alt="Aloita normaali peli" onMouseUp="document.norminappi.src='Normaali.bmp'; normaali();" onMouseDown="document.norminappi.src='Normaalipain .bmp'; " onMouseOut="document.norminappi.src='Normaali.bmp' ;" name="norminappi"></td>
<td class="vaikeus" width="108px"><img class="vaikea" src="Vaikea.bmp" alt="Aloita vaikea peli" onMouseUp="document.vaikeanappi.src='vaikea.bmp'; vaikea();" onMouseDown="document.vaikeanappi.src='Vaikeapain. bmp'; " onMouseOut="document.vaikeanappi.src='Vaikea.bmp'; " name="vaikeanappi"></td>
</tr>
</table>

<div id="pelialue" align="center" class="pelialue">Pelialue
</div>

</body>
</html>[/HTML]
Aug 12 '08 #1
10 2026
acoder
16,027 Expert Mod 8TB
The first problem is this line:
[html]<script language=">[/html]Replace that with:
[html]<script type="text/javascript">[/html]

PS. please use code tags when posting code. Thanks!
Aug 12 '08 #2
rnd me
427 Expert 256MB
document.helpponappi.src is not the propert way to point to a tag by id.

use document.getElementById("id") instead.
Aug 12 '08 #3
Humakt
42
Thanks for the help, and now I've managed to make it print some table cells with test clauses but it still doesn't get quite finished with the table. The problem is in function kokoa(), I believe.

In other words, it seems to stuck in eternal loop somehow.
Aug 13 '08 #4
acoder
16,027 Expert Mod 8TB
That's probably because it doesn't meet the if condition to decrease the paloja value (to end the loop). What exactly is the while loop supposed to do, i.e. what are you expecting it to do, step by step?
Aug 13 '08 #5
Humakt
42
I think I solved it for now. Thanks for the help people. Indeed cause was in the if statement in kokoa function.

I'll ask here if I encounter new problems.
Aug 13 '08 #6
acoder
16,027 Expert Mod 8TB
No problem. Glad to hear that you managed to solve it. Can you post the solution?
Aug 13 '08 #7
Humakt
42
Well, it didn't function properly actually after I did the fix. But now it works after I started to make a detailed post about the problem with code comments of it here (which made me double check and rethink most bits).

(Fixed) code for the function:

Expand|Select|Wrap|Line Numbers
  1. function kokoa(paloja){
  2.     var pelipalat = "";
  3.     var kerta = 0;
  4.     var tosipala = paloja;
  5.     var valittuja = 0;
  6.     var random;
  7.     while (paloja > 0){
  8.         random = Math.floor(Math.random() * palat.length);
  9.         while (palat[random].monta==0){random = Math.floor(Math.random() * palat.length);}
  10.         if(!(palat[random].monta==2 && valittuja>(tosipala/2))){
  11.             if(palat[random].monta==2){valittuja++;}
  12.             pelipalat = pelipalat + "<td><img src='Tausta.bmp' name='"+palat[random].nimi+"' onclick='valittu("+palat[random].nimi+");'></td>";
  13.             palat[random].monta = palat[random].monta - 1;
  14.             paloja--;
  15.             kerta++;
  16.             if(kerta==5){
  17.                 pelipalat= pelipalat + "</tr><tr>";
  18.                 kerta=0;
  19.             }
  20.         }
  21.     }
  22.     return pelipalat;
  23. }
I had changed it a bit but the real stupid oversight was this:

if(!(palat[random].monta==2 && valittuja>(paloja/2))){

Notice what was the problem?
Aug 13 '08 #8
acoder
16,027 Expert Mod 8TB
Thanks for posting. It'd make it easier if you could translate the variable names for me to answer that question.
Aug 13 '08 #9
Humakt
42
Thanks for posting. It'd make it easier if you could translate the variable names for me to answer that question.
I don't think strict translations would be enough in that case, I'd have to explain them as well. But it isn't important. What is important is that I've completed this pairs game.

But the reason why it got stuck on the loop was valittuja > paloja / 2.
You see that I decrease paloja in the loop. This was the oversight that made it stuck in loop. That's why I added another variable tosipala, to replace paloja / 2, which doesn't get decreased.
Aug 15 '08 #10
acoder
16,027 Expert Mod 8TB
Thanks for the explanation. There wasn't even a need for the translation - I could've just replaced the variables with x/y; just being a bit lazy ;)
Aug 17 '08 #11

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

Similar topics

2
by: Lee Garrington | last post by:
Hey, Recently I decided to learn C++ so that I could port over one of my Java programs to make it faster. Basically everything has ported over fine so far until I came up against the following...
13
by: hurry | last post by:
In order to avoid declaring static variables in a function I was asked to write a scratch memory. Reserve a block of memory outside the function and assigning pointers to the memory locations as...
1
by: Van Dugall | last post by:
Hello I have been writing a game in javascript which works fine but I wanted to step it up and request the data of the game from the server. I haven't really used php...all I know is from the...
1
by: giovannino | last post by:
Dear all, I did a query which update a sequence number (column NR_SEQUENZA) in a table using a nice code (from Trevor !). 1) Given that I'm not a programmer I can't understand why...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.