Skip to main content
1 of 3
James P.
  • 413
  • 2
  • 8
  • 14

Attempt at recursive method to draw ascii diamond

I'm not so good with recursion so decided to tackle an exercise consisting in drawing this pattern using characters.

      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Here is my code so far. What could be improved ? Furthermore, does recursion suit a task like this or not ?

class Main
{
    public static void main (String[] args)
    {
        Main.drawDiamond("*", 0, 5 );
    }
    
    public static void drawDiamond( String seed, int turn, int centerLevel ){
        
        if( seed.length() <= 0 ){
            return;
        }
        
        char[] chars = new char[Math.abs(turn-centerLevel-1)];
        Arrays.fill(chars, ' ' );
        String spaces = new String(chars);
        
        System.out.print( spaces );
        
        System.out.println( seed );
        
        if( turn <= centerLevel ){
            seed += "**";
        } else {
            if( seed.length() >= 3 ) {
                 seed = seed.substring(1,seed.length()-1); 
            } else {
                seed = "";
            }
        }

        turn++;
        
        drawDiamond( seed, turn, centerLevel );
    }
}
James P.
  • 413
  • 2
  • 8
  • 14