Example #1
0
        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);
        }
Example #2
0
        private void SetCandidates(ref Button button)
        {
            var candidates = SudokuPuzzle.GetCandidates(button);

            candidates = candidates.Replace(SelectedValue.ToString(), " ");
            SudokuPuzzle.SetCandidates(button, candidates);
            button.Content = Tools.FormatCandidates(candidates);
        }
Example #3
0
        private void LoadSavedGame(bool isReload = false)
        {
            // LoadGameFromDisk loads the values for each cell from a filed saved on disk.
            if (GameState == string.Empty)
            {
                GameState = Tools.RemoveNewLines(LoadGameFromDisk());
                Trace.WriteLine(GameState);
                if (GameState.Length != 81)
                {
                    throw new Exception(@"Missing file or incorrect file format.");
                }
            }

            // This reads in the file contents.
            var counter = 0;

            foreach (var row in Enumerable.Range(1, 9))
            {
                foreach (var col in Enumerable.Range(1, 9))
                {
                    var value  = int.Parse(GameState[counter++].ToString());
                    var button = (Button)FindName($"Cr{col}{row}");
                    if (button == null)
                    {
                        throw new Exception("Button control not found.");
                    }

                    if (!isReload && value > 0)
                    {
                        SudokuPuzzle.SetValue(button, value);
                        SudokuPuzzle.SetIsGiven(button, true);
                        button.Content   = value.ToString();
                        button.IsEnabled = false;
                        SetAsGivenCell(ref button);
                    }
                    else if (!isReload && value == 0)
                    {
                        SudokuPuzzle.SetValue(button, value);
                        SudokuPuzzle.SetIsGiven(button, false);
                        button.ContextMenu = FindResource("ContextMenu1") as ContextMenu;
                        SetCandidates(ref button);
                        SetAsMarkupCell(ref button);
                    }
                    else if (isReload && value > 0 && button.IsEnabled)
                    {
                        button.Content = value.ToString();
                        SetAsUserCell(ref button);
                        SetCandidates(ref button);
                    }
                }
            }
        }
Example #4
0
        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!");
            }
        }