Browse Source

Implement checkDice method

AIPlayer
FelixKrull 3 years ago
parent
commit
10848a7b46
  1. 43
      src/main/java/Game.java
  2. 7
      src/main/java/Player.java
  3. 63
      src/test/java/GameTest.java
  4. 14
      src/test/java/PlayerTest.java

43
src/main/java/Game.java

@ -1,21 +1,48 @@
import java.util.ArrayList;
public class Game { public class Game {
Gameboard gb; Gameboard gb;
Player p1;
Player p2;
Player p3;
Player p4;
ArrayList<Player> players;
public static void main(String[] args) { public static void main(String[] args) {
Game g = new Game(); Game g = new Game();
/*while (checkDice() != 0) {
int dice = g.players[0].rollDice();
System.out.println("Würfel: " + dice);
}*/
} }
public Game() { public Game() {
this.gb = new Gameboard(); this.gb = new Gameboard();
gb.initGameboard(); gb.initGameboard();
p1 = new Player("Rot", 40, 43);
p2 = new Player("Blau", 44, 47);
p3 = new Player("Gelb", 48, 51);
p4 = new Player("Grün", 52, 55);
players = new ArrayList<>();
players.add(new Player("Rot", 40, 43));
players.add(new Player("Blau", 44, 47));
players.add(new Player("Gelb", 48, 51));
players.add(new Player("Grün", 52, 55));
}
public int checkDice(int dice, Player p) {
int figuresInBase = p.checkFigureInBase(p.figures);
if(figuresInBase == 4) {
if(dice == 6) {
return 1;
} else {
return 0;
}
} else if(figuresInBase > 0) {
if(dice == 6) {
return 3;
} else {
return 2;
}
} else {
if(dice == 6) {
return 5;
} else {
return 4;
}
}
} }
} }

7
src/main/java/Player.java

@ -41,16 +41,17 @@ public class Player {
return true; return true;
} }
public boolean checkFigureInBase(ArrayList<Figure> figures) {
public int checkFigureInBase(ArrayList<Figure> figures) {
Iterator<Figure> it = figures.iterator(); Iterator<Figure> it = figures.iterator();
Figure f; Figure f;
int count = 0;
while(it.hasNext()) { while(it.hasNext()) {
f = it.next(); f = it.next();
if(f.getPosition() == -1) { if(f.getPosition() == -1) {
return true;
count++;
} }
} }
return false;
return count;
} }
public int choose() { public int choose() {

63
src/test/java/GameTest.java

@ -1,4 +1,14 @@
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.stream.Stream;
public class GameTest { public class GameTest {
@ -10,4 +20,57 @@ public class GameTest {
g = new Game(); g = new Game();
} }
@ParameterizedTest
@MethodSource("checkDiceTestData")
void checkDiceTest(String testname, Player testPlayer, Collection<Integer> positions, int dice, int expectedResult) {
Iterator<Figure> it = testPlayer.figures.iterator();
Iterator<Integer> it2 = positions.iterator();
while(it.hasNext()) {
it.next().setPosition(it2.next());
}
int calculatedResult = g.checkDice(dice, testPlayer);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
}
static Stream<Arguments> checkDiceTestData () {
return Stream.of(
Arguments.of("No figures on Field - not a 6",
new Player("Rot", 40, 43),
Arrays.asList(-1, -1, -1, -1),
1,
0
),
Arguments.of("No figures on Field - a 6",
new Player("Rot", 40, 43),
Arrays.asList(-1, -1, -1, -1),
6,
1
),
Arguments.of("Few Figures in Field - not a 6",
new Player("Rot", 40, 43),
Arrays.asList(25, -1, 13, -1),
2,
2
),
Arguments.of("Few Figures in Field - a 6",
new Player("Rot", 40, 43),
Arrays.asList(25, -1, 13, -1),
6,
3
),
Arguments.of("All Figures in Field - not a 6",
new Player("Rot", 40, 43),
Arrays.asList(2, 10, 15, 21),
3,
4
),
Arguments.of("All Figures in Field - a 6",
new Player("Rot", 40, 43),
Arrays.asList(2, 10, 15, 21),
6,
5
)
);
}
} }

14
src/test/java/PlayerTest.java

@ -64,13 +64,13 @@ public class PlayerTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("BaseTestData") @MethodSource("BaseTestData")
void checkFigureInBase(String testname, ArrayList<Figure> figures, Collection<Integer> positions, boolean expectedResult) {
void checkFigureInBase(String testname, ArrayList<Figure> figures, Collection<Integer> positions, int expectedResult) {
Iterator<Figure> it = figures.iterator(); Iterator<Figure> it = figures.iterator();
Iterator<Integer> it2 = positions.iterator(); Iterator<Integer> it2 = positions.iterator();
while(it.hasNext()) { while(it.hasNext()) {
it.next().setPosition(it2.next()); it.next().setPosition(it2.next());
} }
boolean calculatedResult = p.checkFigureInBase(figures);
int calculatedResult = p.checkFigureInBase(figures);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
} }
@ -134,7 +134,7 @@ public class PlayerTest {
new Figure(), new Figure(),
new Figure()) new Figure())
), Arrays.asList(-1, -1, -1, -1), ), Arrays.asList(-1, -1, -1, -1),
true),
4),
Arguments.of("Three Figures in Base", Arguments.of("Three Figures in Base",
new ArrayList<>( new ArrayList<>(
Arrays.asList( Arrays.asList(
@ -143,7 +143,7 @@ public class PlayerTest {
new Figure(), new Figure(),
new Figure()) new Figure())
), Arrays.asList(40, -1, -1, -1), ), Arrays.asList(40, -1, -1, -1),
true),
3),
Arguments.of("Two Figures in Base", Arguments.of("Two Figures in Base",
new ArrayList<>( new ArrayList<>(
Arrays.asList( Arrays.asList(
@ -152,7 +152,7 @@ public class PlayerTest {
new Figure(), new Figure(),
new Figure()) new Figure())
), Arrays.asList(40, 41, -1, -1), ), Arrays.asList(40, 41, -1, -1),
true),
2),
Arguments.of("One Figure in Base", Arguments.of("One Figure in Base",
new ArrayList<>( new ArrayList<>(
Arrays.asList( Arrays.asList(
@ -161,7 +161,7 @@ public class PlayerTest {
new Figure(), new Figure(),
new Figure()) new Figure())
), Arrays.asList(40, 41, 42, -1), ), Arrays.asList(40, 41, 42, -1),
true),
1),
Arguments.of("No Figures in Base", Arguments.of("No Figures in Base",
new ArrayList<>( new ArrayList<>(
Arrays.asList( Arrays.asList(
@ -170,7 +170,7 @@ public class PlayerTest {
new Figure(), new Figure(),
new Figure()) new Figure())
), Arrays.asList(40, 41, 42, 43), ), Arrays.asList(40, 41, 42, 43),
false)
0)
); );
} }

Loading…
Cancel
Save