Exemplo n.º 1
0
        private void CellDidChange(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewTextBoxCell cell = (DataGridViewTextBoxCell)DGGameBoardView.CurrentCell;

            _board.SetUpTile(cell.RowIndex, cell.ColumnIndex, Convert.ToInt32(cell.Value), false);

            bool        rowSolved             = _board.CheckRow(cell.RowIndex);
            bool        colSolved             = _board.CheckCol(cell.ColumnIndex);
            List <bool> listOfAllRegionStatus = new List <bool> {
            };
            int rowsInRegion = 0;

            switch (_board.Size)
            {
            case 9:
                rowsInRegion = 3;
                break;

            case 6:
                rowsInRegion = 2;
                break;

            default:
                break;
            }
            for (int r = 0; r < (_board.Size); r += rowsInRegion)
            {
                for (int c = 0; c < (_board.Size); c += 3)
                {
                    bool regionSolved = _board.CheckRegion(r, c, rowsInRegion);
                    if (regionSolved)
                    {
                        listOfAllRegionStatus.Add(true);
                    }
                }
            }

            bool regionsComplete = false;

            if (listOfAllRegionStatus.Count == _board.Size)
            {
                regionsComplete = true;
            }

            if (rowSolved && colSolved && regionsComplete)
            {
                SolvedLabel.Text = "SOLVED";
            }
        }
Exemplo n.º 2
0
        private void CellDidChange(object sender, DataGridViewCellEventArgs e)
        {
            //User entered something into a cell, figure out if it is valid, and check if they solved any regions, rows, columns, or the whole thing.
            DataGridViewTextBoxCell cell = (DataGridViewTextBoxCell)DGGameBoardView.CurrentCell;

            try {
                _board.SetUpTile(cell.RowIndex, cell.ColumnIndex, Convert.ToInt32(cell.Value), false);
            } catch {
                //User entered something other than a number. Sudoku != Crossword.
                StatusLabel.Text      = "Error: Please enter a number from 1 - 9";
                StatusLabel.ForeColor = Color.Red;
                StatusLabel.Visible   = true;
                cell.Value            = null;
            }

            bool rowSolved = _board.CheckRow(cell.RowIndex);

            if (rowSolved)
            {
                for (int i = 0; i < _board.Size; i++)
                {
                    cell.OwningRow.Cells[i].Style.BackColor = Color.LightGreen;
                }
            }
            bool colSolved = _board.CheckCol(cell.ColumnIndex);

            if (colSolved)
            {
                for (int i = 0; i < _board.Size; i++)
                {
                    cell.OwningColumn.DefaultCellStyle.BackColor = Color.LightGreen;
                }
            }
            List <bool> listOfAllRegionStatus = new List <bool> {
            };

            for (int r = 0; r < (_board.Size); r += _board.RowsInRegion)
            {
                for (int c = 0; c < (_board.Size); c += _board.ColsInRegion)
                {
                    bool regionSolved = _board.CheckRegion(r, c);

                    if (regionSolved)
                    {
                        listOfAllRegionStatus.Add(true);
                    }
                }
            }

            bool regionsComplete = false;

            if (listOfAllRegionStatus.Count == _board.Size)
            {
                regionsComplete = true;
            }

            if (rowSolved && colSolved && regionsComplete)
            {
                //User did a sudoku.
                StatusLabel.Text      = "Puzzle Solved!";
                StatusLabel.ForeColor = Color.Black;
                StatusLabel.Visible   = true;
            }
        }