Exemplo n.º 1
0
 private void AssembleMap()
 {
     Map = new List <List <Piece> >();
     for (int y = 0; y < Height; y++)
     {
         Map.Add(new List <Piece>());
         for (int x = 0; x < Width; x++)
         {
             if (Walls.Any(p => p.X == x && p.Y == y))
             {
                 Map[y].Add(new Piece(x, y, PieceType.Wall, Color.Blue));
             }
             else if (Snakes.Any(s => s.snakePieces.Any(p => p.X == x && p.Y == y && p.Type == PieceType.SnakePiece)))
             {
                 Map[y].Add(new Piece(x, y, PieceType.SnakePiece, Snakes.Where(s => s.IsAtPos(x, y)).FirstOrDefault().snakePieces.Where(p => p.X == x && p.Y == y).FirstOrDefault().Color));
             }
             else if (Snakes.Any(s => s.snakePieces.Any(p => p.X == x && p.Y == y && p.Type == PieceType.HeadPiece)))
             {
                 Map[y].Add(new Piece(x, y, PieceType.HeadPiece, Snakes.Where(s => s.IsAtPos(x, y)).FirstOrDefault().snakePieces.Where(p => p.X == x && p.Y == y).FirstOrDefault().Color));
             }
             else if (FoodPieces.Any(p => p.X == x && p.Y == y))
             {
                 Piece piece = FoodPieces.Where(p => p.X == x && p.Y == y).FirstOrDefault();
                 Map[y].Add(piece);
             }
             else
             {
                 Map[y].Add(new Piece(x, y, PieceType.Empty));
             }
         }
     }
 }
Exemplo n.º 2
0
        public void RunTurn()
        {
            if (!Snakes.Any())
            {
                HasEnded = true;
            }
            else
            {
                TurnsCount++;

                GrassBoard.UpdateGrass();

                MoveSnakes();
            }
        }
Exemplo n.º 3
0
        private void SetSnakesOnGame(List <List <int> > snakeIndices)
        {
            foreach (var snakeIndex in snakeIndices)
            {
                if (Snakes.Any(x => x.Head == snakeIndex[0]))
                {
                    //Skipping two snake heads at same point
                    continue;
                }

                var snake = new Snake()
                {
                    Head = snakeIndex[0],
                    Tail = snakeIndex[1]
                };
                Snakes.Add(snake);
            }
        }
Exemplo n.º 4
0
        private bool Search(out int availableMoves, bool x, bool checkPlus, int topValue, int variable, int pos, int pos_less, int pos_plus, Direction direction_less, Direction direction_plus)
        {
            availableMoves = 0;

            var found = false;

            if (checkPlus)
            {
                variable++;
            }
            else
            {
                variable--;
            }

            var whiteSpacesLess = 0;
            var whiteSpacesPlus = 0;

            while (!found && variable > -1 && variable < topValue)
            {
                var previous = x ? Space.Map[pos_less, variable] : Space.Map[variable, pos_less];
                var actual   = x ? Space.Map[pos, variable] : Space.Map[variable, pos];
                var next     = x ? Space.Map[pos_plus, variable] : Space.Map[variable, pos_plus];

                if (previous != 0)
                {
                    if (Snakes.Any(s => s.Id == previous && s.X == (x ? pos_less : variable) && s.Y == (x ? variable : pos_less) && s.Direction == direction_less.ToString()))
                    {
                        found = true;
                        continue;
                    }
                }
                else if (pos != pos_less)
                {
                    whiteSpacesLess++;
                }

                if (next != 0)
                {
                    if (Snakes.Any(s => s.Id == previous && s.X == (x ? pos_plus : variable) && s.Y == (x ? variable : pos_plus) && s.Direction == direction_plus.ToString()))
                    {
                        found = true;
                        continue;
                    }
                }
                else if (pos != pos_plus)
                {
                    whiteSpacesPlus++;
                }

                if (actual == 0)
                {
                    availableMoves++;
                }
                else
                {
                    found = true;
                }

                if (checkPlus)
                {
                    variable++;
                }
                else
                {
                    variable--;
                }
            }

            //bool lockingWay_less;
            //if (pos == pos_less)
            //    lockingWay_less = true;
            //else
            //    lockingWay_less = whiteSpacesLess == AvailableMovesUp;

            //bool lockingWay_plus;
            //if (pos == pos_plus)
            //    lockingWay_plus = true;
            //else
            //    lockingWay_plus = whiteSpacesPlus == AvailableMovesUp;

            return(whiteSpacesLess == 0 && whiteSpacesPlus == 0);
        }