Click here to Skip to main content
Click here to Skip to main content

Anagrams - A Word Game in C#

, 4 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
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 PinmvpMika Wendelius4-Nov-12 10:18 
GeneralRe: My vote of 5 PinmvpJohn Simmons / outlaw programmer4-Nov-12 12:06 
QuestionDid u manually create the text files PinmemberVigneshPT12-Aug-11 22:39 
AnswerRe: Did u manually create the text files PinmvpJohn Simmons / outlaw programmer14-Aug-11 7:50 
I found the words on google and just created the text files. Smile | :)
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

QuestionThis is not working for me . What to do ? Pinmemberraj83_2818-Jul-11 8:13 
AnswerRe: This is not working for me . What to do ? PinmvpJohn Simmons / outlaw programmer12-Aug-11 5:45 
AnswerRe: This is not working for me . What to do ? PinmemberVigneshPT12-Aug-11 22:35 
GeneralMy vote of 5 Pinmemberthatraja2-Sep-10 4:26 
Questionplaying an anagram? Pinmemberchangcruz20-Jul-10 23:54 
AnswerRe: playing an anagram? PinmemberJohn Simmons / outlaw programmer21-Jul-10 12:36 
GeneralAddictive Pinmemberbenoli25-Apr-08 5:26 
GeneralRe: Addictive PinmvpJohn Simmons / outlaw programmer26-Apr-08 4:15 
GeneralWay to talk!! Pinmember3-Fingers-Bill17-Apr-08 1:16 
GeneralRe: Way to talk!! PinmvpJohn Simmons / outlaw programmer22-Apr-08 12:13 
GeneralWorking on the Network Version PinmvpJohn Simmons / outlaw programmer9-Apr-08 11:27 
GeneralRe: Working on the Network Version Pinmemberkmaricich29-Mar-09 16:40 
GeneralRe: Working on the Network Version PinmvpJohn Simmons / outlaw programmer31-Mar-09 0:10 
Generalvery good PinmemberAnil Srivastava8-Apr-08 3:46 
General5 from me !! PinmemberNiiiissssshhhhhuuuuu7-Apr-08 9:17 
GeneralRe: 5 from me !! PinmemberDoubin7-Apr-08 22:01 
GeneralVery cool! PinprotectorMarc Clifton7-Apr-08 7:32 
GeneralRe: Very cool! PinmvpJohn Simmons / outlaw programmer7-Apr-08 7:39 
GeneralCool PinmemberMustafa Ismail Mustafa7-Apr-08 6:40 
GeneralNice PinmemberRama Krishna Vavilala7-Apr-08 6:11 
GeneralRe: Nice PinmvpJohn Simmons / outlaw programmer7-Apr-08 6:24 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 4 Nov 2012
Article Copyright 2008 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid