public void Move(Direction direction, bool eat) { Point nextPoint; var mod = ApplicationSettings.MatrixSize - 1; switch (direction) { case Direction.Up: var y = (Head.Y - 1) % mod; if (y < 0) { y = mod; } nextPoint = new Point(Head.X, y); break; case Direction.Right: nextPoint = new Point((Head.X + 1) % mod, Head.Y); break; case Direction.Down: nextPoint = new Point(Head.X, (Head.Y + 1) % mod); break; case Direction.Left: var x = (Head.X - 1) % mod; if (x < 0) { x = mod; } nextPoint = new Point(x, Head.Y); break; default: throw new ArgumentOutOfRangeException(nameof(direction), direction, null); } if (State[nextPoint.X, nextPoint.Y]) { throw new GameOverException("Invalid coordinate"); } State[nextPoint.X, nextPoint.Y] = true; if (direction != CurrentDirection) { Joints.Insert(0, new Joint(Head, direction)); } if (!eat) { var count = Joints.Count; var end = Joints[count - 1]; State[end.Point.X, end.Point.Y] = false; switch (end.Direction) { case Direction.Up: end.Point.Y--; break; case Direction.Right: end.Point.X++; break; case Direction.Down: end.Point.Y++; break; case Direction.Left: end.Point.X--; break; default: throw new ArgumentOutOfRangeException(nameof(direction), direction, null); } if (count > 2 && Joints[count - 2].Point.Equals(end.Point)) { Joints.Remove(end); } } Head = nextPoint; }