Exemplo n.º 1
0
        public void UpdateSnakePositions()
        {
            Snake snakeHead = SnakeParts[0];
            Snake snakeTail = SnakeParts[SnakeParts.Count - 1];

            if (snakeHead.HeadDirection == Direction.Left)
            {
                if (snakeHead.UnitPosition.Column == 0 && !(snakeHead.UnitPosition.Row == 0))
                {
                    snakeHead.HeadDirection = Direction.Up;
                    snakeHead.UnitPosition  = new Position(snakeHead.UnitPosition.Row - 1, snakeHead.UnitPosition.Column);
                }
                else if (snakeHead.UnitPosition.Column == 0 && snakeHead.UnitPosition.Row == 0)
                {
                    snakeHead.HeadDirection = Direction.Down;
                    snakeHead.UnitPosition  = new Position(snakeHead.UnitPosition.Row + 1, snakeHead.UnitPosition.Column);
                }
                else
                {
                    snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column - 1);
                }

                if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Bomb)
                {
                    GameOver = true;
                }
                else if ((GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Snake) && !(snakeHead.UnitPosition == snakeTail.UnitPosition))
                {
                    GameOver = true;
                }
                else if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Fruit)
                {
                    Pontos++;
                    GameMap.AddUnit(new Snake(snakeHead.UnitPosition));
                    Fruits.RemoveAll(x => x.UnitPosition == snakeHead.UnitPosition);
                    SnakeParts[0] = snakeHead;
                    for (int i = 1; i < SnakeParts.Count - 1; i++)
                    {
                        SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition;
                    }
                    SnakeParts.Insert(SnakeParts.Count - 1, new Snake((SnakeParts[SnakeParts.Count - 2].PreviousPosition)));
                    for (int i = 0; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition;
                    }
                }
                else
                {
                    GameMap.AddUnit(new Snake(snakeHead.UnitPosition));
                    GameMap.RemoveUnit(snakeTail.UnitPosition);
                    SnakeParts[0] = snakeHead;
                    for (int i = 1; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition;
                    }
                    for (int i = 0; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition;
                    }
                }
            }
            else if (snakeHead.HeadDirection == Direction.Right)
            {
                if (snakeHead.UnitPosition.Column == (GameMap.NumberOfColumns - 1) && snakeHead.UnitPosition.Row != 0)
                {
                    snakeHead.HeadDirection = Direction.Up;
                    snakeHead.UnitPosition  = new Position(snakeHead.UnitPosition.Row - 1, snakeHead.UnitPosition.Column);
                }
                else if (snakeHead.UnitPosition.Column == (GameMap.NumberOfColumns - 1) && snakeHead.UnitPosition.Row == 0)
                {
                    snakeHead.HeadDirection = Direction.Down;
                    snakeHead.UnitPosition  = new Position(snakeHead.UnitPosition.Row + 1, snakeHead.UnitPosition.Column);
                }
                else
                {
                    snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column + 1);
                }

                if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Bomb)
                {
                    GameOver = true;
                }
                else if ((GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Snake) && !(snakeHead.UnitPosition == snakeTail.UnitPosition))
                {
                    GameOver = true;
                }
                else if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Fruit)
                {
                    Pontos++;
                    GameMap.AddUnit(new Snake(snakeHead.UnitPosition));
                    Fruits.RemoveAll(x => x.UnitPosition == snakeHead.UnitPosition);
                    SnakeParts[0] = snakeHead;
                    for (int i = 1; i < SnakeParts.Count - 1; i++)
                    {
                        SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition;
                    }
                    SnakeParts.Insert(SnakeParts.Count - 1, new Snake((SnakeParts[SnakeParts.Count - 2].PreviousPosition)));
                    for (int i = 0; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition;
                    }
                }
                else
                {
                    GameMap.AddUnit(new Snake(snakeHead.UnitPosition));
                    GameMap.RemoveUnit(snakeTail.UnitPosition);
                    SnakeParts[0] = snakeHead;
                    for (int i = 1; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition;
                    }
                    for (int i = 0; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition;
                    }
                }
            }
            else if (snakeHead.HeadDirection == Direction.Up)
            {
                if (snakeHead.UnitPosition.Row == 0 && snakeHead.UnitPosition.Column != 0)
                {
                    snakeHead.HeadDirection = Direction.Left;
                    snakeHead.UnitPosition  = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column - 1);
                }
                else if (snakeHead.UnitPosition.Row == 0 && snakeHead.UnitPosition.Column == 0)
                {
                    snakeHead.HeadDirection = Direction.Right;
                    snakeHead.UnitPosition  = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column + 1);
                }
                else
                {
                    snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row - 1, snakeHead.UnitPosition.Column);
                }

                if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Bomb)
                {
                    GameOver = true;
                }
                else if ((GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Snake) && !(snakeHead.UnitPosition == snakeTail.UnitPosition))
                {
                    GameOver = true;
                }
                else if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Fruit)
                {
                    Pontos++;
                    GameMap.AddUnit(new Snake(snakeHead.UnitPosition));
                    Fruits.RemoveAll(x => x.UnitPosition == snakeHead.UnitPosition);
                    SnakeParts[0] = snakeHead;
                    for (int i = 1; i < SnakeParts.Count - 1; i++)
                    {
                        SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition;
                    }
                    SnakeParts.Insert(SnakeParts.Count - 1, new Snake((SnakeParts[SnakeParts.Count - 2].PreviousPosition)));
                    for (int i = 0; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition;
                    }
                }
                else
                {
                    GameMap.AddUnit(new Snake(snakeHead.UnitPosition));
                    GameMap.RemoveUnit(snakeTail.UnitPosition);
                    SnakeParts[0] = snakeHead;
                    for (int i = 1; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition;
                    }
                    for (int i = 0; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition;
                    }
                }
            }
            else if (snakeHead.HeadDirection == Direction.Down)
            {
                if (snakeHead.UnitPosition.Row == (GameMap.NumberOfRows - 1) && snakeHead.UnitPosition.Column != 0)
                {
                    snakeHead.HeadDirection = Direction.Left;
                    snakeHead.UnitPosition  = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column - 1);
                }
                else if (snakeHead.UnitPosition.Row == (GameMap.NumberOfRows - 1) && snakeHead.UnitPosition.Column == 0)
                {
                    snakeHead.HeadDirection = Direction.Right;
                    snakeHead.UnitPosition  = new Position(snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column + 1);
                }
                else
                {
                    snakeHead.UnitPosition = new Position(snakeHead.UnitPosition.Row + 1, snakeHead.UnitPosition.Column);
                }

                if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Bomb)
                {
                    GameOver = true;
                }
                else if ((GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Snake) && !(snakeHead.UnitPosition == snakeTail.UnitPosition))
                {
                    GameOver = true;
                }
                else if (GameMap.MapUnits[snakeHead.UnitPosition.Row, snakeHead.UnitPosition.Column] is Fruit)
                {
                    Pontos++;
                    GameMap.AddUnit(new Snake(snakeHead.UnitPosition));
                    Fruits.RemoveAll(x => x.UnitPosition == snakeHead.UnitPosition);
                    SnakeParts[0] = snakeHead;
                    for (int i = 1; i < SnakeParts.Count - 1; i++)
                    {
                        SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition;
                    }
                    SnakeParts.Insert(SnakeParts.Count - 1, new Snake((SnakeParts[SnakeParts.Count - 2].PreviousPosition)));
                    for (int i = 0; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition;
                    }
                }
                else
                {
                    GameMap.AddUnit(new Snake(snakeHead.UnitPosition));
                    GameMap.RemoveUnit(snakeTail.UnitPosition);
                    SnakeParts[0] = snakeHead;
                    for (int i = 1; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].UnitPosition = SnakeParts[i - 1].PreviousPosition;
                    }
                    for (int i = 0; i < SnakeParts.Count; i++)
                    {
                        SnakeParts[i].PreviousPosition = SnakeParts[i].UnitPosition;
                    }
                }
            }
        }