Apps Home
|
Create an App
Test app
Author:
greetorp
Description
Source Code
Launch App
Current Users
Created by:
Greetorp
/* Name: Blackjack Author: mx2k6 Version History ============================================ v1.0 02/02/2013: First release */ var total_tipped = 0; var display_twice = false; var blackjack = { handInProgress: false, cardNames: ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King'], cardValues: {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Ace': 11, 'Jack': 11, 'Queen': 11, 'King': 11}, playerHands: [], players: [], dealerHand: { 'total': 0, blackjack: false, getAllCards: function () { return this.total + (this.blackjack ? " (Blackjack!)" : ""); } }, onBust: function (user) {}, onCardDrawn: function (user, card) {}, onGameOver: function (dealerWin, winnerName) {}, setupDealerHand: function () { blackjack.dealerHand.total = Math.floor(Math.random() * 4) + 17; if (blackjack.dealerHand.total === 21) { blackjack.dealerHand.blackjack = Math.floor(Math.random() * 2) === 0 ? true : false; } }, setupPlayerHand: function (user) { blackjack.players[blackjack.players.length] = user; blackjack.playerHands[user] = { user: user, busted: false, stood: false, getTotal: function () { var total = 0; var cardIndex = 0; for (cardIndex = 0; cardIndex < this.cards.length; cardIndex++) { if (this.cards[cardIndex] !== 'Ace') { total += blackjack.cardValues[this.cards[cardIndex]]; } else { if (total + 11 > 21) { total++; } else { total += blackjack.cardValues[this.cards[cardIndex]]; } } } return total; }, addCard: function (card) { this.cards[this.cards.length] = card; if (this.getTotal() > 21) { this.busted = true; blackjack.onBust(this.user); blackjack.checkReady(); } }, getAllCards: function () { var cardIndex = 0; var cardString = ""; for (cardIndex = 0; cardIndex < this.cards.length; cardIndex++) { cardString += this.cards[cardIndex] + ", "; } return cardString.substring(0, cardString.length - 2); }, stand: function () { this.stood = true; blackjack.checkReady(); }, cards: [] }; blackjack.playerHands[user].addCard(blackjack.getNewCard()); blackjack.playerHands[user].addCard(blackjack.getNewCard()); }, drawCard: function (user) { var card = blackjack.getNewCard(); blackjack.playerHands[user].addCard(card); blackjack.onCardDrawn(user, card); }, getNewCard: function () { return blackjack.cardNames[Math.floor(Math.random() * 13)]; }, gameOver: function () { var playerIndex = 0; var highestHand = {user: null, total: 0}; for (playerIndex = 0; playerIndex < blackjack.players.length; playerIndex++) { if (blackjack.playerHands[blackjack.players[playerIndex]].getTotal() > highestHand.total && !blackjack.playerHands[blackjack.players[playerIndex]].busted) { highestHand.user = blackjack.playerHands[blackjack.players[playerIndex]].user; highestHand.total = blackjack.playerHands[blackjack.players[playerIndex]].getTotal(); } } cb.log("gameOver(): highest hand is " + highestHand.total + " by " + highestHand.user); if (highestHand.total > blackjack.dealerHand.total || (highestHand.total === blackjack.dealerHand.total && !blackjack.dealerHand.blackjack)) { blackjack.onGameOver(false, highestHand.user); } else { blackjack.onGameOver(true, null); } }, getIsPlaying: function (user) { var returnValue = false; if (typeof (blackjack.playerHands[user]) !== "undefined") { returnValue = true; } return returnValue; }, checkReady: function () { var playerIndex = 0; var ready = true; for (playerIndex = 0; playerIndex < this.playerHands.length; playerIndex++) { ready = (ready && (blackjack.playerHands[playerIndex].busted || this.playerHands[playerIndex].stood)); } if (ready) { blackjack.gameOver(); } } }; cb.settings_choices = [ {name: 'prize', label: 'Prize for winner', type: 'str', minLength: 1, maxLength: 75}, {name: 'players_required', label: 'Wait for how many players to start a round?', type: 'int', minValue: 1, maxValue: 5, defaultValue: 1}, {name: 'tokens_per_round', label: 'Token cost to buy into a round', type: 'int', minValue: 1, maxValue: 100, defaultValue: 10}, {name: 'tokens_per_card', label: 'Token cost per card drawn', type: 'int', minValue: 1, maxValue: 100, defaultValue: 10} ]; function setSubject(subject) { cb.changeRoomSubject(subject); } function update_App() { var new_subject = ""; if (!blackjack.handInProgress) { new_subject = "Let's play Blackjack! " + cb.settings.tokens_per_round + " tokens to deal in! Winner gets " + cb.settings.prize; } else { new_subject = "We're playing Blackjack! A new round will be starting soon, so please wait patiently"; } setSubject(new_subject); } function onBust(user) { cb.chatNotice("Sorry, but you just busted! Better luck next time!", user); } function onCardDrawn(user, card) { cb.chatNotice("You drew a " + card + ", for a total of " + blackjack.playerHands[user].getTotal(), user); } function onGameOver(dealerWin, winnerName) { if (dealerWin) { cb.chatNotice("Game Over! Dealer wins this round!"); } else { cb.chatNotice("Game Over! And the winner this round is " + winnerName); } cb.chatNotice("To start a new round, type /reset", cb.room_slug); } function init() { blackjack.setupDealerHand(); blackjack.onBust = onBust; blackjack.onCardDrawn = onCardDrawn; blackjack.onGameOver = onGameOver; update_App(); } function reset() { blackjack.handInProgress = false; blackjack.playerHands = []; blackjack.players = []; cb.drawPanel(); init(); } function format_username(val) { var returnVal = ""; if (val === null) { returnVal = "--"; } else { returnVal = val.substring(0, 12); } return returnVal; } cb.onTip(function (tip) { total_tipped += tip.amount; if (!blackjack.handInProgress) { if (!blackjack.isPlaying(tip.from_user) && tip.amount >= cb.settings.tokens_per_round) { blackjack.setupPlayerHand(tip.from_user); cb.chatNotice("Your cards are: " + blackjack.playerHands[tip.from_user].getAllCards() + ". Tip to deal another card, or type '/stand' to stick with what you've got.", tip.from_user); } else if (blackjack.isPlaying(tip.from_user) && tip.amount >= cb.settings.tokens_per_card) { blackjack.drawCard(tip.from_user); } else if (!blackjack.getIsPlaying(tip.from_user) && tip.amount < cb.settings.tokens_per_round) { cb.chatNotice("Sorry, you need to tip at least " + cb.settings.tokens_per_round + " tokens to play. But thank you for your tip!", tip.from_user); } else if (blackjack.getIsPlaying(tip.from_user) && tip.amount < cb.settings.tokens_per_card) { cb.chatNotice("Sorry, you need to tip at least " + cb.settings.tokens_per_card + " tokens to draw a card. But thank you for your tip!", tip.from_user); } } else { cb.chatNotice("Sorry, but you can't join in when a round is in progress. But thank you for your tip!", tip.from_user); } update_App(); cb.drawPanel(); }); cb.onDrawPanel(function (user) { var returnVal = null; if (user === cb.room_slug) { returnVal = { 'template': '3_rows_11_21_31', 'row1_value': 'B L A C K J A C K', 'row2_value': "Your hand: " + blackjack.dealerHand.getAllCards(), 'row3_value': "" }; } else if (blackjack.getIsPlaying(user)) { returnVal = { 'template': '3_rows_11_21_31', 'row1_value': 'B L A C K J A C K', 'row2_value': "Your cards: " + blackjack.playerHands[user].getAllCards() + " (" + blackjack.playerHands[user].getTotal() + ")", 'row3_value': cb.settings.tokens_per_card + " to draw" }; } else { returnVal = { 'template': '3_rows_11_21_31', 'row1_value': 'B L A C K J A C K', 'row2_value': cb.settings.tokens_per_round + " to deal in", 'row3_value': cb.settings.tokens_per_card + " to draw" }; } return returnVal; }); cb.onMessage(function (message) { if (message.m.substring(0, 1) === "/") { message["X-Spam"] = true; if (message.m.toLowerCase() === "/stand") { blackjack.playerHands[message.user].stand(); } if (message.m.toLowerCase() === "/reset") { reset(); } if (message.user === cb.room_slug) { if (message.m.toLowerCase() === "/display") { if (!display_twice) { cb.chatNotice("WARNING: This command displays your hand to the viewers. Please type it again in order to do this!", cb.room_slug); display_twice = true; } } else { cb.chatNotice("Dealer's cards total " + blackjack.dealerHand.total); display_twice = false; } } } return message; }); init();
© Copyright Chaturbate 2011- 2026. All Rights Reserved.