Beispiel #1
0
        public CellViewModel(Cell cell, IEventAggregator events)
        {
            _cell = cell;

            GotFocus = new DelegateCommand<string>( CellFocused);

            _selectionEvent = events.GetEvent<CellSelectedEvent>();
            _selectionEvent.Subscribe( c =>
                                  {
                                      Selected = _cell == c;
                                  }); // Note: Could be memory leak due to event link

            events.GetEvent<HintProvidedEvent>().Subscribe( hint =>
                {
                    if( hint.Cells.Contains( _cell ) )
                    {
                        if (CellHinted != null) CellHinted(this, EventArgs.Empty);
                    }
                });

            var numberRequestEvent = events.GetEvent<NumberRequestEvent>();
            numberRequestEvent.Subscribe(ToggleNumber);

            var modeRequestEvent = events.GetEvent<ModeRequestEvent>();
            modeRequestEvent.Subscribe(ChangeMode);
        }
Beispiel #2
0
 protected IEnumerable<Cell> CreateCellsWithActuals(params int[] actuals)
 {
     return actuals.Select(i =>
                               {
                                   var cell = new Cell(new CellId(-1, -1));
                                   cell.RequestToggleNumber(i);
                                   return cell;
                               });
 }
Beispiel #3
0
 protected IList<Cell> CreateCellsWithActuals(params int[] actuals)
 {
     return actuals.Select(i =>
                               {
                                   var cell = new Cell(new CellId(i, -1));
                                   cell.ChangeMode(Mode.PlayGame);
                                   cell.RequestToggleNumber(i);
                                   return cell;
                               }).ToList();
 }
Beispiel #4
0
 public Cell Copy()
 {
     var copy = new Cell(_id);
     foreach (var potential in _potentials)
     {
         copy._potentials.Add(potential);
     }
     copy._mode = _mode;
     copy._fixed = _fixed;
     return copy;
 }
Beispiel #5
0
 protected IList<Cell> CreateNineEmptyCellsInPlay()
 {
     var cells = new List<Cell>();
     for(int i = 0; i < 9; i++)
     {
         var cell = new Cell(new CellId(0, i));
         cell.ChangeMode(Mode.PlayGame);
         cells.Add(cell);
     }
     return cells;
 }
Beispiel #6
0
        public void ShouldFixTheNumberIfItHasAnActualWhenGameIsStarted()
        {
            // Given a cell is created and an actual is added
            var cell = new Cell(new CellId(-1, -1));
            cell.RequestToggleNumber(3);

            // When the mode is changed to playing the game
            cell.ChangeMode(Mode.PlayGame);

            // Then the cell should be fixed with that number
            Assert.IsTrue(cell.Fixed);
            Assert.AreEqual(3, cell.Actual);
        }
Beispiel #7
0
        public void ShouldClearPotentialsWhenGameIsRecreated()
        {
            // Given a cell with some numbers in
            var cell = new Cell(new CellId(-1, -1));
            cell.ChangeMode(Mode.PlayGame);
            cell.RequestToggleNumber(6);
            cell.RequestToggleNumber(7);
            cell.RequestToggleNumber(8);

            // When we start a new game
            cell.ChangeMode(Mode.NewGame);

            // Then the cell should be cleared
            Assert.IsEmpty(cell.Potentials);
        }
Beispiel #8
0
        public void ShouldBeAbleToCopyItselfWithTheSamePotentialsActualsAndMode()
        {
            // Given a cell with some potentials
            var cell = new Cell(new CellId(-1, -1));
            cell.ChangeMode(Mode.PlayGame);
            cell.RemovePotential(6);
            cell.RemovePotential(7);

            // When we copy the cell
            var copy = cell.Copy();

            // Then the copy should have the same potentials
            Assert.AreEqual(new int[]{1, 2, 3, 4, 5, 8, 9}.ToList(), cell.Potentials);

            // And be in the same mode
            copy.RequestToggleNumber(5);
            Assert.AreEqual(new int[]{1, 2, 3, 4, 8, 9}.ToList(), cell.Potentials);
        }
Beispiel #9
0
        public void ShouldClearFixedNumbersWhenGameIsRecreated()
        {
            // Given a cell with a fixed number in
            var cell = new Cell(new CellId(-1, -1));
            cell.RequestToggleNumber(8);
            cell.ChangeMode(Mode.PlayGame);

            // When we create a new game
            cell.ChangeMode(Mode.NewGame);

            // Then it should have emptied
            Assert.Null(cell.Actual);

            // When we start the game again
            cell.ChangeMode(Mode.PlayGame);

            // Then it should still not be fixed
            Assert.IsFalse(cell.Fixed);
        }
        public void ShouldProvideAHintIfTheNineCellsOnlyHaveOneSpace()
        {
            // Given eight cells with Actuals
            var cellsWithActuals = CreateCellsWithActuals(1, 2, 3, 4, 5, 6, 7, 8);

            // And one without
            var cellWithout = new Cell(new CellId(-1, -1));

            // When we pass all of them to the rule
            var allCells = new List<Cell>();
            allCells.AddRange(cellsWithActuals);
            allCells.Add(cellWithout);

            var rule = new OnlyOneSpace();
            var hint = rule.HelpWith(allCells);

            // Then the rule should give us back some text about the only empty cell having to be the 9,
            // containing all 9 cells in the hint.
            Assert.IsTrue(hint.Text.Contains("only one"));
            Assert.IsTrue(hint.Text.Contains("9"));
            CollectionAssert.AreEquivalent(hint.CellIds, allCells.Select(c => c.Id));
        }
Beispiel #11
0
        public void ShouldHavePotentialsIfMoreThanOneNumberToggled()
        {
            // Given a cell is created and the game is started
            var cell = new Cell(new CellId(-1, -1));
            cell.ChangeMode(Mode.PlayGame);

            // When one number is toggled
            cell.RequestToggleNumber(1);

            // Then it should be an actual
            Assert.AreEqual(1, cell.Actual);
            Assert.IsEmpty(cell.Potentials.ToList());

            // When a second number is toggled
            cell.RequestToggleNumber(2);

            // Then they should be potentials
            Assert.AreEqual(2, cell.Potentials.Count());
            Assert.AreEqual(1, cell.Potentials.First());
            Assert.AreEqual(2, cell.Potentials.ElementAt(1));
            Assert.Null(cell.Actual);
        }
Beispiel #12
0
        public void ShouldRemoveNumbersWhenToggledTwice()
        {
            // Given a cell with a few numbers in
            var cell = new Cell(new CellId(-1, -1));
            cell.ChangeMode(Mode.PlayGame);
            cell.RequestToggleNumber(3);
            cell.RequestToggleNumber(4);
            cell.RequestToggleNumber(5);

            // When we toggle again
            cell.RequestToggleNumber(5);

            // Then it should remove the number
            Assert.False(cell.Potentials.Contains(5));
        }
Beispiel #13
0
        public void ShouldNotHaveAnythingInToStartWith()
        {
            // Given a cell's been created
            var cell = new Cell(new CellId(-1, -1));

            // Then it should have nothing in it
            Assert.Null(cell.Actual);
            Assert.IsEmpty(cell.Potentials.ToList());
        }
Beispiel #14
0
        public void ShouldNotAllowPotentialsForANewGame()
        {
            // Given a cell has just been created
            var cell = new Cell(new CellId(-1, -1));

            // When we toggle a number then another one
            cell.RequestToggleNumber(1);
            cell.RequestToggleNumber(2);

            // Then there should be no potentials
            Assert.IsEmpty(cell.Potentials.ToList());

            // And one actual, which should be 2.
            Assert.AreEqual(cell.Actual, 2);
        }
Beispiel #15
0
        public void ShouldNotAllowFixedNumbersToBeToggled()
        {
            // Given a fixed number
            var cell = new Cell(new CellId(-1, -1));
            cell.RequestToggleNumber(3);
            cell.ChangeMode(Mode.PlayGame);

            // When we toggle the number
            cell.RequestToggleNumber(5);

            // Then nothing should change
            Assert.AreEqual(3, cell.Actual);
            Assert.IsEmpty(cell.Potentials);
        }