コード例 #1
0
 private void CopyGrid(frm_Sudoku_TE trialSudoku)
 {
     // Enter the grid as we know it
     for (int j = 0; j < this.dgv_Sudoku.Rows.Count; j++)
     {
         for (int i = 0; i < this.dgv_Sudoku.Columns.Count; i++)
         {
             trialSudoku.dgv_Sudoku[i, j].Value = this.dgv_Sudoku[i, j].Value;
         }
     }
 }
コード例 #2
0
        private bool TrialAndError()
        {
            this.dgv_Sudoku.Refresh();

            // Create a new instance of the Form2 class
            frm_Sudoku_TE trialSudoku = new frm_Sudoku_TE();

            // Show the form
            trialSudoku.Show();
            trialSudoku.Left = this.Left + this.Width + 100;
            trialSudoku.Top  = this.Top;

            // I couldn't get this done in design view of the other form for some reason
            trialSudoku.dgv_Sudoku.Enabled = false;

            string cellValue = "";

            // Find an unknown entry in this grid
            for (int j = 0; j < this.dgv_Sudoku.Rows.Count; j++)
            {
                for (int i = 0; i < this.dgv_Sudoku.Columns.Count; i++)
                {
                    // Store the cell value
                    cellValue = this.dgv_Sudoku[i, j].Value.ToString();

                    // Check we have an unknown
                    if (cellValue.Length > 1)
                    {
                        // Loop through each character in the string
                        for (int x = 0; x < cellValue.Length; x++)
                        {
                            // Copy the grid into the new form
                            CopyGrid(trialSudoku);

                            // Store character (i.e. potential value for the cell)
                            string cellChar = cellValue.Substring(x, 1);

                            // Assume possible value is correct value
                            trialSudoku.dgv_Sudoku[i, j].Value = cellChar;
                            trialSudoku.dgv_Sudoku[i, j].Style.ApplyStyle(styleTest);

                            // Try and solve
                            trialSudoku.codeRunning = true;
                            trialSudoku.SolveGrid();
                            trialSudoku.codeRunning = false;

                            // Check if the grid is complete
                            bool checkGrid = trialSudoku.CheckGrid();

                            ShowReviewMessage_Trial(i, j, cellValue, cellChar, checkGrid);

                            if (checkGrid)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }