Wednesday, March 19, 2008

Bot Code (for Jesse) or anyone else who likes code

WARNING: strictly a code post!

Here's the basic code I use for a chatbot in ActionScript 3.0. You can see it's fussy with everything needing to be declared before things get started. It takes the line "Clean the house" and checks it against three different regular expressions. Then it chooses a random answer from a group of answers in the my_ans array.

I'd thought switch/case might make it all cleaner but now I think I'll stick with the system that's working. Flash doesn't have as many features in regular expressions as JavaScript.


var i:int;
var m:int;
var questnum:int=3;
var allquests:Array= new Array();
var my_ans:Array= new Array();


var rg0:RegExp = /goomba/i
var rg1:RegExp = /It is/i
var rg2:RegExp = /clean/i


var testline:String = "Clean the house";


for(i=0;i<questnum;i++) {
allquests.push(this["rg"+i]);//so can access in next line
}

for(i=0;i<questnum;i++) {
if(testline.search(allquests[i])!=-1){
trace("gotcha!"+i)
if(i==0){my_ans=["goomba is nice","GOOMBA?","goomba-never"]}
if(i==1){my_ans=["It certainly is","is what?"]}
if(i==2){my_ans=["It's clean already","how clean","cleaner than dirt","like, Mr. Clean?"]}
m = Math.floor(Math.random() * my_ans.length);
trace(my_ans[m])}
}

The other thing I don't like about this is the answers are far away in the code, from the questions, and it can be a pain adding things. I may try making individual functions for each answer, since there are often qualifying conditions within a reply, as in this example from Whinsey:

if(a==106){
var hum=/CONDO|MOTEL|HOTEL|HOUSE|CASTLE|DUMP|APARTMENT/i
if(righto.search(hum)!=-1){ans=["I have seen the inside of your "+RegExp.lastMatch+" and you should clean it!"]}

else{ans=[righto+ " is my home away from home!"]}
mood=1}>

Flash doesn't do RegExp.lastMatch for instance. righto is RegExp.RightContext.

Can you believe I know all this? NUTS!

6 comments:

Namowal (Jennifer Bourne) said...

Wow. I'm impressed.
I have a lot to learn.
I understand declaring variables, for and if statements, and that == means is equal... but that's like saying I know what springs and gears are while not knowing how to build a watch.
p.s.
["It's clean already","how clean","cleaner than dirt","like, Mr. Clean?"] made me laugh.

Sally said...

actually it's not that hard if you don't go too fast. An array is like a file cabinet, and each file has a number starting with 0. You could take this code I've set up and get it to work in Flash MX EXCEPT regular expressions only work in the latest version of Flash.

I figured a way to use functions for each answer so I can put them right below each question, will definitely help keep things in working order.

When I learned ActionScript there was a different mood in the air, for lack of better cliche. There was a lot of experimentation going on and people were eager to share code, particularly through the forum were-here, where I ended up being a moderator. If you ran into a problem you could post it and easily get an answer that same day. Flash coding was still so new that people were doing it just for fun.

Jesse said...

Warning: even codier than the original. :)

Trackback for revised code ideas.

Anonymous said...

Wait. Did you say "Flash doesn't do windows"?

Sally said...

Hey Jesse, I'm looking at the code now. Thanks so much for taking the time. And could you explain about the trackback? I like what it does, (the link below)

Jesse said...

(about trackback) In most other blog environments, instead of leaving a comment you can just make your own blog post elsewhere, and instruct it to ping the blog(s) you want to trackback to.

The receiving blogs then generate links back to the originator.

Blogger doesn't seem to work that way though? The "create link" button at the bottom is hokey. It just puts you in compose mode for a new blog entry.

On the other hand it seems as though if you make a link somewhere, Google just finds the link and makes a trackback. Quite confusing :]