Gameplay
Whenever the player presses the "HIT" button, a random card is drawn and the card total is updated. The player tries to get a card total that is closer than the computer to 21. If the player's card total is greater than 21, they lose money and the computer has automatically won that round. If the player has gone bankrupt or the game has reached the maximum number of rounds, then the game ends.
071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | function hit() { // The player draws a random card and the card total is updated ranNum = Math.floor(Math.random()*10)+1 document.blackJack.yourCard.value = ranNum yourCard = parseInt(document.blackJack.yourCard.value) yourTotal = parseInt(document.blackJack.yourTotal.value) + yourCard document.blackJack.yourTotal.value = yourTotal yourMoneyBet = document.blackJack.potMoney.value if (yourTotal > 21) { // If the player's card total is greater than 21, the computer has won the round alert( "Your total is over 21. You lose this round and $" + yourMoneyBet + "." ) yourLoss = parseInt(document.blackJack.yourLoss.value) + 1 document.blackJack.yourLoss.value = yourLoss compWin = parseInt(document.blackJack.compWin.value) + 1 document.blackJack.compWin.value = compWin document.blackJack.yourCard.value = 0 document.blackJack.yourTotal.value = 0 yourTotal = 0 numRounds = parseInt(document.blackJack.numRounds.value) document.blackJack.numRounds.value = numRounds + 1 yourMoneyTotal = document.blackJack.yourMoneyTotal.value yourMoneyBet = document.blackJack.potMoney.value document.blackJack.yourMoneyTotal.value = yourMoneyTotal - yourMoneyBet checkMoney(); checkNumRounds(); if ((document.blackJack.yourMoneyTotal.value < document.blackJack.potMoney.value) && (document.blackJack.yourMoney[4].checked == true )) { // If the player decided their own amount // After losing some money, the player might be betting more than they have chooseMoney(); } } else if (yourTotal == 21) { // If the player's card total is 21, the player has automatically won the round alert( "Your total is 21. You won this round and $" + yourMoneyBet + "." ) yourWin = parseInt(document.blackJack.yourWin.value) + 1 document.blackJack.yourWin.value = yourWin compLoss = parseInt(document.blackJack.compLoss.value) + 1 document.blackJack.compLoss.value = compLoss document.blackJack.yourCard.value = 0 document.blackJack.yourTotal.value = 0 yourTotal = 0 numRounds = parseInt(document.blackJack.numRounds.value) document.blackJack.numRounds.value = numRounds + 1 yourMoneyTotal = parseInt(document.blackJack.yourMoneyTotal.value) yourMoneyBet = parseInt(document.blackJack.potMoney.value) document.blackJack.yourMoneyTotal.value = yourMoneyTotal + yourMoneyBet checkNumRounds(); if (document.blackJack.yourMoney[3].checked == true ) { // If the player decided to go all in // After winning some money, the amount of money in the pot must be updated allYourMoney(); } } } |
function hit() { // The player draws a random card and the card total is updated ranNum = Math.floor(Math.random()*10)+1 document.blackJack.yourCard.value = ranNum yourCard = parseInt(document.blackJack.yourCard.value) yourTotal = parseInt(document.blackJack.yourTotal.value) + yourCard document.blackJack.yourTotal.value = yourTotal yourMoneyBet = document.blackJack.potMoney.value if (yourTotal > 21) { // If the player's card total is greater than 21, the computer has won the round alert("Your total is over 21. You lose this round and $" + yourMoneyBet + ".") yourLoss = parseInt(document.blackJack.yourLoss.value) + 1 document.blackJack.yourLoss.value = yourLoss compWin = parseInt(document.blackJack.compWin.value) + 1 document.blackJack.compWin.value = compWin document.blackJack.yourCard.value = 0 document.blackJack.yourTotal.value = 0 yourTotal = 0 numRounds = parseInt(document.blackJack.numRounds.value) document.blackJack.numRounds.value = numRounds + 1 yourMoneyTotal = document.blackJack.yourMoneyTotal.value yourMoneyBet = document.blackJack.potMoney.value document.blackJack.yourMoneyTotal.value = yourMoneyTotal - yourMoneyBet checkMoney(); checkNumRounds(); if ((document.blackJack.yourMoneyTotal.value < document.blackJack.potMoney.value) && (document.blackJack.yourMoney[4].checked == true)) { // If the player decided their own amount // After losing some money, the player might be betting more than they have chooseMoney(); } } else if (yourTotal == 21) { // If the player's card total is 21, the player has automatically won the round alert("Your total is 21. You won this round and $" + yourMoneyBet + ".") yourWin = parseInt(document.blackJack.yourWin.value) + 1 document.blackJack.yourWin.value = yourWin compLoss = parseInt(document.blackJack.compLoss.value) + 1 document.blackJack.compLoss.value = compLoss document.blackJack.yourCard.value = 0 document.blackJack.yourTotal.value = 0 yourTotal = 0 numRounds = parseInt(document.blackJack.numRounds.value) document.blackJack.numRounds.value = numRounds + 1 yourMoneyTotal = parseInt(document.blackJack.yourMoneyTotal.value) yourMoneyBet = parseInt(document.blackJack.potMoney.value) document.blackJack.yourMoneyTotal.value = yourMoneyTotal + yourMoneyBet checkNumRounds(); if (document.blackJack.yourMoney[3].checked == true) { // If the player decided to go all in // After winning some money, the amount of money in the pot must be updated allYourMoney(); } } }
When the player presses the "STAND" button, the player stops receiving random cards that round. The computer will continue to draw random cards until their total is greater than the player's or is 21. The player will win or lose their bet depending on who gets the closest card total to 21 without going over.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | function stand() { // The player decides to end their turn alert( "Your total is " + yourTotal + "." ) yourMoneyBet = document.blackJack.potMoney.value // The computer continues to draw random cards until either its card total is greater than 21 or the player's card total for (compTotal = 0; compTotal < 21; compTotal + compCard) { ranNum = Math.floor(Math.random()*10)+1 document.blackJack.compCard.value = ranNum compCard = parseInt(document.blackJack.compCard.value) compTotal = compTotal + compCard document.blackJack.compTotal.value = compTotal alert( "Computer's card is " + compCard+ ", and the computer's total is " +compTotal+ "." ) // If the computer's greater card total than the player's but is still less than 21, the computer has won the round if (compTotal > yourTotal && compTotal <= 21) { alert( "The computer has automatically won this round since it has a total greater than yours and yet, is still under or equal to 21. \nYou have lost $" + yourMoneyBet + "." ) document.blackJack.compCard.value = 0 document.blackJack.compTotal.value = 0 document.blackJack.yourTotal.value = 0 document.blackJack.yourCard.value = 0 yourLoss = parseInt(document.blackJack.yourLoss.value) + 1 document.blackJack.yourLoss.value = yourLoss compWin = parseInt(document.blackJack.compWin.value) + 1 document.blackJack.compWin.value = compWin numRounds = parseInt(document.blackJack.numRounds.value) document.blackJack.numRounds.value = numRounds + 1 yourMoneyTotal = parseInt(document.blackJack.yourMoneyTotal.value) yourMoneyBet = parseInt(document.blackJack.potMoney.value) document.blackJack.yourMoneyTotal.value = yourMoneyTotal - yourMoneyBet checkMoney(); checkNumRounds(); if ((document.blackJack.yourMoneyTotal.value < document.blackJack.potMoney.value) && (document.blackJack.yourMoney[4].checked == true )) { // If the player decided their own amount // After losing some money, the player might be betting more than they have chooseMoney(); } break } } // If the computer has a card total greater than 21, the player has won that round if (compTotal > 21) { alert( "The computer's total is over 21. You won this round and $" + yourMoneyBet + "." ) document.blackJack.compCard.value = 0 document.blackJack.compTotal.value = 0 document.blackJack.yourTotal.value = 0 document.blackJack.yourCard.value = 0 yourWin = parseInt(document.blackJack.yourWin.value) + 1 document.blackJack.yourWin.value = yourWin compLoss = parseInt(document.blackJack.compLoss.value) + 1 document.blackJack.compLoss.value = compLoss numRounds = parseInt(document.blackJack.numRounds.value) document.blackJack.numRounds.value = numRounds + 1 yourMoneyTotal = parseInt(document.blackJack.yourMoneyTotal.value) yourMoneyBet = parseInt(document.blackJack.potMoney.value) document.blackJack.yourMoneyTotal.value = yourMoneyTotal + yourMoneyBet checkNumRounds(); if (document.blackJack.yourMoney[3].checked == true ) { // If the player decided to go all in // After winning some money, the amount of money in the pot must be updated allYourMoney(); } } // The cards and card totals are reset for the next round compCard = 0 compTotal = 0 yourCard = 0 yourTotal = 0 } |
function stand() { // The player decides to end their turn alert("Your total is " + yourTotal + ".") yourMoneyBet = document.blackJack.potMoney.value // The computer continues to draw random cards until either its card total is greater than 21 or the player's card total for (compTotal = 0; compTotal < 21; compTotal + compCard) { ranNum = Math.floor(Math.random()*10)+1 document.blackJack.compCard.value = ranNum compCard = parseInt(document.blackJack.compCard.value) compTotal = compTotal + compCard document.blackJack.compTotal.value = compTotal alert("Computer's card is " + compCard+", and the computer's total is "+compTotal+".") // If the computer's greater card total than the player's but is still less than 21, the computer has won the round if (compTotal > yourTotal && compTotal <= 21) { alert("The computer has automatically won this round since it has a total greater than yours and yet, is still under or equal to 21. \nYou have lost $" + yourMoneyBet + ".") document.blackJack.compCard.value = 0 document.blackJack.compTotal.value = 0 document.blackJack.yourTotal.value = 0 document.blackJack.yourCard.value = 0 yourLoss = parseInt(document.blackJack.yourLoss.value) + 1 document.blackJack.yourLoss.value = yourLoss compWin = parseInt(document.blackJack.compWin.value) + 1 document.blackJack.compWin.value = compWin numRounds = parseInt(document.blackJack.numRounds.value) document.blackJack.numRounds.value = numRounds + 1 yourMoneyTotal = parseInt(document.blackJack.yourMoneyTotal.value) yourMoneyBet = parseInt(document.blackJack.potMoney.value) document.blackJack.yourMoneyTotal.value = yourMoneyTotal - yourMoneyBet checkMoney(); checkNumRounds(); if ((document.blackJack.yourMoneyTotal.value < document.blackJack.potMoney.value) && (document.blackJack.yourMoney[4].checked == true)) { // If the player decided their own amount // After losing some money, the player might be betting more than they have chooseMoney(); } break } } // If the computer has a card total greater than 21, the player has won that round if (compTotal > 21) { alert("The computer's total is over 21. You won this round and $" + yourMoneyBet + ".") document.blackJack.compCard.value = 0 document.blackJack.compTotal.value = 0 document.blackJack.yourTotal.value = 0 document.blackJack.yourCard.value = 0 yourWin = parseInt(document.blackJack.yourWin.value) + 1 document.blackJack.yourWin.value = yourWin compLoss = parseInt(document.blackJack.compLoss.value) + 1 document.blackJack.compLoss.value = compLoss numRounds = parseInt(document.blackJack.numRounds.value) document.blackJack.numRounds.value = numRounds + 1 yourMoneyTotal = parseInt(document.blackJack.yourMoneyTotal.value) yourMoneyBet = parseInt(document.blackJack.potMoney.value) document.blackJack.yourMoneyTotal.value = yourMoneyTotal + yourMoneyBet checkNumRounds(); if (document.blackJack.yourMoney[3].checked == true) { // If the player decided to go all in // After winning some money, the amount of money in the pot must be updated allYourMoney(); } } // The cards and card totals are reset for the next round compCard = 0 compTotal = 0 yourCard = 0 yourTotal = 0 }
When the game ends, the player has the option to restart or to close the page. I decided to change closing the page to refreshing the page.
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | function playAgain() { // When the game ends because to the player has gone bankrupt // The player is given the option to restart var playAgain = confirm( "The game is over, because you have gone bankrupt. Would you like to play again?" ) if (playAgain == true ) { document.blackJack.yourMoneyTotal.value = 500 document.blackJack.numRounds.value = 0 document.blackJack.yourWin.value = 0 document.blackJack.yourLoss.value = 0 document.blackJack.compWin.value = 0 document.blackJack.compLoss.value = 0 document.blackJack.yourMoney[0].checked= true ; document.blackJack.potMoney.value = document.blackJack.yourMoney[0].value } else { // When I submitted this game for my Computers 11 class, the page would close if the player did not want to play another game // I decided to change this to refreshing the page when I incorporated it into this website location.reload( true ); } } |
function playAgain() { // When the game ends because to the player has gone bankrupt // The player is given the option to restart var playAgain = confirm("The game is over, because you have gone bankrupt. Would you like to play again?") if (playAgain == true) { document.blackJack.yourMoneyTotal.value = 500 document.blackJack.numRounds.value = 0 document.blackJack.yourWin.value = 0 document.blackJack.yourLoss.value = 0 document.blackJack.compWin.value = 0 document.blackJack.compLoss.value = 0 document.blackJack.yourMoney[0].checked=true; document.blackJack.potMoney.value = document.blackJack.yourMoney[0].value } else { // When I submitted this game for my Computers 11 class, the page would close if the player did not want to play another game // I decided to change this to refreshing the page when I incorporated it into this website location.reload(true); } }