JavaScript - Adding 3 Different Random Numbers For Blackjack / 21??
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%"> </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%"> </td> </tr> </tr> <tr> <td> </td> <td> </td> <td colspan="2"> </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 jchudson@mts.net Similar TutorialsHi, I have this bit of java code that puts a random/unique number in a field on my web page: <html> <head> </head> <body> <form> <input type="text" name="MyField" /> </form> <script langueage="javascript" type="text/javascript"> var d = new Date(); var tm = d.getTime(); document.getElementsByName('MyField')[0].value=tm; </script> </body> </html> My problem is that I want to limit the number to just 6 digits. Is this possible? Any help would be much appreciated Thank you Dave 1st, Hi Coding forums! I'll get to the point, I'm a noob -.- I'm using greasemonkey for a certain website. Basically, I'm using gm to refresh the page after a certain time, I was however wondering, If I can set it to a random number between 2 specific times? The code I'm using atm is setTimeout(function() { document.location.reload(); } , 10000); I know I have to use math.random, well, I think I do. But I'm not sure how to do it between 2 certain times? So to summarise, I'm trying to refresh a page at any given random time between say 5-10 minutes. Any help is appreciated, thanks! hi, My script creates random numbers as shown in ; 2,5,8 1,4,5 8,9,4 5,3,3 1,1,2 2,5,8 5,3,3 ...... ...... but there are many duplications as in row1 and row6(2,5,8.)....row4 and row7 how can we modify the script so that there wont be any smilir lines.. thanks.. here is my script. PHP Code: <script> keywords1 = ["1","4","5","7","2","3"]; keywords2 = ["5", "9", "2", "10","11","6","4"]; keywords3 = ["13", "1", "3", "4", "6","7","8","2"]; var toplam=0; z1=keywords1.length z2=keywords2.length z3=keywords3.length toplam=z1*z2*z3; say=0; zaman=setInterval("yaz()",500); function yaz() { var keyword1 = keywords1[Math.floor(Math.random()*keywords1.length)]; var keyword2 = keywords2[Math.floor(Math.random()*keywords2.length)]; var keyword3 = keywords3[Math.floor(Math.random()*keywords3.length)]; say++; document.getElementById("kutu").innerHTML+=keyword1+".."+keyword2+".."+keyword3; document.getElementById("kutu").innerHTML+="<br>"; document.getElementById("kutu2").value=say; document.getElementById("kutu3").value=toplam; if(say>=toplam) dur(); } function dur() { clearInterval(zaman); } </script> <input type=button value=yazz onclick="yaz()">.. .<input type=button value=dur bakalım onclick="dur()"> ....<input type=text value="" id=kutu2 size=3> ....<input type=text value="" id=kutu3 size=3> <div id="kutu" style="position:absolute;top:5;left:250;background-color:yellow;border-style:solid;border-width:5;border-color:red;"></div> <div id="kutux1" style="position:absolute;top:5;left:350;background-color:yellow;border-style:solid;border-width:5;border-color:red;"></div> </body> </html> Hi all.. I've been trying to get random numbers in JS. It works when I assign fixed values, but when I want those values to come from a form, I get all the problems.. any comments on my script? Thanks. M [CODE] <script type="text/javascript"> function random() { var x=document.getElementById("text1").value;// num ingresado por el usuario var y=document.getElementById("text2").value; //var x=1; //var y=6; num=Math.floor(x + Math.random() * y); var r=document.getElementById("demo") r.innerHTML=num; } </script> [CODE] Hello all, I hope someone can help me with this. I have a form wich has a add/remove row button, this so the visitor can select multiple sets of data. All is fine and is being submitted via PHP. Only problem is from number 10 and up, i am getting strange random numbers in the new rows that are added in their name. Like 1111 (instead of 11), 122222 (instead of 12). And because of this, every row from 10 and up won't be send through php, giving this random effect. I can't seem to find why this is happening. Hopefully someone can help me out in this. The full form can be viewed http://www.multisearch.info/test/form_ruby_2.html The code i use for my Clone Element is Code: //clone element var clone; function cloneRow(row){ clone=row.cloneNode(true); } function addRowToTable(row){ var tbody=row.parentNode; var inputs=clone.getElementsByTagName('input'), i=0, inp; i=0; while(inp=inputs[i++]){ if(inp.type=='text'){ inp.onfocus=function(){this.value='';} } if(inp.type=='button'){ inp.value=='Add'?inp.onclick=function(){addRowToTable(this.parentNode.parentNode)}:inp.onclick=function(){removeRowFromTable(this.parentNode.parentNode)}; } } tbody.appendChild(clone); cloneRow(tbody.getElementsByTagName('tr')[tbody.getElementsByTagName('tr').length-1]); reorderNames(); } function removeRowFromTable(row){ row.parentNode.removeChild(row); reorderNames(); } function reorderNames(){ var rows=document.getElementById('tblSample').getElementsByTagName('tbody')[0].getElementsByTagName('tr'), r, i=0, el, e, j; while(r=rows[i++]){ el=r.getElementsByTagName('*'); j=0; while(e=el[j++]){ e.nodeName=='SELECT'||(e.nodeName=='INPUT'&&e.type=='text')?e.name=e.name.replace(/\d/,i):null; } } } onload=function(){ var row=document.getElementById('tblSample').getElementsByTagName('tr')[0]; cloneRow(row); } i have a table with a couple random numbers, and i want to click a button that will dynamically regenerate the numbers each time the button is clicked without re-generating the entire table., im using DOM and innerhtml for these random numbers. heres the javascript and html code. so far, it just generates the random numbers when the page loads. var random = Math.floor(Math.random()*40 + 1) //sets variables for random numbers to generate var random2 = Math.floor(Math.random()*40 + 1) var random3 = Math.floor(Math.random()*40 + 1) var random4 = Math.floor(Math.random()*40 + 1) var random5 = Math.floor(Math.random()*40 + 1) var random6 = Math.floor(Math.random()*40 + 1) //create table function makeTable(lotto) document.getElementById("tableSpan").innerHTML = '<table border="1" id="lotto">'; var caption=document.getElementById('lotto').createCaption(); caption.innerHTML="JavaScript Random Numbers"; var x=document.getElementById('lotto').insertRow(0); var cell1=x.insertCell(0); var cell2=x.insertCell(1); var cell3=x.insertCell(2); var cell4=x.insertCell(3); var cell5=x.insertCell(4); var cell6=x.insertCell(5); cell1.innerHTML='<td class = "normal">'+ random +'</td>'; cell2.innerHTML='<td class = "normal">'+ random2 +'</td>'; cell3.innerHTML='<td class = "normal">'+ random3 +'</td>'; cell4.innerHTML='<td class = "normal">'+ random4 +'</td>'; cell5.innerHTML='<td class = "normal">'+ random5 +'</td>'; cell6.innerHTML='<td class = "red">'+ random6 +'</td>'; } heres the HTML file: <body onload="makeTable('lotto');"> <div id="container"> <div id="header"> <h1>Welcome</h1> </div> <div id="content"> <span id="tableSpan"></span> <input type="button" value="Re-generate Numbers" onclick="makeTable('lotto');" /> </div> { Hello! I am trying to create a javascript code in which I prompt the user for a number (an integer from 0 to 100) to search for, then search a function with the number they entered and then display whether the number was found, and if found, a location where it can be found within the list. Can you please tell me what is wrong? <html> <head> </head> <body> <script language = "javascript"> var list = new Array(200); for (var i=0; i < 200; i++){ list=Math.round(Math.random()*100) + " "; document.write(list); } var value=prompt("What is the number you are searching for? ", " "); if (list[i]==value){ {alert("Number has been found in location" +i)}; } </script> </body> </html> Thank you Hi, Want to add numbers using JS. Have 4 text boxes for user entry. Want either a label (preferred) or fifth text box to automatically sum those entries. Did following but not working. Any suggestions? <script language="javascript"> var addRange = function() { var NoHrsRangeComp = document.getElementById("NoHrsRangeComp"); var NoHrsRangeCred = document.getElementById("NoHrsRangeCred"); var NoHrsRangeOT = document.getElementById("NoHrsRangeOT"); var NoHrsRangeRC = document.getElementById("NoHrsRangeRC"); var RangeSum = document.getElementById("RangeSum"); var sum = 0; if (isNaN(parseFloat(NoHrsRangeComp.value))){ NoHrsRangeComp.value = ""; } if (isNaN(parseFloat(NoHrsRangeCred.value))){ NoHrsRangeCred.value = ""; } if (isNaN(parseFloat(NoHrsRangeOT.value))){ NoHrsRangeOT.value = ""; } if (isNaN(parseFloat(NoHrsRangeRC.value))){ NoHrsRangeRC.value = ""; } sum = parseFloat(NoHrsRangeComp.value) + parseFloat(NoHrsRangeCred.value) + parseFloat(NoHrsRangeOT.value) + parseFloat(NoHrsRangeRC.value); RangeSum.innerHTML = sum; } </script> Input as follows: <input style="width: 50px" type="text" name="NoHrsRangeComp" id="NoHrsRangeComp" onblur="addRange();"/> <input style="width: 50px" type="text" name="NoHrsRangeCred" id="NoHrsRangeCred" onblur="addRange();"/> <input style="width: 50px" type="text" name="NoHrsRangeOT" id="NoHrsRangeOT" onblur="addRange();"/> <input style="width: 50px" type="text" name="NoHrsRangeRC" id="NoHrsRangeRC" onblur="addRange();"/> <input style="width: 50px" type="text" name="RangeSum" id="RangeSum" onblur="addRange();"/> For above entry prefer - label makes it look more like usual addition and does not confuse users to want to make entry. <label id="RangeSum" onblur="addRange();" style="border-bottom:medium"></label> John Hey, I'm trying to do a homework problem that involves using cookies to add numbers. Here is what I've done so far: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Homework 26.9</title> <script language="javascript"> function createCookie(name, value, expiredays) { todayDate = new Date(); todayDate.setDate(todayDate.getDate() + expiredays); document.cookie = name + "=" + value + "; expires =" + todayDate.toGMTString() + ";"; } function printCookies() { document.write("Cookies a " + document.cookie + "<br />"); document.write("Length of cookie string = " + document.cookie.length); } function overwriteCookie(name, value, expiredays) { todayDate = new Date(); todayDate.setDate(todayDate.getDate() + expiredays); document.cookie = name + "=" + value + "; expires=" + todayDate.toGMTString() + ";"; } function readCookieValue() { allCookies = document.cookie.length; if (allCookies == 1) { //split each name/value pair into its name and value args = document.cookie.split(';'); for (i = 0; i < args.length; i++) { //split each name/value pair into its name and value nameValue = args[i].split("="); name = nameValue[0]; value = nameValue[1]; //remove leading whitespace from cookie name if (name.length != 0) { if (name.charAt(0) == " ") { name = name.substr(1, name.length - 1); } } //unescape the values name = unescape(name); value = unescape(value); return value; } } } var a = prompt("How many numbers would you like to add?"); var b; var c; var d; for (var i = 1; i <= a; i++) { if (i == 1) { b = prompt("Enter Number " + i + ":"); createCookie("sum", b, 1); } else { b = prompt("Enter Number " + i + ":"); c = readCookieValue(); d = b + c; overwriteCookie("sum", d, 1); } } printCookies(); </script> </head> <body> </body> </html> The code is simply supposed to prompt the user asking how many numbers he/she would like to add. Then it is supposed to have additional prompts asking for each individual number. As the numbers are being inputted, it is supposed to keep a running sum via cookies and then output the total at the end. The problem that I'm having is that I don't think the cookies are even being created which is weird because the createCookie(), printCookie(), and overWriteCookie() methods are copied directly from the textbook. Any help would be greatly appreciated. Thanks. I have the following code that I wrote myself (except for the function isNumberKey). However, I don't know how to actually add the numbers together. My plan is to store the value entered in the forms as a variable, then manipulate the variables to get my final number. The final number would be stored in variable named Total and would be displayed after Total: at the bottom of my page. Could anyone help me? I can provide additional information if you need it. Code: <HTML> <TITLE>Magazine Prices </TITLE> <BODY> <SCRIPT language=Javascript> <!-- Price = oForm.elements["price"].value; Quantity = oForm.elements["quantity"].value; Subtotal = oForm.elements["subtotal"].value; Shipping = oForm.elements["shipping"].value; function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } //--> </SCRIPT> <!-- Price * Quantity = Subtotal. Subtotal value should fill in automatically in the Subtotal input field. Subtotal * Shipping = Total. //--> <TABLE> <TR><TD>Price: </TD><TD><INPUT id="Price" name="Price" onkeypress="return isNumberKey(event)" type="text" name="txtChar"> </TD></TR> <TR><TD>Quantity:</TD> <TD><INPUT id="Quantity" name="Quantity" onkeypress="return isNumberKey(event)" type="text" name="txtChar"> </TD></TR> <TR><TD>Subtotal:</TD> <TD><INPUT id="Subtotal" name="Subtotal" onkeypress="return isNumberKey(event)" type="text" readonly="readonly" name="txtChar"> </TD></TR> <TR><TD>Shipping:</TD> <TD><INPUT id="Shipping" name="Shipping" onkeypress="return isNumberKey(event)" type="text" value="10%" readonly="readonly" name="txtChar"> </TD></TR> </TABLE> <b>Total: </b> </BODY> </HTML> I've been looking for a solid solution for how to add numbers to markers. Most that I've seen involve an asp page with a post back of an image based on the query string. I am trying to do something like what MarkerClusterer does. Here is an example: http://google-maps-utility-library-v...e_example.html I've looked over their code and cant seem to pull out the part where it adds the numbers to the marker, I'm still new to jquery. Here is a link to the source code: http://google-maps-utility-library-v...erclusterer.js any help would be appreciated. Below is the script and form fields I am working with. What I want to do is sum the two textbox fields and have the result show in the total textbox. The code works fine and the total textbox is updated with the value of form1.basic. The problem occurs when I add the "+ parseInt(document.form2.supporter.value)" code in the script section. What am I doing wrong? Thank you for your time and help. Code: <script type="text/javascript"><!-- function updatesum() { document.frmtotal.total.value = parseInt(document.form1.basic.value) + parseInt(document.form2.supporter.value)} //--></script> <tr> <td width="111"><form id="form1" name="form1" method="post" action=""> <label for="basic">$</label> <input name="basic" type="text" id="basic" size="7" maxlength="7" onchange="updatesum(this.form)" /> </form></td> </tr> <tr> <td><form id="form2" name="supporter" method="post" action=""> <label for="supporter">$</label> <input name="supporter" type="text" id="supporter" size="7" maxlength="7" onchange="updatesum(this.form)" /> </form> On my website, I have random logos appear on the home page as products that I recommend. I would like to make these logos into links to the website they are from, so that when one clicks on the picture, it directs you to the site, but I can't quite get it to work, and I know very little about Javascript (as you may know from my previous question about Javascript). If someone could help me crack the code, that would be great Here's my current JS code: Code: <!-- // Set up the image files to be used. var theImages = new Array() // do not change this // To add more image files, continue with the // pattern below, adding to the array. Rememeber // to increment the theImages[x] index! theImages[0] = 'http://i200.photobucket.com/albums/aa216/Jilldear/classicequinelogo-1.jpg' theImages[1] = 'images/logo2.jpg' theImages[2] = 'http://i200.photobucket.com/albums/aa216/Jilldear/pyranha-1.jpg' // ====================================== // do not change anything below this line // ====================================== var j = 0 var p = theImages.length; var preBuffer = new Array() for (i = 0; i < p; i++){ preBuffer[i] = new Image() preBuffer[i].src = theImages[i] } var whichImage = Math.round(Math.random()*(p-1)); function showImage(){ document.write('<img src="'+theImages[whichImage]+'">'); } //--> And my current HTML code: Code: <script language="JavaScript"> <!-- showImage(); //--> </script> Sorry for being so green with JS Hey guys I need help on making the images on this game show up. Im not sure how to get the card images to load when hitting the deal button. Code: <html> <head> <title> Blackjack </title> <script LANGUAGE="JavaScript1.1"> var dealer_hand = new Array(); var player_hand = new Array(); var game_over = false; function Card(num,suit) { this.num = num; this.suit = suit; this.fname = fname; } function fname() { return this.num + this.suit + ".gif"; } function Deck() { this.cards = new Array(52); this.next_card = 0; // fill the deck (in order, for now) for (i=1; i<14; i++) { this.cards[i-1] = new Card(i,"c"); this.cards[i+12] = new Card(i,"h"); this.cards[i+25] = new Card(i,"s"); this.cards[i+38] = new Card(i,"d"); } this.shuffle = shuffle; this.dealCard = dealCard; } function shuffle() { for (i=1; i<1000; i++) { // switch two randomly selected cards card1 = Math.floor( 52*Math.random() ); card2 = Math.floor( 52*Math.random() ); temp = this.cards[card2]; this.cards[card2] = this.cards[card1]; this.cards[card1] = temp; } this.next_card = 0; } function dealCard() { return this.cards[ this.next_card++ ]; } var deck = new Deck(); deck.shuffle(); function newGame() { if ( deck.next_card > 39 ) { deck.shuffle(); } dealer_hand = new Array(); player_hand = new Array(); dealer_hand[ 0 ] = deck.dealCard(); // This is the hole card. document.images[0].src = "http://www.litchzen.com/cardback.PNG"; // The hole card is not shown dealer_hand[ 1 ] = deck.dealCard(); document.images[ 1 ].src = dealer_hand[ 1 ].fname(); for ( i=2; i<6; i++) { document.images[i].src = "http://www.litchzen.com/cardback.PNG"; } num = i + 1; player_hand[ 0 ] = deck.dealCard(); document.images[ 6 ].src = player_hand[ 0 ].fname(); player_hand[ 1 ] = deck.dealCard(); document.images[ 7 ].src = player_hand[ 1 ].fname(); for (i=8; i<12; i++) { document.images[i].src = "http://www.litchzen.com/cardback.PNG"; } window.status = ""; document.form1.dealer.value = ""; document.form1.result.value = ""; document.form1.player.value = score( player_hand ); game_over = false; } // end function newGame() function hit() { var total = 0; var new_card = 0; // index for the new card position if ( game_over ) { window.status = "Game over. Click the Deal button to start a new hand." } else { new_card = player_hand.length; player_hand[ new_card ] = deck.dealCard(); document.images[ new_card + 6 ].src = player_hand[ new_card ].fname(); total = score( player_hand ); if ( total > 21 ) { // Busted, game over. document.form1.player.value = total + " busted"; document.images[ 0 ].src = dealer_hand[ 0 ].fname(); document.form1.dealer.value = score( dealer_hand ); winner(); game_over = true; } else { document.form1.player.value = total; } } } // end function hit() function stand() { var total = 0; var new_card = 0; if ( game_over ) { window.status = "Game over. Click the Deal button to start a new hand." } else { document.images[ 0 ].src = dealer_hand[ 0 ].fname(); while ( score( dealer_hand ) < 17 ) { new_card = dealer_hand.length; dealer_hand[ new_card ] = deck.dealCard(); document.images[ new_card ].src = dealer_hand[ new_card ].fname(); } total = score( dealer_hand ); if ( total > 21 ) { // Busted document.form1.dealer.value = total + " busted"; } else { document.form1.dealer.value = total; } } winner(); game_over = true; // The game ends after the player stands. } // end function stand() function score(hand) { var total = 0; var soft = 0; // This variable counts the number of aces in the hand. var pips = 0; // The trump pictures on a card used to be called pips. for ( i=0; i<hand.length; i++ ) { pips = hand[i].num; if ( pips == 1 ) { soft = soft + 1; total = total + 11; } else { if ( pips == 11 || pips == 12 || pips == 13 ) { total = total + 10; } else { total = total + pips; } } } while ( soft > 0 && total > 21 ) { // Count the aces as 1 instead total = total - 10; // of 11 if the total is over 21 soft = soft - 1; } return total; } // end function score function winner() { var player_total = score( player_hand ); var dealer_total = score( dealer_hand ); if ( player_total > 21 ) { // Busted document.form1.result.value = "Dealer wins"; } else { if ( dealer_total > 21 ) { // Busted document.form1.result.value = "Player wins"; } else { if ( player_total == dealer_total ) { document.form1.result.value = "Tie game"; } else { if ( player_total > dealer_total ) { document.form1.result.value = "Player wins"; } else { document.form1.result.value = "Dealer wins"; } } } } } </script> </head> <body> <h1>Blackjack Javascript Example</h1> <hr> <form NAME="form1"> <table> <tr> <td> <B>Dealer: </B> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> </td> </tr> <tr> <td> <B>Player: <B> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> <td> <img border=0 src="http://www.litchzen.com/cardback.PNG" height=125 width=95> </tr> <tr> <td> <B>Dealer Sco </B><BR> <input TYPE="TEXT" SIZE=12 NAME="Dealer" VALUE="0"></td> <td> <B>Player Sco </B><BR> <input TYPE="TEXT" SIZE=12 NAME="You" VALUE="0"> <td> <input TYPE="BUTTON" VALUE=" Stay " onClick="stand();"> <td> <input TYPE="BUTTON" VALUE=" Hit " onClick="hit();"> <td> <a href="#" onClick="newGame();" onMouseOver = "window.status = 'Deal a new hand.'; return true; " onMouseOut = "window.status = '';"> <img border=0 src="http://us.cdn3.123rf.com/168nwm/fuzzbones/fuzzbones1105/fuzzbones110500782/9628185-deal-word-in-male-hand.jpg" height=50 width=106></a> <td> <B>Game Result:</B><BR> <input TYPE="TEXT" SIZE=10 NAME="result" VALUE=""</td> </tr> </table> </form> <P>Click the Deal button to start a new game.<BR> Click the Hit button to get another card.<BR> Click the Stay button to end your turn. Try and get as close to 21 as possible to win. Good luck!</P> </body> </html> Ok basically i am making a blackjack game and so far i have a random generator for my cards, once i press deal 4 cards are dealt at random, but when i try to insert a new button for the player to press for another card "hit", the card comes down and is not random at all. I could really do with this help. Thank you. Code: <html> <HEAD> <SCRIPT LANGUAGE="JavaScript"> var deck=new Array; var hand=new Array(53); var hcrd=new Array(53); var hsut=new Array(53); var isPlaying=-1; var betn=1; var dpos=0; var crdts; var ncard=0; /* ----------------------------------------------------------------------------------------- */ function initdeck(){ crd=0; for(i=0;i<4;i++){ if(i==0){st="club";} else if(i==1){st="heart";} else if(i==2){st="diamond";}else{st="spade";} for(c=1;c<14;c++){ if(c==1){num="ace";}else if(c==11){num="jack";} else if(c==12){num="queen";}else if(c==13){num="king";}else{num=c;} deck[crd++]=st+"|"+num; } //for c } //for i } /* ----------------------------------------------------------------------------------------- */ function showcard(cnum) { p = hand[cnum].indexOf("|"); s = hand[cnum].substring(0,p); c = hand[cnum].substring(p+1,hand[cnum].length); if(cnum==0){document.images.card1.src="cards/"+s+c+".jpg";} else if(cnum==1){document.images.card2.src="cards/"+s+c+".jpg";} else if(cnum==2){document.images.card3.src="cards/"+s+c+".jpg";} else if(cnum==3){document.images.card4.src="cards/"+s+c+".jpg";} else{document.images.card11.src="cards/"+s+c+".jpg";} } function hit(ncard) { p = hand[ncard].indexOf("|"); s = hand[ncard].substring(0,p); c = hand[ncard].substring(p+1,hand[ncard].length); if(ncard==4){document.images.card5.src="cards/"+s+c+".jpg";} else if(ncard==5){document.images.card6.src="cards/"+s+c+".jpg";} else if(ncard==6){document.images.card7.src="cards/"+s+c+".jpg";} } function stand() { document.images.card8.src="cards/"+s+c+".jpg"; } function deal() { if(isPlaying < 0) { // new game dpos = 0; isPlaying=1; for(i=0;i<500;i++) { //shuffle the cards(mix up the deck) tmp = Math.round(Math.random()*51); tmp2= Math.round(Math.random()*51); stmp = deck[tmp]; deck[tmp] = deck[tmp2]; deck[tmp2] = stmp; } for(i=0;i<53;i++) { hand[i]=deck[dpos++];showcard(i); hand[i]=deck[dpos++];hit(i); } } } function quit(){window.close();} /* ----------------------------------------------------------------------------------------- */ initdeck(); // End --> </script> </HEAD> <BODY> <table> <tr><form name="drwcrd"> <td width=590></td> <td><b>Computer</b></td> </tr><tr></form> <table> <tr><form name="drwcrd"> <td width=500></td> <td width=30 align=center><img name="card1" src="cards/cardback.jpg"></td> <td width=30 align=center><img name="card3" src="cards/cardback.jpg"></td> <td width=30 align=center><img name="card8" src=""></td> <td width=30 align=center><img name="card9" src=""></td> <td width=30 align=center><img name="card10" src=""></td> <table> <tr><form name="drwcrd"> <td width=590></td> <td><b>Player</b></td> </tr><tr></form> <table> <tr><form name="drwcrd"> <td width=500></td> <td width=30 align=center><img name="card2" src="cards/cardback.jpg"</td> <td width=30 align=center><img name="card4" src="cards/cardback.jpg"</td> <td width=30 align=center><img name="card5" src=""</td> <td width=30 align=center><img name="card6" src=""</td> <td width=30 align=center><img name="card7" src=""</td> </tr><tr></form> <table> <tr><td><input type="button" value ="DEAL Cards" onClick="javascript:deal();"></td> <td><input type="button" value ="Hit" onClick="javascript:hit();"></td> <td><input type="button" value ="Stand" onClick="javascript:stand();"></td> </body> </html> if anyone has any help id be grateful or if anyone has any ideas on how to approach this differently. thanks in advance please excuse the code its a little messy atm. sorry Hi, ive been asked to make a blackjack game in javascript but I have a little bit of a problem. When the page loads, it deals cards to the player and the dealer, showing which cards have been dealt and the current scores. When I press the HIT button, i need to add another card to the array player_hand_img, and the value of this card to player_hand. I have successfully created this code, but my question is, how do I update the information (the score and the cards) on the screen after it has loaded? Can you do this in JavaScript or do I need another language? Thanks very much in advance! I've looked for a solution to this issue, but it seems like a little different scenario than other situations. I made a system for generating friend requests on Facebook. I have a grid that is 6 x 3, for a total of 18 cells. Each cell has a picture in it, and the picture is linked to the Facebook friend request page. My problem is that since each cell is populated at random from the array, I'm getting lots of repeats. For example, some picutures are in 5 cells, and some are in none. I'm trying to figure out how to make it so that once a picture is used once in the grid, it does not get used again in the same grid. I still want every cell filled at random on each page load, I just want to prevent the repeating. Here's my current code: Code: <script type="text/javascript"> var vip_list=new Array( new Array('http://profile.ak.fbcdn.net/v225/1616/88/s1220771654_2158.jpg','http://www.facebook.com/addfriend.php?id=1220771654'), new Array('http://profile.ak.fbcdn.net/v223/1233/29/s904885342_9055.jpg','http://www.facebook.com/addfriend.php?id=904885342'), new Array('http://profile.ak.fbcdn.net/v229/1574/66/s1752031238_626.jpg','http://www.facebook.com/addfriend.php?id=1752031238'), new Array('http://profile.ak.fbcdn.net/v223/768/71/n661155042_7325.jpg','http://www.facebook.com/addfriend.php?id=661155042'), new Array('http://profile.ak.fbcdn.net/v226/732/26/n1827289885_2478.jpg','http://www.facebook.com/addfriend.php?id=1827289885'), new Array('http://profile.ak.fbcdn.net/v229/1631/70/s1425313768_1140.jpg','http://www.facebook.com/addfriend.php?id=1425313768'), new Array('http://static.ak.fbcdn.net/pics/s_silhouette.jpg','http://www.facebook.com/addfriend.php?id=1667023416'), new Array('http://profile.ak.fbcdn.net/v225/1146/29/s506485704_9532.jpg','http://www.facebook.com/addfriend.php?id=506485704'), new Array('http://profile.ak.fbcdn.net/profile6/270/32/s692160490_8745.jpg','http://www.facebook.com/addfriend.php?id=692160490'), new Array('http://profile.ak.fbcdn.net/v229/114/83/s1218176198_7375.jpg','http://www.facebook.com/addfriend.php?id=1218176198'), new Array('http://profile.ak.fbcdn.net/v226/946/4/s1470171885_4973.jpg','http://www.facebook.com/addfriend.php?id=1470171885'), new Array('http://static.ak.fbcdn.net/pics/s_silhouette.jpg','http://www.facebook.com/addfriend.php?id=1329505888'), new Array('http://static.ak.fbcdn.net/pics/s_silhouette.jpg','http://www.facebook.com/addfriend.php?id=1325496968'), new Array('http://profile.ak.fbcdn.net/v223/1546/92/s1536913202_2017.jpg','http://www.facebook.com/addfriend.php?id=1536913202'), new Array('http://static.ak.fbcdn.net/pics/s_silhouette.jpg','http://www.facebook.com/addfriend.php?id=1624715433'), new Array('http://profile.ak.fbcdn.net/v228/1282/58/s713998257_3682.jpg','http://www.facebook.com/addfriend.php?id=713998257') ); var chosen_vip=Math.floor(vip_list.length*Math.random()); var chosen_vip1=Math.floor(vip_list.length*Math.random()); var chosen_vip2=Math.floor(vip_list.length*Math.random()); var chosen_vip3=Math.floor(vip_list.length*Math.random()); var chosen_vip4=Math.floor(vip_list.length*Math.random()); var chosen_vip5=Math.floor(vip_list.length*Math.random()); var chosen_vip6=Math.floor(vip_list.length*Math.random()); var chosen_vip7=Math.floor(vip_list.length*Math.random()); var chosen_vip8=Math.floor(vip_list.length*Math.random()); var chosen_vip9=Math.floor(vip_list.length*Math.random()); var chosen_vip10=Math.floor(vip_list.length*Math.random()); var chosen_vip11=Math.floor(vip_list.length*Math.random()); var chosen_vip12=Math.floor(vip_list.length*Math.random()); var chosen_vip13=Math.floor(vip_list.length*Math.random()); var chosen_vip14=Math.floor(vip_list.length*Math.random()); var chosen_vip15=Math.floor(vip_list.length*Math.random()); var chosen_vip16=Math.floor(vip_list.length*Math.random()); var chosen_vip17=Math.floor(vip_list.length*Math.random()); document.write('<center>'); document.write('<a href="',vip_list[chosen_vip][1],'" target="_blank"><img src="',vip_list[chosen_vip][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip1][1],'" target="_blank"><img src="',vip_list[chosen_vip1][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip2][1],'" target="_blank"><img src="',vip_list[chosen_vip2][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip3][1],'" target="_blank"><img src="',vip_list[chosen_vip3][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip4][1],'" target="_blank"><img src="',vip_list[chosen_vip4][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip5][1],'" target="_blank"><img src="',vip_list[chosen_vip5][0],'" height="60" width="60"></a>'); document.write('<br>'); document.write('<a href="',vip_list[chosen_vip6][1],'" target="_blank"><img src="',vip_list[chosen_vip6][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip7][1],'" target="_blank"><img src="',vip_list[chosen_vip7][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip8][1],'" target="_blank"><img src="',vip_list[chosen_vip8][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip9][1],'" target="_blank"><img src="',vip_list[chosen_vip9][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip10][1],'" target="_blank"><img src="',vip_list[chosen_vip10][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip11][1],'" target="_blank"><img src="',vip_list[chosen_vip11][0],'" height="60" width="60"></a>'); document.write('<br>'); document.write('<a href="',vip_list[chosen_vip12][1],'" target="_blank"><img src="',vip_list[chosen_vip12][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip13][1],'" target="_blank"><img src="',vip_list[chosen_vip13][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip14][1],'" target="_blank"><img src="',vip_list[chosen_vip14][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip15][1],'" target="_blank"><img src="',vip_list[chosen_vip15][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip16][1],'" target="_blank"><img src="',vip_list[chosen_vip16][0],'" height="60" width="60"></a>'); document.write('<a href="',vip_list[chosen_vip17][1],'" target="_blank"><img src="',vip_list[chosen_vip17][0],'" height="60" width="60"></a>'); document.write('<br>'); </script> Any suggestions? Thank you! Hay! I have a silly question, I have a row of numbers, 1 to 250. The numbers are all in one horizantal line. But I want to use a <br> after every 10 numbers. I tried: if(i % 10) { document.write("<br>"); } This is how I wanted to be: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, Does any one know how can I achieve this I'm still a noob. |