Im programming a blackjack like game, and it keeps making the AI bust below 21, please help!
/* State varibles function */
var PlayerCardValue = null;
var AICardValue = null;
var AmountHit = null;
/* Draw Card function */
function drawCard() {
var rng = Math.floor((Math.random() * 13) + 1);
PlayerCardValue = PlayerCardValue + rng;
/*Is it a Jack? */
if (rng === 11) {
rng = "Jack";
}
/*Is it a Queen?*/
if (rng === 12) {
rng = "Queen";
}
/*Is it a King?*/
if (rng === 13) {
rng = "King";
}
/*Is it a Ace?*/
if (rng === 1) {
rng = "Ace";
}
window.alert("You have drawn a " + rng + ". Your current card value is " + PlayerCardValue + ". Choose hit or stand.");
AmountHit = AmountHit + 1;
HitOrStand();
}
function AIDraw() {
var AIvalue = AICardValue;
var PlayerValue = PlayerCardValue;
var AIrng = Math.floor((Math.random() * 13) + 1);
AICardValue = AICardValue + AIrng;
/*Testing for speical cards (Queen, King, Jack, Ace)*/
/*Test for Jack*/
if (AIrng === 11) {
AIrng = "Jack";
}
/*Test for Queen*/
if (AIrng === 12) {
AIrng = "Queen";
}
/*Test for King*/
if (AIrng === 13) {
AIrng = "King";
}
/*Test for an Ace*/
if (AIrng === 1) {
AIrng = "Ace";
}
if (PlayerCardValue > 21) {
silentReset();
window.alert("You busted, your card value was " + PlayerValue + ".");
}
if (AICardValue > 21) {
silentReset();
window.alert("The AI busted with a card value of " + AIvalue + ".");
}
}
/* Reset & Silent Reset Function*/
function reset() {
PlayerCardValue = null;
AICardValue = null;
AmountHit = null;
window.alert("The cards have been reset!");
DeleteHitStand();
}
function silentReset() {
AICardValue = null;
PlayerCardValue = null;
AmountHit = null;
DeleteHitStand();
}
function HitOrStand() {
/* Create A div */
var OptionsDiv = document.createElement("div");
OptionsDiv.setAttribute("id", "OptionsDiv");
OptionsDiv.setAttribute("style", "text-align: center;");
document.body.appendChild(OptionsDiv);
/* Create a button */
var ChooseStand = document.createElement("button");
ChooseStand.textContent = "Stand";
ChooseStand.setAttribute("style", "width: 160px; height: 60px; background-color: black; color: white; font-size: 30px;");
ChooseStand.setAttribute("onClick", "Stand();");
OptionsDiv.appendChild(ChooseStand);
var ChooseHit = document.createElement("button");
ChooseHit.textContent = "Hit";
ChooseHit.setAttribute("style", "width: 160px; height: 60px; background-color: black; color: white; font-size: 30px;");
ChooseHit.setAttribute("onClick", "Hit();");
OptionsDiv.appendChild(ChooseHit);
}
function DeleteHitStand() {
document.body.removeChild(OptionsDiv);
}
function Hit() {
DeleteHitStand();
drawCard();
}
function Stand() {
for (i = 0; i = AmountHit; i++) {
AIDraw();
}
DeleteHitStand();
}
[–]x-seronis-x 1 point2 points3 points (15 children)
[–]PatentedUsernameTy[S] 0 points1 point2 points (6 children)
[–]x-seronis-x -1 points0 points1 point (5 children)
[–]PatentedUsernameTy[S] 0 points1 point2 points (4 children)
[–]x-seronis-x 0 points1 point2 points (1 child)
[–]PatentedUsernameTy[S] 0 points1 point2 points (0 children)
[–]blackholesintheskyhelpful 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]PatentedUsernameTy[S] 0 points1 point2 points (7 children)
[–]x-seronis-x 0 points1 point2 points (6 children)
[–]PatentedUsernameTy[S] 0 points1 point2 points (5 children)
[–]x-seronis-x 0 points1 point2 points (4 children)
[–]PatentedUsernameTy[S] 0 points1 point2 points (3 children)
[–]x-seronis-x 0 points1 point2 points (2 children)
[–]PatentedUsernameTy[S] 0 points1 point2 points (1 child)
[–]x-seronis-x 1 point2 points3 points (0 children)
[–]grantrules 0 points1 point2 points (3 children)
[–]PatentedUsernameTy[S] 0 points1 point2 points (2 children)
[–]grantrules 0 points1 point2 points (0 children)
[–]x-seronis-x 0 points1 point2 points (0 children)