Browse Source

Merge commit '90109e9a2588549f17e7f9f134577e98aacb58bb' into HEAD

master
Jenkins 2 years ago
parent
commit
0d5e17cfdc
  1. 49
      src/main/java/Game.java
  2. 76
      src/main/java/Gameboard.java
  3. 36
      src/test/java/GameTest.java

49
src/main/java/Game.java

@ -15,12 +15,14 @@ public class Game {
for (Player p : g.players) {
int c = 0;
int dice;
System.out.println("Spieler " + p.name + " an der Reihe.");
do {
clearScreen();
int figId;
dice = p.rollDice();
TimeUnit.SECONDS.sleep(1L);
System.out.println("Würfel: " + dice);
System.out.println(g.printGameboard(g,p,dice));
TimeUnit.MILLISECONDS.sleep(600L);
System.out.println("Würfel: "+ dice);
TimeUnit.MILLISECONDS.sleep(600L);
c++;
ArrayList<Integer> usableFigures = g.getUsableFigures(dice, p, g);
if(usableFigures.size() > 0) {
@ -28,8 +30,10 @@ public class Game {
figId = p.choose(usableFigures);
} while(figId == -1);
g.setFigure(figId, dice, p, g);
clearScreen();
System.out.println(g.printGameboard(g,p,dice));
}
} while (g.checkDice(dice, p, c));
} while (g.checkDice(dice, p, c, g));
if(p.checkGameWin(p.figures)) {
winner = p;
System.out.println("Spieler " + winner.name + " gewinnt!");
@ -49,8 +53,8 @@ public class Game {
players.add(new Player("Grün",30, 52, 29));
}
public String printGameboard(Game g, Player p) {
return gb.printBoard(g, p);
public String printGameboard(Game g, Player p, int dice) {
return gb.printBoard(g, p, dice);
}
public static void clearScreen() {
@ -63,12 +67,21 @@ public class Game {
* @param dice Value of dice got from rolldice()
* @param p active Player
* @param countRolls Counter how often the Player already rolled the dice
* @param g game instance
* @return true if Player can roll the dice another time
*/
public boolean checkDice(int dice, Player p, int countRolls) {
public boolean checkDice(int dice, Player p, int countRolls, Game g) {
int figuresInBase = p.checkFigureInBase(p.figures);
if(figuresInBase == 4) {
return countRolls < 3;
if(countRolls >= 3) return dice == 6;
if(figuresInBase == 4) {
return true;
} else if (figuresInBase < 4) {
for(int i = 0; i < 4 - figuresInBase; i++) {
if(checkFieldClear(p.startHome+3-i,p,g) == 0) {
return dice == 6;
}
}
return true;
} else return dice == 6;
}
@ -126,7 +139,7 @@ public class Game {
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() == p.startPos;
}
return f.getPosition() == -1;
} else if (f.getPosition() <= p.jumpToHome && (f.getPosition() + dice) > p.jumpToHome) {
@ -135,7 +148,9 @@ public class Game {
} else {
return false;
}
} else if(f.getPosition() != -1) {
} else if(f.getPosition() >= p.startHome && (f.getPosition() + dice) < p.startHome + 4) {
return g.checkFieldClear(f.getPosition() + dice, p, g) == 0;
} else if(f.getPosition() != -1 && f.getPosition() < 40) {
return g.checkFieldClear((f.getPosition() + dice) % 40, p, g) != 2;
}
return false;
@ -151,21 +166,23 @@ public class Game {
*/
public int setFigure(int figId, int dice, Player p, Game g) {
int preCalculated;
if(p.figures.get(figId).getPosition() == -1) {
if (p.figures.get(figId).getPosition() == -1) {
preCalculated = p.startPos;
} else {
} else if (p.figures.get(figId).getPosition() <= p.jumpToHome && (p.figures.get(figId).getPosition() + dice) > p.jumpToHome) {
preCalculated = p.startHome + (dice - (p.jumpToHome - p.figures.get(figId).getPosition()) - 1);
} else if (p.figures.get(figId).getPosition() >= p.startHome) {
preCalculated = p.figures.get(figId).getPosition() + dice;
}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;
return preCalculated;
}
}

76
src/main/java/Gameboard.java

@ -28,8 +28,9 @@ public class Gameboard {
}
public String printBoard(Game g, Player p) {
public String printBoard(Game g, Player p, int dice) {
String[] f = getFiguresOnBoard(g);
String[] d = getDice(dice);
String color = "";
switch (p.name) {
case "Rot" -> color = RED;
@ -38,11 +39,11 @@ public class Gameboard {
case "Grün" -> color = GREEN;
}
return "" +
BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" +
BLUE +"| "+ f[60] + BLUE +" | | "+ f[61] + BLUE +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] + YELLOW + " | | "+ f[65] + YELLOW +" |\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" +
BLUE +"| "+ f[62] + BLUE + " | | "+ f[63] + BLUE +" |"+ RESET +" | "+ f[17] +" | "+ YELLOW +" | "+ f[48] + YELLOW +" | "+ RESET +" | "+ f[21] +" | "+ YELLOW +"| "+ f[66] + YELLOW +" | | "+ f[67] + YELLOW +" |\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+"+ RESET +" ---------\n" +
BLUE +"| "+ f[60] + BLUE +" | | "+ f[61] + BLUE +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] + YELLOW + " | | "+ f[65] + YELLOW +" | "+ RESET + " | "+ d[0] +" "+ d[1] +" "+ d[2] +" |\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+"+ RESET +" | "+ d[3] +" |\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+"+ RESET +" | "+d[4]+" "+d[5]+" "+d[6]+" |\n" +
BLUE +"| "+ f[62] + BLUE + " | | "+ f[63] + BLUE +" |"+ RESET +" | "+ f[17] +" | "+ YELLOW +" | "+ f[48] + YELLOW +" | "+ RESET +" | "+ f[21] +" | "+ YELLOW +"| "+ f[66] + YELLOW +" | | "+ f[67] + YELLOW +" | "+ RESET +" ---------\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + RESET +
" +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" +
" | "+ f[16] +" | "+ YELLOW +" | "+ f[49] + YELLOW +" | "+ RESET +" | "+ f[22] +" |\n" +
@ -77,7 +78,68 @@ public class Gameboard {
public static void main(String[] args) {
Game g = new Game();
System.out.println(g.gb.printBoard(g, g.players.get(0)));
System.out.println(g.gb.printBoard(g, g.players.get(0), 6));
}
public String[] getDice (int dice) {
String[] res = new String[7];
switch (dice) {
case 1 -> {
res[0] = " ";
res[1] = " ";
res[2] = " ";
res[3] = "o";
res[4] = " ";
res[5] = " ";
res[6] = " ";
}
case 2 -> {
res[0] = "o";
res[1] = " ";
res[2] = " ";
res[3] = " ";
res[4] = " ";
res[5] = " ";
res[6] = "o";
}
case 3 -> {
res[0] = "o";
res[1] = " ";
res[2] = " ";
res[3] = "o";
res[4] = " ";
res[5] = " ";
res[6] = "o";
}
case 4 -> {
res[0] = "o";
res[1] = " ";
res[2] = "o";
res[3] = " ";
res[4] = "o";
res[5] = " ";
res[6] = "o";
}
case 5 -> {
res[0] = "o";
res[1] = " ";
res[2] = "o";
res[3] = "o";
res[4] = "o";
res[5] = " ";
res[6] = "o";
}
case 6 -> {
res[0] = "o";
res[1] = "o";
res[2] = "o";
res[3] = " ";
res[4] = "o";
res[5] = "o";
res[6] = "o";
}
}
return res;
}
public String[] getFiguresOnBoard(Game g) {

36
src/test/java/GameTest.java

@ -24,9 +24,6 @@ public class GameTest {
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
@ -37,7 +34,7 @@ public class GameTest {
while(it.hasNext()) {
it.next().setPosition(it2.next());
}
boolean calculatedResult = g.checkDice(dice, p1, c);
boolean calculatedResult = g.checkDice(dice, p1, c, g);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
}
@ -59,7 +56,7 @@ public class GameTest {
Arrays.asList(-1, -1, -1, -1),
1,
3,
true
false
),
Arguments.of("No figures on Field - d: 1 - c: 4",
Arrays.asList(-1, -1, -1, -1),
@ -78,6 +75,18 @@ public class GameTest {
6,
1,
true
),
Arguments.of("Figures on Field - d: 6 - c: 1",
Arrays.asList(42, -1, -1, -1),
5,
1,
false
),
Arguments.of("Figures on Field - d: 6 - c: 1",
Arrays.asList(42, 43, -1, -1),
5,
1,
true
)
);
}
@ -162,21 +171,30 @@ public class GameTest {
6,
Arrays.asList(-1, -1, -1, 0),
new ArrayList<>(List.of(3))
),
Arguments.of( //Würfel 6 - 1 Figur auf dem Spielfeld - StartFeld besetzt
"Figur auf Startfeld",
5,
Arrays.asList(-1, 41, -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);
void setFigureTest(String testname, int figId, int dice, int currentPosition, int expectedResult) {
p2.figures.get(figId).setPosition(currentPosition);
int calculatedResult = g2.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)
Arguments.of("Figur wird auf Feld gesetzt - Niemand gekicked", 0, 4, 5, 9),
Arguments.of("Figur wird auf Feld gesetzt - Jemand gekicked", 1, 4, 10, 14),
Arguments.of("Figur wird ins Haus gesetzt", 3, 4, 38, 42),
Arguments.of("Figur wird im Haus gesetzt", 3, 3, 41, 44)
);
}
}

Loading…
Cancel
Save