Browse Source

Change Output of choose method to match gameboard

AIPlayer
FelixKrull 3 years ago
parent
commit
63ec77cf5c
  1. 19
      src/main/java/Player.java
  2. 20
      src/test/java/PlayerTest.java

19
src/main/java/Player.java

@ -50,19 +50,28 @@ public class Player {
} }
public int choose(ArrayList<Integer> usableFigures) { public int choose(ArrayList<Integer> usableFigures) {
StringBuilder out = new StringBuilder("[");
Iterator<Integer> it = usableFigures.iterator();
for(int i = 0; i<usableFigures.size(); i++) {
out.append(usableFigures.get(i) + 1);
if(i < usableFigures.size()-1) {
out.append(",");
}
}
out.append("]");
scanner = new Scanner(System.in); scanner = new Scanner(System.in);
System.out.print("Wählen Sie eine Figur " + usableFigures.toString() + ": ");
System.out.print("Wählen Sie eine Figur " + out + ": ");
try{ try{
int input = scanner.nextInt(); int input = scanner.nextInt();
if (input > Collections.max(usableFigures) || input < Collections.min(usableFigures)) {
if (input > Collections.max(usableFigures) + 1 || input < Collections.min(usableFigures) + 1) {
System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" + System.out.println("Die eingegebene Zahl war zu groß oder zu klein.\n" +
"Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
"Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1) + " eingeben.");
return -1; return -1;
} }
return input;
return input - 1;
} catch (Exception e) { } catch (Exception e) {
System.out.println("Die Eingabe hat keine Zahl bekommen.\n" + System.out.println("Die Eingabe hat keine Zahl bekommen.\n" +
"Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
"Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1)+ " eingeben.");
return -1; return -1;
} }
} }

20
src/test/java/PlayerTest.java

@ -174,16 +174,16 @@ public class PlayerTest {
static Stream<Arguments> checkChooseMethodData() { static Stream<Arguments> checkChooseMethodData() {
return Stream.of( return Stream.of(
Arguments.of("Figure 1 choosen", "1\n", new ArrayList<>(Arrays.asList(1,2,3,4)), 1),
Arguments.of("Figure 2 choosen", "2\n", new ArrayList<>(Arrays.asList(1,2,3,4)), 2),
Arguments.of("Figure 3 choosen", "3\n", new ArrayList<>(Arrays.asList(1,2,3,4)),3),
Arguments.of("Figure 4 choosen", "4\n", new ArrayList<>(Arrays.asList(1,2,3,4)),4),
Arguments.of("Index out of bounds choosen", "16\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1),
Arguments.of("Index out of bounds choosen", "-2\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1),
Arguments.of("Input a letter or char", "g\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1),
Arguments.of("Input a empty string", "\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1),
Arguments.of("Input a special character", "&\n",new ArrayList<>(Arrays.asList(1,2,3,4)), -1),
Arguments.of("Only one Figure available", "1\n", new ArrayList<>(List.of(1)),1),
Arguments.of("Figure 1 choosen", "1\n", new ArrayList<>(Arrays.asList(0,1,2,3)), 0),
Arguments.of("Figure 2 choosen", "2\n", new ArrayList<>(Arrays.asList(0,1,2,3)), 1),
Arguments.of("Figure 3 choosen", "3\n", new ArrayList<>(Arrays.asList(0,1,2,3)),2),
Arguments.of("Figure 4 choosen", "4\n", new ArrayList<>(Arrays.asList(0,1,2,3)),3),
Arguments.of("Index out of bounds choosen", "16\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Index out of bounds choosen", "-2\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Input a letter or char", "g\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Input a empty string", "\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Input a special character", "&\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Only one Figure available", "1\n", new ArrayList<>(List.of(0)), 0),
Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1) Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1)
); );
} }

Loading…
Cancel
Save