Monday, July 18, 2011

The Game of TextTwist

Related Articles

  1. Permutation Generator
  2. Combination Generator

Introduction

The game of text twist is a game of finding words from a given set of letters. For example, you are given the following 6 letters :
["F","O","C","I","E","F"]

And your tasks is to find out all possible words by selecting subsets of the letters. The solution would be :

[ "ICE","OFF","FOE","FOCI","OFFICE" ]

You may compile and play the game to have a better understanding. Source code will be given at the end of this article.

Dictionary

To generate the problem and the solutions, you need a dictionary file. First, select a 6-letters word from the dictionary file. For example, let's choose the word "ABSENT".

Now we have 6 letters, ["A","B","S","E","N","T"]. To create the problem, we shuffle those letters to form a random arrangement. For example :
["S","A","E","B","N","T"]

Now we can generate all combinations and permutations from the set of letters, and check it against the dictionary file again. If it is in the dictionary, then it is part of the solution.
Depending on your dictionary file, possible solution of the above problem would be :

[SEA, SAT, EST, SET, TEA, ATE, EAT, BAN, TAB, BAT, TAN, ANT, BEN, BET, TEN, NET,
BASE, SANE, EATS, EAST, SEAT, BANS, BATS, TABS, STAB, ANTS, BETS, BEST, NETS, TENS,
NEST, SENT, BANE, BEAN, BETA, BATE, BEAT, NEAT, BENT, BEANS, BEATS, BATES, BASTE,
BEAST, ABSENT]


Collection of Problems

We have seen two sample problems so far :
["F","O","C","I","E","F"]
["S","A","E","B","N","T"]

We can easily generate more problems and form a collections. Then we can save all the problems in a class file, such as :


GUI Control

Once we have the problem collections, we can pick a problem at random and start the game. For simplicity, the sample below will use a hard-coded problem instead of really picking one from the Problem class. It is because I want to focus on the GUI control on the game. So you will see the following declaration at the very beginning of TextTwist.java


We will use standard SWING components such as JButton and JLabel. The code should be straight forward enough to be understood. Of course, you should first compile and run it before reading the code. The program flow is best documented by running the program yourself.



External Link

I have created a C# version in programmingforums.org

For those who are interested may visit there to have a look.

2 comments:

  1. how do i exit afterwards the game is finished?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete