carlitosway17 0 Junior Poster in Training

Hi I am supposed to create a program that prints out a rectangle made of asterisks using Turtle Graphics in Java I have attached a document describing the project. My code works fine and prints fine but the problem is I cant seem to get the right sequence to print out the asterisks rectangle.

Here is my code

// Exercise 7.21: TurtleGraphics.java
// Drawing turtle graphics based on turtle commands.
import java.util.Scanner;

public class TurtleGraphics
{
   private static final int MAXCOMMANDS = 100; // maximum size of command array
   private static final int SIZE = 20; // size of the drawing area

   private int[][] floor; // array representing the floor
   private int[][] commandArray; // list of commands

   private int count; // the current number of commands
   private int rowPos; // the row Position of the turtle
   private int columnPos; // the column Position of the turtle

   // enters the commands for the turtle graphics
   public void enterCommands()
   {
      Scanner input = new Scanner( System.in );
      
      count = 0;
      commandArray = new int[ MAXCOMMANDS ][ 2 ];
      floor = new int[ SIZE ][ SIZE ];

      System.out.print( "Enter command (9 to end input): " );
      int inputCommand = input.nextInt();

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

         // prompt for forward spaces
         if ( inputCommand == 5 )
         {
            System.out.print( "Enter forward spaces: " );
            commandArray[ count ][ 1 ] = input.nextInt();
         } // end if

         count++;

         System.out.print( "Enter command (9 to end input): " );
         inputCommand = input.nextInt();
      } // end while
   } // end method enterCommands

   // executes the commands in the command array
   public void executeCommands()
   {
      int commandNumber = 0; // the current position in the array
      int direction = 0; // the direction the turtle is facing
      int distance = 0; // the distance the turtle will travel
      int command; // the current command
      boolean penDown = false; // whether the pen is up or down
      rowPos = 0;
      columnPos = 0;

      command = commandArray[ commandNumber ][ 0 ];

      // continue executing commands until either reach the end
      // or reach the max commands
      while ( commandNumber < count )
      {
         //System.out.println("Executing...");
         // determine what command was entered 
         // and perform desired action
         switch ( command )
         {
            case 1: // pen up
               penDown = false;
               break;               
            case 2: // pen down
               penDown = true;
               break;               
            case 3: // turn right
               direction = turnRight( direction );
               break;               
            case 4: // turn left
               direction = turnLeft( direction );
               break;               
            case 5: // move
               distance = commandArray[ commandNumber ][ 1 ];
               movePen( penDown, floor, direction, distance );
               break;               
            case 6: // display the drawing
               System.out.println( "\nThe drawing is:\n" );
               printArray( floor );
               break;      
         }  // end switch
      
         command = commandArray[ ++commandNumber ][ 0 ];
      }  // end while
   } // end method executeCommands

   // method to turn turtle to the right
   public int turnRight( int d ) 
   {
      //return ++d > 3 ? 0 : d;
	  // turtle can only have 4 directions: 
	  // 0: right, 1: down, 2: left, 3: up. 
	  // if the turtle is moving right (0) currently
	  // the turnRight command will let the turtle move down (1)
	  // if the turtle is moving down (1) now, 
	  // the next turnRight command will let the turtle move left (2)
	  // if the turtle is moving left (2) now, 
	  // the next turnRight command will let the turtle move up (3)
	  // if the turtle is moving up (3) now, 
	  // the next turnRight command will let the turtle move right (0)

	  d++;
	  if (d>3)
		return 0; 
	  else
	    return d;
   } // end method turnRight

   // method to turn turtle to the left
   public int turnLeft( int d ) 
   {
        return --d < 0 ? 3 : d;
    // end method turnLeft

      // To do: reduce d by 1.
	  // Check if d < 0, return 3
	  // otherwise, return d
   } // end method turnLeft

   // method to move the pen 
   public void movePen( boolean down, int a[][], int dir, int dist )
   {
      int j; // looping variable

      // determine which way to move pen
      switch ( dir )
      {
         case 0: // move to right
            for ( j = 1; j <= dist && columnPos + j < SIZE; ++j )
               if ( down )
                  a[ rowPos ][ columnPos + j ] = 1;

            columnPos += j - 1;
            break;

         case 1: // move down
            // To do: add move down logic
            break;

         case 2: // move to left
            // To do: Add move left logic

         case 3: // move up
            for ( j = 1; j <= dist && rowPos - j >= 0; ++j )
               if ( down )
                  a[ rowPos - j ][ columnPos ] = 1;
            
            rowPos -= j - 1;
            break;   
      } // end switch
   } // end method movePen

   // method to print array drawing
   public void printArray( int[][] a )
   {
      // display array
      for ( int i = 0; i < SIZE; ++i )
      {
         for ( int j = 0; j < SIZE; ++j )
           System.out.print( ( a[ i ][ j ] == 1 ? "*" : "-" ) );
      
         System.out.println();
      } // end for
   } // end method printArray
} // end class TurtleGraphics
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.