Browse Source

Implement method to get Figures on Gameboard

AIPlayer
FelixKrull 3 years ago
parent
commit
2d27095edf
  1. 55
      src/main/java/Gameboard.java
  2. 78
      src/test/java/GameboardTest.java

55
src/main/java/Gameboard.java

@ -1,4 +1,5 @@
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
public class Gameboard { public class Gameboard {
int[] board; int[] board;
@ -27,16 +28,16 @@ public class Gameboard {
} }
public String toString() {
String[] f = new String[72];
Arrays.fill(f, " ");
public String printBoard(Game g) {
String[] f;
f = getFiguresOnBoard(g);
return "" + return "" +
BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" + BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" +
BLUE +"| "+ f[60] +" | | "+ f[61] +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] +" | | "+ f[65] +" |\n" +
BLUE +"| "+ f[60] + BLUE +" | | "+ f[61] + BLUE +" |"+ RESET +" | "+ f[18] +" | | "+ f[19] +" | | "+ f[20] +" | "+ YELLOW +"| "+ f[64] +" | | "+ f[65] +" |\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" + BLUE +"+---+ +---+"+ RESET +" +---+ +---+ +---+ "+ YELLOW +"+---+ +---+\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" +
BLUE +"| "+ f[62] +" | | "+ f[63] +" |"+ RESET +" | "+ f[17] +" | "+ YELLOW +" | "+ f[48] +" | "+ RESET +" | "+ f[21] +" | "+ YELLOW +"| "+ f[66] +" | | "+ f[67] +" |\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + RESET +
BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" +
BLUE +"| "+ f[62] +" | | "+ f[63] +" |"+ RESET +" | "+ f[17] +" | "+ YELLOW +" | "+ f[48] +" | "+ RESET +" | "+ f[21] +" | "+ YELLOW +"| "+ f[66] +" | | "+ f[67] +" |\n" +
BLUE +"+---+ +---+"+ RESET +" +---+ "+ YELLOW +"+---+"+ RESET +" +---+ "+ YELLOW +"+---+ +---+\n" + RESET +
" +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" +
" | "+ f[16] +" | "+ YELLOW +" | "+ f[49] +" | "+ RESET +" | "+ f[22] +" |\n" + " | "+ f[16] +" | "+ YELLOW +" | "+ f[49] +" | "+ RESET +" | "+ f[22] +" |\n" +
" +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" + " +---+ "+ YELLOW +" +---+ "+ RESET +" +---+\n" +
@ -68,7 +69,45 @@ public class Gameboard {
public static void main(String[] args) { public static void main(String[] args) {
Game g = new Game(); Game g = new Game();
System.out.println(g.gb.toString());
System.out.println(g.gb.printBoard(g));
}
public String[] getFiguresOnBoard(Game g) {
String[] res = new String[72];
Arrays.fill(res, " ");
Iterator<Player> pIt = g.players.iterator();
while(pIt.hasNext()) {
Player p = pIt.next();
String color = new String();
int start = 0;
if(p.name.equals("Rot")) {
color = RED;
start = 56;
} else if (p.name.equals("Blau")) {
color = BLUE;
start = 60;
} else if (p.name.equals("Gelb")) {
color = YELLOW;
start = 64;
} else if (p.name.equals("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;
} }
} }

78
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
@ -60,16 +66,68 @@ 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();
Iterator<Figure> figureIt = p.figures.iterator();
while (figureIt.hasNext()) {
Figure f = figureIt.next();
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"}
)
);
}
} }
Loading…
Cancel
Save