Exemplo n.º 1
0
        private void openConfiguarionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            errorConfGroup = null;
            errorConfNum   = null;
            errConfiSecond = null;
            OpenFileDialog open = new OpenFileDialog(); //new open file dialog

            open.Title  = "Open Folders";               //title
            open.Filter = "File(.txt)|*.txt";           //extension
            if (open.ShowDialog() == DialogResult.OK)
            {
                //succeed
                string file = open.FileName;
                MessageBox.Show(file, "selected reminder", MessageBoxButtons.OK, MessageBoxIcon.Information);
                StreamReader sr           = new StreamReader(open.FileName);
                string       strGroup     = sr.ReadLine();
                string       pointPerWord = sr.ReadLine();
                ValidFile    valFil       = new ValidFile();
                string       regex        = @"(\d+)";
                errorConfGroup = valFil.valConfGroup(strGroup);
                int group = match(regex, strGroup);
                if (valFil.valConfSecond(pointPerWord) != null)
                {
                    errConfiSecond = valFil.valConfSecond(pointPerWord);
                }
                else
                {
                    point = match(regex, pointPerWord);
                }

                int    i = 0;
                string line;
                //int[] chaScore = new int[52];
                //stat to read configuration file from the third line
                while ((line = sr.ReadLine()) != null)
                {
                    if (valFil.valConfNum(line) != null)
                    {
                        errorConfNum = valFil.valConfNum(line) + errorConfNum;
                    }
                    else
                    {
                        chaScore[i] = match(regex, line);
                    }

                    //Console.Write(chaScore[i]);
                    i++;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Step 1: Open file dialog
        /// Step 2: Select input file
        /// Step 3: Obtain filename
        /// Step 4: Validate input file and crozzle
        /// Step 5: Calculate score
        /// Step 6: Display crozzle, score, and errors and log
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog(); //Define OpenFileDialog

            open.Title  = "Open Folders";               //Dialog Title
            open.Filter = "File(.txt)|*.txt";           //File extension
            if (open.ShowDialog() == DialogResult.OK)
            {
                ValidFile val = new ValidFile();
                //Succeed
                string file = open.FileName;
                MessageBox.Show(file, "selected reminder", MessageBoxButtons.OK, MessageBoxIcon.Information);
                StreamReader sr1     = new StreamReader(open.FileName);
                int          lineNum = sr1.ReadToEnd().Split('\n').Length - 2; //obtain the number of word in crozzle
                sr1.Close();

                StreamReader sr        = new StreamReader(open.FileName);
                char[]       separator = { ',' };
                string       line;
                string[]     wordArr       = new string[lineNum];
                int[]        hor           = new int[lineNum];
                int[]        ver           = new int[lineNum];
                string[]     direction     = new string[lineNum];
                string[]     firstLine     = sr.ReadLine().Split(separator);
                string       errorFirstLen = @"<p>" + val.validdateFirstLen(firstLine.Length) + @"</p>";
                string[]     secondLine    = sr.ReadLine().Split(separator);
                string       errorWordList = null;
                //validate whether the second line match specific pattern
                for (int m = 0; m < secondLine.Length; m++)
                {
                    if (secondLine[m] == "")
                    {
                        errorWordList = @"<p>There is a space in wold list, which is invalid.</p>" + errorWordList;
                    }
                    if (!val.validateIfWord(secondLine[m]))
                    {
                        errorWordList = @"<p>" + secondLine[m] + @" is an invalid word in word list" + errorWordList;
                    }
                }

                string   level      = firstLine[0];
                string[] errorFirst = new string[firstLine.Length - 1];
                int[]    numFirst   = new int[firstLine.Length - 1];
                string   firstError = null;
                //Validate whether the first line match specific pattern
                for (int k = 0; k < firstLine.Length - 1; k++)
                {
                    if (val.validateIfNum(firstLine[k + 1]))
                    {
                        numFirst[k] = Convert.ToInt16(firstLine[k + 1]);
                    }
                    else if (firstLine[k + 1] == "")
                    {
                        errorFirst[k] = @"<p>There is a space in the first line, which is invalid.</p>";
                    }
                    else
                    {
                        errorFirst[k] = @"<p>" + firstLine[k + 1] + @" is not a number in the first line.</p>";
                    } firstError = firstError + errorFirst[k];
                }

                int    wordList    = numFirst[0];
                string errorActNum = val.validateActuNum(wordList, secondLine);
                int    rowsNum     = numFirst[1];
                int    colsNum     = numFirst[2];
                char[,] wordcha = new char[rowsNum, colsNum];
                char[,] wordHor = new char[rowsNum, colsNum];
                char[,] wordVer = new char[rowsNum, colsNum];
                for (int rows = 0; rows < rowsNum; rows++)
                {
                    for (int cols = 0; cols < colsNum; cols++)
                    {
                        wordcha[rows, cols] = '\0';
                    }
                }

                //Crozzle crozzleDis = new Crozzle();
                string errorDirec = null;
                string errorWord  = null;
                string errorEdge  = null;
                int    i          = 0;
                //start to read input file from the third line
                while ((line = sr.ReadLine()) != null)
                {
                    string[] wordSpl = line.Split(separator);
                    //separate and store each line to an array
                    direction[i] = wordSpl[0];
                    hor[i]       = Convert.ToInt16(wordSpl[1]);
                    ver[i]       = Convert.ToInt16(wordSpl[2]);
                    wordArr[i]   = wordSpl[3];
                    if (!val.validateIfWord(wordArr[i])) //check whether word is a word
                    {
                        errorWord = @"<p>" + errorWord + wordArr[i] + @" is an invalid word in crozzle";
                    }
                    if (!val.validateDirec(direction[i])) //check whether direction is HORIZONTAL or VERTICAL
                    {
                        errorDirec = errorDirec + @"<p>There is a word " + direction[i] + @" cannot be seen as direction. (HORIZONTAL or VERTICAL)</p> ";
                    }
                    if (direction[i] == "HORIZONTAL")
                    {
                        if (val.validateEdgeHor(ver[i], colsNum, hor[i], rowsNum, wordArr[i]) == null)
                        {
                            crozzleDis.addHor(hor[i], ver[i], wordArr[i], wordcha, wordHor); //add word to crozzle array and horizontal array
                        }
                        else
                        {
                            errorEdge = errorEdge + val.validateEdgeHor(ver[i], colsNum, hor[i], rowsNum, wordArr[i]);
                        }
                    }
                    if (direction[i] == "VERTICAL")
                    {
                        if (val.validateEdgeVer(hor[i], rowsNum, ver[i], colsNum, wordArr[i]) == null)
                        {
                            crozzleDis.addVer(hor[i], ver[i], wordArr[i], wordcha, wordVer);
                        }
                        else
                        {
                            errorEdge = errorEdge + val.validateEdgeVer(hor[i], rowsNum, ver[i], colsNum, wordArr[i]);
                        }
                    }
                    i++;
                }
                Configuration config      = new Configuration();
                ValidCro      valCro      = new ValidCro();
                int[]         insert      = new int[lineNum];
                string        errorInsert = null;
                string        errorGroup  = null;
                char[,] insertArr = new char[rowsNum, colsNum];
                config.storeInsert(wordHor, wordVer, rowsNum, colsNum, insertArr);
                //check different level constraints
                for (int m = 0; m < wordArr.Length; m++)
                {
                    if (direction[m] == "HORIZONTAL")
                    {
                        if (val.validateEdgeHor(ver[m], colsNum, hor[m], rowsNum, wordArr[m]) == null)
                        {
                            insert[m] = valCro.InsertHor(hor[m], ver[m], wordArr[m], insertArr, rowsNum, colsNum);
                            if (level == "EASY")
                            {
                                errorInsert = errorInsert + valCro.insertEasyNum(insert[m], wordArr[m], direction[m]);
                            }
                            if (level == "MEDIUM")
                            {
                                errorInsert = errorInsert + valCro.insertMedNum(insert[m], wordArr[m], direction[m]);
                            }
                            if (level == "HARD")
                            {
                                errorInsert = errorInsert + valCro.insertHardNum(insert[m], wordArr[m], direction[m]);
                            }
                        }
                    }
                    if (direction[m] == "VERTICAL")
                    {
                        if (val.validateEdgeVer(hor[m], rowsNum, ver[m], colsNum, wordArr[m]) == null)
                        {
                            insert[m] = valCro.InsertVer(hor[m], ver[m], wordArr[m], insertArr, rowsNum, colsNum);
                            if (level == "EASY")
                            {
                                errorInsert = errorInsert + valCro.insertEasyNum(insert[m], wordArr[m], direction[m]);
                            }
                            if (level == "MEDIUM")
                            {
                                errorInsert = errorInsert + valCro.insertMedNum(insert[m], wordArr[m], direction[m]);
                            }
                            if (level == "HARD")
                            {
                                errorInsert = errorInsert + valCro.insertHardNum(insert[m], wordArr[m], direction[m]);
                            }
                        }
                    }
                }
                string errorTouch = null;
                if (level == "EASY") //check touch
                {
                    errorTouch = valCro.easyTouchHor(wordHor, rowsNum, colsNum) + valCro.easyTouchVer(wordVer, rowsNum, colsNum);
                }
                if (level == "HARD") //check group
                {
                    errorGroup = valCro.decideGroup(direction, wordArr, hor, ver);
                }

                int    score = config.score(config.decideInsert(wordHor, wordVer, rowsNum, colsNum), chaScore); //caculate score
                string error = errorConfGroup + errConfiSecond + errorConfNum + val.validateWordListNum(wordList) + val.validateLevel(level) + val.validateRowsNum(rowsNum) + val.validateColNum(colsNum) + val.validateWordExist(wordArr, secondLine) + val.validateRepeat(wordArr) + errorTouch + errorGroup + firstError + errorFirstLen + errorActNum + errorInsert + errorWordList + errorDirec + errorEdge + errorWord;

                int finalSco = 0;
                if (error == "<p></p>")
                {
                    finalSco = point * wordArr.Length + score;
                    error    = "No Errors";
                }

                string scoreDis = @"<h1>Score: " + finalSco + @"</h1>";
                string levelDis = @"<h3>Level: " + level + @"</h3>";
                //display
                webBrowsercrozzle.DocumentText = header + crozzleDis.display(rowsNum, colsNum, wordcha) + scoreDis + levelDis + footer;
                webBrowsererror.DocumentText   = header + error + footer;
                //log errors
                LogFile lf = new LogFile();
                lf.Log(error, "error");
            }
        }