Browse Source

4gewinnt: refactored code with setter/getter for board

4gewinnt
Steffen Helmke 2 years ago
committed by Lorenz Hohmann
parent
commit
853e57a703
  1. 76
      src/main/java/de/tims/viergewinnt/ai/Logic.java
  2. 25
      src/test/java/de/tims/viergewinnt/ai/LogicTest.java

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

@ -14,7 +14,7 @@ import javax.swing.JPanel;
public class Logic {
int[][] board = new int[6][6];
int currentPlayer;
int currentPlayer = 1;
public void setField(int player, int row, int column) {
board[row][column] = player;
@ -32,13 +32,13 @@ public class Logic {
return currentPlayer;
}
public int playChip(int[][] playfield, int column, int player) {
public int playChip(int column, int player) {
//add board update
updateGui(playfield);
if(testForWin(playfield, player)) {
calcScore(playfield);
updateGui();
if(testForWin(player)) {
calcScore();
}
currentPlayer = (player % 2) + 1;
return 0;
@ -75,12 +75,12 @@ public class Logic {
return contentPanel;
}
public void updateGui(int[][] playfield) {
for(int i = 0; i < playfield.length; i++) {
for(int j = 0; j < playfield[i].length; j++) {
if(playfield[i][j] == 0) {
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 + 6 * i].setBackground(Color.white);
} else if(playfield[i][j] == 1) {
} else if(getField(i,j) == 1) {
gamefield[j + 6 * i].setBackground(Color.red);
} else {
gamefield[j + 6 * i].setBackground(Color.yellow);
@ -95,7 +95,7 @@ public class Logic {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < buttons.length; i++) {
if(e.getSource() == buttons[i]) {
playChip(board, i, currentPlayer);
playChip(i, currentPlayer);
break;
}
}
@ -103,11 +103,11 @@ public class Logic {
}
public boolean testForWin(int[][] playfield, int player) {
public boolean testForWin(int player) {
int chain = 0;
for(int i = 0; i < playfield[0].length; i++) {
for(int j = 0; j < playfield.length; j++) {
if(playfield[j][i] == player) {
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;
@ -116,9 +116,9 @@ public class Logic {
}
}
chain = 0;
for(int i = 0; i < playfield.length; i++) {
for(int j = 0; j < playfield[0].length; j++) {
if(playfield[i][j] == player) {
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;
@ -127,13 +127,13 @@ public class Logic {
}
}
chain = 0;
for(int i = 0; i < playfield.length - 3; i++) {
for(int j = 0; j < playfield[i].length - 3; j++) {
if(playfield[i][j] == player) {
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(playfield[i][j] == player) {
if(getField(i,j) == player) {
chain++;
if(chain == 4) return true;
} else {
@ -145,13 +145,13 @@ public class Logic {
}
}
chain = 0;
for(int i = 0; i < playfield.length - 3; i++) {
for(int j = playfield[i].length - 1; j > 2; j--) {
if(playfield[i][j] == player) {
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(playfield[i][j] == player) {
if(getField(i,j) == player) {
chain++;
if(chain == 4) return true;
} else {
@ -165,30 +165,30 @@ public class Logic {
return false;
}
public int calcScore(int[][] playfield) {
public int calcScore() {
int score = 1000;
for(int i = 0; i < playfield.length; i++) {
for(int i = 0; i < board.length; i++) {
int oldScore = score;
for(int j = 0; j < playfield[i].length; j++) {
if(playfield[i][j] != 0) {
for(int j = 0; j < board[i].length; j++) {
if(getField(i,j) != 0) {
score += 10;
}
}
if(score - (10 * playfield[0].length) == oldScore) {
score += (10 * playfield[0].length);
if(score - (10 * board[0].length) == oldScore) {
score += (10 * board[0].length);
}
}
for(int i = 0; i < playfield[0].length; i++) {
for(int j = 0; j < playfield.length; j++) {
if(playfield[j][i] == 0) {
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 == playfield.length - 1) {
score += (10 * playfield.length);
if(j == board.length - 1) {
score += (10 * board.length);
}
}
}
if(score == (1000 + 3 * (10 * playfield.length * playfield[0].length))) {
if(score == (1000 + 3 * (10 * board.length * board[0].length))) {
score += 500;
}
return score;

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

@ -22,33 +22,36 @@ class LogicTest {
@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);
}
}
boolean realResult = testObj.testForWin(playfield, player);
boolean realResult = testObj.testForWin(player);
assertEquals(expectedResult, realResult);
}
@Test
void playChipTest() {
int[][] playfield = new int[6][6];
int column = 0;
int player = 1;
int expectedResult = 0;
testObj.create4gewinntGui();
int realResult = testObj.playChip(playfield, column, player);
int realResult = testObj.playChip(column, player);
assertEquals(expectedResult, realResult);
}
@Test
void changePlayerTo2Test() {
int[][] playfield = new int[6][6];
int column = 0;
int player = 1;
int expectedResult = 2;
testObj.create4gewinntGui();
testObj.playChip(playfield, column, player);
testObj.playChip(column, player);
int realResult = testObj.currentPlayer;
assertEquals(expectedResult, realResult);
@ -56,13 +59,12 @@ class LogicTest {
@Test
void changePlayerTo1Test() {
int[][] playfield = new int[6][6];
int column = 0;
int player = 2;
int expectedResult = 1;
testObj.create4gewinntGui();
testObj.playChip(playfield, column, player);
testObj.playChip(column, player);
int realResult = testObj.currentPlayer;
assertEquals(expectedResult, realResult);
@ -71,8 +73,13 @@ class LogicTest {
@ParameterizedTest(name = "[{index}] {0} with {1}")
@MethodSource("playfieldForScore")
void calcScoreTest(String testName, int[][] playfield, int expectedResult) {
int realResult = testObj.calcScore(playfield);
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);
}

Loading…
Cancel
Save