JAVASCRIPT - Simple Blackjack game


SUBMITTED BY: efbee

DATE: Oct. 4, 2016, 11:06 p.m.

FORMAT: JavaScript

SIZE: 4.9 kB

HITS: 400

  1. <!-- THREE STEPS TO INSTALL BLACKJACK:
  2. 1. Copy the coding into the HEAD of your HTML document
  3. 2. Add the onLoad event handler into the BODY tag
  4. 3. Put the last coding into the BODY of your HTML document -->
  5. <!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
  6. <SCRIPT LANGUAGE="JavaScript">
  7. <!-- Original: Mike McGrath (mike_mcgrath@lineone.net) -->
  8. <!-- Web Site: http://home.clara.net/mikem -->
  9. <!-- Begin
  10. var gameOver; var cardCount;
  11. function Shuffle(max){
  12. var num=Math.random()*max;
  13. return Math.round(num)+1;
  14. }
  15. function getSuit(){
  16. suit = Shuffle(4);
  17. if(suit == 1) return "Spades";
  18. if(suit == 2) return "Clubs";
  19. if(suit == 3) return "Diamonds";
  20. else return "Hearts";
  21. }
  22. function cardName(card){
  23. if(card == 1) return "Ace";
  24. if(card == 11) return "Jack";
  25. if(card == 12) return "Queen";
  26. if(card == 13) return "King";
  27. return "" + card;
  28. }
  29. function cardValue(card,strWho){
  30. if(card == 1) {
  31. if(strWho =="You" && document.display.you.value >10){
  32. document.display.say2.value=document.display.say2.value+" Low"; return 1;}
  33. else return 11; }
  34. if(card > 10) return 10;
  35. return card;
  36. }
  37. function PickACard(strWho){
  38. card = Shuffle(12);
  39. suit = getSuit();
  40. if(strWho =="You")
  41. document.display.say2.value=(cardName(card) + " of " + suit);
  42. else
  43. document.display.say1.value=(cardName(card) + " of " + suit);
  44. return cardValue(card,strWho);
  45. }
  46. function NewHand(form){
  47. if(gameOver !=0)
  48. {form.say1.value=("Hand in Play!"); form.say2.value=(""); return;}
  49. else
  50. {form.dealer.value = 0; form.you.value = 0; cardCount=0;
  51. form.dealer.value = eval(form.dealer.value) + PickACard("Dealer");
  52. form.you.value = eval(form.you.value) + PickACard("You");
  53. gameOver= -1; cardCount+=1;}
  54. }
  55. function Dealer(form){
  56. if (gameOver ==0)
  57. {form.say1.value=("Deal the Cards!"); form.say2.value=(""); return;}
  58. else
  59. if(form.you.value<10)
  60. {form.say1.value=("Not Below Ten!"); form.say2.value=("Take a Hit!"); return;}
  61. else
  62. if (cardCount <2)
  63. {form.say1.value=("Minimum 2 Cards!"); form.say2.value=("Hit Again!"); return;}
  64. else
  65. while(form.dealer.value < 17)
  66. {form.dealer.value = eval(form.dealer.value) + PickACard("Dealer");}
  67. }
  68. function User(form){
  69. if (gameOver ==0)
  70. {form.say1.value=("Deal the Cards!"); form.say2.value=(""); return;}
  71. else
  72. {cardCount+=1; form.say1.value=("You Get...");
  73. form.you.value = eval(form.you.value) + PickACard("You");}
  74. if(form.you.value > 21)
  75. {form.say1.value=("You Busted!");
  76. gameOver=0; form.numgames.value=eval(form.numgames.value)-1;}
  77. }
  78. function LookAtHands(form){
  79. if (gameOver ==0 || form.you.value<10 || cardCount <2){return;}
  80. else
  81. if(form.dealer.value > 21)
  82. {form.say1.value=("House Busts!"); form.say2.value=("You Win! $$$$$$");
  83. gameOver=0; form.numgames.value=eval(form.numgames.value)+1;}
  84. else
  85. if(form.you.value > form.dealer.value)
  86. {form.say1.value=("You Win!"); form.say2.value=("$$$$$$$$$$$$$$$");
  87. gameOver=0; form.numgames.value=eval(form.numgames.value)+1;}
  88. else
  89. if(form.dealer.value == form.you.value)
  90. {form.say1.value=("Game Tied!"); form.say2.value=("Try Again!");
  91. gameOver=0; form.numgames.value=eval(form.numgames.value)-1;}
  92. else
  93. {form.say1.value=("House Wins!"); form.say2.value=("Tough Luck!");
  94. gameOver=0; form.numgames.value=eval(form.numgames.value)-1;}
  95. }
  96. function setBj(){
  97. gameOver=0; cardCount=0;
  98. document.display.dealer.value="";
  99. document.display.you.value="";
  100. document.display.numgames.value="0";
  101. document.display.say1.value=" Hit 'Deal'";
  102. document.display.say2.value=" To Start!";
  103. }
  104. // End -->
  105. </script>
  106. <!-- STEP TWO: Insert the onLoad event handler into your BODY tag -->
  107. <BODY OnLoad="setBj()">
  108. <!-- STEP THREE: Copy this code into the BODY of your HTML document -->
  109. <center>
  110. <form name="display">
  111. <table bgcolor="#c0c0c0" border="1" cellspacing="0" cellpadding="3">
  112. <tr>
  113. <td><center>Score:</center></td>
  114. <td><center>Dealer</center></td>
  115. <td><center><input type=text name="dealer" size="2"></center></td>
  116. <td><center>Card(s): <input type=text name="say1" size="18" value=""></center></td>
  117. </tr>
  118. <tr>
  119. <td><center><input type=text name="numgames" size="3" value="0"></center></td>
  120. <td><center>Player</center></td>
  121. <td><center><input type=text name="you" size="2"></center></td>
  122. <td><center>Card(s): <input type=text name="say2" size="18" value=""></center></td>
  123. </tr>
  124. <tr>
  125. <td><center><input type=button value="Deal" onClick="NewHand(this.form)"></center></td>
  126. <td colspan=3><center>
  127. <input type=button value="Stand" onClick="Dealer(this.form);LookAtHands(this.form);">
  128. <input type=button value=" Hit " onClick="User(this.form)"></center></td></tr>
  129. </table>
  130. </form>
  131. </center>
  132. <p><center>
  133. <font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
  134. by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
  135. </center><p>
  136. <!-- Script Size: 4.57 KB -->

comments powered by Disqus