59 Commits

Author SHA1 Message Date
Jenkins 08bfd221d3 Merge commit '6ee0ec873ca03747940a4b30bf9307c11f6b0631' into HEAD 3 years ago
Steffen Helmke 6ee0ec873c 4gewinnt: refactored gui elements to enable variable boardsize 3 years ago
Steffen Helmke 645770c07c 4gewinnt: finished checkButtons method 3 years ago
Steffen Helmke d3ec638be0 4gewinnt: added checkButtons method 3 years ago
Steffen Helmke 29c241de55 4gewinnt: updated mutltiple methods to deactivate buttons on full columns 3 years ago
Steffen Helmke a6ed74febe 4gewinnt: updated playChip method and tests 3 years ago
Steffen Helmke 5614b7f180 4gewinnt: refactored and updated GameOver method and tests 3 years ago
Steffen Helmke a7d3315ecb 4gewinnt: finished clearField method 3 years ago
Steffen Helmke 44ff79411e 4gewinnt: added clearField method 3 years ago
Steffen Helmke caec8e73d7 4gewinnt: finished getFreePlaceMethod 3 years ago
Steffen Helmke 869355bddd 4gewinnt: getFreePlaceTestWithFilledField added 3 years ago
Steffen Helmke 8961708346 4gewinnt: added getFreePlace method 3 years ago
Steffen Helmke caf11d2ee2 4gewinnt: refactored setter and getter 3 years ago
Steffen Helmke ce2f8ddd8b 4gewinnt: added gameover method 3 years ago
Malte Schellhardt 2af74bef6b 4gewinnt: fixed typo in getBoardSize test name 3 years ago
Malte Schellhardt 1c7f4a513e 4gewinnt: fixed GuiTest because of new parameterized constructor 3 years ago
Malte Schellhardt bd09c5783b 4gewinnt: finished board creation with individual size 3 years ago
Malte Schellhardt 2d4b0be4e5 4gewinnt: added test for board with size 0 3 years ago
Malte Schellhardt 2530467f14 4gewinnt: added getter method for board size 3 years ago
Malte Schellhardt ddf26d199a 4gewinnt: added constructor to create game logic 3 years ago
Steffen Helmke f42de527a2 4gewinnt: added setter/getter for player2Score 3 years ago
Steffen Helmke 97c66b727e 4gewinnt: refactored attributs and test methods 3 years ago
Steffen Helmke f409fdcb75 4gewinnt: added getter for player1Score 3 years ago
Steffen Helmke 12cf4f330c 4gewinnt: added setter for player1Score 3 years ago
Steffen Helmke 92f33426d8 4gewinnt: refactored code with setter/getter for currentPlayer 3 years ago
Steffen Helmke 853e57a703 4gewinnt: refactored code with setter/getter for board 3 years ago
Steffen Helmke f03b32f0dd 4gewinnt: added getter for currentPlayer 3 years ago
Steffen Helmke 2d40913c30 4gewinnt: added setter for currentPlayer 3 years ago
Steffen Helmke e5abbc26f5 4gewinnt: added getter for board 3 years ago
Steffen Helmke affdaad4b4 4gewinnt: finished setter for Board 3 years ago
Steffen Helmke 9897836eec 4gewinnt: setter for Board with player 1 3 years ago
Steffen Helmke 10c67f8bcb 4gewinnt: updated playChip 3 years ago
Steffen Helmke 9f99538406 4gewinnt: calcScore refactored 3 years ago
Steffen Helmke 08bd0b7fad 4gewinnt: calcScore bonusForFullField 3 years ago
Steffen Helmke 3589384226 4gewinnt: calcScore pointsForAnyColumn 3 years ago
Steffen Helmke dcf1bde6f9 4gewinnt: calcScore pointsForFirstColumn 3 years ago
Steffen Helmke 0db7e779ba 4gewinnt: calcScore pointsForAnyRow 3 years ago
Steffen Helmke 9107574206 4gewinnt: calcScore pointsForFirstRow added 3 years ago
Steffen Helmke 6dd8f7a409 4gewinnt: calcScore pointsPerChip 3 years ago
Steffen Helmke e2bd09d713 4gewinnt: refactored calcScore testmethods 3 years ago
Steffen Helmke 09ed2fe825 4gewinnt: refactored playChip testmethods 3 years ago
Steffen Helmke 5c0d4221b8 4gewinnt: added calcScore method 3 years ago
Steffen Helmke 76a32694c0 4gewinnt: updateGui Method created and added to playChip 3 years ago
Steffen Helmke 6425dc9d9b 4gewinnt: change to player 1 after player 2 played added 3 years ago
Steffen Helmke 27c4b6122b 4gewinnt: change to player 2 after player 1 played added 3 years ago
Steffen Helmke 743162ef15 4gewinnt: refactored actionListener 3 years ago
Steffen Helmke 4c9bdb2f0c 4gewinnt: added playChip method 3 years ago
Steffen Helmke dc09cc88a9 4gewinnt: added actionListener to buttons 3 years ago
Steffen Helmke 12a1010b63 4gewinnt: refactored and externalized gui-testing 3 years ago
Steffen Helmke 973c03544b 4gewinnt: finished gui layout 3 years ago
Steffen Helmke 43abd90de1 4gewinnt: added gui-layout and testing methods 3 years ago
Steffen Helmke 0770df9ef8 4gewinnt: testForWin diagonal left win 3 years ago
Steffen Helmke 2639a99f67 4gewinnt: testForWin diagonal right win 3 years ago
Steffen Helmke 1cfa0df92f 4gewinnt: testForWin win in a row 3 years ago
Steffen Helmke 57bc37a4d8 4gewinnt: testForWin win in a column 3 years ago
Steffen Helmke bf73c5dc96 4gewinnt: testForWin column1 player1 check 3 years ago
Steffen Helmke a0add6efc0 4gewinnt: testForWin testmethod refactored 3 years ago
Steffen Helmke b111515622 4gewinnt: testForWin emtpy Field 3 years ago
Steffen Helmke 66b669d976 4gewinnt workingspace created 3 years ago
  1. 21
      src/main/java/de/tims/viergewinnt/ai/GuiTest.java
  2. 264
      src/main/java/de/tims/viergewinnt/ai/Logic.java
  3. 348
      src/test/java/de/tims/viergewinnt/ai/LogicTest.java

21
src/main/java/de/tims/viergewinnt/ai/GuiTest.java

@ -0,0 +1,21 @@
package de.tims.viergewinnt.ai;
import javax.swing.JFrame;
public class GuiTest {
JFrame frame;
public GuiTest() {
frame = new JFrame();
frame.setSize(500, 500);
Logic logic = new Logic(6);
frame.add(logic.create4gewinntGui());
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
GuiTest testGui = new GuiTest();
}
}

264
src/main/java/de/tims/viergewinnt/ai/Logic.java

@ -0,0 +1,264 @@
package de.tims.viergewinnt.ai;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Logic {
private int[][] board;
private int currentPlayer = 1;
private int player1Score = 0;
private int player2Score = 0;
public Logic(int size) {
this.board = new int[size][size];
this.buttons = new JButton[getBoardSize()];
this.gamefield = new JLabel[getBoardSize() * getBoardSize()];
}
public void setField(int player, int row, int column) {
this.board[row][column] = player;
}
public int getField(int row, int column) {
return this.board[row][column];
}
public void clearField() {
this.board = new int[getBoardSize()][getBoardSize()];
}
public int getBoardSize() {
return this.board.length;
}
public void setCurrentPlayer(int player) {
this.currentPlayer = player;
}
public int getCurrentPlayer() {
return this.currentPlayer;
}
public void setPlayer1Score(int score) {
this.player1Score = score;
}
public int getPlayer1Score() {
return this.player1Score;
}
public void setPlayer2Score(int score) {
this.player2Score = score;
}
public int getPlayer2Score() {
return this.player2Score;
}
public int getFreePlace(int column) {
int size = getBoardSize() - 1;
while(getField(size, column) != 0) {
size--;
if(size < 0) {
return -1;
}
}
return size;
}
public int playChip(int column) {
setField(getCurrentPlayer(), getFreePlace(column), column);
if(getFreePlace(column) == -1) {
buttons[column].setEnabled(false);
}
updateGui();
if(testForWin()) {
gameover();
} else if(!checkButtons()) {
gameover();
}
setCurrentPlayer((getCurrentPlayer() % 2) + 1);
return 0;
}
public boolean checkButtons() {
boolean res = false;
for(int i = 0; i < buttons.length; i++) {
res = buttons[i].isEnabled() | res;
}
return res;
}
JPanel contentPanel;
JPanel buttonPanel;
JPanel playfieldPanel;
JButton[] buttons;
JLabel[] gamefield;
public JPanel create4gewinntGui() {
contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS));
buttonPanel = new JPanel(new GridLayout(1, buttons.length));
playfieldPanel = new JPanel(new GridLayout(buttons.length, buttons.length));
contentPanel.add(buttonPanel);
contentPanel.add(playfieldPanel);
for(int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(Integer.toString(i+1));
buttons[i].addActionListener(new ButtonListener());
buttonPanel.add(buttons[i]);
}
for(int i = 0; i < gamefield.length; i++) {
gamefield[i] = new JLabel();
gamefield[i].setBorder(BorderFactory.createLineBorder(Color.gray));
gamefield[i].setOpaque(true);
gamefield[i].setBackground(Color.white);
playfieldPanel.add(gamefield[i]);
}
return contentPanel;
}
public void updateGui() {
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
if(getField(i,j) == 0) {
gamefield[j + getBoardSize() * i].setBackground(Color.white);
} else if(getField(i,j) == 1) {
gamefield[j + getBoardSize() * i].setBackground(Color.red);
} else {
gamefield[j + getBoardSize() * i].setBackground(Color.yellow);
}
}
}
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < buttons.length; i++) {
if(e.getSource() == buttons[i]) {
playChip(i);
break;
}
}
}
}
public boolean testForWin() {
int player = getCurrentPlayer();
int chain = 0;
for(int i = 0; i < board[0].length; i++) {
for(int j = 0; j < board.length; j++) {
if(getField(j,i) == player) {
chain++;
} else {
chain = 0;
}
if(chain == 4) return true;
}
}
chain = 0;
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(getField(i,j) == player) {
chain++;
} else {
chain = 0;
}
if(chain == 4) return true;
}
}
chain = 0;
for(int i = 0; i < board.length - 3; i++) {
for(int j = 0; j < board[i].length - 3; j++) {
if(getField(i,j) == player) {
chain++;
while(true) {
i++; j++;
if(getField(i,j) == player) {
chain++;
if(chain == 4) return true;
} else {
chain = 0;
break;
}
}
}
}
}
chain = 0;
for(int i = 0; i < board.length - 3; i++) {
for(int j = board[i].length - 1; j > 2; j--) {
if(getField(i,j) == player) {
chain++;
while(true) {
i++; j--;
if(getField(i,j) == player) {
chain++;
if(chain == 4) return true;
} else {
chain = 0;
break;
}
}
}
}
}
return false;
}
public int calcScore() {
int score = 1000;
for(int i = 0; i < board.length; i++) {
int oldScore = score;
for(int j = 0; j < board[i].length; j++) {
if(getField(i,j) != 0) {
score += 10;
}
}
if(score - (10 * board[0].length) == oldScore) {
score += (10 * board[0].length);
}
}
for(int i = 0; i < board[0].length; i++) {
for(int j = 0; j < board.length; j++) {
if(getField(j,i) == 0) {
break;
}
if(j == board.length - 1) {
score += (10 * board.length);
}
}
}
if(score == (1000 + 3 * (10 * board.length * board[0].length))) {
score += 500;
}
return score;
}
public void gameover() {
if(getCurrentPlayer() == 1) {
setPlayer1Score(calcScore());
} else {
setPlayer2Score(calcScore());
}
clearField();
for(int i = 0; i < getBoardSize(); i++) {
buttons[i].setEnabled(true);
}
updateGui();
}
}

348
src/test/java/de/tims/viergewinnt/ai/LogicTest.java

@ -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)
);
}
}
Loading…
Cancel
Save