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>
<head><title>Cards</title>
<script type="text/javascript">
function spin()
{
var rands1 = Math.ceil((Math.random()*4));
var randn1 = Math.ceil((Math.random()*13));
var img = new Image();
if (randn1==10)
{
randn1="0"
}
if (randn1==11)
{
randn1="J"
img.src = "weba.jpg";
}
if (randn1==12)
{
randn1="Q"
}
if (randn1==13)
{
randn1="K"
}
cardname1=rands1+"-"+randn1+".jpg";
document.getElementById("sometext").innerHTML = cardname1;
var rands2 = Math.ceil((Math.random()*4));
var randn2 = Math.ceil((Math.random()*13));
if (randn2==10)
{
randn2="0"
}
if (randn2==11)
{
randn2="J"
}
if (randn2==12)
{
randn2="Q"
}
if (randn2==13)
{
randn2="K"
}
cardname2=rands2+"-"+randn2+".jpg";
document.getElementById("sometext2").innerHTML = cardname2;
if (((randn1==0) && (randn2==0) ||(randn1==1) && (randn2==1) ||(randn1==2) && (randn2==2) ||(randn1==3) && (randn2==3) ||(randn1==4) && (randn2==4)))
{
document.getElementById("sometext3").innerHTML = "snap";
}
else
{
document.getElementById("sometext3").innerHTML = "thi is fun";
}
}
</script>
</head>
<body>
<br/><br/><br/><br/>
<button type="button" onclick="spin()">deal</button>
<div id="sometext">A</div>
<div id="sometext2">B</div>
<div id="sometext3">who is going to win</div>
</body>
</html>
var rands1 = Math.ceil((Math.random()*4));
var randn1 = Math.ceil((Math.random()*13));
In each case, there is a small but FINITE CHANCE that the number you get will be ZERO!
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);
var randn1 = 1 + Math.floor(Math.random()*13);
In general the formula is:
Code:
function getRandomInt( low, high )
{
return low + Math.floor( Math.random() * ( high - low + 1 ) );
}
(which code assumes low and high are themselves integers).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
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";
so you are only (trying!) to affect an image for player 1 and then only when the card is a Jack.
And then, as you wrote
Quote:
they just come out on the page like 1-k, 1-4 and so on.
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.
is there a way to get we say an image of a playing card up rather than each set of numbers.
Not unless you *HAVE* a set of images of all the cards.
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"???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
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