public Food(int id, Point position, int nutrition, int lifespan) : base(FoodService.GetName(id), position) { Id_real = id; Nutrition_real = nutrition; LifeSpan_real = lifespan; }
public string Move() { //change direction if scheduled ChangeDirection(); Point newPosition = new Point( SnakeParts[0].Position.X + moveCoordinate.X, SnakeParts[0].Position.Y + moveCoordinate.Y); //new position for the head //check the next tile //SPECIAL CASE: if out of bounds, wrap around if (newPosition.X == BoardSizeX) { newPosition.X = 0; } else if (newPosition.X == -1) { newPosition.X = BoardSizeX - 1; } if (newPosition.Y == BoardSizeY) { newPosition.Y = 0; } else if (newPosition.Y == -1) { newPosition.Y = BoardSizeY - 1; } ushort nextValue = Board[newPosition.X, newPosition.Y]; //NOT SO SPECIAL CASE: check if next value is a wall or snake part switch (nextValue) { case (int)BoardValue.SnakePart: //SPECIAL CASE: if next tile is the last snake part, don't crash but continue moving if (!(newPosition.X == SnakeParts[SnakeParts.Count - 1].Position.X && newPosition.Y == SnakeParts[SnakeParts.Count - 1].Position.Y)) { //die return(Die("Intersection")); } break; case (int)BoardValue.Wall: //die return(Die("Wall")); } if (FoodService.IsBoardValueFruit(nextValue)) { EatCurrentFood(nextValue, newPosition); } //move the whole body Point prevPosition = SnakeParts[0].Position; SnakeParts[0].Position = newPosition; Board[newPosition.X, newPosition.Y] = (int)BoardValue.SnakeHead; for (int i = 1; i < SnakeParts.Count; ++i) { Point currPosition = SnakeParts[i].Position; SnakeParts[i].Position = prevPosition; Board[prevPosition.X, prevPosition.Y] = (int)BoardValue.SnakePart; prevPosition = currPosition; } //grow if you can if (toGrow > 0) { SnakeParts.Add(new SnakePart("SnakePart", prevPosition)); Board[prevPosition.X, prevPosition.Y] = (int)BoardValue.SnakePart; toGrow--; } else { //cant't grow so set last board value to 0 Board[prevPosition.X, prevPosition.Y] = 0; } //didn't die return(""); }