Exemple #1
0
        public PuzzleCell FindCell(short cellNumber)
        {
            //*** FIND THE ROW AND COLUMN OF A SPECIFIC CELL GIVEN THE CELL NUMBER***

            //first check if (cell number > 0) and (cellnumber < (_numRows * _numRows)) the maximun number of cells
            //Debug.Assert(cellNumber < _numRows * _numRows && cellNumber > 0);

            //Find a cell buy going through the puzzle one cell at a time untill the cell if found
            if ((cellNumber < (_numRows * _numRows)) && cellNumber > 0)
            {
                for (int r = 0; r < _numRows; r++)
                {
                    for (int c = 0; c < _numRows; c++)
                    {
                        if (_cells[r, c] == cellNumber)
                        {
                            _foundCell = new PuzzleCell(r, c, cellNumber);
                        }
                    }
                }
            }
            else
            {
                //if the cell is not found show error a message.
                //Debug.Assert(false, "Should have found a matching cell");
                _foundCell = new PuzzleCell(-1, -1, -1);
            }
            return(_foundCell);
        }
Exemple #2
0
        public PuzzleCell MovePiece(int row, int col)
        {
            try
            {
                //*** MOVE THE SELECTED TILE INTO THE EMPTY CELL ****

                // As long as the move status is not = 'BadMove', find the direction the tile can move
                // Debug.Assert(GetMoveStatus(row, col) != MoveStatus.BadMove);

                //move only if it is one of ther permissible moves
                MoveStatus _move = GetMoveStatus(row, col);
                if (_move != MoveStatus.BadMove)
                {
                    _cell = new PuzzleCell(_emptyRow, _emptyCol, EmptyCellId);

                    short origCell = _cells[row, col]; //find the location of the empty cell in the cell array

                    //swap the original cell with the empty cell in the cell array
                    _cells[_emptyRow, _emptyCol] = origCell;
                    _cells[row, col]             = EmptyCellId;

                    //swap the empty cell's row and col with the original cell
                    _emptyCol = col;
                    _emptyRow = row;
                }
                return(_cell); //return the cell of the newly opened position
            }
            catch (Exception ex)
            {
                MessageBox.Show("The error is :" + ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return(_cell);
            }
        }
Exemple #3
0
        public void MixUpPuzzle()
        {
            _puzzleLogic.MixUpPuzzle(); //Call MixUpPuzzle to shuffle and move the puzzles

            short cellNumber = 1;

            foreach (Button b in Children)
            {
                PuzzleCell location = _puzzleLogic.FindCell(cellNumber++);
                b.SetValue(ColumnProperty, location.Col);
                b.SetValue(RowProperty, location.Row);
            }
        }
        public PuzzleCell MovePiece(int row, int col)
        {
            try
            {
                //*** MOVE THE SELECTED TILE INTO THE EMPTY CELL ****

                // As long as the move status is not = 'BadMove', find the direction the tile can move
                // Debug.Assert(GetMoveStatus(row, col) != MoveStatus.BadMove);

                //move only if it is one of ther permissible moves
                MoveStatus _move = GetMoveStatus(row, col);
                if (_move != MoveStatus.BadMove)
                {
                    _cell = new PuzzleCell(_emptyRow, _emptyCol, EmptyCellId);

                    short origCell = _cells[row, col]; //find the location of the empty cell in the cell array

                    //swap the original cell with the empty cell in the cell array
                    _cells[_emptyRow, _emptyCol] = origCell;
                    _cells[row, col] = EmptyCellId;

                    //swap the empty cell's row and col with the original cell
                    _emptyCol = col;
                    _emptyRow = row;
                }
                return _cell; //return the cell of the newly opened position
            }
            catch (Exception ex)
            {
                MessageBox.Show("The error is :" + ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return _cell;
            }
        }
        public PuzzleCell FindCell(short cellNumber)
        {
            //*** FIND THE ROW AND COLUMN OF A SPECIFIC CELL GIVEN THE CELL NUMBER***

            //first check if (cell number > 0) and (cellnumber < (_numRows * _numRows)) the maximun number of cells
            //Debug.Assert(cellNumber < _numRows * _numRows && cellNumber > 0);

            //Find a cell buy going through the puzzle one cell at a time untill the cell if found
            if ((cellNumber < (_numRows*_numRows)) && cellNumber > 0)
            {
                for (int r = 0; r < _numRows; r++)
                {
                    for (int c = 0; c < _numRows; c++)
                    {
                        if (_cells[r, c] == cellNumber)
                        {
                            _foundCell = new PuzzleCell(r, c, cellNumber);
                        }
                    }
                }
            }
            else
            {
                //if the cell is not found show error a message.
                //Debug.Assert(false, "Should have found a matching cell");
                _foundCell = new PuzzleCell(-1, -1, -1);
            }
            return _foundCell;
        }