Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    134
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Blackjack Scoring

    I'm working on a blackjack game for fun, I'm having a problem adding the cards though. What would I do to add the last card to a score that was put down?

    It's not a complete game yet, its just a project and please don't bash my code lol

    Code:
    <html>
    <head>
    
    <style type="text/css">
    #playing_area{
    	height: 400px;
    	width: 600px;
    	position: relative;
    	border: 1px solid black;
    	background-color: #088A4B;
    }
    #dealer_area{
     border: 1px solid black;
    }
    #player_area{
    
    }
    .card{
    	height: 60px;
    	width: 40px;
    	background-color: white;
    	border: 1px solid black;
    	float: left;
    	-moz-border-radius: 10px 10px;
    	border-radius: 10px 10px;
    	padding: 5px;
    	box-shadow: 5px 5px 5px;
    	-moz-box-shadow: 5px 5px 5px;
    	
    }
    </style>
    </head>
    <body>
    
    <div id="playing_area">
    <button id="start">Start Game</button>
    <div id="dealer_area">
    <h3 style="text-align: center;">Dealer</h3>
    <div id="dealer_cards"></div>
    <div id="dealer_score"></div>
    </div>
    
    <div id="player_area"></div>
    
    </div>
    <script type="text/javascript">
    
    var start=document.getElementById('start');
    var dealer=document.getElementById('dealer_area');
    var dealerCards=document.getElementById('dealer_cards');
    var dealerScore = document.getElementById('dealer_score');
    var i;
    var score=0;
    
    //declare the array of the deck of cards
    var deck = new Array();
    
    
    deck[0]=1;
    deck[1]=2;
    deck[2]=3;
    deck[3]=4;
    deck[4]=5;
    deck[5]=6;
    deck[6]=7;
    deck[7]=8;
    deck[8]=9;
    deck[9]=10;
    deck[10]=11;
    deck[11]=12;
    
    
    
    function gamestart(){
    var i= Math.round( Math.random(i)*11);
    
    var newCard = document.createElement('div');
    
    var card=dealerCards.appendChild(newCard);
    card.innerHTML = deck[i];
    card.setAttribute('class','card');
    
    
    
    
    
    }
    
    start.setAttribute('onclick', 'gamestart()');
    
    </script>
    </body>
    </html>

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    i figured out a way to add them up, but you will need to do something if bust/win/lose.
    maybe you can use some of the parts below in your code, like the suite 'icons', or the scoring routine, help yourself to whatev.

    Code:
    <!doctype html>
    <html>
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>blackjack or whatev</title>
     <style> 
    	html, body { margin: 0; padding: 0; background: aliceblue; font-family: monospace; font-size: 20px; }
    	textarea { width: 47.5%;  float: left; margin: 1%; }
    	
    	 #IN{display: none;}  #OUT{display: inline-block;} #BUTTON{display: inline-block;}
     </style>  <link rel="icon" href="http://danml.com/sandbox/sandbox.png" type="image/x-icon">
     </head>
     <body onload='main()'  id="body">
    <h1>blackjack or whatev</h1>
    
    <button id="BUTTON" onclick="main()"> Draw </button>
    
    
    <br />
    
    <label class='box'><b class='title'></b>
    <textarea class="textbox" rows="35" name="IN" id="IN"  cols="80" onchange="IN.title=IN.value.length+' Bytes.';return true; " tabindex="1" >
    
    //this is the blackjack code to play with:
    
    var suites=unescape("♦♥♠♣").split("");
    
    
    
    Game={
      score: 0,
      drawn: 0,
      hand:  [],
      draw: function drawOne(){
        var card=Game.deck.pop();
        Game.drawn++;
        Game.score=Game.score + ((card.slice(1)*1)||10)   ;
        Game.hand.push(card);
        return JSON.stringify(Game, null, "\t");
      },//end draw()
      deck: suites.map(function(a,b){
                return "AA23456789XJQK".split("").join(" "+a).slice(1);
            }).join(" ").trim().split(" ")
            .filter(Boolean)
            .map(function shuffle(a,b,c){if(!b){shuffle.r=c.slice();}return shuffle.r.splice(Math.random()*(c.length-b),1)[0];})
    }
    </textarea>
    </label>
    
    <label class='box'><b class='title'></b>
    <textarea class="textbox" rows="35" name="OUT" id="OUT"  tabindex="1"  cols="80" onchange="IN.title=IN.value.length+' Bytes.';return true; " ></textarea>
    </label>
    
    <br />
    
    <script type="text/late" id="late">
     
    // the button code:
    
    if(!window.Game){eval(IN.value);window.Game=Game;}
    Game.draw();
    
    </script>
    <script>
     //just for demo, ignore this code....
    
    (function(){
    	 var elmNames=["IN","OUT"];
    		window.elms=elmNames.map(el).map(function(a,b){return window[elmNames[b]]=a; });
    }());
    
    function showTab(tabId){
     elms.map(function hide(a){ a.style.display="none"; });
     el(tabId).style.display='block';
    }
    
    
    window.main=function ol() {
      var resp=eval(el("late").textContent);
      if(resp.split){ OUT.value=resp;}else{  OUT.value=JSON.stringify(resp, null, "\t"); }
    };
    
    </script>
    </body>
    </html>
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Posts
    134
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Thanks for the reply.

    But I don't think your code will do. The thing with your code is that you rewrote my whole code and it's far to complicated which i don't think it should be to add a few simple cards.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,657 Times in 4,619 Posts
    You have no protection in your code to stop the system from, for example, dealing 7 aces of spades in a row.

    You need to create a full deck (including suits) and then decide on how many decks the dealer will deal from (nobody in Nevada deals from one deck...typically there are 4 or more decks in the "shoe").

    I agree that RndMe's code is more complex than you probably need/are looking for, but yours is way too simplistic.

    Just as a for instance: If somebody has an Ace and a 6 (for 17 points) and draws a 7, will you be smart enough to convert the Ace to 1 point and make the total now by 14, instead of 24?
    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.

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Posts
    134
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I know that, like I said in the initial post, no where near done. Basically it's not even blackjack yet, it's jut 21. I figured out a way how to make it add up the amounts, but I need to make it build dynamically.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,657 Times in 4,619 Posts
    This line
    start.setAttribute('onclick', 'gamestart()');
    should be
    start.setAttribute('onclick', gamestart);

    or could even be simpler:
    start.onclick = gamestart;
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •