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. - function turnOver(e)
-
{
-
if (matchCount<8 & !matched[e.name])
-
{
-
-
if (clicked<1)
-
{
-
firstCard=e; //grab first card object
-
pair=cards[firstCard.name]; //get first pair number from cards array
-
e.src=document.images["load"+pair].src; //flip over first card
-
clicked++;
-
}
-
else
-
{
-
e.src=document.images["load"+cards[e.name]].src; //flip over second card
-
if (pair!==cards[e.name])
-
{ //check for snap
-
alert("That's not a match, try again!");
-
firstCard.src=document.images["load0"].src; //flip first card back
-
e.src=document.images["load0"].src; //flip second card back
-
count1 ++;
-
-
}
-
else
-
{
-
//This is a match so record matched cards and increase matchCount
-
matched[firstCard.name]=true;
-
matched[e.name]=true;
-
matchCount++;
-
}
-
clicked=0;
-
}
-
}
-
var plus = (matchCount*5);
-
var minus = count1;
-
var score = (plus - minus);
-
if (matchCount > 7)
-
{
-
alert("Congratulations you matched "+matchCount+" pairs, for "+plus+" points with "+minus+" misses, giving you " +score+ " overall");
-
}
-
-
}
-
-
var high score=0;
-
var old score=0;
-
if (score > high score)
-
{
-
old score = high score;
-
high score = score;
-
alert("gfag "+high score+" gfgfg");
-
}
6 3907
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.
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
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: - var highScore=0;
-
var oldScore=0;
-
if (score > highScore)
-
{
-
oldScore = highScore;
-
highScore = score;
-
alert("gfag "+highScore+" gfgfg");
-
}
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.
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: - var highScore=0;
-
var oldScore=0;
-
if (score > highScore)
-
{
-
oldScore = highScore;
-
highScore = score;
-
alert("gfag "+highScore+" gfgfg");
-
}
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.
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.
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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
|
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
|
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()
{
...
|
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 =...
| |
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
...
|
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 - ...
|
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.
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |