PDA

View Full Version : Adding 3 different random numbers for Blackjack / 21??



jchudson
Oct 19th, 2009, 12:38 AM
I would like to create a Blackjack style game for one of my high school programming classes. I can generate 3 different random numbers and use these random numbers to display an image with that number on it.

How do I add the 3 separately generated numbers together to show the total??

Here is my code so far. Specifically I would like to add the myNumber1(), myNumber2() and myNumber3() results together.

<html>
<head>
<title>Play 21</title>

<script language="javascript">
function myNumber1(){
var rand_no = Math.random();
var myTotal;
var ComputerTotal;
rand_no = 5 * rand_no + 1;
rand_no = Math.floor(rand_no);
document.pic1.src = rand_no + ".gif"

myNumber2();
myNumber3();


myNumber1D();
myNumber2D();
myNumber3D();

}
</script>

<script language="javascript">
function myNumber2(){
var rand_no2 = Math.random();
rand_no2 = 5 * rand_no2 + 1;
rand_no2 = Math.floor(rand_no2);
document.pic2.src = rand_no2 + ".gif"

}
</script>


<script language="javascript">
function myNumber3(){
var rand_no3 = Math.random();
rand_no3 = 5 * rand_no3 + 1;
rand_no3 = Math.floor(rand_no3);
document.pic3.src = rand_no3 + ".gif"

}
</script>


<script language="javascript">
function myNumber1D(){
var rand_no1D = Math.random();
rand_no1D = 5 * rand_no1D + 1;
rand_no1D = Math.floor(rand_no1D);
document.pic1D.src = rand_no1D + "D.gif"

}
</script>

<script language="javascript">
function myNumber2D(){
var rand_no2D = Math.random();
rand_no2D = 5 * rand_no2D + 1;
rand_no2D = Math.floor(rand_no2D);
document.pic2D.src = rand_no2D + "D.gif"

}
</script>


<script language="javascript">
function myNumber3D(){
var rand_no3D = Math.random();
rand_no3D = 5 * rand_no3D + 1;
rand_no3D = Math.floor(rand_no3D);
document.pic3D.src = rand_no3D + "D.gif"

}
</script>

</head>

<body>

<table width="94%" border="1">
<tr>
<td width="39%"><div align="center">First Number
<img src ="1.gif" name="pic1" id=1/>
</div></td>
<td width="32%"><div align="center">Second Number
<img src ="2.gif" name=pic2 id=2/></div></td>
<td width="14%"><div align="center">Third Number
<img src ="3.gif" name=pic3 id=3/> </div></td>
<td width="15%">&nbsp;</td>
</tr>
<tr>
<td width="39%"><div align="center">First Number
<img src ="1D.gif" name="pic1D" id=1/>
</div></td>
<td width="32%"><div align="center">Second Number
<img src ="2D.gif" name=pic2D id=2/></div></td>
<td width="14%"><div align="center">Third Number
<img src ="3D.gif" name=pic3D id=3/> </div></td>
<td width="15%">&nbsp;</td>
</tr>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td colspan="4"><div align="center">
<input type="submit" name="Deal_Cards" id="Deal_Cards" value="Deal Cards" onClick="myNumber1()" />
</div></td>
</tr>
</table>
</body>
</html>

jchudson:confused:
[email protected]

Old Pedant
Oct 19th, 2009, 06:55 AM
Well, first of all, you should have only *ONE* random number routine, if you are trying to get the same range of numbers each time.

But the basic answer is that you return the value from the function and perhaps store it in a variable. And then you add the variables (or returned values).

What I don't get is why you are picking only numbers from 1 to 5 for a blackjack game.

Philip M
Oct 19th, 2009, 09:39 AM
Try this:-


<script type="text/javascript">
var list = new Array();
var total = 0;
for (var i =0; i<=2; i++) { //do it 3 times
list[i] = Math.ceil(Math.random() * 5); // a random number from 1 - 5
total = total + list[i];
}
alert (list);
alert (total);

document.pic1.src = list[0] + ".gif"; // remember an array index starts at 0
document.pic2.src = list[1] + ".gif";
document.pic3.src = list[2] + ".gif";

</script>


<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.


"I have not failed. I've just found 10,000 ways that won't work. " - Thomas Edison