473,804 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ 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 3907
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
2692
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
2036
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
1548
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 data over a tcp/ip socket after it is resident in memory. I want to send the string data to the memmory resident exe from javascript code running in IE 6/7
2
4700
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 link up, and I'm not sure why. Anyone mind taking a look at my code and seeing what the problem is? board = new int; /*Sets the background of your memory board to black*/ public void init() { ...
10
2062
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 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 =...
3
2247
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 turns over twp tiles with the same image, the tiles remain face up. If the player turns over tiles with different images, the tiles are flipped over in 0.8 seconds. I've uploaded my files into the following links: freewebs.com/ky11a/index.htm ...
1
2160
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 should I use functions that defines lists and for-slings? Whats the easiest way? Im thinking about lists, and then functions to do all the commands from the user and everything else. its going to look something like this: 1 2 3 4 5 6 A - ...
4
7898
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
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3827
muto222
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.