[MUSHClient] Blackjack Script for Materia Magica - Help cleaning up my code?

Posted by forral on Wed 05 Dec 2012 10:59 PM — 6 posts, 27,132 views.

USA #0
Howdy,

I'm learning to script recently and was able to get some codebits working last night to automatically play a game of blackjack in the Materia Magica MUD.

Here is my code so far:


String: Your current total is: *
Send to variable: %1 (variable bjtotal)


String: A blackjack dealer asks, 'Will you hit, stay, or double down, Forral?'
Send to script: 

local bjtotal

bjtotal = tonumber (GetVariable("bjtotal"))

if bjtotal <= 12
   then SendNoEcho ("say hit")
elseif bjtotal >= 13
   then SendNoEcho ("say stay")
end

The alternative version of that string is: 'Will you hit, stay, or double down, Forral?'

Send to script: same code as above.




So from this, I have a few questions that I was hoping I could get some clarification on:

1) How can I combine both of those strings so I don't have to use two different triggers? I'm not yet learned in the ways of regex, but I know its a powerful tool.

2) The game will sometimes have an output of (soft), such as 'Your current total is: 13 (soft). How can I incorporate this into my script, and give it a bit more logic? The (soft) plays my ace as either 1 or 11, so how can I allow the computer to make this decision optimally?

3) Is there a better way for me to send my value to the variable without having a separate trigger to match?

Thanks for any help, and sorry if my questions are noobish. I'm constantly consulting the scripts functions list but I'm not familiar with some of the syntax so I'm trying to learn here.

Thanks,
Forral
Australia Forum Administrator #1
I don't mind the two triggers, but you could use a multi-line one to capture both lines at once.


Quote:

The game will sometimes have an output of (soft), ...


Use an "optional group" in the regexp.

Template:regexp
Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.


To help see what you are doing if you have more questions can you copy the actual trigger? Like this:

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.
USA #2
As requested, here are my triggers in XML format Nick.

I had to add two triggers because I noticed the string is to match is different if the (soft) comes into play.


<triggers>
  <trigger
   enabled="y"
   group="Blackjack"
   match="'Will you hit or stay, Forral?' a blackjack dealer asks."
   send_to="12"
   sequence="100"
  >
  <send>local bjtotal

bjtotal = tonumber (GetVariable("bjtotal"))

if bjtotal &lt;= 13
   then SendNoEcho ("say hit")
elseif bjtotal &gt; 13
   then SendNoEcho ("say stay")
end
</send>
  </trigger>
</triggers>


<triggers>
  <trigger
   enabled="y"
   group="Blackjack"
   match="'Will you hit, stay, or double down, Forral?' a blackjack dealer asks."
   send_to="12"
   sequence="100"
  >
  <send>local bjtotal

bjtotal = tonumber (GetVariable("bjtotal"))

if bjtotal &lt;= 13
   then SendNoEcho ("say hit")
elseif bjtotal &gt; 13
   then SendNoEcho ("say stay")
end
</send>
  </trigger>
</triggers>


<triggers>
  <trigger
   enabled="y"
   group="Blackjack"
   match="A blackjack dealer asks, 'Will you hit or stay, Forral?'"
   send_to="12"
   sequence="100"
  >
  <send>local bjtotal

bjtotal = tonumber (GetVariable("bjtotal"))

if bjtotal &lt;= 13
   then SendNoEcho ("say hit")
elseif bjtotal &gt; 13
   then SendNoEcho ("say stay")
end
</send>
  </trigger>
</triggers>


<triggers>
  <trigger
   enabled="y"
   group="Blackjack"
   match="A blackjack dealer asks, 'Will you hit, stay, or double down, Forral?'"
   send_to="12"
   sequence="100"
  >
  <send>local bjtotal

bjtotal = tonumber (GetVariable("bjtotal"))

if bjtotal &lt;= 13
   then SendNoEcho ("say hit")
elseif bjtotal &gt; 13
   then SendNoEcho ("say stay")
end
</send>
  </trigger>
</triggers>


I also changed the trigger to set my variable slightly:


<triggers>
  <trigger
   enabled="y"
   expand_variables="y"
   group="Blackjack"
   match="Your current total is: *"
   send_to="12"
   sequence="100"
   variable="bjtotal"
  >
  <send>SetVariable ("bjtotal", "%1")
</send>
  </trigger>
</triggers>


I'll look into the optional group in Regex.

Thanks,
Forral
Amended on Thu 06 Dec 2012 12:31 PM by forral
Australia Forum Administrator #3
First thing is you can call a script. Not much point duplicating what the trigger does over and over.
Australia Forum Administrator #4
This does the work of about four triggers:


<triggers>
  <trigger
   enabled="y"
   group="Blackjack"
   match="^(A blackjack dealer asks, ){0,1}'Will you hit,{0,1} (or ){0,1}stay,{0,1}( or double down){0,1},{0,1} Forral\?'( a blackjack dealer asks\.){0,1}$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>print "matched!"</send>
  </trigger>
</triggers>

USA #5
Nick Gammon said:

This does the work of about four triggers:


<triggers>
  <trigger
   enabled="y"
   group="Blackjack"
   match="^(A blackjack dealer asks, ){0,1}'Will you hit,{0,1} (or ){0,1}stay,{0,1}( or double down){0,1},{0,1} Forral\?'( a blackjack dealer asks\.){0,1}$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>print "matched!"</send>
  </trigger>
</triggers>




That's exactly what I was looking for to combine all those triggers. Makes life -much- easier.

I'll research more into regex as I go along here, but this helps alot Nick, thanks!!