Browse Source

Game end: Draw, with Test

feature_TicTacToe_BasicPlayfield
kfkama 3 years ago
parent
commit
072610ab1b
  1. 7
      src/main/java/TicTacToe/TicTacToeGame.java
  2. 32
      src/test/java/TicTacToe/TicTacToeGameTest.java

7
src/main/java/TicTacToe/TicTacToeGame.java

@ -18,6 +18,7 @@ public class TicTacToeGame extends JPanel {
public cell[] field; public cell[] field;
public int playerID = 1; public int playerID = 1;
public int turns = 0;
public TicTacToeGame() { public TicTacToeGame() {
this.setSize(width, height); this.setSize(width, height);
@ -76,6 +77,12 @@ public class TicTacToeGame extends JPanel {
System.exit(0); System.exit(0);
} }
turns++;
if(turns >= playFieldSize) {
JOptionPane.showMessageDialog(getParent(),"Draw!");
System.exit(0);
}
playerID++; playerID++;
if(playerID >= maxPlayers) { if(playerID >= maxPlayers) {
playerID = 1; playerID = 1;

32
src/test/java/TicTacToe/TicTacToeGameTest.java

@ -24,6 +24,21 @@ class TicTacToeGameTest {
assertEquals(_winner, realWinner); assertEquals(_winner, realWinner);
} }
@ParameterizedTest
@MethodSource("testFieldsDraw")
void testGameEndDraw(int[] _field) {
TicTacToeGame ttt = new TicTacToeGame();
for(int i = 0; i < ttt.field.length; i++) {
ttt.field[i].playerID = _field[i];
}
int noWinner = ttt.checkPlayfield();
assertEquals(0, noWinner);
}
private static Stream<Arguments> testFieldsWinning(){ private static Stream<Arguments> testFieldsWinning(){
return Stream.of( return Stream.of(
Arguments.of(new int[]{ 1,2,1, Arguments.of(new int[]{ 1,2,1,
@ -39,6 +54,23 @@ class TicTacToeGameTest {
1,2,1, 1,2,1,
1,1,2}, 2) 1,1,2}, 2)
); );
}
private static Stream<Arguments> testFieldsDraw(){
return Stream.of(
Arguments.of(new int[]{ 2,1,1,
2,2,2,
1,2,1}),
Arguments.of(new int[]{ 2,1,2,
2,2,1,
1,2,1}),
Arguments.of(new int[]{ 2,1,2,
1,2,2,
1,2,1}),
Arguments.of(new int[]{ 2,1,1,
1,2,2,
2,1,1})
);
} }

Loading…
Cancel
Save