Browse Source

Merge branch 'GameLoop' into develop

AIPlayer
FelixKrull 3 years ago
parent
commit
ff29fc2a01
  1. 124
      src/main/java/Game.java
  2. 36
      src/main/java/Player.java
  3. 171
      src/test/java/GameTest.java
  4. 52
      src/test/java/PlayerTest.java

124
src/main/java/Game.java

@ -1,21 +1,125 @@
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import static java.lang.System.exit;
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) throws InterruptedException {
Game g = new Game(); 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() { 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",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;
} }
}
}

36
src/main/java/Player.java

@ -1,30 +1,25 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.*;
public class Player { public class Player {
String name; String name;
ArrayList<Figure> figures; ArrayList<Figure> figures;
int startPos;
int startHome; int startHome;
int endHome;
int jumpToHome;
Scanner scanner; Scanner scanner;
public Player (String name, int startHome, int endHome) {
public Player(String name, int startPos, int startHome, int jumpToHome) {
this.name = name; this.name = name;
this.startPos = startPos;
this.startHome = startHome; this.startHome = startHome;
this.endHome = endHome;
this.jumpToHome = jumpToHome;
figures = new ArrayList<>(); figures = new ArrayList<>();
for(int i = 0; i < 4; i++) { for(int i = 0; i < 4; i++) {
figures.add(new Figure()); figures.add(new Figure());
} }
} }
@Override
public String toString() {
return name;
}
public int rollDice() { public int rollDice() {
return (int) (Math.random() * 6 + 1); return (int) (Math.random() * 6 + 1);
} }
@ -34,39 +29,40 @@ public class Player {
Figure f; Figure f;
while(it.hasNext()) { while(it.hasNext()) {
f = it.next(); f = it.next();
if(!(f.getPosition() >= startHome && f.getPosition() <= endHome)) {
if(!(f.getPosition() >= startHome && f.getPosition() <= startHome+3)) {
return false; return false;
} }
} }
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(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;
} }
} }

171
src/test/java/GameTest.java

@ -1,13 +1,184 @@
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach; 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 { public class GameTest {
private Game g; private Game g;
private Player p1;
private Game g2;
private Player p2;
@BeforeEach @BeforeEach
void setup() { void setup() {
g = new Game(); 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)
);
}
} }

52
src/test/java/PlayerTest.java

@ -7,10 +7,7 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.*;
import java.util.stream.Stream; import java.util.stream.Stream;
public class PlayerTest { public class PlayerTest {
@ -19,7 +16,7 @@ public class PlayerTest {
@BeforeEach @BeforeEach
void setup() { void setup() {
p = new Player("Rot", 40, 43);
p = new Player("Rot",0, 40, 39);
} }
@Test @Test
@ -28,13 +25,6 @@ public class PlayerTest {
assertThat(calculatedResult).describedAs("Dice result").isBetween(1,6); assertThat(calculatedResult).describedAs("Dice result").isBetween(1,6);
} }
@Test
void testToString() {
String expectedResult = "Rot";
String calculatedResult = p.toString();
assertThat(calculatedResult).describedAs("Player Name").isEqualTo(expectedResult);
}
@Test @Test
void checkStartPositionOfFigures() { void checkStartPositionOfFigures() {
int expectedResult = -1; int expectedResult = -1;
@ -64,13 +54,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 +124,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 +133,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 +142,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 +151,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,29 +160,31 @@ 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)
); );
} }
@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<>(List.of(1)),1),
Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1)
); );
} }
} }
Loading…
Cancel
Save