public void ShouldReturnFalse_CompareContextOfOriginalAndClonedInstances()
        {
            Cell firstCell = new Cell();
            firstCell.Content = 1;
            Cell secondCell = firstCell.Clone() as Cell;
            secondCell.Content = 2;

            Assert.IsFalse(firstCell.Content == secondCell.Content);
        }
        /// <summary>
        /// This method rearrange the puzzle field by chosen cell.
        /// </summary>
        /// <param name="selectedCell">The cell for move.</param>
        public void RearrangePuzzleField(Cell selectedCell)
        {
            int selectedCellFieldIndex = selectedCell.Col + (selectedCell.Row * this.Field.MatrixSize);
            selectedCell = this.Field.Body[selectedCellFieldIndex];

            int emptySpaceCell = this.Field.EmptyCell.Content;
            this.Field.EmptyCell.Content = selectedCell.Content;
            selectedCell.Content = emptySpaceCell;
        }
 public void ShouldThrowArgumentException_SetInvalidValueForCellRow()
 {
     try
     {
         Cell testCell = new Cell();
         testCell.Row = -1;
     }
     catch (ArgumentException ex)
     {
         Assert.AreEqual("The number of row must be a positive integer!", ex.Message);
         throw;
     }
 }
 public void ShouldBeEqual_FillPuzzleBodyMethodTest()
 {
     PuzzleField testField = new PuzzleField(3);
     List<Cell> expectedBody=new List<Cell>();
     Cell firstCell=new Cell();
     firstCell.Content = 1;
     firstCell.Row = 0;
     firstCell.Col = 0;
     Cell secondCell=new Cell();
     secondCell.Content = 2;
     secondCell.Row = 0;
     secondCell.Col = 1;
     Cell thirdCell=new Cell();
     thirdCell.Content = 3;
     thirdCell.Row = 0;
     thirdCell.Col = 2;
     Cell fourthCell=new Cell();
     fourthCell.Content = 4;
     fourthCell.Row = 1;
     fourthCell.Col = 0;
     Cell fifthCell=new Cell();
     fifthCell.Content = 5;
     fifthCell.Row = 1;
     fifthCell.Col = 1;
     Cell sixthCell=new Cell();
     sixthCell.Content = 6;
     sixthCell.Row = 1;
     sixthCell.Col = 2;
     Cell seventhCell=new Cell();
     seventhCell.Content = 7;
     seventhCell.Row = 2;
     seventhCell.Col = 0;
     Cell eigthCell=new Cell();
     eigthCell.Content = 8;
     eigthCell.Row = 2;
     eigthCell.Col = 1;
     Cell ninthCell=new Cell();
     ninthCell.Content = 0;
     ninthCell.Row = 2;
     ninthCell.Col = 2;
     expectedBody.Add(firstCell);
     expectedBody.Add(secondCell);
     expectedBody.Add(thirdCell);
     expectedBody.Add(fourthCell);
     expectedBody.Add(fifthCell);
     expectedBody.Add(sixthCell);
     expectedBody.Add(seventhCell);
     expectedBody.Add(eigthCell);
     expectedBody.Add(ninthCell);
     Assert.AreEqual(expectedBody[6].Content, testField.Body[6].Content);
 }
        public void ShouldPrintSpecificMessage_PrintMethodTestWithTwoDigitContent()
        {
            Cell testCell = new Cell();
            testCell.Content = 11;
            using (var writer = new StringWriter())
            {
                Console.SetOut(writer);
                testCell.Print();

                writer.Flush();

                string result = writer.GetStringBuilder().ToString();
                string expected = "11 ";
                Assert.AreEqual(expected, result);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// This method checks if a number from a cell can be moved.
        /// </summary>
        /// <param name="row">Row of game field.</param>
        /// <param name="col">Column of game field.</param>
        /// <returns>Returns "true" if the move are legal or "false" if the move are illegal.</returns>
        private bool CheckIsTheMoveAreLegal(Cell cell)
        {
            if ((cell.Row == this.PuzzleField.EmptyCell.Row - 1 || cell.Row == this.PuzzleField.EmptyCell.Row + 1)
                && cell.Col == this.PuzzleField.EmptyCell.Col)
            {
                return true;
            }

            if ((cell.Row == this.PuzzleField.EmptyCell.Row) && (cell.Col == this.PuzzleField.EmptyCell.Col - 1
                || cell.Col == this.PuzzleField.EmptyCell.Col + 1))
            {
                return true;
            }

            return false;
        }
        /// <summary>
        /// This method shuffle all cells in PuzzleField object.
        /// </summary>
        /// <param name="puzzleField">This is the field for shuffle.</param>
        public override void Shuffle(PuzzleFieldManager manager)
        {
            PuzzleField puzzleField = manager.Field;
            Random randomGenerator = new Random();

            for (int i = 0; i < NumberOfShuffling; i++)
            {
                int randomNumber = randomGenerator.Next(3);
                Cell selectedCell = new Cell();

                if (randomNumber == 0)
                {
                    selectedCell.Col = puzzleField.EmptyCell.Col;
                    if (puzzleField.EmptyCell.Row > 0)
                    {
                        selectedCell.Row = puzzleField.EmptyCell.Row - 1;
                        manager.RearrangePuzzleField(selectedCell);
                    }
                    else
                    {
                        randomNumber++;
                    }
                }

                if (randomNumber == 1)
                {
                    selectedCell.Row = puzzleField.EmptyCell.Row;
                    if (puzzleField.EmptyCell.Col < puzzleField.MatrixSize - 1)
                    {
                        selectedCell.Col = puzzleField.EmptyCell.Col + 1;
                        manager.RearrangePuzzleField(selectedCell);
                    }
                    else
                    {
                        randomNumber++;
                    }
                }

                if (randomNumber == 2)
                {
                    selectedCell.Col = puzzleField.EmptyCell.Col;
                    if (puzzleField.EmptyCell.Row < puzzleField.MatrixSize - 1)
                    {
                        selectedCell.Row = puzzleField.EmptyCell.Row + 1;
                        manager.RearrangePuzzleField(selectedCell);
                    }
                    else
                    {
                        randomNumber++;
                    }
                }

                if (randomNumber == 3)
                {
                    selectedCell.Row = puzzleField.EmptyCell.Row;
                    if (puzzleField.EmptyCell.Col > 0)
                    {
                        selectedCell.Col = puzzleField.EmptyCell.Col - 1;
                        manager.RearrangePuzzleField(selectedCell);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// This method returns the empty cell from PuzzleField
        /// </summary>
        /// <returns>Returns object of empty cell.</returns>
        private Cell FindEmptyCell()
        {
            Cell searchedCell = new Cell();

            for (int i = 0; i < this.Body.Count; i++)
            {
                searchedCell = this.Body[i];
                if (searchedCell.Content == 0)
                {
                    break;
                }
            }

            return searchedCell;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// This method fill the list of elements in puzzle field.
        /// </summary>
        public void FillPuzzleBody()
        {
            Cell singleCell = new Cell();
            int currentValue = 1;

            for (int row = 0; row < this.MatrixSize; row++)
            {
                for (int col = 0; col < this.MatrixSize; col++)
                {
                    // Prototype design pattern.
                    Cell currentCell = singleCell.Clone() as Cell;

                    if (currentValue == this.MatrixSize * this.MatrixSize)
                    {
                        currentValue = 0;
                    }

                    currentCell.Content = currentValue;
                    currentCell.Row = row;
                    currentCell.Col = col;
                    this.Body.Add(currentCell);
                    currentValue++;
                }
            }
        }