Beispiel #1
0
 private void LoadFromGameState(GameState gameState)
 {
     nextColors  = gameState.NextColors;
     nextPoints  = Point.FromDrawingPoints(gameState.NextPoints).ToArray();
     Score       = gameState.Score;
     scoreStreak = gameState.ScoreStreak;
     Field.LoadFromState(gameState);
 }
Beispiel #2
0
        public bool Deploy(Point point, Color color)
        {
            if (color == noneColor || !IsCellFree(point))
            {
                return(false);
            }

            field[point.X, point.Y] = color;
            buffer.Clear();
            FieldUpdated?.Invoke();
            return(true);
        }
Beispiel #3
0
        private GameState GetGameState()
        {
            var state = new GameState
            {
                NextColors  = nextColors,
                NextPoints  = Point.ToDrawingPoints(nextPoints).ToArray(),
                Score       = Score,
                ScoreStreak = scoreStreak
            };

            return(Field.SaveToGameState(state));
        }
Beispiel #4
0
        public bool Move(Point p1, Point p2)
        {
            if (!IsCellFilled(p1) || !IsCellFree(p2))
            {
                return(false);
            }
            var success = Deploy(p2, GetColorAt(p1));

            if (success)
            {
                ClearCell(p1);
            }
            return(success);
        }
Beispiel #5
0
        public int RemoveFullLines()
        {
            var cellsToClear = new HashSet <Point>();

            foreach (var point in GetFilledCells())
            {
                foreach (var direction in Point.GetAllNonEmptyDirections())
                {
                    var pointsInLine = new HashSet <Point>();
                    var prevColor    = noneColor;
                    var s            = 0;
                    var p            = point;
                    while (!IsOutside(p))
                    {
                        if (IsCellFilled(p) && prevColor == GetColorAt(p))
                        {
                            s++;
                        }
                        else
                        {
                            if (s >= Game.MinLineLength)
                            {
                                cellsToClear.AddRange(pointsInLine);
                            }

                            s         = 1;
                            prevColor = GetColorAt(p);
                            pointsInLine.Clear();
                        }
                        pointsInLine.Add(p);

                        p = p.Offset(direction);
                    }
                    if (s >= Game.MinLineLength)
                    {
                        cellsToClear.AddRange(pointsInLine);
                    }
                }
            }
            foreach (var point in cellsToClear)
            {
                ClearCell(point);
            }
            return(cellsToClear.Count);
        }
Beispiel #6
0
        public IEnumerable <Point> GetPath(Point from, Point to)
        {
            if (from == to)
            {
                return(null);
            }

            var key = Tuple.Create(from, to);

            if (buffer.ContainsKey(key))
            {
                return(buffer[key]);
            }


            var path = PathSearcher.Search(@from, to, IsCellFree);

            AddToBuffer(key, path);
            return(path);
        }
Beispiel #7
0
        public bool MakeMove(Point p1, Point p2)
        {
            var path = GetPath(p1, p2);

            if (path == null)
            {
                return(false);
            }

            var gameState = GetGameState();

            if (!Field.Move(p1, p2))
            {
                return(false);
            }

            SaveGameState(gameState);

            foreach (var t in path.ToList().ToTuples())
            {
                BallMoving?.Invoke(t.Item1, t.Item2);
            }


            if (TryFindFullLinesAndUpdateScore())
            {
                return(true);
            }

            scoreStreak = 0;
            DeployNext();
            TryFindFullLinesAndUpdateScore();

            GenerateNext();

            return(true);
        }
Beispiel #8
0
 private bool IsCellBlocked(Point point)
 {
     return(IsCellBlocked(point.X, point.Y));
 }
Beispiel #9
0
 private bool IsCellFree(Point point)
 {
     return(!IsCellBlocked(point));
 }
Beispiel #10
0
 public Color GetColorAt(Point point)
 {
     return(GetColorAt(point.X, point.Y));
 }
Beispiel #11
0
 private bool IsOutside(Point point)
 {
     return(IsOutside(point.X, point.Y));
 }
Beispiel #12
0
 private void ClearCell(Point point)
 {
     ClearCell(point.X, point.Y);
 }
Beispiel #13
0
 private bool IsCellFilled(Point point)
 {
     return(IsCellFilled(point.X, point.Y));
 }
Beispiel #14
0
 public Color GetColorAt(Point point)
 {
     return(Field.GetColorAt(point));
 }
Beispiel #15
0
 public IEnumerable <Point> GetPath(Point p1, Point p2)
 {
     return(Field.GetPath(p1, p2));
 }