Mini-game conundrum! (need coding help

Anthony the tiger
16 Dec 2017, 08:32

So, I want to make a mini-game of blackjack for my text adventure game. Problem is each card has to be accounted for to add up to 21 or over 21. In short, I think I'd have to code every possibility that could add up to 21 and over with my playing cards. Is there an easier way to code this. Like maybe giving the object itself a value and have it add the values of the other cards in hand and when the player reaches 21 or close to 21 it will notify the player of such. And if the dealer is higher the player wins, if the dealer gets lower they lose. Or should I scrap the idea altogether?


K.V.
16 Dec 2017, 09:33

Hello.

We were constructing a deck of cards here:

http://textadventures.co.uk/forum/quest/topic/qj5flx_bmkwusr-jhiybcw/has-anyone-ever-created-a-deck-of-cards

(To give credit where it's due, mrangel came up with most of the good stuff.)


One of my ways to create a deck (not the best, as far as the Aces are concerned when playing blackjack):

deck.values = NewList()
deck.cards = NewObjectList()
i = 2
while (i<15) {
  list add (deck.values, i)
  i = i + 1
}
cardValue = 0
deck.suits = Split("Clubs;Spades;Hearts;Diamonds", ";")
foreach (suit, deck.suits) {
  foreach (value, deck.values) {
    cardValue = cardValue + 1
    create (value+""+Mid(suit,1,1))
    newCard = GetObject(value+""+Mid(suit,1,1))
    if (not value = 14) {
      if (not value >10) {
        newCard.blackjack_value = value
      }
      else {
        newCard.blackjack_value = 10
      }
    }
    else {
      newCard.set_ blackjack_value => {
        ShowMenu ("Is the value of this Ace 1 or 11?", Split("1;11", ";"), false) {
          if (result = "1") {
            this.blackjack_value = 1
            msg ("Done.  The value of this Ace is 1.")
          }
          else {
            this.blackjack_value = 11
            msg ("Done.  The value of this Ace is 11.")
          }
        }
      }
      newCard.blackjack_value = 1
    }
    // msg (newCard)
    switch (value) {
      case (11) {
        value = "Jack"
      }
      case (12) {
        value = "Queen"
      }
      case (13) {
        value = "King"
      }
      case (14) {
        value = "Ace"
      }
    }
    newCard.designator = newCard.name
    newCard.parent = deck
    list add (deck.cards, newCard)
    newCard.alias = value+" of "+suit
    // msg (newCard.alias)
    newCard.value = cardValue
    newCard.look => {
      if (not HasAttribute(this, "set_blackjack_value")) {
        msg ("It's the "+this.alias+".")
      }
      else {
        invoke (this.set_blackjack_value)
      }
    }
    // msg (newCard.value)
    // msg ("blackjack value: "+newCard.blackjack_value)
  }
}

With this script:

2 through 10 have the blackjack_value of 2 through 10.

Jack, Queen, and King each have the blackjack_value of 10.

The default blackjack_value of an Ace would be 1.

If you look at (or EXAMINE) the Ace, it will run the card's set_blackjack_value script, which asks if you want the Ace to be 1 or 11.

This is my lazy script.

I have a function somewhere that checks the value of all the cards on the object's card object list (the object being the player or the dealer or an NPC), then decides if the Ace should be worth 1 or 11.

I'll find it and post it.


EDITED

Changed the default blackjack_value of aces to 1.


K.V.
16 Dec 2017, 09:37

PS

If you like the way these dice roll, I'll post the script for the dice and the craps game:

http://textadventures.co.uk/games/view/4t2f3vrome_hqz9jxjnc_a/gambling-hall