// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   babbleGameGen.java

package boggle.server.games;

import boggle.server.*;
import boggle.settings;
import java.io.*;

public class babbleGameGen extends gameGen
{

    public babbleGameGen(File file, File file1)
        throws dictionaryException
    {
        super(file, file1);
        AdjMatrix = (new int[][] {
            row0, row1, row2, row3, row4, row5, row6, row7, row8, row9, 
            rowA, rowB, rowX, rowC, rowD, rowE, rowF, rowG, rowH, rowI, 
            rowJ, rowK, rowL, rowM, rowN
        });
        validWords = "";
        rolldice();
    }

    public void run()
    {
        String s = "[BHFCF]\nBabble 5x5 fixed S game\n25\n" + layout + "\n[Valid Words]\n";
        if(output == null)
        {
            System.out.println("\n\t" + layout.substring(0, 5) + "\n\t" + layout.substring(5, 10) + "\n\t" + layout.substring(10, 15) + "\n\t" + layout.substring(15, 20) + "\n\t" + layout.substring(20, 25) + "\n");
            System.out.println("Proper file output format coming up:");
            System.out.print(s);
            try
            {
                findAllWords();
            }
            catch(dictionaryException dictionaryexception)
            {
                System.out.println("babbleGameGen: please check, error on " + output.getName());
                System.out.println(dictionaryexception.getMessage());
                dictionaryexception.printStackTrace();
            }
            System.out.print(validWords);
            return;
        }
        try
        {
            BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter(output));
            bufferedwriter.write(s, 0, s.length());
            bufferedwriter.flush();
            findAllWords();
            bufferedwriter.write(validWords, 0, validWords.length());
            bufferedwriter.flush();
            bufferedwriter.close();
            return;
        }
        catch(Exception exception)
        {
            System.out.println("babbleGameGen: please check, error on " + output.getName());
            System.out.println(exception.getMessage());
            exception.printStackTrace();
            return;
        }
    }

    private int randomint(int i)
    {
        return (int)Math.floor(Math.random() * (double)i);
    }

    private void rolldice()
    {
        layout = "";
        for(int j = 0; j < 24; j++)
        {
            int i = randomint(16);
            String s = settings.getDice()[i][randomint(6)];
            if(s.equals("S"))
                j--;
            else
                layout = layout + s;
        }

        layout = layout.substring(0, 12) + "S" + layout.substring(12, 24);
    }

    private void findAllWords()
        throws dictionaryException
    {
        int ai[] = new int[settings.getMaxWordLength()];
        for(int i = 0; i < 25; i++)
        {
            ai[0] = i;
            findWords(layout.substring(i, i + 1), 1, (int[])ai.clone());
        }

        validWords = quicksort(validWords);
    }

    private void findWords(String s, int i, int ai[])
        throws dictionaryException
    {
        if(i > 2)
        {
            if(!language.validStart(s, i))
                return;
            addToWordList(s, i);
            if(i == settings.getMaxWordLength())
                return;
        }
        for(int j = -6; j < 7; j++)
        {
            int k = ai[i - 1] + j;
            if(!isNotValid(k, i, ai))
            {
                int ai1[] = (int[])ai.clone();
                ai1[i] = k;
                String s1 = new String(s) + layout.substring(k, k + 1);
                findWords(s1, i + 1, ai1);
            }
        }

    }

    private void addToWordList(String s, int i)
        throws dictionaryException
    {
        String s1 = s;
        int j = s1.indexOf(81);
        if(j > -1)
            s1 = s1.substring(0, j + 1) + "U" + s1.substring(j + 1, i);
        if(language.validWord(s) && validWords.indexOf(s) == -1)
            validWords += s + "\n";
    }

    private boolean isNotValid(int i, int j, int ai[])
    {
        if(i < 0 || i > 24)
            return true;
        if(AdjMatrix[i][ai[j - 1]] == 0)
            return true;
        for(int k = 0; k < j; k++)
            if(i == ai[k])
                return true;

        return false;
    }

    public static void main(String args[])
        throws dictionaryException
    {
        System.out.println("This is the test function for babbleGameGen");
        File file = new File("babbletest.en");
        babbleGameGen babblegamegen = new babbleGameGen(new File(new File(settings.getGameDirectory()), "english.dict"), file);
        babblegamegen.start();
        System.out.println("Waggling fingers to say test is finished");
    }

    private int row0[] = {
        0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row1[] = {
        1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row2[] = {
        0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row3[] = {
        0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row4[] = {
        0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row5[] = {
        1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
        1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row6[] = {
        1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 
        1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row7[] = {
        0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 
        0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row8[] = {
        0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 
        0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int row9[] = {
        0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 
        0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int rowA[] = {
        0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 
        0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int rowB[] = {
        0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 
        1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 
        0, 0, 0, 0, 0
    };
    private int rowX[] = {
        0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 
        0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 
        0, 0, 0, 0, 0
    };
    private int rowC[] = {
        0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
        0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 
        0, 0, 0, 0, 0
    };
    private int rowD[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
        0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 
        0, 0, 0, 0, 0
    };
    private int rowE[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 
        1, 1, 0, 0, 0
    };
    private int rowF[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 
        1, 1, 1, 0, 0
    };
    private int rowG[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 
        0, 1, 1, 1, 0
    };
    private int rowH[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 
        0, 0, 1, 1, 1
    };
    private int rowI[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 
        0, 0, 0, 1, 1
    };
    private int rowJ[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 
        0, 1, 0, 0, 0
    };
    private int rowK[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 
        1, 0, 1, 0, 0
    };
    private int rowL[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 
        0, 1, 0, 1, 0
    };
    private int rowM[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
        0, 0, 1, 0, 1
    };
    private int rowN[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
        0, 0, 0, 1, 0
    };
    private int AdjMatrix[][];
    private String layout;
    private String validWords;
}
