-1

I am trying to create a "dragon curve" that uses left and right turns to create a shape on the drawing panel. The directions are generated recursively, where for a certain number of iterations or "levels" the direction adds on the previous string of turns to a single right turn plus the reverse complement of the previous string: previous string + "R" + the reverse complement of the previous string.

Based on my result, my complement method is not correctly making the reverse complement and I am not sure why. Here's is my code.

public static void main (String[] args)
   {
      Scanner scan = new Scanner(System.in);
      intro();
      int level = userlevel(scan);
      int size = userSize(scan);
      String fileName = BASE + level + TXT;
      recursion(level, fileName);
      drawDragon(fileName, size, level);
   }

   /**
   * Prints the introductory message
   **/
   public static void intro()
   {
      System.out.println("This program will generate a fractal called the Dragon Curve");
      System.out.println("first explored by John Heighway, Bruce Banks, and William Harter");
      System.out.println("at Nasa in teh 1960's");
   }

   /**
   * Inputs the user's desired level for the dragon curve
   * @param the Scanner object for recieving user inputs
   * @return the user's desired level
   **/
   public static int userlevel(Scanner scan)
   {
      String levelPrompt = "Enter the level of the fractcal you'd like to see (1-25): ";
      int level = MyUtils.getNumber(scan, levelPrompt, MINLEVEL, MAXLEVEL);
      return level;
   }

   /**
   * Inputs the user's desired drawing panel width and height
   * @param the Scanner object for recieving user inputs
   * @return the user's desired drawing panel size
   **/
   public static int userSize(Scanner scan)
   {
      String panelPrompt = "Enter the size of your drawing panel, in pixels (100-2000): ";
      int size = MyUtils.getNumber(scan, panelPrompt, MINSIZE, MAXSIZE);
      return size;
   }

    /**
    * Creates the reverse complement of the previous dragon curve string
    * @param the previous string used the in the recursive loop
    * @return the new reverse complement string
    **/  
   public static String complement(String previousString)
   {
      StringBuilder newString = new StringBuilder();
      char letter = '\0';
      for (int i=previousString.length(); i<0; i--)
      {
         letter = previousString.charAt(i);
         if (letter == 'L')
            newString.append('R');
         else if (letter == 'R')
            newString.append('L');
      }

      return newString.toString();
   }

   /** 
   * Creates the string of directions for the dragon curve using recursion
   * @param level - the user's desired level
   * @param fileName - the file in which the dragon curve string is located
   * @return the new set of directions for the curve after each loop
   **/
   public static String recursion(int level, String fileName)
   { 
      PrintStream output = MyUtils.createOutputFile(fileName);
      String newDirection = "";
      String directions = "";
      if (level==1)
      {
         directions = "R"; 
         output.print(directions);
      }  
      else
      {
         String previousString = recursion(level-1, fileName);
         String nextComplement = complement(previousString);
         newDirection = previousString + "R" + nextComplement;
         output.print(newDirection);
         System.out.println(newDirection);
         //output.flush();
         //output.close();
      }
      return newDirection;
   }

   /**
   * Draws the dragon curve on the drawing panel
   * @param fileName - the file where the dragon curve directions are located
   * @param size - the width and height of the drawing panel
   * @param level - the user's desired level
   **/
   public static void drawDragon(String fileName, int size, int level)
   { 
      Scanner fileScan = MyUtils.getFileScanner(fileName);
      System.out.println("Path generated, writing to file " + fileName + "...");
      String direction = fileScan.nextLine();
      System.out.println("Drawing Curve...");
      DragonDraw dd = new DragonDraw(size);
      dd.drawCurve(fileName, level);
   }
}

Here is the error: I am only getting repeated "R"'s.

Enter the level of the fractcal you'd like to see (1-25): 20
Enter the size of your drawing panel, in pixels (100-2000): 500
R
RR
RRR
RRRR
RRRRR
RRRRRR
RRRRRRR
RRRRRRRR
RRRRRRRRR
RRRRRRRRRR
RRRRRRRRRRR
RRRRRRRRRRRR
RRRRRRRRRRRRR
RRRRRRRRRRRRRR
RRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRR
Path generated, writing to file dragon20.txt...
Drawing Curve...

Thanks so much!

Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    `for (int i=previousString.length(); i<0; i--)` should be `for (int i = previousString.length() - 1; i >= 0; i--)`, because without `- 1` you'd get `IndexOutOfBoundsException`, and with `>= 0` the loop will actually begin to execute. – Andreas Jan 14 '20 at 02:34
  • Please see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). There is a lot of code there that is irrelevant to the question, and prevent us from running your code to reproduce the problem. – Andreas Jan 14 '20 at 02:38

1 Answers1

-1

Take a look at your for loop inside complement method. It is running while the variable i is less than zero, this should run while this variable is greater than zero. So change this for (int i=previousString.length(); i<0; i--) to for (int i=previousString.length() - 1; i >= 0; i--) and it shall work fine.

R. Karlus
  • 2,094
  • 3
  • 24
  • 48