|
@ -0,0 +1,348 @@ |
|
|
|
|
|
package de.tims.viergewinnt.ai; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
class LogicTest { |
|
|
|
|
|
|
|
|
|
|
|
private Logic testObj; |
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
|
|
void setUp() throws Exception { |
|
|
|
|
|
testObj = new Logic(6); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0} Player {2} {3}") |
|
|
|
|
|
@MethodSource("playfieldResults") |
|
|
|
|
|
void testForWinTest(String testName, int[][] playfield, int player, boolean expectedResult) { |
|
|
|
|
|
for(int i = 0; i < playfield.length; i++) { |
|
|
|
|
|
for(int j = 0; j < playfield[i].length; j++) { |
|
|
|
|
|
testObj.setField(playfield[i][j], i, j); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
testObj.setCurrentPlayer(player); |
|
|
|
|
|
|
|
|
|
|
|
boolean realResult = testObj.testForWin(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void playChipTest() { |
|
|
|
|
|
int column = 0; |
|
|
|
|
|
int player = 1; |
|
|
|
|
|
int expectedResult = 0; |
|
|
|
|
|
testObj.create4gewinntGui(); |
|
|
|
|
|
testObj.setCurrentPlayer(player); |
|
|
|
|
|
|
|
|
|
|
|
int realResult = testObj.playChip(column); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult == realResult, 1 == testObj.getField(5, 0)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void changePlayerTo2Test() { |
|
|
|
|
|
int column = 0; |
|
|
|
|
|
int player = 1; |
|
|
|
|
|
int expectedResult = 2; |
|
|
|
|
|
testObj.create4gewinntGui(); |
|
|
|
|
|
testObj.setCurrentPlayer(player); |
|
|
|
|
|
testObj.playChip(column); |
|
|
|
|
|
|
|
|
|
|
|
int realResult = testObj.getCurrentPlayer(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void changePlayerTo1Test() { |
|
|
|
|
|
int column = 0; |
|
|
|
|
|
int player = 2; |
|
|
|
|
|
int expectedResult = 1; |
|
|
|
|
|
testObj.create4gewinntGui(); |
|
|
|
|
|
testObj.setCurrentPlayer(player); |
|
|
|
|
|
testObj.playChip(column); |
|
|
|
|
|
|
|
|
|
|
|
int realResult = testObj.getCurrentPlayer(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0} with {1}") |
|
|
|
|
|
@MethodSource("playfieldForScore") |
|
|
|
|
|
void calcScoreTest(String testName, int[][] playfield, int expectedResult) { |
|
|
|
|
|
for(int i = 0; i < playfield.length; i++) { |
|
|
|
|
|
for(int j = 0; j < playfield[i].length; j++) { |
|
|
|
|
|
testObj.setField(playfield[i][j], i, j); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int realResult = testObj.calcScore(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void setFieldPlayer1Test() { |
|
|
|
|
|
int player = 1; |
|
|
|
|
|
int row = 0; |
|
|
|
|
|
int column = 0; |
|
|
|
|
|
|
|
|
|
|
|
testObj.setField(player, row, column); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, testObj.getField(row, column)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void setFieldPlayer2Test() { |
|
|
|
|
|
int player = 2; |
|
|
|
|
|
int row = 0; |
|
|
|
|
|
int column = 0; |
|
|
|
|
|
|
|
|
|
|
|
testObj.setField(player, row, column); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(player, testObj.getField(row, column)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void getFieldTest() { |
|
|
|
|
|
int player = 2; |
|
|
|
|
|
int row = 0; |
|
|
|
|
|
int column = 0; |
|
|
|
|
|
|
|
|
|
|
|
testObj.setField(player, row, column); |
|
|
|
|
|
int realResult = testObj.getField(row, column); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(player, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void setCurrentPlayerTest() { |
|
|
|
|
|
int player = 2; |
|
|
|
|
|
|
|
|
|
|
|
testObj.setCurrentPlayer(player); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(player, testObj.getCurrentPlayer()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void getCurrentPlayerTest() { |
|
|
|
|
|
int player = 2; |
|
|
|
|
|
|
|
|
|
|
|
testObj.setCurrentPlayer(player); |
|
|
|
|
|
int realResult = testObj.getCurrentPlayer(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(player, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void setPlayer1ScoreTest() { |
|
|
|
|
|
int expectedResult = 500; |
|
|
|
|
|
testObj.setPlayer1Score(expectedResult); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, testObj.getPlayer1Score()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void getPlayer1ScoreTest() { |
|
|
|
|
|
int expectedResult = 500; |
|
|
|
|
|
testObj.setPlayer1Score(expectedResult); |
|
|
|
|
|
|
|
|
|
|
|
int realResult = testObj.getPlayer1Score(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void createGameLogicTest() { |
|
|
|
|
|
int size = 6; |
|
|
|
|
|
Logic expectedResult = testObj; |
|
|
|
|
|
Logic realResult = new Logic(size); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult.getClass(), realResult.getClass()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "[{index}] {0}") |
|
|
|
|
|
@MethodSource("boardSizeParameters") |
|
|
|
|
|
void getBoardSizeTest(String testName, int size) { |
|
|
|
|
|
int expectedResult = size; |
|
|
|
|
|
int realResult = new Logic(size).getBoardSize(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void gameoverTest() { |
|
|
|
|
|
testObj.setField(1, 0, 0); |
|
|
|
|
|
testObj.create4gewinntGui(); |
|
|
|
|
|
|
|
|
|
|
|
testObj.gameover(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1010 == testObj.getPlayer1Score(), 0 == testObj.getField(0,0)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void clearFieldTest() { |
|
|
|
|
|
testObj.setField(1, 0, 0); |
|
|
|
|
|
|
|
|
|
|
|
testObj.clearField(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(0, testObj.getField(0, 0)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void clearFieldTestCompleteClear() { |
|
|
|
|
|
testObj.setField(1, 5, 5); |
|
|
|
|
|
|
|
|
|
|
|
testObj.clearField(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(0, testObj.getField(5, 5)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void getFreePlaceTestWithEmptyField() { |
|
|
|
|
|
assertEquals(testObj.getBoardSize() - 1, testObj.getFreePlace(0)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void getFreePlaceTestWithFilledField() { |
|
|
|
|
|
testObj.setField(1, testObj.getBoardSize() - 1, 0); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(testObj.getBoardSize() - 2, testObj.getFreePlace(0)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void getFreePlaceTestWithAnyField() { |
|
|
|
|
|
testObj.setField(1, testObj.getBoardSize() - 1, 0); |
|
|
|
|
|
testObj.setField(1, testObj.getBoardSize() - 2, 0); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(testObj.getBoardSize() - 3, testObj.getFreePlace(0)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void checkButtonsTestTrue() { |
|
|
|
|
|
testObj.create4gewinntGui(); |
|
|
|
|
|
boolean expectedResult = true; |
|
|
|
|
|
|
|
|
|
|
|
boolean realResult = testObj.checkButtons(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void checkButtonsTestFalse() { |
|
|
|
|
|
testObj.create4gewinntGui(); |
|
|
|
|
|
boolean expectedResult = false; |
|
|
|
|
|
for(int i = 0; i < testObj.buttons.length; i++) { |
|
|
|
|
|
testObj.buttons[i].setEnabled(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
boolean realResult = testObj.checkButtons(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(expectedResult, realResult); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Stream<Arguments> playfieldResults() { |
|
|
|
|
|
int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 0, 0, 0, 0}}; |
|
|
|
|
|
int[][] playfield2 = {{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 1, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 1, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 1, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 1, 0, 0}}; |
|
|
|
|
|
int[][] playfield3 = {{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 1, 1, 1, 0, 0}}; |
|
|
|
|
|
int[][] playfield4 = {{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 1, 0, 0, 0, 0}, |
|
|
|
|
|
{2, 2, 1, 0, 0, 0}, |
|
|
|
|
|
{2, 2, 2, 1, 0, 0}}; |
|
|
|
|
|
int[][] playfield5 = {{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 1}, |
|
|
|
|
|
{0, 0, 0, 0, 1, 2}, |
|
|
|
|
|
{0, 0, 0, 1, 2, 2}, |
|
|
|
|
|
{0, 0, 1, 1, 2, 2}}; |
|
|
|
|
|
return Stream.of( |
|
|
|
|
|
Arguments.of("emptyField", new int[6][6], 1, false), |
|
|
|
|
|
Arguments.of("4 in column1 player1", playfield1, 1, true), |
|
|
|
|
|
Arguments.of("4 in a column player1", playfield2, 1, true), |
|
|
|
|
|
Arguments.of("4 in a row player1", playfield3, 1, true), |
|
|
|
|
|
Arguments.of("4 diagonal right player1", playfield4, 1, true), |
|
|
|
|
|
Arguments.of("4 diagonal left player1", playfield5, 1, true) |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Stream<Arguments> playfieldForScore() { |
|
|
|
|
|
int[][] playfield1 = {{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 1, 2, 0, 0}, |
|
|
|
|
|
{0, 0, 1, 2, 0, 0}, |
|
|
|
|
|
{0, 0, 1, 2, 0, 0}, |
|
|
|
|
|
{0, 0, 1, 1, 2, 0}}; |
|
|
|
|
|
int[][] playfield2 = {{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 1, 1, 2, 2, 2}}; |
|
|
|
|
|
int[][] playfield3 = {{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 1, 2, 2, 1, 2}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{0, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 1, 1, 2, 2, 2}}; |
|
|
|
|
|
int[][] playfield4 = {{2, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{2, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{2, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 0, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 0, 0, 0, 0}}; |
|
|
|
|
|
int[][] playfield5 = {{2, 0, 1, 0, 0, 0}, |
|
|
|
|
|
{2, 0, 1, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 2, 0, 0, 0}, |
|
|
|
|
|
{2, 0, 2, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 2, 0, 0, 0}, |
|
|
|
|
|
{1, 0, 1, 0, 0, 0}}; |
|
|
|
|
|
int[][] playfield6 = {{1, 1, 1, 1, 2, 2}, |
|
|
|
|
|
{2, 1, 1, 1, 1, 2}, |
|
|
|
|
|
{2, 1, 1, 2, 1, 2}, |
|
|
|
|
|
{2, 1, 1, 2, 1, 2}, |
|
|
|
|
|
{2, 1, 2, 2, 1, 2}, |
|
|
|
|
|
{2, 1, 2, 2, 1, 2}}; |
|
|
|
|
|
return Stream.of( |
|
|
|
|
|
Arguments.of("emptyField", new int[6][6], 1000), |
|
|
|
|
|
Arguments.of("9 chips played", playfield1, 1090), |
|
|
|
|
|
Arguments.of("first row filled", playfield2, 1120), |
|
|
|
|
|
Arguments.of("2 rows filled", playfield3, 1240), |
|
|
|
|
|
Arguments.of("first column filled", playfield4, 1120), |
|
|
|
|
|
Arguments.of("2 column filled", playfield5, 1240), |
|
|
|
|
|
Arguments.of("fullyFilled", playfield6, 2580) |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Stream<Arguments> boardSizeParameters() { |
|
|
|
|
|
return Stream.of( |
|
|
|
|
|
Arguments.of("board with size 0", 0), |
|
|
|
|
|
Arguments.of("board with size 6", 6) |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |