Jonas Wagner
3 years ago
4 changed files with 323 additions and 60 deletions
-
124src/main/java/Game.java
-
36src/main/java/Player.java
-
171src/test/java/GameTest.java
-
52src/test/java/PlayerTest.java
@ -1,21 +1,125 @@ |
|||
import java.util.ArrayList; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
import static java.lang.System.exit; |
|||
|
|||
public class Game { |
|||
|
|||
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) throws InterruptedException { |
|||
Game g = new Game(); |
|||
Player winner; |
|||
while(true){ |
|||
for (Player p : g.players) { |
|||
int c = 0; |
|||
int dice; |
|||
System.out.println("Spieler " + p.name + " an der Reihe."); |
|||
do { |
|||
int figId; |
|||
dice = p.rollDice(); |
|||
TimeUnit.SECONDS.sleep(1L); |
|||
System.out.println("Würfel: " + dice); |
|||
c++; |
|||
ArrayList<Integer> usableFigures = g.getUsableFigures(dice, p, g); |
|||
if(usableFigures.size() > 0) { |
|||
do { |
|||
figId = p.choose(usableFigures); |
|||
} while(figId == -1); |
|||
g.setFigure(figId, dice, p, g); |
|||
} |
|||
} while (g.checkDice(dice, p, c)); |
|||
if(p.checkGameWin(p.figures)) { |
|||
winner = p; |
|||
System.out.println("Spieler " + winner.name + " gewinnt!"); |
|||
exit(42); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
public Game() { |
|||
this.gb = new Gameboard(); |
|||
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",0, 40, 39)); |
|||
players.add(new Player("Blau",10, 44, 9)); |
|||
players.add(new Player("Gelb",20, 48, 19)); |
|||
players.add(new Player("Grün",30, 52, 29)); |
|||
} |
|||
|
|||
public boolean checkDice(int dice, Player p, int countRolls) { |
|||
int figuresInBase = p.checkFigureInBase(p.figures); |
|||
if(figuresInBase == 4) { |
|||
return countRolls <= 3; |
|||
} else return dice == 6; |
|||
} |
|||
|
|||
public int checkFieldClear(int posToCheck, Player p, Game g) { |
|||
int mode; |
|||
for (Player currentPlayer : g.players) { |
|||
if (currentPlayer.name.equals(p.name)) { |
|||
mode = 2; |
|||
} else { |
|||
mode = 1; |
|||
} |
|||
for (Figure f : currentPlayer.figures) { |
|||
if (posToCheck == f.getPosition()) { |
|||
return mode; |
|||
} |
|||
} |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
public ArrayList<Integer> getUsableFigures(int dice, Player p, Game g) { |
|||
ArrayList<Integer> result = new ArrayList<>(); |
|||
for(int i = 0; i < p.figures.size(); i++) { |
|||
if(figureIsUsable(dice, i, p, g)) { |
|||
result.add(i); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
public boolean figureIsUsable(int dice, int figId, Player p, Game g) { |
|||
Figure f = p.figures.get(figId); |
|||
|
|||
if(p.checkFigureInBase(p.figures) > 0 && dice == 6){ |
|||
if(g.checkFieldClear(p.startPos, p, g) == 2) { |
|||
return f.getPosition() == p.startPos; //TODO Wenn Figur auf Startpos von anderer Figur blockiert ist |
|||
} |
|||
return f.getPosition() == -1; |
|||
} else if (f.getPosition() <= p.jumpToHome && (f.getPosition() + dice) > p.jumpToHome) { |
|||
if((f.getPosition() + dice) <= p.jumpToHome + 4) { |
|||
return g.checkFieldClear(f.getPosition() + dice - p.jumpToHome + p.startHome - 1, p, g) != 2; |
|||
} else { |
|||
return false; |
|||
} |
|||
} else if(f.getPosition() != -1) { |
|||
return g.checkFieldClear((f.getPosition() + dice) % 40, p, g) != 2; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
public int setFigure(int figId, int dice, Player p, Game g) { |
|||
int preCalculated; |
|||
if(p.figures.get(figId).getPosition() == -1) { |
|||
preCalculated = p.startPos; |
|||
} else { |
|||
preCalculated = (p.figures.get(figId).getPosition() + dice) % 40; |
|||
} |
|||
int kicked = 0; |
|||
for(Player currentPlayer : g.players) { |
|||
for(Figure currentFigure : currentPlayer.figures) { |
|||
if(currentFigure.getPosition() == preCalculated) { |
|||
currentFigure.setPosition(-1); |
|||
kicked = 1; |
|||
} |
|||
} |
|||
} |
|||
p.figures.get(figId).setPosition(preCalculated); |
|||
return kicked; |
|||
} |
|||
} |
|||
} |
@ -1,13 +1,184 @@ |
|||
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; |
|||
|
|||
import java.util.*; |
|||
import java.util.stream.Stream; |
|||
|
|||
public class GameTest { |
|||
|
|||
private Game g; |
|||
private Player p1; |
|||
private Game g2; |
|||
private Player p2; |
|||
|
|||
|
|||
@BeforeEach |
|||
void setup() { |
|||
g = new Game(); |
|||
p1 = g.players.get(0); |
|||
|
|||
g2 = new Game(); |
|||
p2 = g2.players.get(0); |
|||
p2.figures.get(0).setPosition(5); |
|||
p2.figures.get(1).setPosition(10); |
|||
g2.players.get(3).figures.get(1).setPosition(14); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("checkDiceTestData") |
|||
void checkDiceTest(String testname, Collection<Integer> positions, int dice, int c, boolean expectedResult) { |
|||
Iterator<Figure> it = p1.figures.iterator(); |
|||
Iterator<Integer> it2 = positions.iterator(); |
|||
while(it.hasNext()) { |
|||
it.next().setPosition(it2.next()); |
|||
} |
|||
boolean calculatedResult = g.checkDice(dice, p1, c); |
|||
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); |
|||
} |
|||
|
|||
static Stream<Arguments> checkDiceTestData () { |
|||
return Stream.of( |
|||
Arguments.of("No figures on Field - d: 1 - c: 1", |
|||
Arrays.asList(-1, -1, -1, -1), |
|||
1, |
|||
1, |
|||
true |
|||
), |
|||
Arguments.of("No figures on Field - d: 1 - c: 2", |
|||
Arrays.asList(-1, -1, -1, -1), |
|||
1, |
|||
2, |
|||
true |
|||
), |
|||
Arguments.of("No figures on Field - d: 1 - c: 3", |
|||
Arrays.asList(-1, -1, -1, -1), |
|||
1, |
|||
3, |
|||
true |
|||
), |
|||
Arguments.of("No figures on Field - d: 1 - c: 4", |
|||
Arrays.asList(-1, -1, -1, -1), |
|||
1, |
|||
4, |
|||
false |
|||
), |
|||
Arguments.of("Figures on Field - d: 1 - c: 1", |
|||
Arrays.asList(10, -1, 2, -1), |
|||
1, |
|||
1, |
|||
false |
|||
), |
|||
Arguments.of("Figures on Field - d: 6 - c: 1", |
|||
Arrays.asList(10, -1, 2, -1), |
|||
6, |
|||
1, |
|||
true |
|||
) |
|||
); |
|||
} |
|||
|
|||
@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, p1, g); |
|||
assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult); |
|||
} |
|||
|
|||
@Test |
|||
void checkFieldClearTestFieldTakenByOwnFigure() { |
|||
int expectedResult = 2; |
|||
p1.figures.get(1).setPosition(1); |
|||
int calculatedResult = g.checkFieldClear(1, p1, g); |
|||
assertThat(calculatedResult).describedAs("Check Field Clear").isEqualTo(expectedResult); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("usableFiguresData") |
|||
void getUsableFiguresTest(String testname, int dice, Collection<Integer> positions, ArrayList<Integer> expectedResult) { |
|||
Iterator<Figure> figureIt = p1.figures.iterator(); |
|||
Iterator<Integer> posIt = positions.iterator(); |
|||
while(figureIt.hasNext()) { |
|||
Figure f = figureIt.next(); |
|||
f.setPosition(posIt.next()); |
|||
} |
|||
|
|||
List<Integer> calculatedResult = g.getUsableFigures(dice, p1, g); |
|||
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); |
|||
} |
|||
|
|||
static Stream<Arguments> usableFiguresData () { |
|||
return Stream.of( |
|||
Arguments.of( //Würfel 1-6 - Keine Figur im Ziel - Alle Figuren auf dem Spielfeld - StartFeld frei - Keine Figur vor Ziel |
|||
"Alle Figuren", |
|||
1, |
|||
Arrays.asList(10, 25, 2, 17), |
|||
new ArrayList<>(Arrays.asList(0, 1, 2, 3)) |
|||
), |
|||
Arguments.of( //Würfel 6 - Keine Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld frei - Keine Figur vor Ziel |
|||
"Figuren die nicht am Spielfeld stehen", |
|||
6, |
|||
Arrays.asList(10, -1, -1, -1), |
|||
new ArrayList<>(Arrays.asList(1, 2, 3)) |
|||
), |
|||
Arguments.of( //Würfel 6 - Keine Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld besetzt - Keine Figur vor Ziel |
|||
"Figur die das Startfeld besetzt", |
|||
6, |
|||
Arrays.asList(10, 0, -1, -1), |
|||
new ArrayList<>(List.of(1)) |
|||
), |
|||
Arguments.of( //Würfel 5 - Keine Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld besetzt - Figur vor Ziel |
|||
"Figur vor dem Ziel - kann man benutzen", |
|||
5, |
|||
Arrays.asList(10, 37, -1, -1), |
|||
new ArrayList<>(Arrays.asList(0,1)) |
|||
), |
|||
Arguments.of( //Würfel 5 - Keine Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld besetzt - Figur vor Ziel |
|||
"Figur vor dem Ziel - kann man nicht benutzen", |
|||
5, |
|||
Arrays.asList(10, 39, -1, -1), |
|||
new ArrayList<>(List.of(0)) |
|||
), |
|||
Arguments.of( //Würfel 5 - Figur im Ziel - 1 Figur auf dem Spielfeld - StartFeld besetzt - Figur vor Ziel |
|||
"Nur Figuren die auf ein freies Feld kommen", |
|||
2, |
|||
Arrays.asList(10, 12, -1, -1), |
|||
new ArrayList<>(List.of(1)) |
|||
), |
|||
Arguments.of( //Würfel 6 - 1 Figur auf dem Spielfeld - StartFeld besetzt |
|||
"Figur auf Startfeld", |
|||
6, |
|||
Arrays.asList(-1, -1, -1, 0), |
|||
new ArrayList<>(List.of(3)) |
|||
) |
|||
); |
|||
} |
|||
|
|||
@ParameterizedTest |
|||
@MethodSource("setFigureData") |
|||
void setFigureTest(String testname, int figId, int dice, int expectedResult) { |
|||
int calculatedResult = g.setFigure(figId, dice, p2, g2); |
|||
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult); |
|||
} |
|||
|
|||
static Stream<Arguments> setFigureData () { |
|||
return Stream.of( |
|||
Arguments.of("Figur wird auf Feld gesetzt - Niemand gekicked", 0, 4, 0), |
|||
Arguments.of("Figur wird auf Feld gesetzt - Jemand gekicked", 1, 4, 1) |
|||
); |
|||
} |
|||
} |
|||
|
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue