Browse Source

Fix Up Player and PlayerTest Collection to Arraylist

AIPlayer
FelixKrull 3 years ago
parent
commit
8e1b4567ce
  1. 16
      src/main/java/Player.java
  2. 150
      src/test/java/PlayerTest.java

16
src/main/java/Player.java

@ -1,10 +1,10 @@
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
public class Player { public class Player {
String name; String name;
Figure[] figures;
ArrayList<Figure> figures;
int startHome; int startHome;
int endHome; int endHome;
@ -12,9 +12,9 @@ public class Player {
this.name = name; this.name = name;
this.startHome = startHome; this.startHome = startHome;
this.endHome = endHome; this.endHome = endHome;
figures = new Figure[4];
figures = new ArrayList<>();
for(int i = 0; i < 4; i++) { for(int i = 0; i < 4; i++) {
this.figures[i] = new Figure();
figures.add(new Figure());
} }
} }
@ -27,10 +27,10 @@ public class Player {
return (int) (Math.random() * 6 + 1); return (int) (Math.random() * 6 + 1);
} }
public boolean checkGameWin(Collection<Figure> figures) {
public boolean checkGameWin(ArrayList<Figure> figures) {
Iterator<Figure> it = figures.iterator(); Iterator<Figure> it = figures.iterator();
Figure f; Figure f;
for(;it.hasNext();) {
while(it.hasNext()) {
f = it.next(); f = it.next();
if(!(f.getPosition() >= startHome && f.getPosition() <= endHome)) { if(!(f.getPosition() >= startHome && f.getPosition() <= endHome)) {
return false; return false;
@ -39,10 +39,10 @@ public class Player {
return true; return true;
} }
public boolean checkFigureInBase(Collection<Figure> figures) {
public boolean checkFigureInBase(ArrayList<Figure> figures) {
Iterator<Figure> it = figures.iterator(); Iterator<Figure> it = figures.iterator();
Figure f; Figure f;
for(;it.hasNext();) {
while(it.hasNext()) {
f = it.next(); f = it.next();
if(f.getPosition() == -1) { if(f.getPosition() == -1) {
return true; return true;

150
src/test/java/PlayerTest.java

@ -6,6 +6,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
@ -36,9 +37,10 @@ public class PlayerTest {
@Test @Test
void checkStartPositionOfFigures() { void checkStartPositionOfFigures() {
int expectedResult = -1; int expectedResult = -1;
int calculatedResult = p.figures[0].position;
for(int i = 0; i < 4; i++) {
calculatedResult = p.figures[i].position;
Iterator<Figure> it = p.figures.iterator();
int calculatedResult = it.next().position;
while(it.hasNext()) {
calculatedResult = it.next().position;
if(expectedResult != calculatedResult) { if(expectedResult != calculatedResult) {
calculatedResult = 1; calculatedResult = 1;
break; break;
@ -49,69 +51,74 @@ public class PlayerTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("GameWinTestData") @MethodSource("GameWinTestData")
void checkGameWin(String testname, Collection<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
void checkGameWin(String testname, ArrayList<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
Iterator<Figure> it = figures.iterator(); Iterator<Figure> it = figures.iterator();
Iterator<Integer> it2 = positions.iterator(); Iterator<Integer> it2 = positions.iterator();
for(;it.hasNext();) {
while(it.hasNext()) {
it.next().setPosition(it2.next()); it.next().setPosition(it2.next());
} }
boolean calculatedResult = p.checkGameWin(figures); boolean calculatedResult = p.checkGameWin(figures);
assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("BaseTestData") @MethodSource("BaseTestData")
void checkFigureInBase(String testname, Collection<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
void checkFigureInBase(String testname, ArrayList<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
Iterator<Figure> it = figures.iterator(); Iterator<Figure> it = figures.iterator();
Iterator<Integer> it2 = positions.iterator(); Iterator<Integer> it2 = positions.iterator();
for(;it.hasNext();) {
while(it.hasNext()) {
it.next().setPosition(it2.next()); it.next().setPosition(it2.next());
} }
boolean calculatedResult = p.checkFigureInBase(figures); boolean calculatedResult = p.checkFigureInBase(figures);
assertThat(calculatedResult).describedAs("Check Win").isEqualTo(expectedResult);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
} }
static Stream<Arguments> GameWinTestData() { static Stream<Arguments> GameWinTestData() {
return Stream.of( return Stream.of(
Arguments.of("No Figures in House", Arguments.of("No Figures in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(-1, -1, -1, -1),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(-1, -1, -1, -1),
false), false),
Arguments.of("One Figure in House", Arguments.of("One Figure in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, -1, -1, -1),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, -1, -1, -1),
false), false),
Arguments.of("Two Figures in House", Arguments.of("Two Figures in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, -1, -1),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, -1, -1),
false), false),
Arguments.of("Three Figures in House", Arguments.of("Three Figures in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, 42, -1),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, 42, -1),
false), false),
Arguments.of("Four Figures in House", Arguments.of("Four Figures in House",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, 42, 43),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, 42, 43),
true) true)
); );
} }
@ -119,44 +126,49 @@ public class PlayerTest {
static Stream<Arguments> BaseTestData() { static Stream<Arguments> BaseTestData() {
return Stream.of( return Stream.of(
Arguments.of("Four Figures in Base", Arguments.of("Four Figures in Base",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(-1, -1, -1, -1),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(-1, -1, -1, -1),
true), true),
Arguments.of("Three Figures in Base", Arguments.of("Three Figures in Base",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, -1, -1, -1),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, -1, -1, -1),
true), true),
Arguments.of("Two Figures in Base", Arguments.of("Two Figures in Base",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, -1, -1),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, -1, -1),
true), true),
Arguments.of("One Figure in Base", Arguments.of("One Figure in Base",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, 42, -1),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, 42, -1),
true), true),
Arguments.of("No Figures in Base", Arguments.of("No Figures in Base",
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure()),
Arrays.asList(40, 41, 42, 43),
new ArrayList<>(
Arrays.asList(
new Figure(),
new Figure(),
new Figure(),
new Figure())
), Arrays.asList(40, 41, 42, 43),
false) false)
); );
} }

Loading…
Cancel
Save