给出词表,计算填字游戏的最佳方法是什么?

时间:2015-10-04 01:59:34

标签: c# algorithm sorting

我一直在努力尝试改编游戏' crozzle'数字格式。对于那些不知道它的人来说,它本质上是一个填字游戏。我的程序采用一个单词表,并根据难度尝试计算单词的最佳位置。但是,我似乎无法实现这一目标,并认为有人可能会提供帮助。基本上我希望能够以最多的单词存档以获得最高分。

namespace CrozzleApplication
{
    public class CreateCrozzle
    {
        #region properties
        public List<String> StartList;
        public List<String> List;
        public String[,] theGrid;
        public List<TemporaryCrozzle> LastList;

    const Int32 DOWN = 1;
    const Int32 RIGHT = 2;
    const Int32 UP = 3;
    const Int32 LEFT = 4;

    String lastword = "";
    String pendingWord = "";
    public int index1 = -1;
    public static int Total = 0;

    Int32 Horizon1 = 0, Horizon2 = 0;
    Int32 Vertical1 = 0, Vertical2 = 0;
    #endregion

    #region create
    /// <summary>
    /// Creating the crozzle
    /// </summary>
    /// <param name="input"></param>
    public CreateCrozzle(List<String> input)
    {
        LastList = new List<TemporaryCrozzle>();
        StartList = input;
        List = input;
        theGrid = new String[TemporaryCrozzle.MaxRow, TemporaryCrozzle.MaxColumn];

        int i, j;
        for (i = 0; i < TemporaryCrozzle.MaxRow; i++)
        {
            for (j = 0; j < TemporaryCrozzle.MaxColumn; j++)
            {
                theGrid[i, j] = "";
            }
        }
    }
    #endregion

    #region showgrid
    /// <summary>
    /// showing the grid
    /// </summary>
    public void displayGrid()
    {
        Console.WriteLine("******************************************************");
        int i, j;
        for (i = 0; i < TemporaryCrozzle.MaxRow; i++)
        {
            for (j = 0; j < TemporaryCrozzle.MaxColumn; j++)
            {
                if (theGrid[i, j].Equals(""))
                {
                    Console.Write("-     ");
                }
                else Console.Write(theGrid[i, j] + "     ");
            }
            Console.WriteLine("");
        }

        Console.WriteLine("******************************************************");
    }
    #endregion

    #region process
    /// <summary>
    /// Processing the crozzle creating and searching for the best crozzle to display as a final product
    /// </summary>
    public void Process()
    {
        List = null;
        List = new List<string>();
        foreach (String ss in StartList)
            List.Add(ss);

        index1++;
        if ((index1 + 1) >= StartList.Count) return;

        int i, j;
        for (i = 0; i < TemporaryCrozzle.MaxRow; i++)
        {
            for (j = 0; j < TemporaryCrozzle.MaxColumn; j++)
            {
                theGrid[i, j] = "";
            }
        }

        if (index1 == List.Count) return;

        Horizon1 = 0; Horizon2 = 0;
        Vertical1 = 0; Vertical2 = 0;

        lastword = List[index1];
        pendingWord = List[index1];
        putWord(List[index1], 0, 0, RIGHT);
        List.Remove(List[index1]);
        displayGrid();

        if (FindinTheGridDown(lastword, 1, 0, 0))
        {
            displayGrid();
            if (pendingWord.Length <= 1)
            {
                CrozzleFill();
                Process();
                return;
            }

            pendingWord = pendingWord.Remove(0, 1);

            if (Vertical2 == Vertical1)
            {
                if (!FindinTheGridRight(pendingWord, 1, Horizon2 + 2, Vertical2))
                {
                    CrozzleFill();
                    Process();
                    return;
                }
                displayGrid();
            }
            else
            {
                if (!FindinTheGridRight(pendingWord, Vertical2 - Vertical1 - 1, Horizon2 + 1, Vertical2))
                {
                    CrozzleFill();
                    Process();
                    return;
                }
                displayGrid();
            }
        }
        else
        {
            CrozzleFill();
            Process();
            return;
        }

        displayGrid();

        if (pendingWord.Length <= 1)
        {
            CrozzleFill();
            Process();
            return;
        }

        pendingWord = pendingWord.Remove(0, 1);

        while (FindinTheGridDown(pendingWord, Horizon2 - Horizon1 - 1, Horizon2, Vertical2 + 2))
        {
            displayGrid();
            if (pendingWord.Length <= 1)
            {
                CrozzleFill();
                Process();
                return;
            }
            pendingWord = pendingWord.Remove(0, 1);

            if (!FindinTheGridRight(pendingWord, Vertical2 - Vertical1 - 1, Horizon2 + 2, Vertical2))
            {
                CrozzleFill();
                Process();
                return;
            }
            displayGrid();

            if (pendingWord.Length <= 1)
            {
                CrozzleFill();
                Process();
                return;
            }
            pendingWord = pendingWord.Remove(0, 1);
        }
        CrozzleFill();
        Process();
        return;

    }
    #endregion

    #region putting word
    /// <summary>
    /// putting the words in the correct place
    /// </summary>
    /// <param name="word">length of the word</param>
    /// <param name="r">r is represented by the row</param>
    /// <param name="c">c is represented by the coloumn</param>
    /// <param name="dir">dir is repsented by direction</param>
    private void putWord(String word, Int32 r, Int32 c, Int32 dir)
    {
        Int32 WordLength = word.Length;
        int i;

        if (dir == DOWN)
        {
            for (i = 0; i < WordLength; i++) theGrid[i + r, c] = word[i].ToString();
            Vertical1 = Vertical2;
            Vertical2 = c;
        }
        if (dir == RIGHT)
        {
            for (i = 0; i < WordLength; i++) theGrid[r, i + c] = word[i].ToString();
            Horizon1 = Horizon2;
            Horizon2 = r;
        }
        if (dir == UP)
        {
            for (i = 0; i < WordLength; i++) theGrid[r - i, c] = word[i].ToString();
        }
        if (dir == LEFT)
        {
            for (i = 0; i < WordLength; i++) theGrid[r, c - i] = word[i].ToString();
        }
        lastword = word;
        List.Remove(word);
    }
    #endregion

    #region can put the word
    /// <summary>
    /// Checking if the word can be put into the crozzle
    /// </summary>
    /// <param name="word">length of the word</param>
    /// <param name="r">r is represented by the row</param>
    /// <param name="c">c is represented by the coloumn</param>
    /// <param name="dir">dir is repsented by direction</param>
    /// <returns>returns the crozzle</returns>
    private Boolean CanPutWord(String word, Int32 r, Int32 c, Int32 dir)
    {
        Int32 WordLength = word.Length;
        Int32 MaxInteraction = 0;

        if (dir == DOWN)
        {
            if (r + WordLength > TemporaryCrozzle.MaxRow) return false;
            int i;
            for (i = 0; i < WordLength; i++)
            {
                if (theGrid[i + r, c] != "")
                {
                    if (word[i] != theGrid[i + r, c][0]) return false;
                    else
                    {
                        MaxInteraction++;
                        pendingWord = word.Substring(i + 1, word.Length - i - 1);
                        if (MaxInteraction == 2) return false;
                    }
                }
            }
            return true;
        }
        else if (dir == RIGHT)
        {
            if (c + WordLength > TemporaryCrozzle.MaxColumn) return false;
            int i;
            for (i = 0; i < WordLength; i++)
            {
                if (theGrid[r, i + c] != "")
                {
                    if (word[i] != theGrid[r, i + c][0]) return false;
                    else
                    {
                        MaxInteraction++;
                        pendingWord = word.Substring(i + 1, word.Length - i - 1);
                        if (MaxInteraction == 2) return false;
                    }
                }
            }
            return true;

        }
        else if (dir == UP)
        {

            if (r >= (WordLength - 1)) return false;
            int i;
            for (i = 0; i < WordLength; i++)
            {
                if (theGrid[r - i, c] != "")
                {
                    if (word[i] != theGrid[i + r, c][0]) return false;
                    else
                    {
                        MaxInteraction++;
                        if (MaxInteraction == 2) return false;
                    }
                }
            }

            return true;

        }
        else if (dir == LEFT)
        {

        }

        return false;
    }
    #endregion

    #region the grid down
    /// <summary>
    /// checking the crozzle step by step to identify the word
    /// </summary>
    /// <param name="characters">represents the letters</param>
    /// <param name="TopSpaces">checks the top of the crozzle</param>
    /// <param name="StartRow">checks the start of the row in the crozzle</param>
    /// <param name="StartCol">check the start of the coloumn in the crozzle</param>
    /// <returns></returns>
    private Boolean FindinTheGridDown(String characters, Int32 TopSpaces, Int32 StartRow, Int32 StartCol)
    {
        try
        {
            String WordTop = "";
            foreach (String s in List)
            {
                WordTop = s.Substring(0, TopSpaces);
                int i, j;

                for (i = 0; i < WordTop.Length; i++)
                    for (j = 0; j < characters.Length; j++)
                    {
                        if (characters[j] == WordTop[i])
                        {
                            if (CanPutWord(s, StartRow - i, StartCol + j, DOWN))
                            {
                                putWord(s, StartRow - i, StartCol + j, DOWN);
                                Total++;
                                return true;
                            }
                        }
                    }

            }
            return false;
        }
        catch (Exception e)
        {
            CrozzleFill();
            return false;
        }
    }
    #endregion

    #region finding the grid right
    /// <summary>
    /// checking the crozzle step by step to identify the word
    /// </summary>
    /// <param name="characters">represents the letters</param>
    /// <param name="TopSpaces">checks the top of the crozzle</param>
    /// <param name="StartRow">checks the start of the row in the crozzle</param>
    /// <param name="StartCol">check the start of the coloumn in the crozzle</param>
    /// <returns></returns>
    private Boolean FindinTheGridRight(String characters, Int32 LeftSpaces, Int32 StartRow, Int32 StartCol)
    {
        try
        {
            String WordTop = "";
            foreach (String s in List)
            {
                WordTop = s.Substring(0, LeftSpaces);
                int i, j;

                for (i = 0; i < WordTop.Length; i++)
                    for (j = 0; j < characters.Length; j++)
                    {
                        if (characters[j] == WordTop[i])
                        {
                            if (CanPutWord(s, StartRow + j, StartCol - i, RIGHT))
                            {
                                putWord(s, StartRow + j, StartCol - i, RIGHT);
                                Total++;
                                return true;
                            }
                        }
                    }

            }
        }
        catch (Exception e)
        {
            CrozzleFill();
            return false;
        }
        return false;
    }
    #endregion

    #region fill
    /// <summary>
    /// filling in the crozzle
    /// </summary>
    public void CrozzleFill()
    {
        TemporaryCrozzle temp = new TemporaryCrozzle(theGrid, TemporaryCrozzle.MaxRow, TemporaryCrozzle.MaxColumn);
        if (temp.isValidCrozzle())
        {
            LastList.Add(temp);
            CreateCSV(temp);
        }
    }
    #endregion

    #region create csv
    /// <summary>
    /// Creates a csv once the grid is shown
    /// </summary>
    /// <param name="write">write into a csv file</param>
    public void CreateCSV(TemporaryCrozzle write)
    {
        if (!Directory.Exists(Path.GetDirectoryName(Application.ExecutablePath) + "\\CreatedCrozzle"))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(Application.ExecutablePath) + "\\CreatedCrozzles");
        }
        String csvpath;
        String Line = "";
        int i, j;

        csvpath = Path.GetDirectoryName(Application.ExecutablePath) + "\\CreatedCrozzles\\" + write.index + ".txt";
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(csvpath, true))
        {
            Console.WriteLine("******************************************************");
            //file.WriteLine("******************************************************");

            for (i = 0; i < TemporaryCrozzle.MaxRow; i++)
            {
                for (j = 0; j < TemporaryCrozzle.MaxColumn; j++)
                {
                    if (theGrid[i, j].Equals(""))
                    {
                        Console.Write("-     ");
                        file.Write(" ");
                    }
                    else
                    {
                        Console.Write(theGrid[i, j] + "     ");
                        file.Write(theGrid[i, j]);
                    }
                }
                Console.WriteLine("");
                file.WriteLine("");

            }

            Console.WriteLine("******************************************************");

        }
    }
    #endregion
}

干杯。让我知道你们的想法。

0 个答案:

没有答案
相关问题