/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Black Jack game

Hello ppl,

I am trying to make a Black Jack game and need some help. In my code, I want the dealer cards which are the bottom cards to be face down and when I click “stand” to make them appear.

Also, I have declared the card array and their values twice and also the function which displays the card has been done twice. I am sure, there is a way to just do it once and make the program more efficient. Someone please help me do it.

[code=php][CODE]
<html>
<head>
<title>
Card Example
</title>

<style type=”text/css”>
body
{
font-family: Arial;
margin-left: 123px;
}
</style>

<script type=”text/javascript”>
var cards = new Array(52);
var total = 0;
var dtotal = 0;
var score = 500;

cards[0] = “ace-clubs.png”;
cards[1] = “ace-diamonds.png”;
cards[2] = “ace-hearts.png”;
cards[3] = “ace-spades.png”;

cards[4] = “king-clubs.png”;
cards[5] = “king-diamonds.png”;
cards[6] = “king-hearts.png”;
cards[7] = “king-spades.png”;

cards[8] = “queen-clubs.png”;
cards[9] = “queen-diamonds.png”;
cards[10] = “queen-hearts.png”;
cards[11] = “queen-spades.png”;

cards[12] = “jack-clubs.png”;
cards[13] = “jack-diamonds.png”;
cards[14] = “jack-hearts.png”;
cards[15] = “jack-spades.png”;

cards[16] = “ten-clubs.png”;
cards[17] = “ten-diamonds.png”;
cards[18] = “ten-hearts.png”;
cards[19] = “ten-spades.png”;

cards[20] = “nine-clubs.png”;
cards[21] = “nie-diamonds.png”;
cards[22] = “nine-hearts.png”;
cards[23] = “nine-spades.png”;

cards[24] = “eight-clubs.png”;
cards[25] = “eight-diamonds.png”;
cards[26] = “eight-hearts.png”;
cards[27] = “eight-spades.png”;

cards[28] = “seven-clubs.png”;
cards[29] = “seven-diamonds.png”;
cards[30] = “seven-hearts.png”;
cards[31] = “seven-spades.png”;

cards[32] = “six-clubs.png”;
cards[33] = “six-diamonds.png”;
cards[34] = “six-hearts.png”;
cards[35] = “six-spades.png”;

cards[36] = “five-clubs.png”;
cards[37] = “five-diamonds.png”;
cards[38] = “five-hearts.png”;
cards[39] = “five-spades.png”;

cards[40] = “four-clubs.png”;
cards[41] = “four-diamonds.png”;
cards[42] = “four-hearts.png”;
cards[43] = “four-spades.png”;

cards[44] = “three-clubs.png”;
cards[45] = “three-diamonds.png”;
cards[46] = “three-hearts.png”;
cards[47] = “three-spades.png”;

cards[48] = “two-clubs.png”;
cards[49] = “two-diamonds.png”;
cards[50] = “two-hearts.png”;
cards[51] = “two-spades.png”;

function showCard(card)
{
var pick;
var card_value;

if (cards.length)
{
cards.sort(function(){return 0.5 – Math.random()});

pick = cards.pop();

document.getElementById(card.id).src = “card52/” + pick;

card_value = getCardValue(pick);
total += parseFloat(card_value);

document.getElementById(“total”).innerHTML = total;

if (total > 21)
{
alert(“You Busted, Dealer Wins”);
score -= 1;
}

}
else
{
alert(“No more cards”);
}
}

function getCardValue(cardName)
{
var value_str = cardName.split(“-“);
var value_num;

switch (value_str[0])
{
case “ace”:
value_num = 11;
break;

case “king”:
case “queen”:
case “jack”:
case “ten”:
value_num = 10;
break;

case “nine”:
value_num = “9”;
break;

case “eight”:
value_num = “8”;
break;

case “seven”:
value_num = “7”;
break;

case “six”:
value_num = “6”;
break;

case “five”:
value_num = “5”;
break;

case “four”:
value_num = “4”;
break;

case “three”:
value_num = “3”;
break;

case “two”:
value_num = “2”;
break;
}
return value_num;
}

var dcards = new Array(52);
var dtotal = 0;

dcards[0] = “ace-clubs.png”;
dcards[1] = “ace-diamonds.png”;
dcards[2] = “ace-hearts.png”;
dcards[3] = “ace-spades.png”;

dcards[4] = “king-clubs.png”;
dcards[5] = “king-diamonds.png”;
dcards[6] = “king-hearts.png”;
dcards[7] = “king-spades.png”;

dcards[8] = “queen-clubs.png”;
dcards[9] = “queen-diamonds.png”;
dcards[10] = “queen-hearts.png”;
dcards[11] = “queen-spades.png”;

dcards[12] = “jack-clubs.png”;
dcards[13] = “jack-diamonds.png”;
dcards[14] = “jack-hearts.png”;
dcards[15] = “jack-spades.png”;

dcards[16] = “ten-clubs.png”;
dcards[17] = “ten-diamonds.png”;
dcards[18] = “ten-hearts.png”;
dcards[19] = “ten-spades.png”;

dcards[20] = “nine-clubs.png”;
dcards[21] = “nie-diamonds.png”;
dcards[22] = “nine-hearts.png”;
dcards[23] = “nine-spades.png”;

dcards[24] = “eight-clubs.png”;
dcards[25] = “eight-diamonds.png”;
dcards[26] = “eight-hearts.png”;
dcards[27] = “eight-spades.png”;

dcards[28] = “seven-clubs.png”;
dcards[29] = “seven-diamonds.png”;
dcards[30] = “seven-hearts.png”;
dcards[31] = “seven-spades.png”;

dcards[32] = “six-clubs.png”;
dcards[33] = “six-diamonds.png”;
dcards[34] = “six-hearts.png”;
dcards[35] = “six-spades.png”;

dcards[36] = “five-clubs.png”;
dcards[37] = “five-diamonds.png”;
dcards[38] = “five-hearts.png”;
dcards[39] = “five-spades.png”;

dcards[40] = “four-clubs.png”;
dcards[41] = “four-diamonds.png”;
dcards[42] = “four-hearts.png”;
dcards[43] = “four-spades.png”;

dcards[44] = “three-clubs.png”;
dcards[45] = “three-diamonds.png”;
dcards[46] = “three-hearts.png”;
dcards[47] = “three-spades.png”;

dcards[48] = “two-clubs.png”;
dcards[49] = “two-diamonds.png”;
dcards[50] = “two-hearts.png”;
dcards[51] = “two-spades.png”;

function dshowCard(dcard)
{
var dpick;
var dcard_value;

if (dcards.length)
{
dcards.sort(function(){return 0.5 – Math.random()});

dpick = dcards.pop();

document.getElementById(dcard.id).src = “card52/” + dpick;

dcard_value = getCardValue(dpick);
dtotal += parseFloat(dcard_value);

document.getElementById(“dtotal”).innerHTML = dtotal;

if (dtotal > 21)
{
alert(“Dealer Busted, You Win”);
score += 1;
}

}
else
{
alert(“No more cards”);
}
}

function getCardValue(dcardName)
{
var dvalue_str = dcardName.split(“-“);
var dvalue_num;

switch (dvalue_str[0])
{
case “ace”:
dvalue_num = 11;
break;

case “king”:
case “queen”:
case “jack”:
case “ten”:
dvalue_num = 10;
break;

case “nine”:
dvalue_num = “9”;
break;

case “eight”:
dvalue_num = “8”;
break;

case “seven”:
dvalue_num = “7”;
break;

case “six”:
dvalue_num = “6”;
break;

case “five”:
dvalue_num = “5”;
break;

case “four”:
dvalue_num = “4”;
break;

case “three”:
dvalue_num = “3”;
break;

case “two”:
dvalue_num = “2”;
break;
}
return dvalue_num;
}

function stand()
{
if (total > 21)
{
alert(“Dealer Wins”);
score -= 1;
}
else if (dtotal > 21)
{
alert(“You Win”);
score += 1;
}
else if (total > dtotal)
{
alert(“You Win”);
score += 1;
}
else if (dtotal > total)
{
alert(“Dealer Wins”);
score -= 1;
}
else if (total == dtotal)
{
alert(“Tie Game”) ;
}
}

</script>
</head>

<body>
<img border=”1″ src=”card52/P1DeckTwo.png” width=”150″ alt=”” onclick=”showCard(this);this.onclick=function(){return false;}” id=”card1″ />
<img border=”1″ src=”card52/P1DeckTwo.png” width=”150″ alt=”” onclick=”showCard(this);this.onclick=function(){return false;}” id=”card2″ />
<img border=”1″ src=”card52/P1DeckTwo.png” width=”150″ alt=”” onclick=”showCard(this);this.onclick=function(){return false;}” id=”card3″ />
<img border=”1″ src=”card52/P1DeckTwo.png” width=”150″ alt=”” onclick=”showCard(this);this.onclick=function(){return false;}” id=”card4″ />
<img border=”1″ src=”card52/P1DeckTwo.png” width=”150″ alt=”” onclick=”showCard(this);this.onclick=function(){return false;}” id=”card5″ /> <br />

<img border=”1″ src=”card52/P2DeckTwo.png” width=”150″ alt=”” onclick=”dshowCard(this);this.onclick=function(){return false;}” id=”dcard1″ />
<img border=”1″ src=”card52/P2DeckTwo.png” width=”150″ alt=”” onclick=”dshowCard(this);this.onclick=function(){return false;}” id=”dcard2″ />
<img border=”1″ src=”card52/P2DeckTwo.png” width=”150″ alt=”” onclick=”dshowCard(this);this.onclick=function(){return false;}” id=”dcard3″ />
<img border=”1″ src=”card52/P2DeckTwo.png” width=”150″ alt=”” onclick=”dshowCard(this);this.onclick=function(){return false;}” id=”dcard4″ />
<img border=”1″ src=”card52/P2DeckTwo.png” width=”150″ alt=”” onclick=”dshowCard(this);this.onclick=function(){return false;}” id=”dcard5″ /> <br />

<input type=”button” value=”Stand” name=”stand” onclick=”stand();” />
<input type=”button” value=”New Game” name=”clear” onclick=”history.go();” />

<p id=”total”>
</p> <br />

<p id=”dtotal”>
</p> <br />

<p id=”score”>
</p>

</body>
</html>[/CODE]

[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinNov 14.2009 — [code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD><TITLE>Sa3-53am52</TITLE>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<STYLE type=text/CSS>
.bCard{color:black;font-size:33px}
.rCard{color:red;font-size:33px}

</STYLE>
<SCRIPT type="text/javascript">
var cards=[]
var card=['A','K','Q','J','10','9','8','7','6','5','4','3','2']
var suit=['<span class="bCard">&#9827;','<span class="bCard">&#9824;','<span class="rCard">&#9829;','<span class="rCard">&#9830;']
for(i=0,k=0;i<card.length;i++){
for(j=0;j<suit.length;j++){cards[k]=suit[j]+card[i]+'</span> '; k++}
}
for(i=0;i<cards.length;i++){document.write(cards[i]+' ')}
</SCRIPT>
<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
<BODY scroll="auto">

</BODY></HTML>[/code]
Copy linkTweet thisAlerts:
@justinbarneskinNov 14.2009 — the forum transformed the special characters which could make the file size very large saving as utf

respectively-

&# 9827; &# 9824; &# 9829; &# 9830;
Copy linkTweet thisAlerts:
@rajatsaxena87authorNov 14.2009 — I dont want to use a loop. I am required to declare an array and the value of the cards like I have. I just dont want to do it twice. Like I only want to do it once and use it to call both the player cards and the dealer cards.
Copy linkTweet thisAlerts:
@justinbarneskinNov 14.2009 — Its not really fun for us when we can't see the cards.

Sorry for popping the cherry on your thread, I was pretty sure no one would respond.
Copy linkTweet thisAlerts:
@rajatsaxena87authorNov 15.2009 — Here's a link from where you can download and see the file and the cards

http://www.mediafire.com/file/nywmjyvmgfa/BlackJack.zip

The file size is only 60.42KB
Copy linkTweet thisAlerts:
@justinbarneskinNov 15.2009 — You only need one image and a bit of code to see whats up
[code=html]<SCRIPT type="text/javascript">

var cards=['-1px -1px','-74px -1px','-147px -1px','-220px -1px','-293px -1px','-366px -1px','-439px -1px','-512px -1px','-585px -1px','-658px -1px','-731px -1px','-804px -1px','-877px -1px','-1px -103px','-74px -103px','-147px -103px','-220px -103px','-293px -103px','-366px -103px','-439px -103px','-512px -103px','-585px -103px','-658px -103px','-731px -103px','-804px -103px','-877px -103px','-1px -205px','-74px -205px','-147px -205px','-220px -205px','-293px -205px','-366px -205px','-439px -205px','-512px -205px','-585px -205px','-658px -205px','-731px -205px','-804px -205px','-877px -205px','-1px -307px','-74px -307px','-147px -307px','-220px -307px','-293px -307px','-366px -307px','-439px -307px','-512px -307px','-585px -307px','-658px -307px','-731px -307px','-804px -307px','-877px -307px'];
var backs=['-950px -1px','-950px -103px','-950px -205px','-950px -307px']
// 72x96
for(i=0;i<backs.length;i++){ w=71;h=96; if(i%2==0){w=96;h=71}
document.write('<div style="width:'+w+'px;height:'+h+'px;overflow:hidden;position:relative;background-image:url(deck.gif);background-position:'+backs[i]+'" title="'+i+'"></div>')}

var t='<table cellpadding=0 cellspacing=10><tr>'
for(i=0;i<cards.length;i++){ if(i!=0 && i%9==0){t+='</tr><tr>'}
t+='<td style="width:72px;height:96px;overflow:hidden;position:relative;background-image:url(deck.gif);background-position:'+cards[i]+'" title="'+i+'"></td>';
}
document.write(t+'</table>')

</SCRIPT>
[/code]


[upl-file uuid=960ed3a2-3670-47e0-bfe3-b4d9a5846fb9 size=63kB]deck.gif[/upl-file]
×

Success!

Help @rajatsaxena87 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...