Browse Source

Change Choose method to use List of Figures

AIPlayer
FelixKrull 3 years ago
parent
commit
459a35fe2d
  1. 14
      src/main/java/Player.java
  2. 24
      src/test/java/PlayerTest.java

14
src/main/java/Player.java

@ -1,6 +1,4 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.*;
public class Player { public class Player {
@ -56,20 +54,20 @@ public class Player {
return count; return count;
} }
public int choose() {
public int choose(ArrayList<Integer> usableFigures) {
scanner = new Scanner(System.in); scanner = new Scanner(System.in);
System.out.print("Wählen Sie eine Figur(1-4): ");
System.out.print("Wählen Sie eine Figur " + usableFigures.toString() + ": ");
try{ try{
int input = scanner.nextInt(); int input = scanner.nextInt();
if (input > 4 || input < 1) {
if (input > Collections.max(usableFigures) || input < Collections.min(usableFigures)) {
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 1 bis 4 eingeben.");
"Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
return -1; return -1;
} }
return input; return input;
} 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 1 bis 4 eingeben.");
"Bitte nur Zahlen von " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
return -1; return -1;
} }
} }

24
src/test/java/PlayerTest.java

@ -176,23 +176,25 @@ public class PlayerTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("checkChooseMethodData") @MethodSource("checkChooseMethodData")
void checkChoose(String testName, String input, int expectedResult) {
void checkChoose(String testName, String input,ArrayList<Integer> usableFiugres, int expectedResult) {
System.setIn(new ByteArrayInputStream(input.getBytes())); System.setIn(new ByteArrayInputStream(input.getBytes()));
int calculatedResult = p.choose();
int calculatedResult = p.choose(usableFiugres);
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult);
} }
static Stream<Arguments> checkChooseMethodData() { static Stream<Arguments> checkChooseMethodData() {
return Stream.of( return Stream.of(
Arguments.of("Figure 1 choosen", "1\n", 1),
Arguments.of("Figure 2 choosen", "2\n", 2),
Arguments.of("Figure 3 choosen", "3\n", 3),
Arguments.of("Figure 4 choosen", "4\n", 4),
Arguments.of("Index out of bounds choosen", "16\n", -1),
Arguments.of("Index out of bounds choosen", "-2\n", -1),
Arguments.of("Input a letter or char", "g\n", -1),
Arguments.of("Input a empty string", "\n", -1),
Arguments.of("Input a special character", "&\n", -1)
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<>(Arrays.asList(1)),1),
Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1)
); );
} }
} }
Loading…
Cancel
Save