struggling with variables and math...

Posted by Lilbopeep on Tue 08 Nov 2011 06:36 AM — 8 posts, 32,189 views.

USA #0
this is what I have so far:


<!--  Triggers  -->

<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   match="\'Will you hit\, stay\, or double down\?\'"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>local DealHand
local MyHand

DealHand = GetVariable("deal_hand")
MyHand = GetVariable("my_hand")

if DealHand == Ace then
SetVariable("deal_hand", "1")
else
if DealHand == Two then
SetVariable("deal_hand", "2")
else
if DealHand == Three then
SetVariable("deal_hand", "3")
else
if DealHand == Four then
SetVariable("deal_hand", "4")
else
if DealHand == Five then
SetVariable("deal_hand", "5")
else
if DealHand == Six then
SetVariable("deal_hand", "6")
else
if DealHand == Seven then
SetVariable("deal_hand", "7")
else
if DealHand == Eight then
SetVariable("deal_hand", "8")
else
if DealHand == Nine then
SetVariable("deal_hand", "9")
else
if DealHand == Ten then
SetVariable("deal_hand", "10")
else
if DealHand == Jack then
SetVariable("deal_hand", "10")
else
if DealHand == Queen then
SetVariable("deal_hand", "10")
else
if DealHand == King then
SetVariable("deal_hand", "10")
end
end
end
end
end
end
end
end
end
end
end
end
end
DealHand = GetVariable("deal_hand")
MyHand = GetVariable("my_hand")
ColourNote ("Black", "Blue", "DealHand is (" .. DealHand .. ")")
ColourNote ("Black", "Blue", "MyHand is (" .. MyHand .. ")")
</send>
  </trigger>
  <trigger
   enabled="y"
   match="Your current total is\: *(\d+)*$"
   regexp="y"
   send_to="9"
   sequence="100"
   variable="my_hand"
  >
  <send>%1</send>
  </trigger>
  <trigger
   enabled="y"
   match="A blackjack dealer deals (a|an) (.*?) of (.*?) face up to himself\.$"
   regexp="y"
   send_to="9"
   sequence="100"
   variable="deal_hand"
  >
  <send>%2</send>
  </trigger>
</triggers>


Basically, there are a few things I know I need to clean up, but im just trying to see results..

Right now, I get colour notes telling me my hand correctly, and it even tells me the card the dealer has, but it isn't actually 'updating' the variable.

Also, when I tried a different approach it would give me errors relating to doing math on a string or something.

Id like to get them both to numbers that I can do things with to determine how I should proceed at the table.
Amended on Tue 08 Nov 2011 06:37 AM by Lilbopeep
USA Global Moderator #1
Ace, Two, Three, etc need to be strings. "Ace", "Two", "Three", and so on.

Also, holy cow. Make all those elses and ifs into elseifs so that you can collapse that crazy chain of ends down to only one.

use tonumber() to convert a numeric string to the equivalent number
Amended on Tue 08 Nov 2011 07:01 AM by Fiendish
USA #2
Fiendish said:

Ace, Two, Three, etc need to be strings. "Ace", "Two", "Three", and so on.

Also, holy cow. Make all those elses and ifs into elseifs so that you can collapse that crazy chain of ends down to only one.


Heavily condensed:
local DealHand = GetVariable("deal_hand")
local MyHand = GetVariable("my_hand")

local cardnums = {
  Ace = 1,
  Two = 2,
  Three = 3,
  Four = 4,
  Five = 5,
  Six = 6,
  Seven = 7,
  Eight = 8,
  Nine = 9,
  Ten = 10,
  Jack = 10,
  Queen = 10,
  King = 10,
}

if cardnums[DealHand] then
  DealHand = cardnums[DealHand]
  SetVariable("deal_hand", DealHand)
end

ColourNote ("Black", "Blue", "DealHand is (" .. DealHand .. ")")
ColourNote ("Black", "Blue", "MyHand is (" .. MyHand .. ")")


[EDIT]: (If you're -really- curious and don't want your head to explode from my late-night ramblings) - One might wonder why I didn't quote the keys of that table, when Fiendish just said "Ace", "Two", etc. needed to be quoted. Long story short: if you're putting something on the right into something on the left, the thing on the left is probably a variable, and the variable name isn't quoted. Since we're defining these variables within a table, though, we can use a string to get at those variables.
Amended on Tue 08 Nov 2011 08:29 AM by Twisol
USA #3
Ah, this is making a bit more sense now.

Thanks for the help!
USA #4
While hands of blackjack are being played, Id like to keep track of how many hands i've won/lost/drawn this session, as well as keep track of how much money im up or down.


if GetVariable("Lost") == nil then
SetVariable("Lost", tonumber(1))
else
Lost = GetVariable("Lost") +1
Colournote ("Black", "Blue", "You've lost (" .. Lost .. ") hands this session")
end



The problem is it will recognize that it's nil and set it to one, but it doesn't seem to keep 'counting', it just goes to two and then stops.. what am I missing here?
USA Global Moderator #5
tonumber(1) is silly, because 1 is already a number.

GetVariable always returns a string, so you need to do Lost = tonumber(GetVariable("Lost")) +1

You never set Lost again after incrementing it.
USA #6

if GetVariable("Lost") == nil then
Lost = 1
ColourNote ("Black", "Blue", "Lost set to 1")
else
Lost = tonumber(GetVariable("Lost")) + 1
ColourNote ("Black", "Blue", "You have lost (" .. Lost .. ") hands this session.")
end


it's still saying, "You have lost 2 hands this session." every time I lose?

Australia Forum Administrator #7
You need to set the variable back. eg.



if GetVariable("Lost") == nil then
  SetVariable("Lost", "1")
else
  Lost = tonumber (GetVariable("Lost")) + 1
  SetVariable ("Lost", Lost)
  Colournote ("Black", "Blue", "You've lost (" .. Lost .. ") hands this session")
end