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