![]() |
javascript basic card match game
Hi guys, started javascript 2 weeks ago and started working on a basic card came.. Im trying to get to cards with the same number to win. it is real basic now.. and I have it some what working but I want it to show images instead of the numbers here is the code:
Code:
<html> |
basically, you want a list of image urls, and then you can use a common number to refer to each one.
it could working something like : Code:
cardname2='<img src=' + JSON.stringify(randn2+'.jpg') + ' />';ps, i did the same thing once upon a time; see http://danml.com/pub/memory.htm for a peek at how i did it a while back... |
thanks man. Is there a way to do it using images I have on my computer.
|
Quote:
|
ya thats the way i assumed it would work., like for example i named my images like so 1-k, 1-J and so on assuming that this method would work,
Code:
cardname1=rands1+"-"+randn1+".jpg"; |
This way of getting random numbers is *WRONG*:
Code:
var rands1 = Math.ceil((Math.random()*4));That's because there is a small but finite chance that Math.random() can produce a zero value. Multiply zero by anything and then take the Math.ceil() of it and you still have zero. The *CORRECT* formula is Code:
var rands1 = 1 + Math.floor(Math.random()*4);Code:
function getRandomInt( low, high ) |
thanks for that man, would you believe that my lecturer gave me that part of the code. I noticed all over the net was the way you had it too.. so was curious to know the difference.. have you any other input on the specific part that is causing me problems.
|
|
Your lecturer is, therefore, know to be incompetent at coding. In any language. As this method for for getting random integers is essentially identical in JavaScript, Java, C, C++, even BASIC of various flavors, and many other languages. SO if he/she didn't learn the right way in *SOME* language, then he/she has no business trying to teach others.
&&&&&&&&&&&&& There are a lot of things in your code that don't make sense. For starters, the only place you actually ever even *ATTEMPT* to change an <img> is here: Code:
img.src = "weba.jpg";And then, as you wrote Quote:
You are doing Code:
document.getElementById("sometext2").innerHTML = cardname2; |
And I don't pretend to understand your "winner" logic.
Code:
if (((randn1==0) && (randn2==0) ||(randn1==1) && (randn2==1) ||(randn1==2) && (randn2==2) ||(randn1==3) && (randn2==3) ||(randn1==4) && (randn2==4)))And how does the fact that the two NUMBERS are the same *AND* in the range of 1 through 4 cause you to write "Snap" as the "winner"??? I don't understand your rules, at all. |
This probably doesn't conform to your "rules", but at least it does something.
Code:
<html> |
Glad you think so, cause it seems he avoids sharing any knowledge in javascript (maybe he doesnt have the knowledge to, he is well hated!!).. Ya some bits are very messy, I was just throwing bits together of previous samples I had hoping some sort of structure would come together.. is there a way to get we say an image of a playing card up rather than each set of numbers.
|
oh and the game is based on a deck of cards, like if you had we say a 2 of hearts and a 2 of clubs you win, like you keep putting the cards up on top of each other face up until you get a match (suites can be different).. I hadnt fully implemented the full rule logic as I was already experiencing issues.
|
Quote:
Such sets of images are available on the web (google is your friend) but I don't know if any of them are free. But if you don't have a set of images, then at least you should put up meaningful names, as I showed you, rather than "3-0.jpg". How is anybody supposed to know that means "the 10 of hearts"??? |
My knowledge is limited.. and that is how it was being explained and I personally thought it was me lacking and being confused.. but it seems his way is very awkward.. He was saying that if you named the images that way like 1-J etc. it would show the images instead of the text. But obviously not. does this bit a code from above make sense
Code:
cardname1=rands1+"-"+randn1+".jpg"; |
| All times are GMT +1. The time now is 06:25 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.