Ejemplo n.º 1
0
        /// <summary>
        /// Displays the turtles and tracks of the specific draw board in the console.
        /// </summary>
        /// <param name="board">The draw board where the turtles and tracks are stored.</param>
        /// <exception cref="ArgumentNullException">
        /// If board is null.
        /// </exception>
        public void Visit(DrawBoard board)
        {
            if (board == null)
            {
                throw new ArgumentNullException();
            }

            Console.Clear();

            for (int i = 0; i < board.TrackPositions.Count; i++)
            {
                Console.SetCursorPosition(board.TrackPositions[i].Left, board.TrackPositions[i].Top);
                Console.ForegroundColor = board.GetTrackColor(board.TrackPositions[i]);
                Console.Write($"{board.GetTrackChar(board.TrackPositions[i])}");
            }

            for (int i = 0; i < board.Turtles.Count; i++)
            {
                Console.SetCursorPosition(board.Turtles[i].Position.Left, board.Turtles[i].Position.Top);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write($"{board.Turtles[i].TurtleSymbol}");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method ensures that the turtle is inside the draw board and stores the tracks and the turtle in the draw board.
        /// </summary>
        /// <param name="board">The object that should be visited.</param>
        /// <exception cref="ArgumentNullException">
        /// If board is null.
        /// </exception>
        public void Visit(DrawBoard board)
        {
            if (board == null)
            {
                throw new ArgumentNullException();
            }

            Position position = this.Position;

            if (this.Position.Top < 0)
            {
                position = new Position(this.Position.Left, 0);
                if (int.TryParse(this.Turtle.Commands[0].GetValue(), out int value))
                {
                    if (value > 1)
                    {
                        this.Turtle.Commands.RemoveAt(0);
                    }
                }
            }
            else if (this.Position.Top > board.Height)
            {
                position = new Position(this.Position.Left, board.Height);
                if (int.TryParse(this.Turtle.Commands[0].GetValue(), out int value))
                {
                    if (value > 1)
                    {
                        this.Turtle.Commands.RemoveAt(0);
                    }
                }
            }
            else if (this.Position.Left < 0)
            {
                position = new Position(0, this.Position.Top);
                if (int.TryParse(this.Turtle.Commands[0].GetValue(), out int value))
                {
                    if (value > 1)
                    {
                        this.Turtle.Commands.RemoveAt(0);
                    }
                }
            }
            else if (this.Position.Left > board.Width)
            {
                position = new Position(board.Width, this.Position.Top);
                if (int.TryParse(this.Turtle.Commands[0].GetValue(), out int value))
                {
                    if (value > 1)
                    {
                        this.Turtle.Commands.RemoveAt(0);
                    }
                }
            }

            this.Position = position;

            if (this.Draw == true)
            {
                board.SetTrackChar(this.Position, this.TrackSymbol);
                board.SetTrackColor(this.Position, this.TrackColor);
            }

            if (!board.Turtles.Contains(this))
            {
                board.Turtles.Add(this);
            }
        }