Thread

Bouncer Bot (Bots)

Bouncer Bot // Bots

1  |  

azazael

Mar 25, 2001, 5:57pm
I'm currently using the AW SDK as an excuse to learn a little bit about C++
and have come up against a couple of small problems. I wondered if I could
throw them
open for public scrutiny?

I thought it might be nice to build a bouncer bot, who's sole purpose was to
monitor chat for certain key words. If one of these words was detected then
a warning would be issued, repeat offenses would warrant ejection. In
extreme cases, persistant offenders might even be added to an 'Eject On
Sight' list.

I have the basic framework in place, but it is quite limited.

1. It only matches a complete chat string, not occurances within a string.
So for example, if 'aardvark' was a key word the bot would not catch 'you
remind me of an aardvark'. Is there a function that finds instances of one
string within another?

2. Quite often I find myself needing to create an array with an unknown
number of elements. I'm sure it must but I cannot find any reference on
the internet, does C++ support dynamic arrays? If so how do you determine
the number of elements?

Any advice would be welcome

Regards
Az

ananas

Mar 25, 2001, 7:10pm
This is a multi-part message in MIME format.
--------------3564F0C6E6FC66E8DFE43EAD
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

At least 2 commands to check occurance :

- strstr() checks the occurance within a string. The returned pointer is
NULL if it isn't there. There is no case insensitive version of strstr()
so you need to upper- or lowercase both strings before you compare.

- strtok() can break a sentence into words that you can check separate
with stricmp() or the CString method CompareNoCase().



The next two are MFC classes and might not exist in other C++ compilers,
I don't have too much experience in ++ yet :

Dynamic arrays are implemented in the MFC class CObArray and the
subclass CStringArray (other subclasses exist too).

Optional you can use the MFC class CStringList - check which one has
better methods for your purpose.


[View Quote] begin:vcard
n:Hatzenberger;Volker
x-mozilla-html:FALSE
url:oct31.de
adr:;;Bornheimer Strasse 15;Bonn;;53111;Germany
version:2.1
email;internet:vha at oct31.de
end:vcard

--------------3564F0C6E6FC66E8DFE43EAD--

trekkerx

Mar 26, 2001, 12:07am
Also wach out for words like "Hello" Cuz it says Hell, in it and Glass, and stuff
like that

[View Quote] > At least 2 commands to check occurance :
>
> - strstr() checks the occurance within a string. The returned pointer is
> NULL if it isn't there. There is no case insensitive version of strstr()
> so you need to upper- or lowercase both strings before you compare.
>
> - strtok() can break a sentence into words that you can check separate
> with stricmp() or the CString method CompareNoCase().
>
> The next two are MFC classes and might not exist in other C++ compilers,
> I don't have too much experience in ++ yet :
>
> Dynamic arrays are implemented in the MFC class CObArray and the
> subclass CStringArray (other subclasses exist too).
>
> Optional you can use the MFC class CStringList - check which one has
> better methods for your purpose.
>
[View Quote]

hal9000

Mar 26, 2001, 1:27am
don't forget stricmp() that's to do a case-insensitive string comparison,
returns 0 if they are the same

--
-=Hal9000=- world:Discover
--==~~out~~==--
--== http://hal9000.xoasis.com


[View Quote]

ananas

Mar 26, 2001, 5:53am
This is a multi-part message in MIME format.
--------------3D8F0E0901B47AC3D6F9FA28
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Not a problem with the strtok() method, that should be prefered for
larger word lists (better performance)

[View Quote] begin:vcard
n:Hatzenberger;Volker
x-mozilla-html:FALSE
url:oct31.de
adr:;;Bornheimer Strasse 15;Bonn;;53111;Germany
version:2.1
email;internet:vha at oct31.de
end:vcard

--------------3D8F0E0901B47AC3D6F9FA28--

azazael

Mar 26, 2001, 9:00am
Many thanks, strstr did the job when used in conjuction with a _strlwr and
an if not null.

I'm still having bother with dynamic arrays, but I'll post my findings if I
solve it.

Regards

Az

azazael

Mar 26, 2001, 11:12am
I think the problem here is my ignorance of the terminology.
Extensive searching through the alt.lang.c++ groups revealed vector, a
standard template class, which looks as though it might do the job.

Regards
Az

[View Quote]

rough diamond

Mar 26, 2001, 8:23pm
Also you might want to have a basic knowledge of linked lists. Because a
post on such things might be long, I'll include it in my next message, and
don't download it if you don't want.
-RD
[View Quote]

rough diamond

Mar 26, 2001, 8:54pm
This is a multi-part message in MIME format.

------=_NextPart_000_0068_01C0B61C.BB6E9560
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


-------------------------------------------------------------------------=
-------

GUIDE TO PROGRAMMING =
SIMPLE LINKED LISTS
Copyright =A9 =
2001 by Joshua Trask (Rough Diamond)

-------------------------------------------------------------------------=
-------


1.Source code is in italics
2. I know ' isn't a comment in C, and that // is how you do one line =
comments, and that ' is Visual Basic, but I happen to like =20
'.

Linked lists are a very convenient way of storing data for an undefined =
number of elements, since it allows adding and removing of elements =
without wasting any extra memory. In fact, they are often more efficient =
than several other ways of storing data, since you never need to define =
them to be larger than you need.

Linked lists' key is in pointers. Pointers store the address of =
something else, but do not actually store anything. For example, if you =
do int n; and then do a pointer that equals n, the pointer will not =
store the value of n, but rather the address of n in memory.

Also, some functions that may be useful:

malloc(size) - allocates memory of size bytes, returning a pointer to =
the beginning =20
of the memory if successful, and a null pointer if =
not.

sizeof(type) - has as its value the number of bytes needed to contain a =
value of the
specified type.

free(pointer) - releases a block of memory pointed to by pointer that =
was previously
allocated by a calloc or malloc call.

Just as a quick reference, to make a pointer, do int *nameofpointer, for =
example.

GUIDE TO PROGRAMMING SIMPLE LINKED LISTS

-------------------------------------------------------------------------=
-------
The first step is to make your struct to hold all your information, and =
include a pointer to itself called *next. Here's an example:

struct person_data
{
char name[10];
int phonenumber;
struct person_data *next;
}

This creates a struct called person_data which holds a person's name and =
phone number, as well as a pointer to its own type, person_data. The =
next step is to declare pointers for different uses in the list. An =
example of possibilities would be:

struct person_data *first, *last, *current, *holder;

This allows pointers for storing the first element of the list as well =
as the last (to allow faster movement to the ends), as well as the =
current struct you are getting information from, and a holder to store =
structs for removing them from the list.

Each time you need to add an element to the list, you need to allocate =
memory for it, where it was previously blank. An example of how to do =
this is:

current =3D NULL; 'Make sure we aren't allocating over anything.
current =3D malloc(sizeof(person_data)); 'Make 'current' a copy of =
our struct
last -> next =3D current; 'Connect our new struct to the end of our =
list

This basically adds a blank copy of our struct to the end of the list. =
You might notice that instead of using periods, which is what would =
usually be used in linked lists, I am using "->". This is because, when =
using pointers, periods can sometimes be used wrong and give an error, =
though -> always works when doing this, even when periods are =
appropriate.

If you want to search the list for a certain element, then you can just =
go through from the start to the end, like this:

current =3D first;
while(current -> next !=3D NULL && !stricmp(current -> name, =
search_query)) 'Keeps going until you're on the last or right=20
=
'element.
{
current =3D current -> next;
}
'Also, a check could be added to make sure, if you landed on the last =
element, that it is either the one you are searching
'for or you just got it because you don't want to keep counting forever.

Lastly, if you wanted to remove an element from your list, it could be =
done like this:
holder =3D first;
current =3D first -> next;
while(current -> next !=3D NULL && !stricmp(current -> name, =
search_query))
{
holder =3D holder -> next; 'Holder is needed so that you know =
which element comes before the one you delete
current =3D holder -> next;
}
'You may want to install a check to make sure that it wasn't the first =
or last element of the list
holder -> next =3D current -> next; 'Link over the removed element =
(don't have a break, which results in a crash)
free(current);

------=_NextPart_000_0068_01C0B61C.BB6E9560
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>

<META content=3Dtext/html;charset=3Diso-8859-1 =
http-equiv=3DContent-Type>
<META content=3D'"MSHTML 4.72.3110.7"' name=3DGENERATOR>
</HEAD>
<BODY>
<DIV><FONT size=3D2></FONT><FONT color=3D#000000 size=3D2></FONT>
<HR>
</DIV>
<DIV><FONT color=3D#000000=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =

<STRONG><FONT color=3D#000000>GUIDE TO PROGRAMMING SIMPLE LINKED=20
LISTS</FONT></STRONG></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2><STRONG><FONT=20
color=3D#000000></FONT></STRONG></FONT><STRONG><FONT=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
</STRONG><EM>Copyright &copy; 2001 by Joshua Trask (Rough=20
Diamond)</EM></FONT></DIV>
<DIV><FONT size=3D2><EM></EM></FONT>
<HR>
</DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>1.Source code is in =
italics</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT><FONT size=3D2>2. I know ' =
isn't a comment=20
in C, and that // is how you do one line comments, and that ' is Visual =
Basic,=20
but I happen to like&nbsp; </FONT></DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; '.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Linked lists are a very convenient way of storing =
data for an=20
undefined number of elements, since it allows adding and removing of =
elements=20
without wasting any extra memory. In fact, they are often more efficient =
than=20
several other ways of storing data, since you never need to define them =
to be=20
larger than you need.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Linked lists' key is in pointers. Pointers store the =
address=20
of something else, but do not actually store anything. For example, if =
you do=20
<EM>int n;</EM><STRONG> </STRONG>and then do a pointer that equals n, =
the=20
pointer will not store the value of n, but rather the address of n in=20
memory.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Also, some functions that may be =
useful:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>malloc(size) - allocates memory of <EM>size</EM> =
bytes,=20
returning a pointer to the beginning&nbsp; </FONT></DIV>
<DIV><FONT=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
of the memory if successful, and a <EM>null </EM>pointer if =
not.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>sizeof(type) - has as its value the number of bytes =
needed to=20
contain a value of the</FONT></DIV>
<DIV><FONT=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
specified type.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>free(pointer) - releases a block of memory pointed =
to by=20
<EM>pointer</EM> that was previously</FONT></DIV>
<DIV><FONT color=3D#000000=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
allocated by a <EM>calloc </EM>or <EM>malloc</EM> call.</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>Just as a quick reference, to make a =
pointer, do=20
int *nameofpointer, for example.</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2><STRONG>GUIDE TO PROGRAMMING SIMPLE =
LINKED=20
LISTS</STRONG></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2><STRONG>
<HR>
</STRONG>The first step is to make your struct to hold all your =
information, and=20
include a pointer to itself called *next. Here's an =
example:</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2><EM>struct =
person_data</EM></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2><EM>{</EM></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2><EM>&nbsp;&nbsp;&nbsp; char=20
name[10];</EM></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2><EM>&nbsp;&nbsp;&nbsp; int=20
phonenumber;</EM></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2>&nbsp;&nbsp;&nbsp; <EM>struct =
person_data=20
*next;</EM></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2><EM>}</EM></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2><EM></EM></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>This creates a struct called =
person_data which=20
holds a person's name and phone number, as well as a pointer to its own =
type,=20
person_data. The next step is to declare pointers for different uses in =
the=20
list. An example of possibilities would be:</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2><EM>struct person_data *first, =
*last, *current,=20
*holder;</EM></FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2><EM></EM></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>This allows pointers for storing the =
first=20
element of the list as well as the last (to allow faster movement to the =
ends),=20
as well as the current struct you are getting information from, and a =
holder to=20
store structs for removing them from the list.</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>Each time you need to add an element =
to the=20
list, you need to allocate memory for it, where it was previously blank. =
An=20
example of how to do this is:</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><EM><FONT size=3D2>current =3D NULL;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
'Make sure=20
we aren't allocating over anything.</FONT></EM></DIV>
<DIV><EM><FONT size=3D2>current =3D =
malloc(sizeof(person_data));&nbsp;&nbsp;&nbsp;=20
'Make 'current' a copy of our struct</FONT></EM></DIV>
<DIV><EM><FONT size=3D2>last -&gt; next =3D current;&nbsp;&nbsp;&nbsp; =
'Connect our=20
new struct to the end of our list</FONT></EM></DIV>
<DIV><EM><FONT size=3D2></FONT></EM>&nbsp;</DIV>
<DIV><FONT size=3D2>This basically adds a blank copy of our struct to =
the end of=20
the list. You might notice that instead of using periods, which is what =
would=20
usually be used in linked lists, I am using &quot;-&gt;&quot;. This is =
because,=20
when using pointers, periods can sometimes be used wrong and give an =
error,=20
though -&gt; always works when doing this, even when periods are=20
appropriate.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>If you want to search the list for a certain =
element, then you=20
can just go through from the start to the end, like this:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2><EM>current =3D first;</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>while(current -&gt; next !=3D NULL &amp;&amp;=20
!stricmp(current -&gt; name, search_query)) 'Keeps going until you're on =
the=20
last or right </EM></FONT></DIV>
<DIV><FONT=20
size=3D2><EM>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;=20
'element.</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>{</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>&nbsp;&nbsp;&nbsp; current =3D current -&gt;=20
next;</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>}</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>'Also, a check could be added to make sure, if =
you landed=20
on the last element, that it is either the one you are=20
searching</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>'for or you just got it because you don't want =
to keep=20
counting forever.</EM></FONT></DIV>
<DIV><FONT size=3D2><EM></EM></FONT><EM></EM>&nbsp;</DIV>
<DIV><FONT size=3D2>Lastly, if you wanted to remove an element from your =
list, it=20
could be done like this:<EM></FONT></EM></DIV>
<DIV><EM><FONT size=3D2>holder =3D first;</FONT></EM></DIV>
<DIV><FONT size=3D2><EM></FONT><FONT size=3D2>current =3D first -&gt;=20
next;</FONT></EM></DIV>
<DIV><FONT size=3D2><EM>while(current -&gt; next !=3D NULL &amp;&amp;=20
!stricmp(current -&gt; name, search_query))</FONT></EM></DIV>
<DIV><FONT size=3D2><EM>{</FONT></EM></DIV>
<DIV><FONT size=3D2><EM>&nbsp;&nbsp;&nbsp; holder =3D holder -&gt;=20
next;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'Holder is needed so =
that you=20
know which element comes before the one you delete</FONT></EM></DIV>
<DIV><FONT size=3D2><EM>&nbsp;&nbsp;&nbsp; current =3D holder -&gt;=20
next;</FONT></EM></DIV>
<DIV><FONT size=3D2><EM>}</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>'You may want to install a check to make sure =
that it=20
wasn't the first or last element of the list</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>holder -&gt; next =3D current -&gt; =
next;&nbsp;&nbsp;&nbsp;=20
'Link over the removed element (don't have a break, which results in a=20
crash)</EM></FONT></DIV>
<DIV><FONT size=3D2><EM>free(current);</EM></FONT></DIV></BODY></HTML>

------=_NextPart_000_0068_01C0B61C.BB6E9560--

azazael

Mar 27, 2001, 6:52am
Oh RD, what a gem :0)

Regards
Az
[View Quote]

rough diamond

Mar 27, 2001, 8:37pm
thanks :-)

[View Quote]

rough diamond

Mar 29, 2001, 10:23pm
It's more convenient to know C then C++ in case you ever may need it (I
wound up programming Nintendo Game Boy, sooner or later)
-RD
[View Quote]

1  |  
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2024. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn