예제 #1
0
        /// <summary>
        /// Попытка решить судоку
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuActionSolveSudoku_Click(object sender, EventArgs e)
        {
            int[,] board = new int[9, 9];
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    board[i, j] = cells[i, j].CellText;
                }
            }
            SolveClass solveClass = new SolveClass(board, this);

            board = solveClass.SolveSudoku();

            if (board != null)
            {
                ArrayList[,] passibleDigits = solveClass.PossibleDigits;
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        if (cells[i, j].CellType != CellType.closed)
                        {
                            if (board[i, j] != 0)
                            {
                                cells[i, j].CellText = board[i, j];
                            }
                            else
                            {
                                for (int q = 0; q < 3; q++)
                                {
                                    for (int r = 0; r < 2; r++)
                                    {
                                        if (passibleDigits[i, j].Count > q * 2 + r)
                                        {
                                            cells[i, j].SetSmartLabel(q, r, (int)passibleDigits[i, j][q * 2 + r]);
                                        }
                                        else
                                        {
                                            r = 2;
                                            q = 3;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                historyClass = new HistoryClass();
            }
        }
예제 #2
0
        /// <summary>
        /// Загрузка выбранного уровня
        /// </summary>
        private void LoadLevel()
        {
            string fileText;

            string[] cellsInfo;
            using (StreamReader sr = new StreamReader(pathToLevel))
            {
                fileText = sr.ReadToEnd();
            }

            string[] timeInfo = fileText.Split(new char[1] {
                ':'
            });
            int millisec = Convert.ToInt32(timeInfo[1]);
            int hour     = millisec / (1000 * 60 * 60);

            millisec %= (1000 * 60 * 60);
            int minute = millisec / (1000 * 60);

            millisec %= (1000 * 60);
            int second = millisec / 1000;

            millisec %= 1000;
            gameTime  = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, minute, second, millisec);

            cellsInfo = timeInfo[0].Split(new char[1] {
                ';'
            });
            foreach (string cell in cellsInfo)
            {
                int x    = Convert.ToInt32(cell.Substring(0, 1));
                int y    = Convert.ToInt32(cell.Substring(1, 1));
                int fix  = Convert.ToInt32(cell.Substring(2, 1));
                int text = Convert.ToInt32(cell.Substring(3, 1));

                CellType newType = CellType.clear;
                if (fix == 0)
                {
                    newType = CellType.closed;
                }
                else if (text != 0)
                {
                    newType = CellType.value;
                }
                else
                {
                    int smartLabel = 0;
                    for (int q = 4; q < 10; q++)
                    {
                        smartLabel += Convert.ToInt32(cell.Substring(q, 1));
                    }
                    if (smartLabel > 0)
                    {
                        newType = CellType.smartLabel;
                    }
                }
                cells[x, y] = new Cell(this, x, y, newType, text);


                //
                // Получение информации о дополнительных метках для тех ячеек,
                // которые могут изменяться и значение которых не определено
                //
                if (newType == CellType.smartLabel)
                {
                    for (int q = 4; q < 10; q++)
                    {
                        text = Convert.ToInt32(cell.Substring(q, 1));
                        if (text != 0)
                        {
                            cells[x, y].SetSmartLabel((q - 4) / 2, (q - 4) % 2, text);
                        }
                    }
                }
            }

            panelHide.SendToBack();
            buttonClear.Visible = false;
            isPositionSave      = true;

            if (timeInfo.Length < 3 || timeInfo[2] == "0")
            {
                labelInfo.Text      = "Правила игры:\r\nНеобходимо расставить цифры от 1 до 9 в ячейках таким образом, чтобы в каждой строке, в каждом столбце и в каждом выделенном квадрате все цифры были различны.";
                buttonPause.Visible = labelTime.Visible = labelTimeInfo.Visible = true;
                buttonPause.Text    = "Пауза";
                typeAction          = TypeAction.game;
                timerTime.Enabled   = true;
                currentCell.X       = currentCell.Y = 0;
                cells[currentCell.X, currentCell.Y].SelectCell();
                historyClass = new HistoryClass();
            }
            else
            {
                labelInfo.Text      = "Данный уровень пройден\r\n\r\nВремя прохождения уровня: " + DateTimeToString(gameTime) + ".";
                buttonPause.Visible = labelTime.Visible = labelTimeInfo.Visible = false;
                timerTime.Enabled   = false;
                typeAction          = TypeAction.none;
            }
        }