// 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:   boggleGameGen.java

package boggle.server.games;

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

public class boggleGameGen extends gameGen
{

    public boggleGameGen(File file, File file1)
        throws dictionaryException
    {
        super(file, file1);
        AdjMatrix = (new int[][] {
            row0, row1, row2, row3, row4, row5, row6, row7, row8, row9, 
            rowA, rowB, rowC, rowD, rowE, rowF
        });
        dice = settings.getDice();
        validWords = "";
        rolldice();
    }

    public void run()
    {
        String s = "[BHFCF]\nBoggle original Game\n16\n" + layout + "\n[Valid Words]\n";
        if(output == null)
        {
            System.out.println("\n\t" + layout.substring(0, 4) + "\n\t" + layout.substring(4, 8) + "\n\t" + layout.substring(8, 12) + "\n\t" + layout.substring(12, 16) + "\n");
            System.out.println("Proper file output format coming up:");
            System.out.print(s);
            try
            {
                findAllWords();
            }
            catch(dictionaryException dictionaryexception)
            {
                System.out.println("boggleGameGen: 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("boggleGameGen: 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 swapdice(int i, int j)
    {
        String as[] = dice[i];
        dice[i] = dice[j];
        dice[j] = as;
    }

    private void rolldice()
    {
        layout = "";
        for(int j = 16; j > 0; j--)
        {
            int i = randomint(j);
            layout = layout + dice[i][randomint(6)];
            swapdice(i, j - 1);
        }

    }

    private void findAllWords()
        throws dictionaryException
    {
        int ai[] = new int[settings.getMaxWordLength()];
        for(int i = 0; i < 16; 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 = -5; j < 6; 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 > 15)
            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 boggleGameGen");
        File file = new File("boggletest.en");
        boggleGameGen bogglegamegen = new boggleGameGen(new File(new File(settings.getGameDirectory()), settings.getLanguages()[0]), file);
        bogglegamegen.start();
        System.out.println("Waggling fingers to say test is finished");
    }

    private int row0[] = {
        0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0
    };
    private int row1[] = {
        1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 
        0, 0, 0, 0, 0, 0
    };
    private int row2[] = {
        0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 
        0, 0, 0, 0, 0, 0
    };
    private int row3[] = {
        0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 
        0, 0, 0, 0, 0, 0
    };
    private int row4[] = {
        1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 
        0, 0, 0, 0, 0, 0
    };
    private int row5[] = {
        1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 
        1, 0, 0, 0, 0, 0
    };
    private int row6[] = {
        0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 
        1, 1, 0, 0, 0, 0
    };
    private int row7[] = {
        0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 
        1, 1, 0, 0, 0, 0
    };
    private int row8[] = {
        0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 
        0, 0, 1, 1, 0, 0
    };
    private int row9[] = {
        0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 
        1, 0, 1, 1, 1, 0
    };
    private int rowA[] = {
        0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 
        0, 1, 0, 1, 1, 1
    };
    private int rowB[] = {
        0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 
        1, 0, 0, 0, 1, 1
    };
    private int rowC[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
        0, 0, 0, 1, 0, 0
    };
    private int rowD[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
        1, 0, 1, 0, 1, 0
    };
    private int rowE[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
        1, 1, 0, 1, 0, 1
    };
    private int rowF[] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        1, 1, 0, 0, 1, 0
    };
    private int AdjMatrix[][];
    private String dice[][];
    private String layout;
    private String validWords;
}
