I was thinking of a text based game where the users type in various "things." But I know it will quickly degenerate into filth. Is there any way to stop something like this?
Users determined to use inappropriate words can be very creative in getting around even quite sophisticated automatic systems. Imagine you want to block the word, "pear". Can your filter deal with deliberate misspellings like "pair"? And what about false positives, like "appear"? What if the user's real name is actually "Pearson"? But offenders start using the player's name in place of "bad words"?
There are, however, several techniques that can help. The idea is not to prevent offensive behaviour, but rather to take the fun out of it.
- Filter words that are obviously bad. So, you would filter "pear" but not "pair". You're dealing with casual offenders, you're setting the tone, but you'll still allow "pearson" to play.
- As suggested in another answer, show the original words to the person who typed them, but the clean version to others. The genius of this technique is that users may not realise that their words are being filtered, so may not think to invent workarounds.
- Automatically (and politely) remind offenders to stop. Explain why some people find their behaviour inappropriate
- If it isn't an essential aspect of the game, allow users to turn off the chat facility so they don't have to read conversations that they consider offensive
- Allow users to ignore certain players
- Allow peer review. An additional advantage is this can also discourage other "bad" behaviour such as racism or trolling which is not based on word matching. Stack exchange is a great example of how this can work in practice.
- Some games have force users to pick from stock phrases. This is generally perceived as boring, but can be appropriate in some situations.
None of these suggestions will outright prevent bad words, but they can go a long way to helping you keep your game clean.
It's effective in these kinds of systems to show the original words to the user who made the input, but the scrubbed version to everyone else.
One simple option is to have a text file that contains a list of all words and phrases that you want to block. Scrub the input prior accepting it by checking it against the list of words in that file.
Apple Pear Banana
Then scrub the input like so (pseudocode):
if(!blockedWords.Contains(input)) // accept input and do whatever you need to else print 'Hey, be nice!'
input is "Apple", the
blockedWords array read in from the text file will contain it and throw up the message.