PDA

View Full Version : Well I have a disaster!


MrEnder
07-23-2008, 01:49 AM
Well I have quite the disaster here. I got this far but have NO IDEA how to get it any better due to the fact I am quite new with Javascript. Here is the thing. I got borded and for practice decided to make Blackjack in javascript. Well I made it except for a few small problems I don't know how to deal with. First problem is that if you hit the "Hit me" button then try to hit another button afterwards it wont work. I don't even know where to begin to fix this. I know its because it is reloading and reloading wrong but how to fix that I have no idea how. Ok so my second problem is this. I made it but it is very poorly scripted. How would I made it a more simple script there has got to be a way to cut the line amount in half but I don't have a clue how. I've herd about switches and wondered if those could be used to help but I don't know how to use switches. Ok and my last problem is this. I want each of the cards to come in slower as if they are being delt so I want it to pause between each card. Like when you click stay the dealer all of a sudden has all of these cards. I want the person playing to be able to see that he slowly took more cards. I could put in an alert but that would be annoying. I read about the setTimeOut() object and was wondering if that might be possible to use. I only saw it being used with functions however so I'm very lost.

Here is my code and thanks a million for who ever can fix it

Also I apologize for giving so much code but I didn't know what else to do



<head>

<title>Ender's Javascript Blackjack</title>

<style type="text/css">
body {
background-color: white;
padding-left: 31.6%;
padding-top: 25px;
}
input {
background-color: white;
border: 1px dashed #9090FF;
}
td {
border: 5px ridge silver;
padding: 10px;
padding-top: 0px;
background-color: #597750;
}
a {
background-color: white;
border: 1px dashed #9090FF;
color: black;
text-decoration: none;
font-size: 12px;
font-family: Monospace;
padding: 9px;
padding-top: 3px;
padding-bottom: 3px;
}
h1, h2, h3 {color: #DD0000;}
.footerText {margin-left: 25px;font-size: 8px;font-family: Monospace;}
</style>

</head>
<body>
<table>
<tr>
<td>
<script type="text/javascript">
//<![CDATA[
var num = 3;
var whosCardNumber = new Array(num);
var whosCardSuit = new Array(num);

var cardTotal = new Array(0,1);

var winner = "";

var ace = false;

var dealerPot = 100;
var playerPot = 100;
var potPush = 0;

whosCardNumber[0] = "";
whosCardNumber[1] = "";
whosCardNumber[2] = "";
whosCardNumber[3] = "";
whosCardSuit[0] = "";
whosCardSuit[1] = "";
whosCardSuit[2] = "";
whosCardSuit[3] = "";

cardTotal[0] = 0;
cardTotal[1] = 0;
function hitMe() {

document.writeln("<style type='text/css'>body { background-color: white;padding-left: 31.6%;padding-top: 25px;}input {background-color: white;border: 1px dashed #9090FF;}td {border: 5px ridge silver;padding: 10px;padding-top: 0px;background-color: #597750;}a {background-color: white;border: 1px dashed #9090FF;color: black;text-decoration: none;font-size: 12px;font-family: Monospace;padding: 9px;padding-top: 3px;padding-bottom: 3px;}h1, h2, h3 {color: #DD0000;}.footerText {margin-left: 25px;font-size: 8px;font-family: Monospace;}</style>");
document.writeln("<table><tr><td>");
num++;
if(ace==true) {
cardTotal[1] -= 10;
}
document.writeln("<h1>" + "Ender's Blackjack" + "</h1>" + "Dealer "/* + dealerPot*/ + "<br />" + " " + whosCardNumber[0] + whosCardSuit[0] + "<br />" + "&nbsp;&nbsp;Hidden Card" + "<br />" + "&nbsp;&nbsp;Total: " + cardTotal[0] + "<br />" + "<br />" + "Player: "/* + playerPot*/ + "<br />" + " " + whosCardNumber[2] + whosCardSuit[2] + "<br />" + " " + whosCardNumber[3] + whosCardSuit[3] + "<br />");

var randomHitMeCardNumber = Math.floor(Math.random()* 13);
var randomHitMeSuitNumber = Math.floor(Math.random()* 4);

if(randomHitMeCardNumber==0) {
whosCardNumber[num.length] = "&nbsp;&nbsp;Ace";
if(i<2) {
cardTotal[0] += 1;
}
else {
cardTotal[1] += 1;
}
}
else if(randomHitMeCardNumber==1) {
whosCardNumber[num.length] = "&nbsp;&nbsp;2";
if(i<2) {
cardTotal[0] += 2;
}
else {
cardTotal[1] += 2;
}
}
else if(randomHitMeCardNumber==2) {
whosCardNumber[num.length] = "&nbsp;&nbsp;3";
if(i<2) {
cardTotal[0] += 3;
}
else {
cardTotal[1] += 3;
}
}
else if(randomHitMeCardNumber==3) {
whosCardNumber[num.length] = "&nbsp;&nbsp;4";
if(i<2) {
cardTotal[0] += 4;
}
else {
cardTotal[1] += 4;
}
}
else if(randomHitMeCardNumber==4) {
whosCardNumber[num.length] = "&nbsp;&nbsp;5";
if(i<2) {
cardTotal[0] += 5;
}
else {
cardTotal[1] += 5;
}
}
else if(randomHitMeCardNumber==5) {
whosCardNumber[num.length] = "&nbsp;&nbsp;6";
if(i<2) {
cardTotal[0] += 6;
}
else {
cardTotal[1] += 6;
}
}
else if(randomHitMeCardNumber==6) {
whosCardNumber[num.length] = "&nbsp;&nbsp;7";
if(i<2) {
cardTotal[0] += 7;
}
else {
cardTotal[1] += 7;
}
}
else if(randomHitMeCardNumber==7) {
whosCardNumber[num.length] = "&nbsp;&nbsp;8";
if(i<2) {
cardTotal[0] += 8;
}
else {
cardTotal[1] += 8;
}
}
else if(randomHitMeCardNumber==8) {
whosCardNumber[num.length] = "&nbsp;&nbsp;9";
if(i<2) {
cardTotal[0] += 9;
}
else {
cardTotal[1] += 9;
}
}
else if(randomHitMeCardNumber==9) {
whosCardNumber[num.length] = "&nbsp;&nbsp;10";
if(i<2) {
cardTotal[0] += 10;
}
else {
cardTotal[1] += 10;
}
}
else if(randomHitMeCardNumber==10) {
whosCardNumber[num.length] = "&nbsp;&nbsp;Jack";
if(i<2) {
cardTotal[0] += 10;
}
else {
cardTotal[1] += 10;
}
}
else if(randomHitMeCardNumber==11) {
whosCardNumber[num.length] = "&nbsp;&nbsp;Queen";
if(i<2) {
cardTotal[0] += 10;
}
else {
cardTotal[1] += 10;
}
}
else {
whosCardNumber[num.length] = "&nbsp;&nbsp;King";
if(i<2) {
cardTotal[0] += 10;
}
else {
cardTotal[1] += 10;
}
}

if(randomHitMeSuitNumber==0) {
whosCardSuit[num.length] = " of Hearts";
}
else if(randomHitMeSuitNumber==1) {
whosCardSuit[num.length] = " of Diamonds";
}
else if(randomHitMeSuitNumber==2) {
whosCardSuit[num.length] = " of Clubs";
}
else {
whosCardSuit[num.length] = " of Spades";
}
document.writeln(whosCardNumber[num.length] + whosCardSuit[num.length] + "<br />" + "&nbsp;&nbsp;Total: " + cardTotal[1] + "<br />");
if(cardTotal[1]<22) {
document.writeln("<br />" + "&nbsp;&nbsp;<input type='button' onmousedown='hitMe()' name='hitMe()' value='Hit Me' />" + " " + "<input type='button' onmousedown='stay()' name='stay()' value='Stay' />");
}
else {
document.writeln("<h3>" + "Bust!!! You Lose" + "</h3>" + "&nbsp;&nbsp;<a href='index.html'>New Game</a>");
}
document.writeln("</td></tr></table>");
document.writeln("<div class='footerText'>Made by: Shelby Brittain<br />I got bored... man I wish I could be paid to program &copy;</div>");
return;
}
function stay() {
document.writeln("<style type='text/css'>body { background-color: white;padding-left: 31.6%;padding-top: 25px;}input {background-color: white;border: 1px dashed #9090FF;}td {border: 5px ridge silver;padding: 10px;padding-top: 0px;background-color: #597750;}a {background-color: white;border: 1px dashed #9090FF;color: black;text-decoration: none;font-size: 12px;font-family: Monospace;padding: 9px;padding-top: 3px;padding-bottom: 3px;}h1, h2, h3 {color: #DD0000;}.footerText {margin-left: 25px;font-size: 8px;font-family: Monospace;}</style>");
document.writeln("<table><tr><td>");

document.writeln("<h1>" + "Ender's Blackjack" + "</h1>" + "Dealer: "/* + dealerPot*/ + "<br />" + " " + whosCardNumber[0] + whosCardSuit[0] + "<br />");

var randomStayCardNumber = Math.floor(Math.random()* 13);
var randomStaySuitNumber = Math.floor(Math.random()* 4);

var dealerCheck = false;

if(randomStayCardNumber==0) {
whosCardNumber[1] = "&nbsp;&nbsp;Ace";
cardTotal[0] += 11;
}
else if(randomStayCardNumber==1) {
whosCardNumber[1] = "&nbsp;&nbsp;2";
cardTotal[0] += 2;
}
else if(randomStayCardNumber==2) {
whosCardNumber[1] = "&nbsp;&nbsp;3";
cardTotal[0] += 3;
}
else if(randomStayCardNumber==3) {
whosCardNumber[1] = "&nbsp;&nbsp;4";
cardTotal[0] += 4;
}
else if(randomStayCardNumber==4) {
whosCardNumber[1] = "&nbsp;&nbsp;5";
cardTotal[0] += 5;
}
else if(randomStayCardNumber==5) {
whosCardNumber[1] = "&nbsp;&nbsp;6";
cardTotal[0] += 6;
}
else if(randomStayCardNumber==6) {
whosCardNumber[1] = "&nbsp;&nbsp;7";
cardTotal[0] += 7;
}
else if(randomStayCardNumber==7) {
whosCardNumber[1] = "&nbsp;&nbsp;8";
cardTotal[0] += 8;
}
else if(randomStayCardNumber==8) {
whosCardNumber[1] = "&nbsp;&nbsp;9";
cardTotal[0] += 9;
}
else if(randomStayCardNumber==9) {
whosCardNumber[1] = "&nbsp;&nbsp;10";
cardTotal[0] += 10;
}
else if(randomStayCardNumber==10) {
whosCardNumber[1] = "&nbsp;&nbsp;Jack";
cardTotal[0] += 10;
}
else if(randomStayCardNumber==11) {
whosCardNumber[1] = "&nbsp;&nbsp;Queen";
cardTotal[0] += 10;
}
else {
whosCardNumber[1] = "&nbsp;&nbsp;King";
cardTotal[0] += 10;
}

if(randomStaySuitNumber==0) {
whosCardSuit[1] = " of Hearts";
}
else if(randomStaySuitNumber==1) {
whosCardSuit[1] = " of Diamonds";
}
else if(randomStaySuitNumber==2) {
whosCardSuit[1] = " of Clubs";
}
else {
whosCardSuit[1] = " of Spades";
}

while(cardTotal[0]<cardTotal[1]&cardTotal[1]<22) {


var randomDealerStayCardNumber = Math.floor(Math.random()* 13);
var randomDealerStaySuitNumber = Math.floor(Math.random()* 4);

if(randomDealerStayCardNumber==0) {
document.writeln("&nbsp;&nbsp;Ace");
cardTotal[0] += 11;
}
else if(randomDealerStayCardNumber==1) {
document.writeln("&nbsp;&nbsp;2");
cardTotal[0] += 2;
}
else if(randomDealerStayCardNumber==2) {
document.writeln("&nbsp;&nbsp;3");
cardTotal[0] += 3;
}
else if(randomDealerStayCardNumber==3) {
document.writeln("&nbsp;&nbsp;4");
cardTotal[0] += 4;
}
else if(randomDealerStayCardNumber==4) {
document.writeln("&nbsp;&nbsp;5");
cardTotal[0] += 5;
}
else if(randomDealerStayCardNumber==5) {
document.writeln("&nbsp;&nbsp;6");
cardTotal[0] += 6;
}
else if(randomDealerStayCardNumber==6) {
document.writeln("&nbsp;&nbsp;7");
cardTotal[0] += 7;
}
else if(randomDealerStayCardNumber==7) {
document.writeln("&nbsp;&nbsp;8");
cardTotal[0] += 8;
}
else if(randomDealerStayCardNumber==8) {
document.writeln("&nbsp;&nbsp;9");
cardTotal[0] += 9;
}
else if(randomDealerStayCardNumber==9) {
document.writeln("&nbsp;&nbsp;10");
cardTotal[0] += 10;
}
else if(randomDealerStayCardNumber==10) {
document.writeln("&nbsp;&nbsp;Jack");
cardTotal[0] += 10;
}
else if(randomDealerStayCardNumber==11) {
document.writeln("&nbsp;&nbsp;Queen");
cardTotal[0] += 10;
}
else {
document.writeln("&nbsp;&nbsp;King");
cardTotal[0] += 10;
}

if(randomDealerStaySuitNumber==0) {
document.writeln(" of Hearts");
}
else if(randomDealerStaySuitNumber==1) {
document.writeln(" of Diamonds");
}
else if(randomDealerStaySuitNumber==2) {
document.writeln(" of Clubs");
}
else {
document.writeln(" of Spades");
}
dealerCheck = true;
document.writeln("<br />");

if(cardTotal[0]>=22&cardTotal[1]<22) {
winner = "You Win!";
playerPot += 2;
}
else if(cardTotal[0]==cardTotal[1]&cardTotal[1]<22) {
winner = "Push";
potPush += 2;
}
else {
winner = "You Lose";
dealerPot += 2;
}
}
if(cardTotal[0]>=22&cardTotal[1]<22) {
winner = "You Win!";
playerPot += 2;
}
else if(cardTotal[0]==21&dealerCheck==false) {
winner = "You Lose";
dealerPot += 2;
}
else if(cardTotal[0]==cardTotal[1]&cardTotal[1]<22) {
winner = "Push";
potPush += 2;
}
else {
winner = "You Lose";
dealerPot += 2;
}
document.writeln(" " + whosCardNumber[1] + whosCardSuit[1] + "<br />" + "&nbsp;&nbsp;Total: " + cardTotal[0] + "<br />" + "<br />" + "Player: "/* + playerPot*/ + "<br />" + " " + whosCardNumber[2] + whosCardSuit[2] + "<br />" + " " + whosCardNumber[3] + whosCardSuit[3] + "<br />" + "&nbsp;&nbsp;Total: " + cardTotal[1] + "<br />" + "<h3>" + winner + "</h3>" + "&nbsp;&nbsp;<a href='index.html'>New Game</a>");

document.writeln("</td></tr></table>");
document.writeln("<div class='footerText'>Made by: Shelby Brittain<br />I got bored... man I wish I could be paid to program &copy;</div>");
return;
}
for(var i=0; i < 4; i++) {
if(i!=1) {
var randomCardNumber = Math.floor(Math.random()* 13);
var randomSuitNumber = Math.floor(Math.random()* 4);

if(i==0) {
dealerPot -= 1;
playerPot -= 1;
document.writeln("<h1>" + "Ender's Blackjack" + "</h1>" + "Dealer: "/* + dealerPot*/ + "<br />");
}
if(randomCardNumber==0) {
whosCardNumber[i] = "&nbsp;&nbsp;Ace";
if(i<2) {
cardTotal[0] += 11;
}
else {
cardTotal[1] += 11;
ace = true;
}
}
else if(randomCardNumber==1) {
whosCardNumber[i] = "&nbsp;&nbsp;2";
if(i<2) {
cardTotal[0] += 2;
}
else {
cardTotal[1] += 2;
}
}
else if(randomCardNumber==2) {
whosCardNumber[i] = "&nbsp;&nbsp;3";
if(i<2) {
cardTotal[0] += 3;
}
else {
cardTotal[1] += 3;
}
}
else if(randomCardNumber==3) {
whosCardNumber[i] = "&nbsp;&nbsp;4";
if(i<2) {
cardTotal[0] += 4;
}
else {
cardTotal[1] += 4;
}
}
else if(randomCardNumber==4) {
whosCardNumber[i] = "&nbsp;&nbsp;5";
if(i<2) {
cardTotal[0] += 5;
}
else {
cardTotal[1] += 5;
}
}
else if(randomCardNumber==5) {
whosCardNumber[i] = "&nbsp;&nbsp;6";
if(i<2) {
cardTotal[0] += 6;
}
else {
cardTotal[1] += 6;
}
}
else if(randomCardNumber==6) {
whosCardNumber[i] = "&nbsp;&nbsp;7";
if(i<2) {
cardTotal[0] += 7;
}
else {
cardTotal[1] += 7;
}
}
else if(randomCardNumber==7) {
whosCardNumber[i] = "&nbsp;&nbsp;8";
if(i<2) {
cardTotal[0] += 8;
}
else {
cardTotal[1] += 8;
}
}
else if(randomCardNumber==8) {
whosCardNumber[i] = "&nbsp;&nbsp;9";
if(i<2) {
cardTotal[0] += 9;
}
else {
cardTotal[1] += 9;
}
}
else if(randomCardNumber==9) {
whosCardNumber[i] = "&nbsp;&nbsp;10";
if(i<2) {
cardTotal[0] += 10;
}
else {
cardTotal[1] += 10;
}
}
else if(randomCardNumber==10) {
whosCardNumber[i] = "&nbsp;&nbsp;Jack";
if(i<2) {
cardTotal[0] += 10;
}
else {
cardTotal[1] += 10;
}
}
else if(randomCardNumber==11) {
whosCardNumber[i] = "&nbsp;&nbsp;Queen";
if(i<2) {
cardTotal[0] += 10;
}
else {
cardTotal[1] += 10;
}
}
else {
whosCardNumber[i] = "&nbsp;&nbsp;King";
if(i<2) {
cardTotal[0] += 10;
}
else {
cardTotal[1] += 10;
}
}
if(cardTotal[1]==22) {
cardTotal[1]=12;
}
document.writeln(whosCardNumber[i]);

if(randomSuitNumber==0) {
whosCardSuit[i] = " of Hearts";
}
else if(randomSuitNumber==1) {
whosCardSuit[i] = " of Diamonds";
}
else if(randomSuitNumber==2) {
whosCardSuit[i] = " of Clubs";
}
else {
whosCardSuit[i] = " of Spades";
}
document.writeln(whosCardSuit[i]);
}
else {
document.writeln("&nbsp;&nbsp;Hidden Card");
}

document.writeln("<br />");

if(i==1) {
document.writeln("&nbsp;&nbsp;Total: " + cardTotal[0] + "<br />" + "<br />" + "Player: "/* + playerPot*/ + "<br />");
}

if(i==3) {
document.writeln("&nbsp;&nbsp;Total: " + cardTotal[1] + "<br />");
}

whosCardSuit[1] = "Hidden";
whosCardNumber[1] = "Hidden";


}
if(cardTotal[1]==21) {
document.writeln("<h3>" + "Blackjack!!! You Win!" + "</h3>" + "&nbsp;&nbsp;<a href='index.html'>New Game</a>");
}
else {
document.writeln("<br />" + "&nbsp;&nbsp;<input type='button' onmousedown='hitMe()' name='hitMe()' value='Hit Me' />" + " " + "<input type='button' onmousedown='stay()' name='stay()' value='Stay' />");
}


//]]>
</script>

</td>
</tr>
</table>
<div class="footerText">Made by: Shelby Brittain<br />I got bored... man I wish I could be paid to program &copy;</div>
</body>

Philip M
07-23-2008, 08:23 AM
Why invent the wheel? There are several good blackjack games for the taking, e.g.

http://javascript.internet.com/games/blackjack.html

barkermn01
07-23-2008, 01:54 PM
lol he was trying to build it as learning curve ok i know people told me for trying to build a calander but wow hes hit a new high a blackjack game as your learning curve wow

shyam
07-23-2008, 07:07 PM
First problem is that if you hit the "Hit me" button then try to hit another button afterwards it wont work. I don't even know where to begin to fix this. I know its because it is reloading and reloading wrong but how to fix that I have no idea how.
since you are using document.writeln every time you call a function that has document.writeln a new document is created with the arguments that u pass. so, the script is present only the first time the html is loaded onto the browser. the first time you hit _hit me_ the page contents have been replaced with the exact contents of the document.writelns

to fix this you can do it the easier way and use DOM and innerHTML. the hard way would be to use cookies to store the game state (and ofcourse include the js for every document.writeln)

Ok so my second problem is this. I made it but it is very poorly scripted. How would I made it a more simple script there has got to be a way to cut the line amount in half but I don't have a clue how. I've herd about switches and wondered if those could be used to help but I don't know how to use switches.
yea, u seem to have an apetite for mile long if conditions...you could use switch cases but, you only have atmost 3 cases so, go ahead with an if-else routine but, think about how u can do more with little...here's how i'd write that switch case

switch(randomCardNumber) {
case 0: whosCardNumber[num.length] = "&nbsp;&nbsp;Ace";
break;
case 10: whosCardNumber[num.length] = "&nbsp;&nbsp;Jack";
break;
case 11: whosCardNumber[num.length] = "&nbsp;&nbsp;Queen";
break;
case 12: whosCardNumber[num.length] = "&nbsp;&nbsp;King";
break;
default: whosCardNumber[num.length] = "&nbsp;&nbsp;" + (randomCardNumber+1);
}
if(i<2) {
cardTotal[0] += randomCardNumber >= 10 ? 10 : randomCardNumber+1;
} else {
cardTotal[1] += randomCardNumber >= 10 ? 10 : randomCardNumber+1;
}

Ok and my last problem is this. I want each of the cards to come in slower as if they are being delt so I want it to pause between each card. Like when you click stay the dealer all of a sudden has all of these cards. I want the person playing to be able to see that he slowly took more cards. I could put in an alert but that would be annoying. I read about the setTimeOut() object and was wondering if that might be possible to use. I only saw it being used with functions however so I'm very lost.
u can use setTimeout but, i don't think its gonna work well with those document.writelns