private bool SolvedCorrect()
 {
     for (int row = 0; row < grid.CellsInRow; row++)
     {
         for (int column = 0; column < grid.CellsInRow; column++)
         {
             int         cellControlId = (1000 * (row + 1)) + column;
             CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
             if ((cntlFoc.CellValue != cntlFoc.SolutionValue) && cntlFoc.editable)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
 private bool GridIsComplete()
 {
     for (int row = 0; row < grid.CellsInRow; row++)
     {
         for (int column = 0; column < grid.CellsInRow; column++)
         {
             int         cellControlId = (1000 * (row + 1)) + column;
             CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
             if (cntlFoc.CellValue == 0)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
예제 #3
0
 private void ResetGame()
 {
     for (int row = 0; row < grid.CellsInRow; row++)
     {
         for (int column = 0; column < grid.CellsInRow; column++)
         {
             int         cellControlId = (1000 * (row + 1)) + column;
             CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
             if (cntlFoc.editable)
             {
                 cntlFoc.CellValue       = 0;
                 grid.cells[row, column] = 0;
             }
         }
     }
     ResetCandidates();
 }
예제 #4
0
        private void ClearGrid()
        {
            grid.Reset();
            StopTimer();
            gameRunning = false;

            for (int row = 0; row < grid.CellsInRow; row++)
            {
                for (int column = 0; column < grid.CellsInRow; column++)
                {
                    int         cellControlId = (1000 * (row + 1)) + column;
                    CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
                    cntlFoc.editable          = true;
                    cntlFoc.CellValue         = 0;
                    cntlFoc.SolutionValue     = 0;
                    cntlFoc.M_dwDisabledColor = 0xFF000000;
                }
            }
        }
예제 #5
0
 private void ResetCandidates()
 {
     for (int row = 0; row < grid.CellsInRow; row++)
     {
         for (int column = 0; column < grid.CellsInRow; column++)
         {
             int         cellControlId = (1000 * (row + 1)) + column;
             CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
             cntlFoc.ClearCandidates();
             //if (cntlFoc.CellValue == 0)
             {
                 for (int i = 1; i <= 9; i++)
                 {
                     cntlFoc.SetCandidate(i);
                 }
                 cntlFoc.ShowCandidates = _Settings.ShowCandidates;
             }
         }
     }
     CheckCandidates();
 }
예제 #6
0
        public override void OnAction(Action action)
        {
            if (action.wID == Action.ActionType.ACTION_SELECT_ITEM || action.wID == Action.ActionType.ACTION_MOUSE_CLICK)
            {
                int controlId = GetFocusControlId();
                if (controlId >= 1000 && controlId <= 9008)
                {
                    // Show dialog
                    CellControl cntlFoc = (CellControl)GetControl(controlId);
                    int         row     = (controlId / 1000) - 1;
                    int         column  = controlId % 1000;

                    if (cntlFoc.editable)
                    {
                        GUIDialogMenu dlg = (GUIDialogMenu)GUIWindowManager.GetWindow((int)Window.WINDOW_DIALOG_MENU);
                        if (dlg != null)
                        {
                            dlg.Reset();
                            dlg.SetHeading(GUILocalizeStrings.Get(19116)); // Cell value

                            for (int index = 1; index < 10; index++)
                            {
                                dlg.Add("");
                            }
                            dlg.Add(GUILocalizeStrings.Get(19117)); // Clear cell
                            dlg.SelectedLabel = cntlFoc.CellValue - 1;
                            dlg.DoModal(GetWindowId());
                            if (dlg.SelectedLabel < 0)
                            {
                                return;
                            }
                            else
                            {
                                if (dlg.SelectedId == 10)
                                {
                                    cntlFoc.CellValue       = 0;
                                    grid.cells[row, column] = 0;
                                }
                                else
                                {
                                    if (!_Settings.Block || cntlFoc.SolutionValue == dlg.SelectedId)
                                    {
                                        cntlFoc.CellValue       = dlg.SelectedId;
                                        grid.cells[row, column] = dlg.SelectedId;

                                        if (this.GridIsComplete())
                                        {
                                            this.Result();
                                        }
                                    }
                                }
                            }
                        }
                        CheckCandidates();
                    }
                }
            }
            else if (action.wID == Action.ActionType.ACTION_KEY_PRESSED ||
                     (action.wID >= Action.ActionType.REMOTE_0 && action.wID <= Action.ActionType.REMOTE_9))
            {
                int controlId = GetFocusControlId();
                if (controlId >= 1000 && controlId <= 9008)
                {
                    CellControl cntlFoc = (CellControl)GetControl(controlId);
                    int         row     = (controlId / 1000) - 1;
                    int         column  = controlId % 1000;

                    if (cntlFoc != null)
                    {
                        if (action.wID == Action.ActionType.ACTION_KEY_PRESSED)
                        {
                            if (action.m_key.KeyChar == 8)
                            {
                                cntlFoc.CellValue       = 0;
                                grid.cells[row, column] = 0;
                            }
                            else if (action.m_key.KeyChar == 35) // #
                            {
                                _nextNumberIsToggle = true;
                            }
                            else if (action.m_key.KeyChar == 42) // *
                            {
                                _nextNumberIsFilter = true;
                            }
                        }
                        else if (action.wID >= Action.ActionType.REMOTE_0 && action.wID <= Action.ActionType.REMOTE_9)
                        {
                            int value = (action.wID - Action.ActionType.REMOTE_0);

                            if (!_nextNumberIsToggle && !_nextNumberIsFilter)
                            {
                                if (value == 0 || !_Settings.Block || cntlFoc.SolutionValue == value)
                                {
                                    cntlFoc.CellValue       = value;
                                    grid.cells[row, column] = value;

                                    if (this.GridIsComplete())
                                    {
                                        this.Result();
                                    }
                                }
                            }
                            else if (_nextNumberIsToggle)
                            {
                                if (value > 0)
                                {
                                    if (cntlFoc.IsCandidate(value))
                                    {
                                        cntlFoc.RemoveCandidate(value);
                                    }
                                    else
                                    {
                                        cntlFoc.SetCandidate(value);
                                    }
                                }
                                _nextNumberIsToggle = false;
                            }
                            else if (_nextNumberIsFilter)
                            {
                                _Settings.Filter         = value;
                                _Settings.ShowCandidates = true;
                                _nextNumberIsFilter      = false;
                            }
                        }
                        CheckCandidates();
                    }
                }
            }
            else if (action.wID == Action.ActionType.ACTION_PREVIOUS_MENU)
            {
                StopTimer();
            }
            else if (action.wID == Action.ActionType.ACTION_CONTEXT_MENU)
            {
                ShowContextMenu();
            }
            else if (action.wID == Action.ActionType.ACTION_PREVIOUS_MENU)
            {
                StopTimer();
            }
            base.OnAction(action);
        }
예제 #7
0
        protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
        {
            if (control == btnSolve)
            {
                // Solve grid
                Grid solution = Solver.Solve(grid);
                if (solution != null)
                {
                    for (int row = 0; row < grid.CellsInRow; row++)
                    {
                        for (int column = 0; column < grid.CellsInRow; column++)
                        {
                            int         cellControlId = (1000 * (row + 1)) + column;
                            CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
                            if (cntlFoc.editable)
                            {
                                cntlFoc.CellValue         = solution.cells[row, column];
                                cntlFoc.M_dwDisabledColor = m_dwCellIncorrectTextColor;
                                cntlFoc.editable          = false;
                            }
                        }
                    }
                }
            }
            else if (control == btnNewGame)
            {
                //new game
                GUIWaitCursor.Show();
                int minrating = 0;
                int maxrating = 0;
                ClearGrid();
                Grid puzzle = new Grid();
                switch ((LevelName)_Settings.Level)
                {
                case LevelName.Kids:
                    minrating = 550;
                    maxrating = 999;
                    break;

                case LevelName.Easy:
                    minrating = 450;
                    maxrating = 650;
                    break;

                case LevelName.Medium:
                    minrating = 250;
                    maxrating = 550;
                    break;

                case LevelName.Hard:
                    minrating = 0;
                    maxrating = 250;
                    break;
                }
                puzzle     = GenerateLevel(puzzle, minrating, maxrating);
                gameRating = Solver.Rate(puzzle);
                //puzzle = Solver.Generate(3);
                Grid solution = Solver.Solve(puzzle);
                if ((LevelName)_Settings.Level == LevelName.Easy)
                {
                    puzzle     = Solver.FillOutCells(puzzle, solution, 10);
                    gameRating = Solver.Rate(puzzle);
                }
                else if ((LevelName)_Settings.Level == LevelName.Kids)
                {
                    puzzle     = Solver.FillOutCells(puzzle, solution, 20);
                    gameRating = Solver.Rate(puzzle) * 2;
                }

                for (int row = 0; row < grid.CellsInRow; row++)
                {
                    for (int column = 0; column < grid.CellsInRow; column++)
                    {
                        int         cellControlId = (1000 * (row + 1)) + column;
                        CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
                        cntlFoc.CellValue = puzzle.cells[row, column];
                        if (cntlFoc.CellValue > 0)
                        {
                            cntlFoc.editable = false;
                        }
                        else
                        {
                            cntlFoc.SolutionValue = solution.cells[row, column];
                        }
                    }
                }
                grid = puzzle;
                ResetCandidates();
                GUIWaitCursor.Hide();
                StartTimer();
                gameRunning = true;
                if (_Settings.Show || _Settings.Block)
                {
                    isScoreGame = false;
                }
                else
                {
                    isScoreGame = true;
                }
            }
            else if (control == btnBlockInvalidMoves)
            {
                _Settings.Block = btnBlockInvalidMoves.Selected;
                if (btnBlockInvalidMoves.Selected)
                {
                    if (btnShowInvalidMoves.Selected)
                    {
                        _Settings.Show = btnShowInvalidMoves.Selected = false;
                    }
                    isScoreGame = false;
                }
                _Settings.Save();
            }
            else if (control == btnClear)
            {
                ClearGrid();
                ResetCandidates();
            }
            else if (control == btnShowInvalidMoves)
            {
                _Settings.Show = btnShowInvalidMoves.Selected;
                if (btnShowInvalidMoves.Selected)
                {
                    if (btnBlockInvalidMoves.Selected)
                    {
                        _Settings.Block = btnBlockInvalidMoves.Selected = false;
                    }
                    isScoreGame = false;
                }
                ShowInvalid();
                _Settings.Save();
            }
            else if (control == btnLevel)
            {
                switch ((LevelName)_Settings.Level)
                {
                case LevelName.Kids:
                    _Settings.Level = (int)LevelName.Easy;
                    break;

                case LevelName.Easy:
                    _Settings.Level = (int)LevelName.Medium;
                    break;

                case LevelName.Medium:
                    _Settings.Level = (int)LevelName.Hard;
                    break;

                case LevelName.Hard:
                    _Settings.Level = (int)LevelName.Kids;
                    break;
                }
                UpdateButtonStates();
                _Settings.Save();
            }
            else if (control == btnHelpOnce)
            {
                int candidateIndex = random.Next(81 - grid.CountFilledCells());
                int m = -1, row = 0, column = 0;
                isScoreGame = false;

                for (row = 0; row < 9 && m < candidateIndex; row++)
                {
                    for (column = 0; column < 9 && m < candidateIndex; column++)
                    {
                        int         cellControlId = (1000 * (row + 1)) + column;
                        CellControl cntlFoc       = (CellControl)GetControl(cellControlId);
                        if (cntlFoc.editable == true && cntlFoc.CellValue == 0)
                        {
                            m++;
                            if (m == candidateIndex)
                            {
                                cntlFoc.CellValue       = cntlFoc.SolutionValue;
                                grid.cells[row, column] = cntlFoc.SolutionValue;
                            }
                        }
                    }
                    CheckCandidates();
                }
            }
            else if (control == btnResetGame)
            {
                ResetGame();
            }
            base.OnClicked(controlId, control, actionType);
        }