Browse Source

Create method checkFieldClear

AIPlayer
Jonas Wagner 3 years ago
parent
commit
3586cfad3e
  1. 26
      src/main/java/Game.java
  2. 34
      src/test/java/GameTest.java

26
src/main/java/Game.java

@ -1,3 +1,4 @@
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Iterator;
@ -16,7 +17,8 @@ public class Game {
do {
dice = p.rollDice();
c++;
if(p.checkFigureInBase(p.figures) == 4 && dice == 6) {
//g.checkFieldClear(p.startPos, p, g)
if(p.checkFigureInBase(p.figures) > 0 && dice == 6) {
int figId = p.choose() - 1;
//checkIfKicked
p.figures.get(figId).setPosition(p.startPos);
@ -47,4 +49,26 @@ public class Game {
return countRolls <= 3;
} else return dice == 6;
}
public int checkFieldClear(int posToCheck, Player p, Game g) {
int mode;
for (Player currentPlayer : g.players) {
//System.out.println("Player");
if (currentPlayer.name.equals(p.name)) {
System.out.println("TEST");
mode = 2;
} else {
System.out.println("TEST2");
mode = 1;
}
for (Figure f : currentPlayer.figures) {
//System.out.println("Figure");
System.out.println(f.getPosition());
if (posToCheck == f.getPosition()) {
return mode;
}
}
}
return 0;
}
}

34
src/test/java/GameTest.java

@ -1,6 +1,7 @@
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@ -13,24 +14,26 @@ import java.util.stream.Stream;
public class GameTest {
private Game g;
private Player p;
private Player p1;
private Player p2;
@BeforeEach
void setup() {
g = new Game();
p = new Player("Rot", 1, 40, 43);
p1 = new Player("Rot", 1, 40, 43);
p2 = new Player("Blau",10, 44, 47);
}
@ParameterizedTest
@MethodSource("checkDiceTestData")
void checkDiceTest(String testname, Collection<Integer> positions, int dice, int c, boolean expectedResult) {
Iterator<Figure> it = p.figures.iterator();
Iterator<Figure> it = p1.figures.iterator();
Iterator<Integer> it2 = positions.iterator();
while(it.hasNext()) {
it.next().setPosition(it2.next());
}
boolean calculatedResult = g.checkDice(dice, p, c);
boolean calculatedResult = g.checkDice(dice, p1, c);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
}
@ -74,4 +77,27 @@ public class GameTest {
)
);
}
@Test
void checkFieldClearTestFieldNotTaken() {
int expectedResult = 0;
int calculatedResult = g.checkFieldClear(1, p1, g);
assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult);
}
@Test
void checkFieldClearTestFieldTakenByOtherPlayer() {
int expectedResult = 1;
g.players.get(1).figures.get(0).setPosition(1);
int calculatedResult = g.checkFieldClear(1, g.players.get(0), g);
assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult);
}
@Test
void checkFieldClearTestFieldTakenByOwnFigure() {
int expectedResult = 2;
g.players.get(0).figures.get(1).setPosition(1);
int calculatedResult = g.checkFieldClear(1, g.players.get(0), g);
assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult);
}
}
Loading…
Cancel
Save