Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Blackjack NPC

Newbie Spellweaver
Joined
Apr 10, 2009
Messages
91
Reaction score
195
Edit 4: WOW SCREW YOU WHOEVER BUMPED THIS =.=;; I JUST NOTICED

My version o.o;;

PHP:
//Author : Emilyx3
var fee;

var dealerCards = [];
var playerCards = [];

var CARDS = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"];

var status = 0;

function showHand(cards) {
    var ret = "#b[";
    for (var i = 0;i < cards.length;i++) {
        ret += cards[i];
        if (i != cards.length - 1)
            ret += " ";
    }
    ret += "] [Total Value: "+calcHandValue(cards)+"]#k";
    return ret;
}

function calcHandValue(cards) {
    if (cards.length == 2) { //Figure out BlackJack first =.=;;
        if (cards[0] == "A" && (cards[1] == "10" || cards[1] == "J" || cards[1] == "Q" || cards[1] == "K"))
            return "BlackJack";
        if (cards[1] == "A" && (cards[0] == "10" || cards[0] == "J" || cards[0] == "Q" || cards[0] == "K"))
            return "BlackJack";
    }
    var ret = 0;
    var numAces = 0; //Stupid variable aces....
    for (var i = 0;i < cards.length;i++)
        if (!isNaN(cards[i]))
            ret += Number(cards[i]);
        else {
            if (cards[i] == "A") {
                numAces++;
                ret += 11;
            } else
                ret += 10;
        }
    while (numAces > 0 && ret > 21) {
        numAces --;
        ret -= 10;
    }
    return ret;
}

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1)
        cm.dispose();
    else {
        if (mode == 0) {
            cm.sendOk("Your loss. You could have won big money.");
            cm.dispose();
            return;
        }
        if (mode == 1)
            status++;
        else
            status--;
        if (status == 0)
            cm.sendNext("Are you up for some #bBlackjack?#k If you win, you can double your mesos! Dealer stays at 17.");
        else if (status == 1)
            cm.sendGetText("How many mesos would you like to bet?");
        else if (status == 2) {
            fee = cm.getText();
            if (isNaN(fee) || fee == "" || fee < 0) {
                cm.sendOk("You didn't enter a proper bet...");
                cm.dispose();
            } else
                cm.sendYesNo("Are you sure you want to bet #r" + fee + "#k Mesos?... ");
        } else if (status == 3) {
            if (cm.getMeso() < fee) {
                cm.sendOk("You don't have that much money. Bloody fool.");
                cm.dispose();
            } else {
                cm.gainMeso(-fee); //We take it away now! RAWR!
                dealerCards.push(CARDS[Math.floor(Math.random() * CARDS.length)]);
                dealerCards.push(CARDS[Math.floor(Math.random() * CARDS.length)]);
                playerCards.push(CARDS[Math.floor(Math.random() * CARDS.length)]);
                playerCards.push(CARDS[Math.floor(Math.random() * CARDS.length)]);
                cm.sendNext("Dealer is showing a " + dealerCards[1] + ". Your turn...");
            }
        } else if (status == 4) {
            var sendStr = "Your hand is "+showHand(playerCards)+".";
            if (calcHandValue(playerCards) == "BlackJack") {
                sendStr += "\r\nNice, you got blackjack. Let's see if you win?";
                status ++; //Skip to score calc.
                cm.sendNext(sendStr);
            } else {
                sendStr += "\r\n\r\nWhat will you do? \r\n#L0#Hit.#l\r\n#L1#Stay.#l";
                cm.sendSimple(sendStr);
            }
        } else if (status == 5) {
            if (selection == 0) {
                status--;
                playerCards.push(CARDS[Math.floor(Math.random() * CARDS.length)]);
                sendStr = "Your hand is "+showHand(playerCards)+".";
                var val = calcHandValue(playerCards);
                if (val > 21) {
                    sendStr += "\r\nOh no! You busted, sorry you lose.";
                    cm.sendOk(sendStr);
                    cm.dispose();
                } else if (val == 21) {
                    sendStr += "\r\nCongrats, you have 21. Let's see if you win...";
                    status++; //Go straight to 6.
                    cm.sendNext(sendStr);
                } else {
                    sendStr += "\r\n\r\nWhat will you do? \r\n#L0#Hit.#l\r\n#L1#Stay.#l";
                    cm.sendSimple(sendStr);
                }
            } else {
                status++;
            }
        } else if (status != 6) { //If we get here but the status isn't 6, something messed up.
            cm.dispose();
            return;
        }
        //This is purposefully not part of the else-if chain.
        if (status == 6) {
            sendStr = "The dealer's hand is: ";
            if (calcHandValue(dealerCards) == "BlackJack") {
                sendStr += showHand(dealerCards);
                cm.sendOk(sendStr+".\r\n#rSorry, the dealer got BlackJack. You lose!");
                cm.dispose();
                return;
                //We really should penalize the player for 50% more.. but that'll be mean xD
            }
            while (calcHandValue(dealerCards) < 17)
                dealerCards.push(CARDS[Math.floor(Math.random() * CARDS.length)]);
            sendStr += showHand(dealerCards);
            if (calcHandValue(playerCards) == "BlackJack") {
                cm.sendOk(sendStr+".\r\n#rNice, you won with BlackJack. You win 1.5x your bet.");
                cm.gainMeso(2.5 * fee);
                cm.dispose();
                return;
            }
            var fDTot = calcHandValue(dealerCards);
            var fPTot = calcHandValue(playerCards);
            if (fDTot > 21) {
                cm.sendOk(sendStr+".\r\n#rThe dealer busted! You won! You gain your bet.");
                cm.gainMeso(2 * fee);
            } else if (fDTot > fPTot)
                cm.sendOk(sendStr+".\r\n#rSorry, you lost!");
            else if (fDTot == fPTot) {
                cm.sendOk(sendStr+".\r\n#rIt's a draw! You win back your bet.");
                cm.gainMeso(fee);
            } else {
                cm.sendOk(sendStr+".\r\n#rYou won! You gain your bet.");
                cm.gainMeso(2 * fee);
            }
            cm.dispose();
        }
    }
}

Edit: RAWR! Fixed minor errors =.=;;
Edit 2: Fixed Dealer winning with 22+ >_>;;
Edit 3: Fixed improper dispose if you hit 21 xD Should be working perfectly now...
 
Last edited:
Joined
Apr 10, 2008
Messages
4,087
Reaction score
1,264
My version o.o;;

PHP:
//Author: Emilyx3
var fee;

var dealerCards = new Array();
var playerCards = new Array();

var CARDS = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"];

var status = 0;

function showHand(cards) {
    var ret = "#b[";
    for (var i = 0;i < cards.length;i++) {
        ret += cards[i];
        if (i != cards.length - 1)
            ret += " ";
    }
    ret += "] [Total Value: "+calcHandValue(cards)+"]#k";
    return ret;
}

function calcHandValue(cards) {
    if (cards.length == 2) { //Figure out BlackJack first =.=;;
        if (cards[0] == "A" && (cards[1] == "10" || cards[1] == "J" || cards[1] == "Q" || cards[i] == "K"))
            return "BlackJack";
        if (cards[1] == "A" && (cards[0] == "10" || cards[0] == "J" || cards[0] == "Q" || cards[0] == "K"))
            return "BlackJack";
    }
    var ret = 0;
    var numAces = 0; //Stupid variable aces....
    for (var i = 0;i < cards.length;i++)
        if (isNumeric(cards[i]))
            ret += Number(cards[i]);
        else {
            if (cards[i] == "A") {
                numAces++;
                ret += 11;
            } else
                ret += 10;
        }
    while (numAces > 0 && ret > 21) {
        numAces --;
        ret -= 10;
    }
    return ret;
}

function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1)
        cm.dispose();
    else {
        if (mode == 0) {
            cm.sendOk("Your loss. You could have won big money.");
            cm.dispose();
            return;
        }
        if (mode == 1)
            status++;
        else
            status--;
        if (status == 0)
            cm.sendNext("Are you up for some #bBlackjack?#k If you win, you can double your mesos! Dealer stays at 17.");
        else if (status == 1)
            cm.sendGetText("How many mesos would you like to bet?");
        else if (status == 2) {
            fee = cm.getText();
            if (isNan(fee) || !isNumeric(fee) || fee < 0) {
                cm.sendOk("You didn't enter a proper bet...");
                cm.dispose();
            } else
                cm.sendYesNo("Are you sure you want to bet #r" + fee + "#k Mesos?... ");
        } else if (status == 3) {
            if (cm.getMeso() < fee) {
                cm.sendOk("You don't have that much money. Bloody fool.");
                cm.dispose();
            } else {
                cm.gainMeso(-fee); //We take it away now! RAWR!
                dealerCards.push(CARDS[Math.floor(Math.random() * cards.length)]);
                dealerCards.push(CARDS[Math.floor(Math.random() * cards.length)]);
                playerCards.push(CARDS[Math.floor(Math.random() * cards.length)]);
                playerCards.push(CARDS[Math.floor(Math.random() * cards.length)]);
                cm.sendNext("Dealer is showing a " + dealerCards[1] + ". Your turn...");
            }
        } else if (status == 4) {
            var sendStr = "Your hand is "+showHand(playerCards)+".";
            if (calcHandValue(playerCards) == "BlackJack") {
                sendStr += "\r\nNice, you got blackjack. Let's see if you win?";
                status ++; //Skip to score calc.
                cm.sendNext(sendStr);
            } else {
                sendStr += "\r\n\r\nWhat will you do? \r\n#L0#Hit.#l\r\n#L1#Stay.#l";
                cm.sendSimple(sendStr);
            }
        } else if (status == 5) {
            if (selection == 0) {
                status--;
                playerCards.push(CARDS[Math.floor(Math.random() * cards.length)]);
                sendStr = "Your hand is "+showHand(playerCards)+".";
                var val = calcHandValue(playerCards);
                if (val > 21) {
                    sendStr += "\r\nOh no! You busted, sorry you lose.";
                    cm.sendOk(sendStr);
                    cm.dispose();
                } else if (val == 21) {
                    sendStr += "\r\nCongrats, you have 21. Let's see if you win...";
                    status++; //Go straight to 6.
                } else {
                    sendStr += "\r\n\r\nWhat will you do? \r\n#L0#Hit.#l\r\n#L1#Stay.#l";
                    cm.sendSimple(sendStr);
                }
            } else {
                status++;
            }
        } else if (status != 6) { //If we get here but the status isn't 6, something messed up.
            cm.dispose();
            return;
        }
        //This is purposefully not part of the else-if chain.
        if (status == 6) {
            sendStr = "The dealer's hand is: ";
            if (calcHandValue(dealerCards) == "BlackJack") {
                sendStr += showHand(dealerCards);
                cm.sendOk(sendStr+".\r\n#rSorry, the dealer got BlackJack. You lose!");
                cm.dispose();
                return;
                //We really should penalize the player for 50% more.. but that'll be mean xD
            }
            while (calcHandValue(dealerCards) < 17)
                dealerCards.push(CARDS[Math.floor(Math.random() * cards.length)]);
            sendStr += showHand(dealerCards);
            if (calcHandValue(playerCards) == "BlackJack") {
                cm.sendOk(sendStr+".\r\n#rNice, you won with BlackJack. You win 1.5x your bet.");
                cm.gainMeso(2.5 * fee);
                cm.dispose();
                return;
            }
            var fDTot = calcHandValue(dealerCards);
            var fPTot = calcHandValue(playerCards);
            if (fDTot > fPTot)
                cm.sendOk(sendStr+".\r\n#rSorry, you lost!");
            else if (fDTot == fPTot) {
                cm.sendOk(sendStr+".\r\n#rIt's a draw! You win back your bet.");
                cm.gainMeso(fee);
            } else {
                cm.sendOk(sendStr+".\r\n#rYou won! You gain your bet.");
                cm.gainMeso(2 * fee);
            }
            cm.dispose();
        }
    }
}

You're too pro. :love:
 
Custom Title Activated
Loyal Member
Joined
Apr 29, 2008
Messages
1,297
Reaction score
509
Oh, isn't A 10 or 1 when you have 3 card or more?
It's only 11 when you have 2 cards.
 
Newbie Spellweaver
Joined
Apr 10, 2009
Messages
91
Reaction score
195
Oh, isn't A 10 or 1 when you have 3 card or more?
It's only 11 when you have 2 cards.

Nope.


Number-cards count as their natural value; the jack, queen, and king (also known as "face cards" or "pictures") count as 10; aces are valued as either 1 or 11 according to the player's best interest.
 
Newbie Spellweaver
Joined
Dec 29, 2011
Messages
11
Reaction score
0
I got dc to login page after inserting the money that I want to bet?
 
butt > Tits
Loyal Member
Joined
Feb 16, 2009
Messages
658
Reaction score
96
If you're going to bump such an old thread at least provide us the error..
 
Newbie Spellweaver
Joined
Jan 23, 2012
Messages
18
Reaction score
0
wooo im gonna see if i can add this to my server!
Awesome job
 
Back
Top