package de.sebhn.algorithm.excersise4;

import java.math.BigInteger;

/**
 * Prints all path to a certain n
 */
public class PathNodes {

  private static int sumOfPaths;

  public static void main(String[] args) {
    int n = 1;
    sumOfPaths = 0;
    BigInteger numberOfPaths = calculatePaths(n, n, 0, "");
    System.out.println(numberOfPaths);
    System.out.println("sum: " + sumOfPaths);
  }

  private static BigInteger calculatePaths(int x, int y, int step, String path) {
    if (x < 0 || y < 0) {
      return BigInteger.ZERO;
    } else if (x == 0 && y == 0) {
      sumOfPaths += path.length() + 1;
      System.out.println(" " + path + "."); // print path
      return BigInteger.ONE;
    }

    BigInteger numberOfPaths = BigInteger.ZERO;

    if (y < x) {
      numberOfPaths = numberOfPaths.add(calculatePaths(x - 1, y, 0, "R" + path)); // right
    }
    if (y <= x) {
      numberOfPaths = numberOfPaths.add(calculatePaths(x, y - 1, 0, "U" + path)); // up
    }
    if (y >= x) {
      numberOfPaths = numberOfPaths.add(calculatePaths(x - 1, y - 1, 0, "F" + path)); // upRight
    }
    if (y > x + 1 && step != 2) {
      numberOfPaths = numberOfPaths.add(calculatePaths(x + 1, y - 1, 1, "L" + path)); // upLeft
    }
    if (y >= x && step != 1) {
      numberOfPaths = numberOfPaths.add(calculatePaths(x - 1, y + 1, 2, "D" + path)); // downRight
    }

    return numberOfPaths;
  }

}

@@ -221,130 +221,429 @@
 nt n
- = 1;%0A    sumOfPaths = 0;%0A    BigInteger numberOfPaths = calculatePaths(n, n, 0, %22%22);%0A    System.out.println(numberOfPaths
+;%0A    if (args.length == 0) %7B%0A      System.out.println(%22No arguments - use default n=2%22);%0A      n = 2;%0A    %7D else %7B%0A      try %7B%0A        n = Integer.parseInt(args%5B0%5D);%0A      %7D catch (NumberFormatException nfe) %7B%0A        System.out.println(%22Argument is not a number, use default n=2. Error-Message: %22 + nfe);%0A        n = 2;%0A      %7D%0A    %7D%0A%0A    sumOfPaths = 0;%0A    System.out.println(%22n=%22 + n);%0A    calculatePaths(n, n, 0, %22%22
 );%0A 
@@ -908,66 +908,8 @@
  1;%0A
-      System.out.println(%22 %22 + path + %22.%22); // print path%0A
     

add user input, error handling and remove some printouts