Browse Source

Merge branch 'GameboardOutput' into develop

AIPlayer
FelixKrull 3 years ago
parent
commit
6d05b245bc
  1. 9
      src/main/java/Game.java
  2. 114
      src/main/java/Gameboard.java
  3. 22
      src/main/java/Player.java
  4. 79
      src/test/java/GameboardTest.java
  5. 20
      src/test/java/PlayerTest.java

9
src/main/java/Game.java

@ -49,6 +49,15 @@ public class Game {
players.add(new Player("Grün",30, 52, 29)); players.add(new Player("Grün",30, 52, 29));
} }
public String printGameboard(Game g, Player p) {
return gb.printBoard(g, p);
}
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
/** /**
* Check if the Player is allowed to roll the dice again * Check if the Player is allowed to roll the dice again
* @param dice Value of dice got from rolldice() * @param dice Value of dice got from rolldice()

114
src/main/java/Gameboard.java

@ -1,14 +1,20 @@
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
public class Gameboard { public class Gameboard {
int[] board; int[] board;
static String RED = "\u001b[31;1m";
static String BLUE = "\u001b[34;1m";
static String YELLOW = "\u001b[33;1m";
static String GREEN = "\u001b[32;1m";
static String RESET = "\u001b[0m";
public Gameboard() { public Gameboard() {
board = new int[56]; board = new int[56];
} }
public void initGameboard (){ public void initGameboard (){
for(int i = 0; i < 40; i++) {
for(int i = 0; i < 56; i++) {
if ( i % 10 == 0) { if ( i % 10 == 0) {
board[i] = 1; board[i] = 1;
} }
@ -21,9 +27,109 @@ public class Gameboard {
} }
} }
@Override
public String toString() {
return Arrays.toString(board);
public String printBoard(Game g, Player p) {
String[] f = getFiguresOnBoard(g);
String color = "";
switch (p.name) {
case "Rot" -> color = RED;
case "Blau" -> color = BLUE;
case "Gelb" -> color = YELLOW;
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 +" +---+ "+ YELLOW +"+---+ +---+\n" + RESET +
" +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" +
" | "+ f[16] +" | "+ YELLOW +" | "+ f[49] + YELLOW +" | "+ RESET +" | "+ f[22] +" |\n" +
" +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" +
" +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" +
" | "+ f[15] +" | "+ YELLOW +" | "+ f[50] + YELLOW +" | "+ RESET +" | "+ f[23] +" |\n" +
" +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" +
"+---+ +---+ +---+ +---+ +---+ "+ YELLOW +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" +
"| "+ f[10] +" | | "+ f[11] +" | | "+ f[12] +" | | "+ f[13] +" | | "+ f[14] +" | "+ YELLOW +" | "+ f[51] +" | "+ RESET +" | "+ f[24] +" | | "+ f[25] +" | | "+ f[26] +" | | "+ f[27] +" | | "+ f[28] +" | \n" +
"+---+ +---+ +---+ +---+ +---+ "+ YELLOW +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" +
"+---+ "+ BLUE +" +---+ +---+ +---+ +---+ "+ GREEN +"+---+ +---+ +---+ +---+ "+ RESET +" +---+ \n" +
"| "+ f[9] +" | "+ BLUE +" | "+ f[44] + BLUE +" | | "+ f[45] + BLUE + " | | "+ f[46] + BLUE + " | | "+ f[47] + BLUE +" | "+ GREEN +" | "+ f[55] + GREEN + " | | "+ f[54] + GREEN +" | | "+ f[53] + GREEN +" | | "+ f[52] + GREEN +" | "+ RESET +" | "+ f[29] +" | \n" +
"+---+ "+ BLUE +" +---+ +---+ +---+ +---+ "+ GREEN +"+---+ +---+ +---+ +---+ "+ RESET +" +---+ \n" +
"+---+ +---+ +---+ +---+ +---+ "+ RED +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" +
"| "+ f[8] +" | | "+ f[7] +" | | "+ f[6] +" | | "+ f[5] +" | | "+ f[4] +" | "+ RED +" | "+ f[43] + RED +" | "+ RESET +" | "+ f[34] +" | | "+ f[33] +" | | "+ f[32] +" | | "+ f[31] +" | | "+ f[30] +" | \n" +
"+---+ +---+ +---+ +---+ +---+ "+ RED +" +---+ "+ RESET +" +---+ +---+ +---+ +---+ +---+ \n" +
" +---+ "+ RED +" +---+ "+ RESET +" +---+\n" +
" | "+ f[3] +" | "+ RED +" | "+ f[42] + RED +" | "+ RESET +" | "+ f[35] +" |\n" +
" +---+ "+ RED +" +---+ "+ RESET +" +---+\n" +
" +---+ "+ RED +" +---+ "+ RESET +" +---+\n" +
" | "+ f[2] +" | "+ RED +" | "+ f[41] + RED +" | "+ RESET +" | "+ f[36] +" |\n" +
" +---+ "+ RED +" +---+ "+ RESET +" +---+\n" +
RED +"+---+ +---+"+ RESET +" +---+ "+ RED +" +---+ "+ RESET +" +---+ "+ GREEN +"+---+ +---+\n" +
RED +"| "+ f[56] + RED +" | | "+ f[57] + RED +" |"+ RESET +" | "+ f[1] +" | "+ RED +" | "+ f[40] + RED +" | "+ RESET +" | "+ f[37] +" | "+ GREEN +"| "+ f[68] + GREEN +" | | "+ f[69] + GREEN +" |\n" +
RED +"+---+ +---+"+ RESET +" +---+ "+ RED +" +---+ "+ RESET +" +---+ "+ GREEN +"+---+ +---+\n" +
RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" +
RED +"| "+ f[58] + RED +" | | "+ f[59] + RED +" |"+ RESET +" | "+ f[0] +" | | "+ f[39] +" | | "+ f[38] +" | "+ GREEN +"| "+ f[70] + GREEN +" | | "+ f[71] + GREEN +" |\n" +
RED +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ GREEN +"+---+ +---+\n" + RESET +
"===========================================================================\n" +
"Spieler " + color + p.name + RESET + " ist an der Reihe";
}
public static void main(String[] args) {
Game g = new Game();
System.out.println(g.gb.printBoard(g, g.players.get(0)));
} }
public String[] getFiguresOnBoard(Game g) {
String[] res = new String[72];
Arrays.fill(res, " ");
for (Player p : g.players) {
String color = "";
int start = 0;
switch (p.name) {
case "Rot" -> {
color = RED;
start = 56;
}
case "Blau" -> {
color = BLUE;
start = 60;
}
case "Gelb" -> {
color = YELLOW;
start = 64;
}
case "Grün" -> {
color = GREEN;
start = 68;
} }
}
Iterator<Figure> figureIt = p.figures.iterator();
int i = 1;
while (figureIt.hasNext()) {
Figure f = figureIt.next();
if (f.getPosition() == -1) {
res[start++] = color + i + RESET;
i++;
} else {
start++;
res[f.getPosition()] = color + i + RESET;
i++;
}
}
}
return res;
}
}
/*
0-39 Spielfelder
Rot Ziel: 40,41,42,43
Blau Ziel: 44,45,46,47
Gelb Ziel: 48,49,50,51
Grün Ziel: 52.53.54.55
Rot Daneben: 56,57,58,59
Blau Daneben: 60,61,62,63
Gelb Daneben: 64.65.66.67
Grün Daneben: 68.69,70,71
*/

22
src/main/java/Player.java

@ -69,19 +69,31 @@ public class Player {
* @return int figid of the figure chosen * @return int figid of the figure chosen
*/ */
public int choose(ArrayList<Integer> usableFigures) { public int choose(ArrayList<Integer> usableFigures) {
StringBuilder out = new StringBuilder("[");
Iterator<Integer> it = usableFigures.iterator();
for(int i = 0; i<usableFigures.size(); i++) {
out.append(usableFigures.get(i) + 1);
if(i < usableFigures.size()-1) {
out.append(",");
}
}
out.append("]");
scanner = new Scanner(System.in); scanner = new Scanner(System.in);
System.out.print("Wählen Sie eine Figur " + usableFigures.toString() + ": ");
System.out.print("Wählen Sie eine Figur " + out + ": ");
try{ try{
int input = scanner.nextInt(); int input = scanner.nextInt();
if (input > Collections.max(usableFigures) || input < Collections.min(usableFigures)) {
if(input == -1) {
System.exit(0);
}
if (input > Collections.max(usableFigures) + 1 || input < Collections.min(usableFigures) + 1) {
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 " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
"Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1) + " eingeben.");
return -1; return -1;
} }
return input;
return input - 1;
} 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 " + Collections.min(usableFigures) + " bis " + Collections.max(usableFigures) + " eingeben.");
"Bitte nur Zahlen von " + (Collections.min(usableFigures) + 1) + " bis " + (Collections.max(usableFigures) + 1)+ " eingeben.");
return -1; return -1;
} }
} }

79
src/test/java/GameboardTest.java

@ -6,15 +6,21 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.stream.Stream; import java.util.stream.Stream;
public class GameboardTest { public class GameboardTest {
private Gameboard gb; private Gameboard gb;
private Game g;
@BeforeEach @BeforeEach
void setup() { void setup() {
gb = new Gameboard(); gb = new Gameboard();
g = new Game();
} }
@Test @Test
@ -28,9 +34,6 @@ public class GameboardTest {
@Test @Test
void checkGameboardFilled() { void checkGameboardFilled() {
int[] expectedGameboard = new int[56]; int[] expectedGameboard = new int[56];
for(int i = 0; i < expectedGameboard.length; i++) {
expectedGameboard[i] = 0;
}
int [] givenGameboard = gb.board; int [] givenGameboard = gb.board;
assertThat(givenGameboard).describedAs("Initial Gameboard").isEqualTo(expectedGameboard); assertThat(givenGameboard).describedAs("Initial Gameboard").isEqualTo(expectedGameboard);
@ -60,16 +63,66 @@ public class GameboardTest {
); );
} }
@Test
void checkPrintGameboard() {
String expectedResult = "[1, 0, 0, 0, 0, 0, 0, 0, 0, 2, " +
"1, 0, 0, 0, 0, 0, 0, 0, 0, 2, " +
"1, 0, 0, 0, 0, 0, 0, 0, 0, 2, " +
"1, 0, 0, 0, 0, 0, 0, 0, 0, 2, " +
"3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]";
gb.initGameboard();
String calculatedResult = gb.toString();
assertThat(calculatedResult).describedAs("Print Gameboard").isEqualTo(expectedResult);
@ParameterizedTest
@MethodSource("FiguresOnBoardData")
void checkGetFiguresOnBoard (String testname, ArrayList<Integer> positions, String[] expectedResult) {
Iterator<Player> pIt = g.players.iterator();
Iterator<Integer> posIt = positions.iterator();
while(pIt.hasNext()) {
Player p = pIt.next();
for (Figure f : p.figures) {
f.setPosition(posIt.next());
}
}
String[] calculatedResult = g.gb.getFiguresOnBoard(g);
assertThat(calculatedResult).describedAs(testname).isEqualTo(expectedResult);
}
static Stream<Arguments> FiguresOnBoardData () {
return Stream.of(
Arguments.of(
"All Figures in Base",
new ArrayList<>(Arrays.asList(
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
)),
new String[] {
" "," "," "," "," "," "," "," "," "," ",
" "," "," "," "," "," "," "," "," "," ",
" "," "," "," "," "," "," "," "," "," ",
" "," "," "," "," "," "," "," "," "," ",
" "," "," "," "," "," "," "," "," "," ",
" "," "," "," "," "," ","\u001b[31;1m1\u001b[0m", "\u001b[31;1m2\u001b[0m","\u001b[31;1m3\u001b[0m","\u001b[31;1m4\u001b[0m",
"\u001b[34;1m1\u001b[0m","\u001b[34;1m2\u001b[0m","\u001b[34;1m3\u001b[0m","\u001b[34;1m4\u001b[0m","\u001b[33;1m1\u001b[0m","\u001b[33;1m2\u001b[0m","\u001b[33;1m3\u001b[0m","\u001b[33;1m4\u001b[0m","\u001b[32;1m1\u001b[0m","\u001b[32;1m2\u001b[0m",
"\u001b[32;1m3\u001b[0m","\u001b[32;1m4\u001b[0m"}
),
Arguments.of(
"Figures on Board",
new ArrayList<>(Arrays.asList(
0,1,3,5,-1,-1,-1,-1,10,12,16,-1,-1,-1,-1,-1
)),
new String[] {
"\u001b[31;1m1\u001b[0m","\u001b[31;1m2\u001b[0m"," ","\u001b[31;1m3\u001b[0m",
" ","\u001b[31;1m4\u001b[0m"," "," ",
" "," ","\u001b[33;1m1\u001b[0m"," ",
"\u001b[33;1m2\u001b[0m"," "," "," ",
"\u001b[33;1m3\u001b[0m"," "," "," ",
" "," "," "," ",
" "," "," "," ",
" "," "," "," ",
" "," "," "," ",
" "," "," "," ",
" "," "," "," ",
" "," "," "," ",
" "," "," "," ",
" "," "," "," ",
" "," "," "," ",
"\u001b[34;1m1\u001b[0m","\u001b[34;1m2\u001b[0m","\u001b[34;1m3\u001b[0m","\u001b[34;1m4\u001b[0m",
" "," "," ","\u001b[33;1m4\u001b[0m",
"\u001b[32;1m1\u001b[0m","\u001b[32;1m2\u001b[0m","\u001b[32;1m3\u001b[0m","\u001b[32;1m4\u001b[0m"}
)
);
} }
} }

20
src/test/java/PlayerTest.java

@ -174,16 +174,16 @@ public class PlayerTest {
static Stream<Arguments> checkChooseMethodData() { static Stream<Arguments> checkChooseMethodData() {
return Stream.of( return Stream.of(
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 1 choosen", "1\n", new ArrayList<>(Arrays.asList(0,1,2,3)), 0),
Arguments.of("Figure 2 choosen", "2\n", new ArrayList<>(Arrays.asList(0,1,2,3)), 1),
Arguments.of("Figure 3 choosen", "3\n", new ArrayList<>(Arrays.asList(0,1,2,3)),2),
Arguments.of("Figure 4 choosen", "4\n", new ArrayList<>(Arrays.asList(0,1,2,3)),3),
Arguments.of("Index out of bounds choosen", "16\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Index out of bounds choosen", "-2\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Input a letter or char", "g\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Input a empty string", "\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Input a special character", "&\n",new ArrayList<>(Arrays.asList(0,1,2,3)), -1),
Arguments.of("Only one Figure available", "1\n", new ArrayList<>(List.of(0)), 0),
Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1) Arguments.of("Figure not available", "4\n", new ArrayList<>(Arrays.asList(1,2)),-1)
); );
} }

Loading…
Cancel
Save