package de.sebhn.algorithm.excersise4;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Solves a custom traveling sales person problem
 * 
 * @author Phi Long Tran <191624>
 * @author Dimitri Kapcikov <191510>
 * @author Manuel Wessner <191711>
 */
public class Turns {

  /**
   * n einlesen fr n<8 abbrechen - nicht mglich n-dim. boolean-array anlegen -> eintragen welche
   * belegt wurden wenn abstand >= 8 dann neuen ort in int-array an best. stelle hinzufgen wenn
   * letzter ort mind. 8 abstand zu ausgangsort, dann array in arraylist speichern
   */
  public boolean[] visitedCity;
  public ArrayList<int[]> listOfRoutes = new ArrayList<int[]>();
  public int[] route;
  private int amountOfCitiesToVisit;
  private int distance;

  Turns(int amountOfCitiesToVisit, int distance) {
    this.amountOfCitiesToVisit = amountOfCitiesToVisit;
    this.distance = distance;
    this.visitedCity = new boolean[amountOfCitiesToVisit];
    this.route = new int[amountOfCitiesToVisit];
    Arrays.fill(visitedCity, false);
    Arrays.fill(route, 0);
    visitedCity[0] = true;
  }

  Turns(int amountOfCitiesToVisit) {
    this(amountOfCitiesToVisit, 8);
  }

  public static void main(String[] args) {
    Turns turns = createTurnsWithuserInput();
    turns.calculateNeighbour(0, 1);
    turns.printWays();
    turns.printAmountOfWays();
  }

  private void printAmountOfWays() {
    System.out.println("There are " + listOfRoutes.size() + " stops");
  }

  private void printWays() {
    System.out.println("The ways are: ");
    for (int[] route : listOfRoutes) {
      System.out.println(Arrays.toString(route));
    }
  }

  private static Turns createTurnsWithuserInput() {
    int amountOfCities = -1;
    int distance = -1;
    System.out.println("Bitte geben Sie die Anzahl an Orte an, die besucht werden sollen.");
    try (Scanner sc = new Scanner(System.in)) {
      while (amountOfCities < 0) {
        try {
          int i = Integer.parseInt(sc.next());
          amountOfCities = i;
        } catch (Exception exception) {
          System.out.println("Bitte eine natrlich Zahl eingeben.");
        }
      }
      System.out.println("Bitte den Abstand angeben, der zwischen den Stdteindizes liegen soll.");
      while (distance < 0) {
        try {
          int i = Integer.parseInt(sc.next());
          distance = i;
        } catch (Exception exception) {
          System.out.println("Bitte eine natrlich Zahl eingeben.");
        }
      }

    }

    return new Turns(amountOfCities, distance);
  }

  public void calculateNeighbour(int cityNumber, int travelNumber) {
    route[travelNumber - 1] = cityNumber;
    if (travelNumber == amountOfCitiesToVisit && Math.abs(cityNumber) >= distance) {
      listOfRoutes.add(route.clone());
      return;
    }
    visitedCity[cityNumber] = true;
    for (int i = 0; i < amountOfCitiesToVisit; i++) {
      if (Math.abs(cityNumber - i) >= distance && !visitedCity[i]) {
        calculateNeighbour(i, travelNumber + 1);
      }
    }

    visitedCity[cityNumber] = false;
  }
}

@@ -649,23 +649,17 @@
 %3Cint%5B%5D%3E 
-listOfR
+r
 outes = 
@@ -672,21 +672,16 @@
 rayList%3C
-int%5B%5D
 %3E();%0A  p
@@ -1106,86 +1106,8 @@
  %7D%0A%0A
-  Turns(int amountOfCitiesToVisit) %7B%0A    this(amountOfCitiesToVisit, 8);%0A  %7D%0A%0A
   pu
@@ -1361,23 +1361,17 @@
 are %22 + 
-listOfR
+r
 outes.si
@@ -1487,23 +1487,17 @@
 route : 
-listOfR
+r
 outes) %7B
@@ -1690,73 +1690,42 @@
 ln(%22
-Bitte geben Sie die Anzahl an Orte an, die besucht werden sollen.
+How many cities should be visited?
 %22);%0A
@@ -1819,58 +1819,45 @@
     
-try %7B%0A          int i = Integer.parseInt(sc.next()
+amountOfCities = readInputFromuser(sc
 );%0A 
@@ -1865,290 +1865,303 @@
     
+%7D%0A
     
-amountOfCities = i;%0A        %7D catch (Exception exception) %7B%0A          System.out.println(%22Bitte eine natrlich Zahl eingeben.%22);%0A        %7D%0A      %7D%0A      System.out.println(%22Bitte den Abstand angeben, der zwischen den Stdteindizes liegen soll.%22);%0A      while (distance %3C 0) %7B%0A    
+  System.out.println(%22How far should the cities be apart of each other?%22);%0A      while (distance %3C 0) %7B%0A        distance = readInputFromuser(sc);%0A      %7D%0A    %7D%0A    return new Turns(amountOfCities, distance);%0A  %7D%0A%0A  private static int readInputFromuser(Scanner sc) %7B%0A    int toBeRead = -1;%0A
     
@@ -2176,17 +2176,16 @@
     
-    int i
+toBeRead
  = I
@@ -2220,162 +2220,121 @@
     
-      distance = i;%0A        %7D catch (Exception exception) %7B%0A          System.out.println(%22Bitte eine natrlich Zahl eingeben.%22);%0A        %7D%0A      %7D%0A
+%7D catch (NumberFormatException nfe) %7B%0A      System.out.println(%22Please retry with a valid input. %22 + nfe);
 %0A    %7D%0A
-%0A
     
@@ -2344,43 +2344,16 @@
 urn 
-new Turns(amountOfCities, distance)
+toBeRead
 ;%0A  
@@ -2561,15 +2561,9 @@
     
-listOfR
+r
 oute

change to english and some other minor improvements