473,473 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

card memory game in javascript

6 New Member
Hi i'm having some trouble with a card memory game. Typical type. You turn over one card then another, if they dont match you turn them both back over and start again.

I've got this 'if else' loop and the only way i can get the 'count number of turns' to work is by having the variables inside the 'if' statement which doesn't seem right to me.

My main concern though is right at the very bottom of the code, i've tried to take the SCORE variable and see if it is greater than HIGH SCORE, if so HIGH SCORE becomes OLD SCORE and SCORE becomes HIGH SCORE.
For some reason it keeps coming up with error, object expected. I've checked for spelling mistakes and the usual stuff but cant work out whats going on.

Any help muchely appreciated.

Expand|Select|Wrap|Line Numbers
  1. function turnOver(e)
  2. {
  3.   if (matchCount<8 & !matched[e.name])
  4.   {    
  5.  
  6.     if (clicked<1)
  7.     {
  8.       firstCard=e; //grab first card object
  9.       pair=cards[firstCard.name]; //get first pair number from cards array
  10.       e.src=document.images["load"+pair].src; //flip over first card
  11.       clicked++;
  12.     } 
  13.     else
  14.     {
  15.       e.src=document.images["load"+cards[e.name]].src; //flip over second card
  16.       if (pair!==cards[e.name])
  17.       { //check for snap
  18.         alert("That's not a match, try again!");
  19.         firstCard.src=document.images["load0"].src; //flip first card back
  20.         e.src=document.images["load0"].src; //flip second card back
  21.         count1 ++;
  22.  
  23.       }
  24.       else
  25.       {
  26.         //This is a match so record matched cards and increase matchCount
  27.         matched[firstCard.name]=true;
  28.         matched[e.name]=true;
  29.         matchCount++;        
  30.       }      
  31.       clicked=0;  
  32.     }
  33.   }
  34. var plus = (matchCount*5);
  35. var minus = count1;
  36. var score = (plus - minus);
  37. if (matchCount > 7)
  38.   {
  39.   alert("Congratulations you matched "+matchCount+" pairs, for "+plus+" points with "+minus+" misses, giving you " +score+ " overall");
  40.   }
  41.  
  42. }   
  43.  
  44. var high score=0;
  45. var old score=0;
  46. if (score > high score)
  47.     {
  48.     old score = high score;
  49.     high score = score; 
  50.     alert("gfag "+high score+" gfgfg");
  51.     }
Feb 1 '07 #1
6 3882
duye
10 New Member

var high score=0;
var old score=0;
if (score > high score)
{
old score = high score;
high score = score;
alert("gfag "+high score+" gfgfg");
}
var high score=0; do you have a space between high and score? space is not allowed for naming a variable.

try this highScore oldScore.
Feb 1 '07 #2
hicksmcb
6 New Member
var high score=0; do you have a space between high and score? space is not allowed for naming a variable.

try this highScore oldScore.
Tried that, didnt work. My main concern is why i have to name my variables inside the loop to get them to work
Feb 1 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
Tried that, didnt work. My main concern is why i have to name my variables inside the loop to get them to work
Did you change the bottom code to:
Expand|Select|Wrap|Line Numbers
  1. var highScore=0;
  2. var oldScore=0;
  3. if (score > highScore)
  4.     {
  5.     oldScore = highScore;
  6.     highScore = score; 
  7.     alert("gfag "+highScore+" gfgfg");
  8.     }
As Duye said, a variable cannot contain spaces.

As to your second query, you don't have to "name" variables (I assume by that you mean declare them), but you must use them if you are going to maintain a count.

You could declare global variables outside the function, set them to 0 to begin with, then use them freely within the function. Sometimes, you may need to set values to 0 to begin a loop.

Hope that helps. If I've misunderstood or you need a better explanation, please post again. Perhaps you could give examples of which variables you are referring to.
Feb 1 '07 #4
hicksmcb
6 New Member
Yeah sorry i meant 'declare'. I tried to set oldscore, highscore etc as global variables but for some reason it was as if the loop didnt recognise them and i kept getting a '0' value or a not declared value back for the calculations from the loop. I was reading up on functions.
I did try taking away the space for the variables and it still doesnt work. Thanks for your help anyway. If you can think of anything else it would be appreciated.
I'm not very good with if statements so it's probably that i've put the code inside the wrong curly brackets or something.

Did you change the bottom code to:
Expand|Select|Wrap|Line Numbers
  1. var highScore=0;
  2. var oldScore=0;
  3. if (score > highScore)
  4.     {
  5.     oldScore = highScore;
  6.     highScore = score; 
  7.     alert("gfag "+highScore+" gfgfg");
  8.     }
As Duye said, a variable cannot contain spaces.

As to your second query, you don't have to "name" variables (I assume by that you mean declare them), but you must use them if you are going to maintain a count.

You could declare global variables outside the function, set them to 0 to begin with, then use them freely within the function. Sometimes, you may need to set values to 0 to begin a loop.

Hope that helps. If I've misunderstood or you need a better explanation, please post again. Perhaps you could give examples of which variables you are referring to.
Feb 5 '07 #5
acoder
16,027 Recognized Expert Moderator MVP
What do you mean by 'loop'? I can't see any loops in the code that you've posted.
Feb 5 '07 #6
UniDyne
18 New Member
I think the bit of code where you check the highScore and the current score should go in your function right after you display the Congrats message. Checking the scores outside of that would be pointless because you just declared them as zero and they haven't changed yet!
Feb 7 '07 #7

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

Similar topics

1
by: Gary Camblin | last post by:
Has anyone got a script to run a higher or lower card game on a web page. Like the game show play you cards right.
4
by: freedomdsb | last post by:
I am looking for the game 'memory' in C code. This is pretty urgent, so any replies would be appreciated. Thanx
2
by: Peted | last post by:
Hi. i wanted to write a c# exe (no winforms) that can be loaded once and stay resident in memory. this exe accepts paramters like port and ip address on initialization, then transmits string...
2
by: irm2216 | last post by:
I'm trying to make a memory game where you try to link up the two corresponding colored circles, but if they are the wrong colors, they go back to being a blank canvas. The graphics methods do not...
10
by: Humakt | last post by:
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...
3
by: ky11a | last post by:
I've a memory game. It cannot work. It can only be refreshed and show all. The tile image cannot be flipped. It should be turned over by clicking the tile image with the mouse click. If the player...
1
by: kempablavitt | last post by:
Hi. I need some help with a memory game. How should I structure it before I begin to write it. Is it good to have a class that defines a matrix for the play board and something like that? Or...
4
by: pghantasala | last post by:
How to get available physical memory using javascript NOT USING WMI ?? any other way? I need to achieve this by using javascript/vbscript and I cannot use WMI. Please let me know the possibilities.
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.