/// <summary> /// Metoda řešící sudoku pomocí backtrackingu nevyplněných polí. /// </summary> public bool BruteForceSolving() { GridButton NotSolvedButton = FindEmptyCell(); if (NotSolvedButton == null) { return(true); } if (NotSolvedButton != null) { Sudoku.WhereIWantToFillInNumber = NotSolvedButton; } for (int Number = 1; Number < 10; Number++) { if (Sudoku.CanBeFilledIn(Number, NotSolvedButton, ListOfGridButtonsSolver)) { NotSolvedButton.Text = Number.ToString(); if (BruteForceSolving()) { return(true); } } Sudoku.WhereIWantToFillInNumber = NotSolvedButton; } NotSolvedButton.Text = " "; return(false); }
/// <summary> /// Spustí se při kliknutí na jedno z 81 tlačítek v mřížce Sudoku. Vybarví stejná čísla nebo zobrazí nabídku čísel, která je možno doplnit. /// </summary> /// <param name="sender">Obsahuje data o objektu, který událost vyvolal.</param> /// <param name="e">Obsahuje informace o události.</param> private void Cell_Click(object sender, EventArgs e) { List <GridButton> CallerList; GridButton button = sender as GridButton; if (ListOfGridButtons.Contains(button)) { CallerList = ListOfGridButtons; } else { CallerList = Solver1.ListOfGridButtonsSolver; } HighlightSameNumbers(button, CallerList); ShowNumberMenu(button); }
/// <summary> /// Kontroluje, zda je na vybrané tlačítko možno doplnit právě zkoumané číslo. Zkoumá se pouze základní návaznost na ostatní čísla tj. unikátnost v řádku, sloupci a buňce. /// </summary> /// <param name="TestedNumber">Zkoumané číslo (1-9)</param> /// <param name="ButtonName">Označení buňky, u které se zkoumá dostupnost</param> /// <param name="CallerList">Předává metodě seznam tlačítek buď od řešitele sudoku nebo sudoku hry.</param> /// <returns>True - pokud se číslo nevyskytuje ani ve sloupci, řádku, buňce. False - pokud se číslo vyskytuje alespoň ve sloupci, řádku nebo buňce.</returns> public static bool CanBeFilledIn(int TestedNumber, GridButton ButtonName, List <GridButton> CallerList) { foreach (var GridButton in CallerList) { if ((WhereIWantToFillInNumber.Row == GridButton.Row) && (GridButton.Text == TestedNumber.ToString())) { return(false); } if ((WhereIWantToFillInNumber.Column == GridButton.Column) && (GridButton.Text == TestedNumber.ToString())) { return(false); } if ((WhereIWantToFillInNumber.Cell == GridButton.Cell) && (GridButton.Text == TestedNumber.ToString())) { return(false); } } return(true); }
/// <summary> /// Okno se samotnou hrou Sudoku. Detekuje čísla stisknutá na klávesnici. /// </summary> public Sudoku() { InitializeComponent(); for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { int jd = 0, id = 0; if (i >= 4) { id = 4; } if (i >= 7) { id = 8; } if (j >= 4) { jd = 4; } if (j >= 7) { jd = 8; } string NameOfButtonstring = (i).ToString() + (j).ToString(); GridButton NewGridButton = CreateGridButton(NameOfButtonstring, i, j, 35, j * 35 + jd, i * 35 + id, " "); SudokuGrid[i - 1, j - 1] = NewGridButton; Controls.Add(NewGridButton); SudokuGrid[i - 1, j - 1].Show(); GridButton.ListOfGridButtons.Add(NewGridButton); } } KeyPreview = true; }
/// <summary> /// Metoda pro vytvoření ltačítka třídy GridButton = tlačítko mřížky sudoku. /// </summary> /// <param name="Name">Název tlačítka</param> /// <param name="Row">Řádek tlačítka</param> /// <param name="Column">Sloupec tlačítka</param> /// <param name="Width">Šířka tlačítka</param> /// <param name="Left">Odsazení tlačítka zleva</param> /// <param name="Top">Odsazení tlačítka odshora</param> /// <param name="Solution">Řešení tlačítka</param> /// <returns>Metoda vrací vytvořené tlačítko pro sudoku mřížku.</returns> static public GridButton CreateGridButton(string Name, int Row, int Column, int Width, int Left, int Top, string Solution) { GridButton NewGridButton = new GridButton(Name, Row, Column, Width, Left, Top, Solution); return(NewGridButton); }
/// <summary> /// Po kliknutí na již vyplněné WordTypeForPole zvýrazní zeleně všechna ostatní stejná čísla, která jsou umístěna správně, a červeně zvýrazní čísla, která jsou umístěna špatně vzhledem ke sloupci, řádku nebo buňce. /// </summary> /// <param name="ButtonName">Pole, vzhledem ke kterému se vybarvování provádí.</param> /// <param name="ListOfWhichCallerVariable">Seznam tlačítek převzaný od volajícího funkce.</param> public void HighlightSameNumbers(GridButton ButtonName, List <GridButton> ListOfWhichCallerVariable) { List <GridButton> ListOfWhichCaller = ListOfWhichCallerVariable; MainMenu.AlreadyLoaded = false; List <GridButton> ListOfCellsOfTheSameNumber = new List <GridButton>(); Sudoku.WhereIWantToFillInNumber = ButtonName; foreach (var GridButton in ListOfWhichCaller) { if (GridButton.ForeColor == Color.Black || GridButton.ForeColor == Color.DarkGray) { GridButton.BackColor = Color.WhiteSmoke; } else { GridButton.BackColor = Color.White; } } if (Sudoku.HighlightSameNumbersCheckBox != null) { if (Sudoku.HighlightSameNumbersCheckBox.Checked == true) { bool IsInConflict = false; ButtonName.BackColor = Color.LightGreen; foreach (var GridButton in ListOfWhichCaller) { if (GridButton.Text != " ") { if (GridButton.Text == ButtonName.Text) { ListOfCellsOfTheSameNumber.Add(GridButton); GridButton.BackColor = Color.LightGreen; } } } foreach (var Button1 in ListOfCellsOfTheSameNumber) { IsInConflict = false; foreach (var Button2 in ListOfCellsOfTheSameNumber) { if ((Button1.Row == Button2.Row) || (Button1.Column == Button2.Column)) { if ((Button1.Row == Button2.Row) && (Button1.Column == Button2.Column)) { if (!IsInConflict) { Button1.BackColor = Color.LightGreen; } else { Button1.BackColor = Color.Red; } } else { Button1.BackColor = Color.Red; Button2.BackColor = Color.Red; IsInConflict = true; } } else if (IsInTheSameCell(Button1.Cell, Button2.Cell)) { Button1.BackColor = Color.Red; Button2.BackColor = Color.Red; IsInConflict = true; } } } } } else { bool IsInConflict = false; ButtonName.BackColor = Color.LightGreen; foreach (var GridButton in ListOfWhichCaller) { if (GridButton.Text != " ") { if (GridButton.Text == ButtonName.Text) { ListOfCellsOfTheSameNumber.Add(GridButton); GridButton.BackColor = Color.LightGreen; } } } foreach (var Button1 in ListOfCellsOfTheSameNumber) { IsInConflict = false; foreach (var Button2 in ListOfCellsOfTheSameNumber) { if ((Button1.Row == Button2.Row) || (Button1.Column == Button2.Column)) { if ((Button1.Row == Button2.Row) && (Button1.Column == Button2.Column)) { if (!IsInConflict) { Button1.BackColor = Color.LightGreen; } else { Button1.BackColor = Color.Red; } } else { Button1.BackColor = Color.Red; Button2.BackColor = Color.Red; IsInConflict = true; } } else if (IsInTheSameCell(Button1.Cell, Button2.Cell)) { Button1.BackColor = Color.Red; Button2.BackColor = Color.Red; IsInConflict = true; } } } } }
/// <summary> /// Zobrazí novou nabídku pro doplnění čísel po kliknutí na prázdné pole. Pokud má uživatel zaškrtnuto políčko DoNotHighlightDumbNumbersCheckBox, metoda vyřadí čísla, která zjevně nemá podbarvovat. /// </summary> /// <param name="ButtonName">Tlačítko, na které bylo kliknuto.</param> public void ShowNumberMenu(GridButton ButtonName) { MainMenu.AlreadyLoaded = false; foreach (Button button in Sudoku.ListOfNumbers) { MainMenu.Sudoku.Controls.Remove(button); } Sudoku.ListOfNumbers.Clear(); //Graphics g = CreateGraphics(); // SolidBrush ppap = new SolidBrush(Color.White); //g.FillRectangle(ppap, new Rectangle(400, 140, 160, 160)); for (int i = 1; i < 10; i++) { if (ButtonName.ForeColor == Color.MidnightBlue) { int Row; if (i < 4) { Row = 1; } else if (i > 3 && i < 7) { Row = 2; } else { Row = 3; } int Column; if (36 % i == 0 && i != 4 && i != 1 && i != 2) { Column = 3; } else if (40 % i == 0 && i != 4 && i != 1) { Column = 2; } else { Column = 1; } if (Sudoku.DoHighlightGoodNumbersCheckBox.Checked == true && Sudoku.CanBeFilledIn(i, ButtonName, ListOfGridButtons)) { Sudoku.CreateNewButton(i.ToString(), 360 + Column * 50, 100 + Row * 50, Color.LightGreen); } else { Sudoku.CreateNewButton(i.ToString(), 360 + Column * 50, 100 + Row * 50, Color.LightSlateGray); } Sudoku.CreateNewButton("Vymaž", 420, 300, Color.LightSlateGray); Sudoku.CreateNewButton("Napověz", 420, 350, Color.LimeGreen); } } foreach (Button button in Sudoku.ListOfNumbers) { MainMenu.Sudoku.Controls.Add(button); } //ppap.Dispose(); //g.Dispose(); }
/// <summary> /// Konstruktor formuláře Solver1. Inicializuje tlačítka. /// </summary> public Solver1() { ListOfGridButtonsSolver.Clear(); InitializeComponent(); this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Solver1_KeyPress); for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { int jd = 0, id = 0; if (i >= 4) { id = 4; } if (i >= 7) { id = 8; } if (j >= 4) { jd = 4; } if (j >= 7) { jd = 8; } string NameOfButtonstring = (i).ToString() + (j).ToString(); GridButton NewGridButton = Sudoku.CreateGridButton(NameOfButtonstring, i, j, 35, j * 35 + jd, i * 35 + id, " "); SolverGrid[i - 1, j - 1] = NewGridButton; SolverGrid[i - 1, j - 1].Text = " "; SolverGrid[i - 1, j - 1].ForeColor = Color.DarkGray; SolverGrid[i - 1, j - 1].Solved = false; Controls.Add(NewGridButton); SolverGrid[i - 1, j - 1].Show(); ListOfGridButtonsSolver.Add(SolverGrid[i - 1, j - 1]); } } KeyPreview = true; ListOfNotSolvedButtons = new List <GridButton>(ListOfGridButtonsSolver); /* SolverGrid[0, 0].Text = " "; * SolverGrid[0, 1].Text = " "; * SolverGrid[0, 2].Text = " "; * SolverGrid[0, 3].Text = " "; * SolverGrid[0, 4].Text = "4"; * SolverGrid[0, 5].Text = " "; * SolverGrid[0, 6].Text = " "; * SolverGrid[0, 7].Text = " "; * SolverGrid[0, 8].Text = "9"; * SolverGrid[1, 0].Text = " "; * SolverGrid[1, 1].Text = " "; * SolverGrid[1, 2].Text = "2"; * SolverGrid[1, 3].Text = " "; * SolverGrid[1, 4].Text = "1"; * SolverGrid[1, 5].Text = " "; * SolverGrid[1, 6].Text = " "; * SolverGrid[1, 7].Text = " "; * SolverGrid[1, 8].Text = " "; * SolverGrid[2, 0].Text = "5"; * SolverGrid[2, 1].Text = " "; * SolverGrid[2, 2].Text = " "; * SolverGrid[2, 3].Text = " "; * SolverGrid[2, 4].Text = " "; * SolverGrid[2, 5].Text = " "; * SolverGrid[2, 6].Text = " "; * SolverGrid[2, 7].Text = "7"; * SolverGrid[2, 8].Text = "3"; * SolverGrid[3, 0].Text = " "; * SolverGrid[3, 1].Text = "9"; * SolverGrid[3, 2].Text = " "; * SolverGrid[3, 3].Text = " "; * SolverGrid[3, 4].Text = " "; * SolverGrid[3, 5].Text = " "; * SolverGrid[3, 6].Text = " "; * SolverGrid[3, 7].Text = " "; * SolverGrid[3, 8].Text = " "; * SolverGrid[4, 0].Text = " "; * SolverGrid[4, 1].Text = " "; * SolverGrid[4, 2].Text = "4"; * SolverGrid[4, 3].Text = " "; * SolverGrid[4, 4].Text = " "; * SolverGrid[4, 5].Text = " "; * SolverGrid[4, 6].Text = "1"; * SolverGrid[4, 7].Text = " "; * SolverGrid[4, 8].Text = " "; * SolverGrid[5, 0].Text = " "; * * SolverGrid[5, 1].Text = " "; * SolverGrid[5, 2].Text = " "; * SolverGrid[5, 3].Text = "5"; * SolverGrid[5, 4].Text = " "; * SolverGrid[5, 5].Text = "7"; * SolverGrid[5, 6].Text = " "; * SolverGrid[5, 7].Text = " "; * SolverGrid[5, 8].Text = " "; * SolverGrid[6, 0].Text = " "; * SolverGrid[6, 1].Text = " "; * SolverGrid[6, 2].Text = "1"; * SolverGrid[6, 3].Text = " "; * SolverGrid[6, 4].Text = "2"; * SolverGrid[6, 5].Text = " "; * SolverGrid[6, 6].Text = " "; * SolverGrid[6, 7].Text = " "; * SolverGrid[6, 8].Text = " "; * SolverGrid[7, 0].Text = " "; * SolverGrid[7, 1].Text = " "; * SolverGrid[7, 2].Text = " "; * SolverGrid[7, 3].Text = " "; * SolverGrid[7, 4].Text = " "; * SolverGrid[7, 5].Text = "3"; * SolverGrid[7, 6].Text = " "; * SolverGrid[7, 7].Text = "8"; * SolverGrid[7, 8].Text = "5"; * SolverGrid[8, 0].Text = " "; * SolverGrid[8, 1].Text = " "; * SolverGrid[8, 2].Text = " "; * SolverGrid[8, 3].Text = " "; * SolverGrid[8, 4].Text = " "; * SolverGrid[8, 5].Text = " "; * SolverGrid[8, 6].Text = " "; * SolverGrid[8, 7].Text = " "; * SolverGrid[8, 8].Text = " "; * Sudoku.FilledInFields++; * .Solved = true; * ListOfNotSolvedButtons.Remove(); */ }