Browse Source

Refactore: TicTacToe, cell

feature_TicTacToe_BasicPlayfield
kfkama 3 years ago
parent
commit
aad8ef8cf9
  1. 38
      src/main/java/TicTacToe/TicTacToeGame.java
  2. 8
      src/main/java/TicTacToe/cell.java
  3. 7
      src/test/java/TicTacToe/TicTacToeGameTest.java

38
src/main/java/TicTacToe/TicTacToeGame.java

@ -12,9 +12,12 @@ import javax.swing.JPanel;
public class TicTacToeGame extends JPanel {
private static final long serialVersionUID = 1L;
private static int width = 600, height = 600;
private static final int width = 600, height = 600;
private static final int maxPlayers = 3;
private static final int playFieldSize = 9;
public cell[] field;
public int player = 1;
public int playerID = 1;
public TicTacToeGame() {
this.setSize(width, height);
@ -33,7 +36,7 @@ public class TicTacToeGame extends JPanel {
}
public void initField() {
field = new cell[9];
field = new cell[playFieldSize];
for(int i = 0; i < field.length; i++) {
field[i] = new cell(this);
@ -68,30 +71,29 @@ public class TicTacToeGame extends JPanel {
}
public void endTurn() {
if(checkPlayfield() != 0) {
JOptionPane.showMessageDialog(getParent(),"Player: " + player + " Wins!");
JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!");
System.exit(0);
}
player++;
if(player >= 3) {
player = 1;
playerID++;
if(playerID >= maxPlayers) {
playerID = 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)) {
if ((field[0].playerID == playerID && field[1].playerID == playerID && field[2].playerID == playerID)
|| (field[0].playerID == playerID && field[3].playerID == playerID && field[6].playerID == playerID)
|| (field[8].playerID == playerID && field[5].playerID == playerID && field[2].playerID == playerID)
|| (field[8].playerID == playerID && field[7].playerID == playerID && field[6].playerID == playerID)
|| (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID)
|| (field[0].playerID == playerID && field[4].playerID == playerID && field[8].playerID == playerID)
|| (field[2].playerID == playerID && field[4].playerID == playerID && field[6].playerID == playerID)
|| (field[3].playerID == playerID && field[4].playerID == playerID && field[5].playerID == playerID)
|| (field[1].playerID == playerID && field[4].playerID == playerID && field[7].playerID == playerID)) {
return player;
return playerID;
}
return 0;

8
src/main/java/TicTacToe/cell.java

@ -7,7 +7,7 @@ import javax.swing.JButton;
public class cell extends JButton {
public int value = 0;
public int playerID = 0;
private TicTacToeGame ttt;
public cell(TicTacToeGame _ttt) {
@ -22,12 +22,12 @@ public class cell extends JButton {
}
protected void OnMouseClick() {
if(value == 0) {
value = ttt.player;
if(playerID == 0) {
playerID = ttt.playerID;
ttt.endTurn();
setEnabled(false);
switch (value) {
switch (playerID) {
case 1: setBackground(new Color(255,0,0));
break;
case 2: setBackground(new Color(0,0,255));

7
src/test/java/TicTacToe/TicTacToeGameTest.java

@ -1,15 +1,12 @@
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 {
@ -19,9 +16,9 @@ class TicTacToeGameTest {
TicTacToeGame ttt = new TicTacToeGame();
for(int i = 0; i < ttt.field.length; i++) {
ttt.field[i].value = _field[i];
ttt.field[i].playerID = _field[i];
}
ttt.player = _winner;
ttt.playerID = _winner;
int realWinner = ttt.checkPlayfield();
assertEquals(_winner, realWinner);

Loading…
Cancel
Save