Exemple #1
0
 /// <summary>
 /// 更新结果状态
 /// </summary>
 /// <param name="res"></param>
 /// <param name="sudokuNum"></param>
 private void updateResultState(ParamInfo res, int sudokuNum)
 {
     if (res != null)
     {
         int curCompleteNum = Convert.ToInt32(this.txtCompleteNum.Text);
         curCompleteNum++;
         DateTime eTime    = System.DateTime.Now;
         double   costTime = eTime.Subtract(res.StartTime).TotalMilliseconds;
         ShowProcess("当前已完成第【" + res.SudokuNum + "】组数独的所有解计算,耗时:" + costTime + "毫秒");
         if (curCompleteNum == sudokuNum)
         {
             this.labMsg.Text         = "***已完成所有数独的求解***";
             this.btnCalc.Enabled     = true;
             this.btnReadFile.Enabled = true;
             Hashtable resValueht  = res.ResValue;
             String[]  resValueArr = new String[sudokuNum];
             NumHandler.deleteFile("SU_DOKU_AN.txt");
             for (int j = 0; j < sudokuNum; j++)
             {
                 StringBuilder tempBuilder = (StringBuilder)resValueht[(j + 1)];
                 NumHandler.SaveData(tempBuilder.ToString());
             }
         }
         else
         {
             this.txtCompleteNum.Text = curCompleteNum.ToString();
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// 读取资源文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReadFile_Click(object sender, EventArgs e)
        {
            ShowProcess("数据初始化开始...");
            btnCalc.Enabled = false;
            List <String> resData = NumHandler.ReadFile();

            if (resData != null && resData.Count > 0)
            {
                ShowProcess("数据初始化结束,请点击【START】按钮开始数独计算。");
                btnCalc.Enabled = true;
                srcData         = resData;
            }
            else
            {
                btnReadFile.Enabled = true;
            }
        }
Exemple #3
0
        /// <summary>
        /// 计算数独空白单元格值
        /// </summary>
        public ParamInfo calcSudoduData(ParamInfo paramInfo)
        {
            string msg      = "第【" + paramInfo.SudokuNum + "】组数独计算结果为:\r\n\r\n";
            string resValue = null;
            int    resSort  = 1;

            try
            {
                for (int m = 0; m < 3; m++)
                {
                    for (int n = 0; n < 3; n++)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                if (!sudokuData[m][n][i][j].NumFlag)
                                {
                                    int[] curPosition = new int[4] {
                                        m, n, i, j
                                    };
                                    NumCell cellVal = GetCellVal(curPosition);
                                    if (!cellVal.IsHasValue)
                                    {
                                        m = cellVal.CurCell[0];
                                        n = cellVal.CurCell[1];
                                        i = cellVal.CurCell[2];
                                        j = cellVal.CurCell[3];
                                        ClearAfter(cellVal.CurCell);
                                    }
                                    sudokuData[m][n][i][j].NumValue = cellVal.Value;
                                    NumCell lastEmptyCell = allCellValContainer[allCellValContainer.Count - 1];
                                    if (ComparePosition(curPosition, lastEmptyCell.CurCell) && cellValContainer.Count > 0)
                                    {
                                        bool checjRes = checkSudokuResult(sudokuData, paramInfo);
                                        if (!checjRes)
                                        {
                                            throw new Exception();
                                        }
                                        resValue = NumHandler.getResult(sudokuData);
                                        msg      = "[" + paramInfo.SudokuNum + ", " + resSort + "]\r\n";
                                        StringBuilder resValueBuilder = (StringBuilder)paramInfo.ResValue[paramInfo.SudokuNum];
                                        resValueBuilder.Append(msg).Append(resValue);
                                        paramInfo.ResValue[paramInfo.SudokuNum] = resValueBuilder;
                                        //NumHandler.SaveTempData(msg + resValue, paramInfo.SudokuNum.ToString());
                                        NumHandler.ShowProcess(paramInfo.DisplayControl, "已完成第【" + paramInfo.SudokuNum + "】组数独解[" + resSort + "]");
                                        NumCell lastCellVals = cellValContainer[cellValContainer.Count - 1];
                                        m = lastCellVals.CurCell[0];
                                        n = lastCellVals.CurCell[1];
                                        i = lastCellVals.CurCell[2];
                                        j = lastCellVals.CurCell[3];
                                        resSort++;
                                    }
                                }
                            }
                        }
                    }
                }
                resValue = NumHandler.getResult(sudokuData);
            }
            catch (Exception)
            {
                resValue = "[" + paramInfo.SudokuNum + ", 0]";
                //NumHandler.SaveTempData(resValue, paramInfo.SudokuNum.ToString());
                StringBuilder resValueBuilder = (StringBuilder)paramInfo.ResValue[paramInfo.SudokuNum];
                resValueBuilder.Append(resValue).AppendLine();
                paramInfo.ResValue[paramInfo.SudokuNum] = resValueBuilder;
            }
            return(paramInfo);
        }
Exemple #4
0
        private void showResult(int index, int resSort, string resValue, TextBox displayControl)
        {
            string msg = "[" + index + ", " + resSort + "]\r\n\r\n";

            NumHandler.ShowProcess(displayControl, msg + resValue);
        }
Exemple #5
0
        /// <summary>
        /// 开始计算数独
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCalc_Click(object sender, EventArgs e)
        {
            this.labMsg.Text         = "正在计算中...";
            this.txtCompleteNum.Text = "0";
            this.labMsg.Visible      = true;
            this.btnCalc.Enabled     = false;
            this.btnReadFile.Enabled = false;
            if (srcData == null || srcData.Count == 0)
            {
                ShowProcess("获取初始化数据失败!");
                return;
            }
            Hashtable resValueHt = new Hashtable();

            initResValueHt(resValueHt, srcData);
            int sudokuNum = 0;

            for (int i = 0; i < srcData.Count; i++)
            {
                if (string.IsNullOrEmpty(srcData[i]))
                {
                    continue;
                }
                sudokuNum++;
                ParamInfo paramInfo = new ParamInfo();
                paramInfo.ResValue       = resValueHt;
                paramInfo.StartTime      = System.DateTime.Now;
                paramInfo.SudokuNum      = sudokuNum;
                paramInfo.DisplayControl = this.txtShowInfo;
                bool checkRes = NumHandler.checkData(srcData[i]);
                if (!checkRes)
                {
                    string        resValue        = "[" + sudokuNum + ", -1]";
                    StringBuilder resValueBuilder = (StringBuilder)paramInfo.ResValue[paramInfo.SudokuNum];
                    resValueBuilder.Append(resValue).AppendLine();
                    paramInfo.ResValue[paramInfo.SudokuNum] = resValueBuilder;
                    DateTime eTime    = System.DateTime.Now;
                    double   costTime = eTime.Subtract(paramInfo.StartTime).TotalMilliseconds;
                    ShowProcess("当前已完成第【" + sudokuNum + "】组数独的计算,耗时:" + costTime + "毫秒");
                    int curCompleteNum = Convert.ToInt32(this.txtCompleteNum.Text);
                    curCompleteNum++;
                    this.txtCompleteNum.Text = curCompleteNum.ToString();
                    continue;
                }
                string[] numArr = srcData[i].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                ShowProcess("开始计算第【" + sudokuNum + "】组数独值");
                SudokuService sudokuService = new SudokuService();
                sudokuService.initSudokuData(NumHandler.StringToInt(numArr));
                //使用委托实现多线程解决
                DelegateCalcSudoku callMethod = new DelegateCalcSudoku(sudokuService.calcSudoduData);
                if (callMethod == null)
                {
                    MessageBox.Show("系统异常,启动线程传入方法为空!");
                    return;
                }
                callMethod.BeginInvoke(paramInfo, asyResult =>
                {
                    ParamInfo res = callMethod.EndInvoke(asyResult);
                    updateResultState(res, sudokuNum);
                }, null);
            }
        }