Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ [MUSHClient] Blackjack Script for Materia Magica - Help cleaning up my code?

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

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by forral   USA  (79 posts)  Bio
Date Wed 05 Dec 2012 10:59 PM (UTC)
Message
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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 06 Dec 2012 02:59 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by forral   USA  (79 posts)  Bio
Date Reply #2 on Thu 06 Dec 2012 11:51 AM (UTC)

Amended on Thu 06 Dec 2012 12:31 PM (UTC) by forral

Message
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
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 06 Dec 2012 08:42 PM (UTC)
Message
First thing is you can call a script. Not much point duplicating what the trigger does over and over.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #4 on Thu 06 Dec 2012 08:56 PM (UTC)
Message
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>


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by forral   USA  (79 posts)  Bio
Date Reply #5 on Thu 06 Dec 2012 09:25 PM (UTC)
Message
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!!
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


19,841 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.