Click here to Skip to main content
16,018,904 members
Articles / Desktop Programming / Windows Forms

Anagrams - A Word Game in C#

Rate me:
Please Sign up or sign in to vote.
4.71/5 (27 votes)
4 Nov 2012CPOL6 min read 150.4K   3.9K   71   30
The classic word game using words and letter scores allowed in Scrabble
Anagrams2/game_panel.png

Make sure you check out the new WPF version here: Anagrams2 - A Simple WPF Game Application[^]

Introduction

A few years ago, my wife started playing the Anagrams game from the Hoyle Word Games CD. The game presents you with a scrambled 6-letter word, and presents you with an opportunity to find 10-20 words that exist within the original word in a short amount of time. All the while, a pair of off-screen cannibals try to distract you with witty remarks, questions about your computer system, word suggestions (that are never right for the scramble you're currently working with), and sometimes, outright taunting.

While somewhat amusing (based solely on what the off-screen cannibals might be saying), the game is severely limited in its scope, and in my humble opinion, arbitrarily so. First, your scrambled word is never comprised of more than six letters, making for a limited dictionary. Second, the number of words are radically fewer than what actually exist in the scrambled word. Mind you, I'm not talking about obscure and rarely used words, but plain English stuff that any person with even just a mediocre vocabulary would see.

Note: This is not a complex game, and the code certainly doesn't explore or exercise any of the .NET Framework's more esoteric functionality. I simply wrote the game to help me learn .NET (I've only been coding in C# since August of 2007). There is no fancy interface, no amazing 3-D effects, and no attempts to dazzle anyone. It is what it is.

What I Did

After becoming sufficiently annoyed with the game, I decided it wouldn't be too awful hard to duplicate the game, but I wanted to include a much more far-reaching dictionary. So, I searched the web for lists of words of various lengths, from three to ten characters long. I also wanted to make sure I included as many valid words as possible. So, I figured I need to find lists of words that were legal in the game Scrabble. What better source of words than the most famous and popular word game in the world? In no time, I'd gathered a collection of over 125,000 words.

The Word Dictionary

Now that I had a sufficient list of words from which to bamboozle the user, I needed a way to load them all into memory at the same time. I created a "dictionary" class which created a word list object for each set of words based on their character counts.

Remember, we had words with from three to ten letters. As most of us already know, C# lists are zero-based. To make accessing the word lists a little easier to maintain, the dictionary creates 11 word lists, with list index 0 through 2 not being used to store dictionary words. However, there was a use for at least one of these unused list indexes.

Other dictionary functionality includes the ability to mark a word as having been used (so that the user won't see the same scrambled word until all of the other words in that letter group have been seen). When all of the words in a group have been seen, only a percentage of the words are marked as not having been used. This reduces the change that a recently used word will be presented again too soon after its last use.

While the dictionary object makes it easy to manage all of the word lists in a single location, the real use of the dictionary is to allow the program to discover and track words that are contained WITHIN the scrambled word.

Possible Words

Part of the game play support, the program has to be able to quickly identify words that are indeed found within the scrambled word. It would be impractical to search all 120,000+ words to see if one of them was a valid one. So, the program takes the scrambled word, and searches all of the word lists as soon as the scrambled word is available. To store these possible words, we use the 0th index in the list of word list objects.

Finally, we use the numerous dictionary object methods to find and update this possible words object. This includes marking possible words as already having been found and making the validation of user-submitted words much simpler.

Game Configuration

I provided a bit of configuration capability regarding how many letters to allow in the scrambled words, how much time is allotted to play a round, how much bonus time to allow, whether or not to play the game sounds, and whether or not to allow repeating words. These settings are saved via the GameConfig.cs file. Here's the dialog box:

Anagrams2/config_dialog.png

The Game Panel

This is nothing but a Windows Forms application. I had to play some tricks to get the panel to react the way I wanted it to. I needed a way for the user to be able to just hit the enter key and have it submit the typed word. To do this, I created and hid a Submit button and made it the "default" button. With the Submit button taken care of, I had to find a way to illustrate the time left to the user for the current round.

I wanted to display the time remaining inside a progress bar, but I couldn't find a way to do that with the standard progress bar control (which probably means it's there, I just didn't find it). So, I found a control class called SmoothProgressBar (I don't remember where I found it, but I didn't write it), and it allowed the text in bar. As the timer winds down, the progress bar and the text are updated to reflect the amount of time remaining.

The Word List

I wanted a way to show the user what he'd typed, so I decided on a list box. As the program validates and accepts submitted words, they are added to the list box (sorted alphabetically). Then, when the user clicked Solve or the round expired naturally, I wanted to show all of the words that were possible, yet show the words that the user had submitted in such a way as to highlight those words.

To accomplish this, I set the list box's DrawMode property to be OwnerDrawFixed, and then overrode the DrawItem behavior. This allowed me to draw certain items with different appearances. I settled on dark gray text for words that were not discovered by the player, and bold red italic text for words the user found.

Statistics

I made a half-hearted attempt at providing a minimal set of statistics in the game. These stats aren't cataloged, but it wouldn't be hard to expand and thereby provide graphed results and game-play histories. I might even do that a little later.

The Sounds

Yeah, they suck. If you want to replace them with something else, go ahead. In fact, if you know anyone with a clear speaking voice (preferably a soft-spoken yet sultry female), have her record the appropriate vocal indicators and let me know you have a new set of files.

In Closing

I fail to understand why Hoyle imposed arbitrary restrictions on their version of the game. Maybe they wanted to keep the game to a limited intellectual level - I don't know. I find the game to be quite addictive because the rounds are short, and you tend to start saying to yourself, "Just one more round before I go to work..."

History

  • 7th April, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Wendelius4-Nov-12 10:18
mentorWendelius4-Nov-12 10:18 
GeneralRe: My vote of 5 Pin
#realJSOP4-Nov-12 12:06
professional#realJSOP4-Nov-12 12:06 
QuestionDid u manually create the text files Pin
VigneshPT12-Aug-11 22:39
VigneshPT12-Aug-11 22:39 
AnswerRe: Did u manually create the text files Pin
#realJSOP14-Aug-11 7:50
professional#realJSOP14-Aug-11 7:50 
QuestionThis is not working for me . What to do ? Pin
raj83_2818-Jul-11 8:13
raj83_2818-Jul-11 8:13 
AnswerRe: This is not working for me . What to do ? Pin
#realJSOP12-Aug-11 5:45
professional#realJSOP12-Aug-11 5:45 
AnswerRe: This is not working for me . What to do ? Pin
VigneshPT12-Aug-11 22:35
VigneshPT12-Aug-11 22:35 
GeneralMy vote of 5 Pin
thatraja2-Sep-10 4:26
professionalthatraja2-Sep-10 4:26 
Questionplaying an anagram? Pin
changcruz20-Jul-10 23:54
changcruz20-Jul-10 23:54 
AnswerRe: playing an anagram? Pin
#realJSOP21-Jul-10 12:36
professional#realJSOP21-Jul-10 12:36 
GeneralAddictive Pin
benoli25-Apr-08 5:26
benoli25-Apr-08 5:26 
GeneralRe: Addictive Pin
#realJSOP26-Apr-08 4:15
professional#realJSOP26-Apr-08 4:15 
GeneralWay to talk!! Pin
3-Fingers-Bill17-Apr-08 1:16
3-Fingers-Bill17-Apr-08 1:16 
GeneralRe: Way to talk!! Pin
#realJSOP22-Apr-08 12:13
professional#realJSOP22-Apr-08 12:13 
GeneralWorking on the Network Version Pin
#realJSOP9-Apr-08 11:27
professional#realJSOP9-Apr-08 11:27 
GeneralRe: Working on the Network Version Pin
kmaricich29-Mar-09 16:40
kmaricich29-Mar-09 16:40 
GeneralRe: Working on the Network Version Pin
#realJSOP31-Mar-09 0:10
professional#realJSOP31-Mar-09 0:10 
Generalvery good Pin
Anil Srivastava8-Apr-08 3:46
Anil Srivastava8-Apr-08 3:46 
General5 from me !! Pin
Niiiissssshhhhhuuuuu7-Apr-08 9:17
Niiiissssshhhhhuuuuu7-Apr-08 9:17 
GeneralRe: 5 from me !! Pin
Doubin7-Apr-08 22:01
Doubin7-Apr-08 22:01 
GeneralVery cool! Pin
Marc Clifton7-Apr-08 7:32
mvaMarc Clifton7-Apr-08 7:32 
GeneralRe: Very cool! Pin
#realJSOP7-Apr-08 7:39
professional#realJSOP7-Apr-08 7:39 
GeneralCool Pin
Mustafa Ismail Mustafa7-Apr-08 6:40
Mustafa Ismail Mustafa7-Apr-08 6:40 
GeneralNice Pin
Rama Krishna Vavilala7-Apr-08 6:11
Rama Krishna Vavilala7-Apr-08 6:11 
GeneralRe: Nice Pin
#realJSOP7-Apr-08 6:24
professional#realJSOP7-Apr-08 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.