Text Twist Player

 

Modules - Data Structures, File Streaming

 

Teacher’s Notes:

========================================================================

Students REALLY like this program.  

In this program we use the dictionary that we have previously developed using the Word Mining problem (last week's Problem of the Week) to make a program that can play the game TextTwist 2.  TextTwist 2 is an anagram making game where the player is given six letters and required to make a set of word from 3-6 letters.  In order to pass the level, the player must find the 6 letter word.  This program should be able to pass every level within about 1 second.

This program makes use of the JAVA Robot class to simulate keypresses. There are equivalent ways of doing this in Python and C++.

There is a working link to this game at: https://www.scrabblegames.info/play/text-twist-2

This program is part 3/3 of the of the hashcode-->wordminer-->TextTwistPlayer progression of assignments.

A more robust dictionary can be found at: https://github.com/TaoOfChow/ClassroomSupportFiles/blob/main/dictionary.txt 

If the students use this as their word source in the WordMiner, they will need to update the array width to 100 (from 15) to accommodate the increase in collisions.

 

 

Assignment:

========================================================================

The game TextTwist 2 is an online game that gives the user six letters and requires them to enter as many words as they can make out of those six letters.  The player is given points for each word and must find a six letter word to graduate to the next level.  
 
 
In this assignment, you will write a program that will prompt the user to enter the six letters as a single string.  The program will then execute a 5 second delay to give the user time to switch the focus of the computer from your IDE to the TextTwist program (If you don't do this, the words will likely be typed into your programming editor).  Your program will then read words from your dictionary file and check to see if the word can be made from the six letters provided in the input.  If this is possible, the program will call a method that will use the Robot class to simulate the key presses to type in this word (followed by the ENTER key).  Note that you should build in a 10ms delay after entering a letter because your program will enter words faster than the game can handle.   

 

If this program works properly, it should enter all/most of the anagram words and win each level seemingly instantaneously.

*Note: If you encounter a word that is not in your dictionary, you can add it so you won't miss it next time!

 

Solution (JAVA):

========================================================================

 
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Scanner;
public class TextTwist {
    
public static boolean anagram(String s1, String s2) {
    
        char[] word = s1.toCharArray();
        char[] letters = s2.toCharArray();
        
        boolean status = true;
        
        for(int x = 0; x < word.length; x++)
        {
            boolean letterFound = false;
            for(int y = 0; y < letters.length; y++)
                if(word[x] == letters[y]) {
                    letters[y] = '~';
                    letterFound = true;
                    break;
                }
            if(!letterFound){
                status = false;
                break;
            }
        }
        
        return status;
    }
    
    public static void main(String[] args)throws Exception {
        
        File dictionary = new File("words.txt");
        Scanner list = new Scanner(dictionary);
        Scanner input = new Scanner(System.in);
        
        System.out.println("What letters were you given?");
        String letters = input.next();
            
        System.out.println("You have 5 seconds to switch the focus to TextTwist...");
        Thread.sleep(5000);
        
        Robot keyboard = new Robot();
        String word = "";
        while(list.hasNext()) {
            word = list.next();
            if(anagram(word,letters)) {
                
                for(int x = 0; x < word.length(); x++)
                    keyboard.keyPress(KeyEvent.VK_A + word.charAt(x)-'a');
                keyboard.keyPress(KeyEvent.VK_ENTER);
                Thread.sleep(10);        
            }
        }
        System.out.println("done!");
    }
}