Exemple #1
0
        public TurnInfo Drop(int column)
        {
            if (field[column].Count == height) return TurnInfo.Invalid;

            NextCell.SetXy(column, field[column].Count);
            field[column].Add(NextCell);
            var score = 0;

            List<Group> groups;

            while ((groups = RebuildGroups().Where(g => rules.CanTerminate(g)).ToList()).Count > 0)
            {
                score += rules.GetGroupsScore(groups);
                foreach (var group in groups)
                {
                    TerminateGroup(group);
                }

                var selector = rules.AdditionalCellsSelector(groups);
                TerminateCells(field.SelectMany(c => c).Where(c => selector(c)));
            }

            if (field.All(col => col.Count == height)) return TurnInfo.GameOwer;

            NextCell = CreateNewCell();

            return new TurnInfo(score);
        }
Exemple #2
0
 private void OnTerminated(Cell cell)
 {
     cell.Terminated -= OnTerminated;
     cell.Shifted -= OnShifted;
     cell.Updated -= OnUpdated;
     game.RemoveCell(this);
 }
Exemple #3
0
        private void UpdateConnections(Cell newCell)
        {
            var top = Cells.FirstOrDefault(c => c.X == newCell.X && c.Y == newCell.Y + 1);
            if (top != null)
            {
                newCell.ConnectionType |= CellConnectionType.Top;
                top.ConnectionType |= CellConnectionType.Bottom;
            }

            var bottom = Cells.FirstOrDefault(c => c.X == newCell.X && c.Y == newCell.Y - 1);
            if (bottom != null)
            {
                bottom.ConnectionType |= CellConnectionType.Top;
                newCell.ConnectionType |= CellConnectionType.Bottom;
            }

            var left = Cells.FirstOrDefault(c => c.Y == newCell.Y && c.X == newCell.X - 1);
            if (left != null)
            {
                left.ConnectionType |= CellConnectionType.Right;
                newCell.ConnectionType |= CellConnectionType.Left;
            }

            var right = Cells.FirstOrDefault(c => c.Y == newCell.Y && c.X == newCell.X + 1);
            if (right != null)
            {
                newCell.ConnectionType |= CellConnectionType.Right;
                right.ConnectionType |= CellConnectionType.Left;
            }
        }
Exemple #4
0
        public void AddCell(Cell cell)
        {
            if (cell.Color != Color)
                throw new InvalidOperationException("Different cells and groups color");

            UpdateConnections(cell);
            Cells.Add(cell);
            cell.Group = this;
        }
Exemple #5
0
 public CellViewModel(Cell cell, GameViewModel game)
 {
     model = cell;
     this.game = game;
     model.Terminated += OnTerminated;
     model.Shifted += OnShifted;
     model.Updated += OnUpdated;
     Fill = model.Color.FillColor;
     Row = cell.Row;
     Column = cell.X;
 }
Exemple #6
0
 private void OnUpdated(Cell cell)
 {
     ConnectionType = cell.ConnectionType;
     Fill = model.Group.StarsCount > 0 ? model.Color.FillColor : model.Color.GroupColor;
 }
Exemple #7
0
 private IEnumerable<Cell> GetFilteredCells(Cell cell)
 {
     return GetNearestCells(cell.X, cell.Y).Where(c => c.Group == null && c.Color == cell.Color);
 }
Exemple #8
0
 public Group(Cell cell)
 {
     Cells = new List<Cell> { cell };
     Color = cell.Color;
     cell.Group = this;
 }