Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-02-2012, 04:11 PM   PM User | #1
pdiddles03
New Coder

 
Join Date: Jun 2010
Posts: 76
Thanks: 0
Thanked 1 Time in 1 Post
pdiddles03 is an unknown quantity at this point
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>
pdiddles03 is offline   Reply With Quote
Old 10-02-2012, 06:05 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 10-02-2012, 06:14 PM   PM User | #3
pdiddles03
New Coder

 
Join Date: Jun 2010
Posts: 76
Thanks: 0
Thanked 1 Time in 1 Post
pdiddles03 is an unknown quantity at this point
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.
pdiddles03 is offline   Reply With Quote
Old 10-02-2012, 11:33 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 10-03-2012, 05:11 PM   PM User | #5
pdiddles03
New Coder

 
Join Date: Jun 2010
Posts: 76
Thanks: 0
Thanked 1 Time in 1 Post
pdiddles03 is an unknown quantity at this point
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.
pdiddles03 is offline   Reply With Quote
Old 10-03-2012, 08:19 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:49 AM.


Advertisement
Log in to turn off these ads.