Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What's wrong with my code? Why isn't this printing the asterisks? C# Turtle Graphics 8.21 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

What's wrong with my code? Why isn't this printing the asterisks? C# Turtle Graphics 8.21

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

public class TurtleGraphics { const int MAXCOMMANDS = 100; // maximum possible size for the turtleComm array const int ARRAYSIZE = 20; // size for the array that represents the drawing area

int[,] floor; // declare the array for the floor int[,] commandArray; // declare array for storing the list of commands

int count; // the current total of commands int turtleXPos; // the x-coordinate of the turtle int turtleYPos; // the y-coordinate of the turtle

// Method to take the commands public void EnterCommands() { count = 0; commandArray = new int[MAXCOMMANDS, 2]; floor = new int[ARRAYSIZE, ARRAYSIZE];

// prompt user and enter the commands Console.WriteLine("Commands: 1 = pen up, 2 = pen down, 3 = turn right, 4 = turn left " + "5 = forward, 6 = Display board, 9 = end of data, sentinel"); Console.Write("Enter command (or 9 to quit)."); int command = int.Parse(Console.ReadLine());

while (command != 9 && count < MAXCOMMANDS) { commandArray[count, 0] = command;

// check if inputted command is for forwarding the turtle. If so, ask user for # of steps if (command == 5) { Console.Write("Enter the number of forward spaces: "); commandArray[count, 1] = int.Parse(Console.ReadLine()); }

++count;

// Ask user for command again Console.Write("Enter command (or 9 to quit)."); command = int.Parse(Console.ReadLine()); }

// call the method to execute the commands once all the commands have been received ExecuteCommands(); }

// method to execute the commands public void ExecuteCommands() { // set the initial details about the turtle before the turtle can be moved int commandNum = 0; // the present position of turtle in the array int turtleDirection = 0; // store the direction of the turtle int turtleDistance = 0; // store distance travelled by turtle int turtleCommand; //store the present turtle command

// get the position of the pen bool penDown = false; turtleXPos = 0; turtleYPos = 0; turtleCommand = commandArray[commandNum, 0];

// check if the commands are to be executed and there are still unexecuted commands in the array // execute the commands until the last one gets executed while (commandNum < count) { // find the commands and check wher the turtle is to be moved. Use switch-case to execute choice.

switch (turtleCommand) { case 1: penDown = false; // pen up break; case 2: penDown = true; // pen down break; case 3: turtleDirection = TurnRight(turtleDirection); // turn right break; case 4: turtleDirection = TurnLeft(turtleDirection); // turn left break; case 5: turtleDirection = commandArray[commandNum, 1]; // move MovePen(penDown, floor, turtleDirection, turtleDistance); break; case 6: Console.WriteLine(" TheDrawing is: "); // display the drawing PrintArray(floor); // call method to print the floor after the commands have been executed break; }

turtleCommand = commandArray[++commandNum, 0]; } }

// Method to make turtle face right public int TurnRight(int steps) { return ++steps > 3 ? 0 : steps; }

// Method to make turtle face left public int TurnLeft(int steps) { return --steps > 3 ? 0 : steps; }

/* method to get pen ahead * @param down checks whether pen is up or down * @param a is the array holding the command * @tdir is the direction in which the pen is to be moved * @tdist is the direction in which the pen is to be moved * @tdist is the distance to be travelled */ public void MovePen(bool down, int[,] a, int tdir, int tdist) { int j; // looping variable

// determine which way to move pen switch (tdir) { case 0: // move turtle to right for (j = 1; j <= tdist && turtleYPos + j < ARRAYSIZE; j++) if (down) a[turtleXPos, turtleYPos + j] = 1; turtleYPos += j - 1; break; case 1: // move the turtle down for (j = 1; j <= tdist && turtleXPos + j < ARRAYSIZE; j++) if (down) a[turtleXPos + j, turtleYPos] = 1; turtleXPos += j - 1; break; case 2: // move the turtle to left for (j = 1; j <= +tdist && turtleYPos - j >= 0; j++) if (down) a[turtleXPos, turtleYPos - j] = 1; turtleYPos -= j - 1; break; case 3: // move the turtle upward for (j = 1; j <= tdist && turtleXPos - j >= 0; j++) if (down) a[turtleXPos - j, turtleYPos] = 1; turtleXPos -= j - 1; break; } }

/* Method to display array drawing * @param a is the array showing the turtle drawings */ public void PrintArray(int[,] a) { // display array for (int i = 0; i < ARRAYSIZE; i++) { for (int j = 0; j < ARRAYSIZE; j++) Console.Write((a[i, j] == 1 ? "*" : " "));

Console.WriteLine(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago