// Returns a copy of recieved field public Field GetFieldCopy(Point currentPoint) { Field fieldCopy; switch (this[currentPoint]) { case Wall w: fieldCopy = new Wall(currentPoint); break; case Food f: fieldCopy = new Food(currentPoint); break; case SnakeHead h: Direction currentDirection = h.Direction; fieldCopy = new SnakeHead(currentPoint, currentDirection); break; case SnakeBodyPart b: fieldCopy = new SnakeBodyPart(currentPoint); break; case Empty e: fieldCopy = new Empty(currentPoint); break; default: throw new Exception("Unable to find type"); } return(fieldCopy); }
// Update object of type snake. Only updates fields that has been changed public void UpdateSnakePosition(Snake snake, bool bodyAdded) { // Place new snake head on grid this[snake.Head.Point] = snake.Head; // If snake has a body, replace previous position of snake head with snake body if (snake.HasBody) { this[previousSnakeHeadPosition] = new SnakeBodyPart(previousSnakeHeadPosition); } // If no body added in this round, delete tail if (!bodyAdded) { if (this[previousSnakeTailPosition] is Wall) { } this[previousSnakeTailPosition] = new Empty(previousSnakeTailPosition); } // Copy position of head and tail to use when updating next time previousSnakeHeadPosition = new Point(snake.Head.Point.Row, snake.Head.Point.Column); previousSnakeTailPosition = new Point(snake.Body.Last().Point.Row, snake.Body.Last().Point.Column); }