Browse Source

Game end: Player wins, with Test

feature_TicTacToe_BasicPlayfield
kfkama 3 years ago
parent
commit
1b5b077704
  1. 25
      src/main/java/TicTacToe/TicTacToeGame.java
  2. 2
      src/main/java/TicTacToe/cell.java
  3. 40
      src/test/java/TicTacToe/TicTacToeGameTest.java

25
src/main/java/TicTacToe/TicTacToeGame.java

@ -5,6 +5,7 @@ import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@ -67,11 +68,33 @@ public class TicTacToeGame extends JPanel {
}
public void endTurn() {
if(checkPlayfield() != 0) {
JOptionPane.showMessageDialog(getParent(),"Player: " + player + " Wins!");
System.exit(0);
}
player++;
if(player >= 3) {
player = 1;
}
}
public int checkPlayfield() {
if ((field[0].value == player && field[1].value == player && field[2].value == player)
|| (field[0].value == player && field[3].value == player && field[6].value == player)
|| (field[8].value == player && field[5].value == player && field[2].value == player)
|| (field[8].value == player && field[7].value == player && field[6].value == player)
|| (field[0].value == player && field[4].value == player && field[8].value == player)
|| (field[0].value == player && field[4].value == player && field[8].value == player)
|| (field[2].value == player && field[4].value == player && field[6].value == player)
|| (field[3].value == player && field[4].value == player && field[5].value == player)
|| (field[1].value == player && field[4].value == player && field[7].value == player)) {
return player;
}
return 0;
}

2
src/main/java/TicTacToe/cell.java

@ -7,7 +7,7 @@ import javax.swing.JButton;
public class cell extends JButton {
int value = 0;
public int value = 0;
private TicTacToeGame ttt;
public cell(TicTacToeGame _ttt) {

40
src/test/java/TicTacToe/TicTacToeGameTest.java

@ -1,8 +1,48 @@
package TicTacToe;
import static org.junit.jupiter.api.Assertions.*;
import java.util.stream.Stream;
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;
import Arcade64.Arcade64.Code;
class TicTacToeGameTest {
@ParameterizedTest
@MethodSource("testFieldsWinning")
void testGameEndWin(int[] _field, int _winner) {
TicTacToeGame ttt = new TicTacToeGame();
for(int i = 0; i < ttt.field.length; i++) {
ttt.field[i].value = _field[i];
}
ttt.player = _winner;
int realWinner = ttt.checkPlayfield();
assertEquals(_winner, realWinner);
}
private static Stream<Arguments> testFieldsWinning(){
return Stream.of(
Arguments.of(new int[]{ 1,2,1,
2,2,2,
1,2,1}, 2),
Arguments.of(new int[]{ 2,1,2,
2,2,1,
1,1,1}, 1),
Arguments.of(new int[]{ 1,1,2,
1,2,2,
1,2,1}, 1),
Arguments.of(new int[]{ 2,1,1,
1,2,1,
1,1,2}, 2)
);
}
}
Loading…
Cancel
Save