|
|
@ -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; |
|
|
|