private bool IsMoveValid() { foreach (var button in _buttons.Where(button => SudokuPuzzle.GetColumn(button) == SudokuPuzzle.GetColumn(SelectedButton))) { if (SudokuPuzzle.GetValue(button) == SelectedValue) { return(false); } } foreach (var button in _buttons.Where(button => SudokuPuzzle.GetRow(button) == SudokuPuzzle.GetRow(SelectedButton))) { if (SudokuPuzzle.GetValue(button) == SelectedValue) { return(false); } } foreach (var button in _buttons.Where(button => SudokuPuzzle.GetRegion(button) == SudokuPuzzle.GetRegion(SelectedButton))) { if (SudokuPuzzle.GetValue(button) == SelectedValue) { return(false); } } TextBlock1.Text = string.Empty; return(true); }
private void Cell_Click(object sender, RoutedEventArgs e) { if (!(sender is MenuItem mi)) { return; } if (!(mi.CommandParameter is ContextMenu cm)) { return; } if (!(cm.PlacementTarget is Button button)) { return; } if (mi.Header.ToString() == @"Erase") { button.Content = string.Empty; return; } SelectedButton = button; SelectedValue = int.Parse(mi.Header.ToString().Substring(5)); var col = SudokuPuzzle.GetColumn(button); var row = SudokuPuzzle.GetRow(button); if (GameState == string.Empty) { GameState = LoadGameFromDisk(); } if (GameState.Length != 81) { throw new Exception("Input string format was not valid!"); } var oldString = GameState; Trace.WriteLine($"GameState = {oldString}"); var sb = new StringBuilder(oldString); var startPosition = GetPositionInFileContents(col, row); sb.Remove(startPosition, 1); sb.Insert(startPosition, SelectedValue); var newString = sb.ToString(); GameState = newString; Trace.WriteLine($"GameState = {newString}"); OnPropertyRaised(GameState); if (IsMoveValid()) { button.Content = SelectedValue; SetCandidates(ref button); SetAsUserCell(ref button); LoadSavedGame(true); } else { ShowMessage("Invalid move!"); } }