SOLVED: Custom name. How to filter for bad words / empty string

Started by Hobbes, Sun 04/11/2018 14:56:30

Previous topic - Next topic

Hobbes

Slowly slowly working away on my prototype a bit more... I'm trying to add a character name to the creation screen.

I've got 2 questions. It's working fine, but right now the game will allow any name + empty string.

1. As a test I tried to get it to non parse "Shit" as a string. That works. However I don't really want to write 10000 if-else statements. Is there a way to parse all the swearwords in one if-statement?

2. Also, how would I parse an empty string and not accept it?

This is what I have so far:

Code: ags

function Button1_OnClick(GUIControl *control, MouseButton button)
{

if (TextBox1.Text == "Shit") { Display("Try again... foulmouthed person!"); }

else {
 player.Name = TextBox1.Text;
 player.ChangeRoom(3, 536, 229, eDirectionLeft);
}
}

Crimson Wizard

Quote from: Hobbes on Sun 04/11/2018 14:56:30
1. As a test I tried to get it to non parse "Shit" as a string. That works. However I don't really want to write 10000 if-else statements. Is there a way to parse all the swearwords in one if-statement?

Technically there is, you can use OR operator like (TextBox1.Text == "Shit" || TextBox1.Text == "Crap"), but that will end up in a single giant "if" condition, which still may not be the optimal way code-wise.
Instead, you could define an array of words and iterate over it:

Code: ags

int BadWordsCount;
String BadWords[];

void InitBadWords()
{
    BadWordsCount = 100;
    BadWords = new String[BadWordsCount];
    BadWords[0] = "Shit";
    BadWords[1] = "Crap";
    ...

    // You could even read this list of words from the file
}

function game_start()
{
   InitBadWords();
}


The bad words test:
Code: ags

function Button1_OnClick(GUIControl *control, MouseButton button)
{
   for (int i = 0; i < BadWordsCount; i++)
   {
      if (TextBox1.Text.CompareTo(BadWords[i]) == 0)
      {
          Display("Try again... foulmouthed person!");
          return;
      }
   }

   // passed the test
}


Notice the use of "CompareTo" function, it deals with case-insensitive comparison, which means that it will detect "Shit", "SHit" and "SHIT" alltogether.

Quote from: Hobbes on Sun 04/11/2018 14:56:30
2. Also, how would I parse an empty string and not accept it?

There is String.IsNullOrEmpty() function that detects the zero-length string and null-pointers. But that won't detect a string full of whitespaces. So you perhaps need a more sophisticated test:

Code: ags

bool IsNullOrWhitespace(String s)
{
    if (s == null)
       return true;
    for (int i = 0; i < s.Length; i++)
    {
        if (s.Chars[i] != ' ')
            return false; // found at least one non-whitespace character
    }
    return true;
}


Then -
Code: ags

function Button1_OnClick(GUIControl *control, MouseButton button)
{
    if (IsNullOrWhitespace(TextBox1.Text))
    {
        Display("Type an actual name!");
        return;
    }

Khris

You could use the built-in text parser to do that. Add the first word to the list. Then keep right-clicking it and choose "add synonym". In your code, use

Code: ags
  Parser.ParseText(TextBox1.Text);
  if (Parser.Said("shit")) ...

All other swear words are synonyms, so this should match any word you added. And it even catches leading or trailing spaces, afaik.


Hobbes

Text parser is working fine, thanks for this. Together with the IsNullOrWhitespace code, this is now working flawlessly. Thank you both!!

Snarky

So the first puzzle of the game is "find the bad word that the programmers forgot to add to the filter". That's a classic!

Click'd

Quote from: Snarky on Sun 04/11/2018 15:40:13
So the first puzzle of the game is "find the bad word that the programmers forgot to add to the filter". That's a classic!
Another one is the "offensive string inside a perfectly harmless word".
https://en.wikipedia.org/wiki/Scunthorpe_problem

Hobbes

(nod)

Looking forward to what creative things people can come up with. And what silly easter eggs I might put in.

Cassiebsg

Uhm, that's how I ended up not being allowed to use my nick in a forum... cause it contained the word "ass"... (roll)
There are those who believe that life here began out there...

Hobbes

Even though the BSG part should've overruled everything in my opinion. That should be an automatic pass. So say we all!

Cassiebsg

There are those who believe that life here began out there...

SMF spam blocked by CleanTalk