Esempio n. 1
0
        /// <summary>
        /// Does basic initialization of the soduko puzzle.
        /// </summary>
        /// <param name="colsAndRows">Represents the columns as well as the rows.</param>
        /// <param name="canvas">The height and the width must be equal.</param>
        public SodukoPuzzle(int colsAndRows, Canvas canvas, HintMode hintMode)
        {
            _hintMode     = hintMode;
            _showSelfHint = true;

            if (canvas.Height != canvas.Width)
            {
                throw new ArgumentException("Canvas height must equal width!");
            }
            if (colsAndRows == 0)
            {
                throw new ArgumentNullException("colsAndRows");
            }

            _canvas = canvas;
            _rows   = colsAndRows;

            _sodukoPieces  = new SodukoPiece[_rows, _rows];
            _currentValues = new int[_rows, _rows];
            _startingState = new int[_rows, _rows];

            _blankState = new int[_rows, _rows];
            for (int row = 0; row < _rows; ++row)
            {
                for (int col = 0; col < _rows; ++col)
                {
                    // So the values are rendered as undef.
                    _blankState[row, col] = -1;
                }
            }
        }
Esempio n. 2
0
        public void SetHintMode(HintMode newHintMode, SodukoPiece selectedPiece)
        {
            if (_hintMode == HintMode.Everything && newHintMode != HintMode.Everything)
            {
                ClearAllSuggestionText();
            }

            if (selectedPiece != null && _hintMode == HintMode.Adjacent && newHintMode != HintMode.Adjacent)
            {
                ClearNeighborsSuggestionText(selectedPiece);
            }

            if (newHintMode == HintMode.Everything && _hintMode != HintMode.Everything)
            {
                _hintMode = newHintMode;
                SetSuggestions(0, 0);
                return;
            }
            else if (newHintMode == HintMode.Adjacent && _hintMode != HintMode.Adjacent)
            {
                _hintMode = newHintMode;
                if (selectedPiece != null)
                {
                    SetSuggestions(selectedPiece);
                }
                return;
            }
            else if (newHintMode == HintMode.Off && selectedPiece != null)
            {
                selectedPiece.SetSuggestionTextToNothing();
            }


            _hintMode = newHintMode;
        }
Esempio n. 3
0
        public static void SavePuzzleState(SodukoPiece[,] p,int[,] originalState, int[,] currentPositions)
        {
            var state = new ApplicationDataCompositeValue();
            StringBuilder sb = new StringBuilder();
            foreach (SodukoPiece piece in p)
            {
                sb.Append(piece.GetInfoAsString());
            }

            state["Pieces"] = sb.ToString();

            sb.Clear();
            foreach (int i in originalState)
            {
                sb.Append(i.ToString());
                sb.Append(";");
            }

            state["OriginalState"] = sb.ToString();

            sb.Clear();
            foreach (int i in currentPositions)
            {
                sb.Append(i.ToString());
                sb.Append(";");
            }

            state["CurrentPos"] = sb.ToString();


            ApplicationData.Current.LocalSettings.Values["PuzzleState"] = state;
        }
Esempio n. 4
0
        private async void InitPuzzle()
        {
            _puzzle = new SodukoPuzzle(9, PuzzleCanvas, HintMode.Off);
            _puzzle.OnCompletedGame    += _puzzle_OnCompletedGame;
            _puzzle.ShowNumberSelector += OnObjectTapped;

            _puzzle.InitPuzzle();


            if (NumberSelectorStackPanel.Children.Count != 0)
            {
                return;
            }

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
            {
                var clearPiece = new SodukoPiece(true, false);
                clearPiece.SetUnknown();
                clearPiece.OnTappedEvent += NumberSelectorTapped;
                NumberSelectorStackPanel.Children.Add(clearPiece);

                for (int i = 1; i < 10; ++i)
                {
                    var piece            = new SodukoPiece(true, false);
                    piece.NumberValue    = i;
                    piece.OnTappedEvent += NumberSelectorTapped;
                    NumberSelectorStackPanel.Children.Add(piece);
                }
            });
        }
Esempio n. 5
0
 public void SetSuggestions(SodukoPiece sp)
 {
     if (_hintMode == HintMode.Off)
     {
         return;
     }
     SetSuggestions(sp.Row, sp.Col);
 }
Esempio n. 6
0
        private void InitPuzzlePieces(int[,] puzzleValues)
        {
            int       vTicker      = 0;
            int       rTicker      = 0;
            bool      useBlue      = true;
            bool      startUseBlue = useBlue;
            const int boxSize      = 3;

            _currentValues = puzzleValues;

            for (int row = 0; row < _rows; ++row)
            {
                if (rTicker % 3 == 0 && rTicker != 0)
                {
                    startUseBlue = !startUseBlue;
                }
                useBlue = startUseBlue;
                vTicker = 0;
                for (int col = 0; col < _rows; ++col)
                {
                    int  pieceVal  = puzzleValues[row, col];
                    bool isDefined = false;

                    if (pieceVal != -1)
                    {
                        isDefined = true;
                    }
                    if (_sodukoPieces[row, col] != null)
                    {
                        // This means we are restoring so we can use the preset values.
                        isDefined = _sodukoPieces[row, col].IsPresetPiece;
                    }
                    var piece = new SodukoPiece(isDefined);
                    piece.OnTappedEvent += OnPieceTapped;
                    piece.SetScaling(_pieceLength);
                    piece.SetPosition(row, col);
                    if (vTicker % boxSize == 0 && vTicker != 0)
                    {
                        useBlue = !useBlue;
                    }
                    piece.SetBorderBrush(useBlue);

                    _sodukoPieces[row, col] = piece;

                    if (pieceVal != -1)
                    {
                        piece.NumberValue = pieceVal;
                    }

                    _canvas.Children.Add(piece);
                    ++vTicker;
                }
                ++rTicker;
            }
        }
Esempio n. 7
0
        private void OnObjectTapped(object sender, EventArgs e)
        {
            SodukoPiece p = (SodukoPiece)sender;

            if (_selectedPiece != null)
            {
                _selectedPiece.SetFocus(false);
            }

            _selectedPiece = p;
            _selectedPiece.SetFocus(true);
        }
Esempio n. 8
0
        public void RestoreState()
        {
            var    state             = Serilizer.GetPuzzleState();
            string piecesTextRep     = (string)state["Pieces"];
            string initPosTextRep    = (string)state["OriginalState"];
            string currentPosTextRep = (string)state["CurrentPos"];

            string[] pieceData = piecesTextRep.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (_rows * _rows != pieceData.Length)
            {
                throw new IOException();
            }
            int loc = 0;

            for (int i = 0; i < _rows; ++i)
            {
                for (int j = 0; j < _rows; ++j)
                {
                    SodukoPiece p = new SodukoPiece(false);
                    p.InitFromInfoString(pieceData[loc]);
                    _sodukoPieces[i, j] = p;
                    ++loc;
                }
            }

            string[] initPosData = initPosTextRep.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            loc = 0;
            for (int i = 0; i < _rows; ++i)
            {
                for (int j = 0; j < _rows; ++j)
                {
                    _startingState[i, j] = int.Parse(initPosData[loc]);
                    ++loc;
                }
            }

            string[] currentPosData = currentPosTextRep.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            loc = 0;
            for (int i = 0; i < _rows; ++i)
            {
                for (int j = 0; j < _rows; ++j)
                {
                    _currentValues[i, j] = int.Parse(currentPosData[loc]);
                    ++loc;
                }
            }

            _solver.LoadState();
        }
Esempio n. 9
0
        public bool EnsureValidMove(SodukoPiece p, int value, bool lightUpInvalidPiece)
        {
            int blockingCol;
            int blockingRow;

            bool result = EnsureValidMove(_currentValues, p.Row, p.Col, value, out blockingRow, out blockingCol);

            if (!result && lightUpInvalidPiece)
            {
                _sodukoPieces[blockingRow, blockingCol].LightPiece();
            }


            return(result);
        }
Esempio n. 10
0
        private void OnObjectTapped(object sender, EventArgs e)
        {
            SodukoPiece p = (SodukoPiece)sender;

            if (_selectedPiece != null)
            {
                _selectedPiece.SetFocus(false);
                _puzzle.ClearAllSuggestionText();
            }
            if (!p.IsPresetPiece)
            {
                _selectedPiece = p;
                _selectedPiece.SetFocus(true);
                _puzzle.SetSuggestions(_selectedPiece);
            }
        }
Esempio n. 11
0
        public void SetSelfHint(bool selfHint, SodukoPiece selectedPiece)
        {
            if (selfHint != _showSelfHint)
            {
                // _showSelfHint has changed.
                if (!selfHint && _hintMode != HintMode.Everything)
                {
                    selectedPiece.SetSuggestionTextToNothing();
                }
                else if (selfHint && _hintMode != HintMode.Off)
                {
                    SetSuggestions(selectedPiece);
                }

                _showSelfHint = selfHint;
            }
        }
Esempio n. 12
0
        //BUG:
        // For some odd reason this function doesn't work it
        // makes all operations involving the UI just silently fail.
        // This all happens if this function is only called once on during startup.
        private void AddPuzzlePieces(int[,] puzzleValues)
        {
            int       vTicker      = 0;
            int       rTicker      = 0;
            bool      useBlue      = true;
            const int boxSize      = 3;
            bool      startUseBlue = useBlue;

            _canvas.Children.Clear();
            for (int row = 0; row < _rows; ++row)
            {
                if (rTicker % 3 == 0 && rTicker != 0)
                {
                    startUseBlue = !startUseBlue;
                }
                useBlue = startUseBlue;
                vTicker = 0;
                for (int col = 0; col < _rows; ++col)
                {
                    bool preDefined = false;
                    if (_startingState[row, col] != -1)
                    {
                        preDefined = true;
                    }
                    SodukoPiece p = new SodukoPiece(preDefined);

                    p.SetScaling(_pieceLength);
                    p.SetPosition(row, col);
                    if (vTicker % boxSize == 0 && vTicker != 0)
                    {
                        useBlue = !useBlue;
                    }
                    p.SetBorderBrush(useBlue);
                    p.OnTappedEvent += OnPieceTapped;
                    p.NumberValue    = puzzleValues[row, col];
                    _canvas.Children.Add(p);


                    ++vTicker;
                }
                ++rTicker;
            }
        }
Esempio n. 13
0
        public void ClearNeighborsSuggestionText(SodukoPiece p)
        {
            int row = p.Row;
            int col = p.Col;

            if (row + 1 < 9)
            {
                _sodukoPieces[row + 1, col].SetSuggestionTextToNothing();
            }
            if (row - 1 > -1)
            {
                _sodukoPieces[row - 1, col].SetSuggestionTextToNothing();
            }
            if (col + 1 < 9)
            {
                _sodukoPieces[row, col + 1].SetSuggestionTextToNothing();
            }
            if (col - 1 > -1)
            {
                _sodukoPieces[row, col - 1].SetSuggestionTextToNothing();
            }
        }
Esempio n. 14
0
        private async void ShowNumberSelector()
        {
            if (NumberSelectorStackPanel.Children.Count != 0)
            {
                return;
            }

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
            {
                var clearPiece = new SodukoPiece(true, false);
                clearPiece.SetUnknown();
                clearPiece.OnTappedEvent += NumberSelectorTapped;
                NumberSelectorStackPanel.Children.Add(clearPiece);

                for (int i = 1; i < 10; ++i)
                {
                    var piece            = new SodukoPiece(true, false);
                    piece.NumberValue    = i;
                    piece.OnTappedEvent += NumberSelectorTapped;
                    NumberSelectorStackPanel.Children.Add(piece);
                }
            });
        }
Esempio n. 15
0
        private void OnObjectTapped(object sender, EventArgs e)
        {
            SodukoPiece p = (SodukoPiece)sender;
            if (_selectedPiece != null)
            {
                _selectedPiece.SetFocus(false);
            }

            _selectedPiece = p;
            _selectedPiece.SetFocus(true);
        }
Esempio n. 16
0
        private async void ShowNumberSelector()
        {
            if (NumberSelectorStackPanel.Children.Count != 0)
                return;

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
                {
                    var clearPiece = new SodukoPiece(true, false);
                    clearPiece.SetUnknown();
                    clearPiece.OnTappedEvent += NumberSelectorTapped;
                    NumberSelectorStackPanel.Children.Add(clearPiece);

                    for (int i = 1; i < 10; ++i)
                    {
                        var piece = new SodukoPiece(true,false);
                        piece.NumberValue = i;
                        piece.OnTappedEvent += NumberSelectorTapped;
                        NumberSelectorStackPanel.Children.Add(piece);
                    }
                });
        }
Esempio n. 17
0
        private void OnObjectTapped(object sender, EventArgs e)
        {
            SodukoPiece p = (SodukoPiece)sender;
            if (_selectedPiece != null)
            {
                _selectedPiece.SetFocus(false);
                _puzzle.ClearAllSuggestionText();
            }
            if (!p.IsPresetPiece)
            {
                _selectedPiece = p;
                _selectedPiece.SetFocus(true);
                _puzzle.SetSuggestions(_selectedPiece);
            }

        }
Esempio n. 18
0
        private async void InitPuzzle()
        {
            _puzzle = new SodukoPuzzle(9, PuzzleCanvas, HintMode.Off);
            _puzzle.OnCompletedGame += _puzzle_OnCompletedGame;
            _puzzle.ShowNumberSelector += OnObjectTapped;

            _puzzle.InitPuzzle();


            if (NumberSelectorStackPanel.Children.Count != 0)
                return;

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
            {
                var clearPiece = new SodukoPiece(true, false);
                clearPiece.SetUnknown();
                clearPiece.OnTappedEvent += NumberSelectorTapped;
                NumberSelectorStackPanel.Children.Add(clearPiece);

                for (int i = 1; i < 10; ++i)
                {
                    var piece = new SodukoPiece(true, false);
                    piece.NumberValue = i;
                    piece.OnTappedEvent += NumberSelectorTapped;
                    NumberSelectorStackPanel.Children.Add(piece);
                }
            });
        }
Esempio n. 19
0
 public void ResetPuzzle()
 {
     _canvas.Children.Clear();
     _sodukoPieces = new SodukoPiece[_rows, _rows];
     InitPuzzlePieces(_startingState);
 }