2020-02-05 15:01:40 +01:00
|
|
|
package de.itsblue.ConnectFour;
|
|
|
|
|
2020-02-12 15:01:51 +01:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
|
2020-02-05 15:01:40 +01:00
|
|
|
|
|
|
|
public class GameBoard extends JPanel {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
public int BoardRows = 6;
|
|
|
|
public int BoardColumns = 7;
|
|
|
|
JPanel[][] BoardContainers = new JPanel[BoardColumns][BoardRows];
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
public boolean insertPlate(Plate plate, int column) {
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
// check if the column is out of range
|
|
|
|
if(column > BoardColumns-1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// search for an empty row
|
|
|
|
for(int i = BoardRows-1; i >= 0; i--) {
|
|
|
|
if(this.BoardContainers[column][i].getComponents().length == 0) {
|
|
|
|
// if the container is empty -> add the plate
|
|
|
|
this.BoardContainers[column][i].add(plate);
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-12 15:01:51 +01:00
|
|
|
}
|
2020-02-12 15:37:51 +01:00
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initBoard() {
|
2020-02-12 15:37:51 +01:00
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
// configure the main layout
|
|
|
|
this.setLayout(new GridLayout(this.BoardColumns,this.BoardRows+1));
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
// fill the grid with containers
|
|
|
|
for (int i = 0; i < BoardRows; i++) {
|
|
|
|
for (int j = 0; j < BoardColumns; j++) {
|
|
|
|
// create the container
|
2020-02-12 15:37:51 +01:00
|
|
|
this.BoardContainers[j][i] = new JPanel(new GridBagLayout());
|
|
|
|
this.BoardContainers[j][i].setPreferredSize(new Dimension(20, 20));
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
// add the container
|
2020-02-12 15:37:51 +01:00
|
|
|
this.add(this.BoardContainers[j][i]);
|
2020-02-12 15:01:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
|
|
|
|
/*void winningCondition() {
|
2020-02-12 15:01:51 +01:00
|
|
|
for (int i = 0; i <= 2; i++)
|
|
|
|
for (int j = 0; j <= 6; j++) {
|
|
|
|
if (GameBoard[i][j] == p && GameBoard[i + 1][j] == p && GameBoard[i + 2][j] == p
|
|
|
|
&& GameBoard[i + 3][j] == p) {
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i <= 5; i++)
|
|
|
|
for (int j = 0; j <= 3; j++) {
|
|
|
|
if (GameBoard[i][j] == p && GameBoard[i][j + 1] == p && GameBoard[i][j + 2] == p
|
|
|
|
&& GameBoard[i][j + 3] == p)
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i <= 2; i++)
|
|
|
|
for (int j = 0; j <= 3; j++) {
|
|
|
|
if (GameBoard[i][j] == p && GameBoard[i + 1][j + 1] == p && GameBoard[i + 2][j + 2] == p
|
|
|
|
&& GameBoard[i + 3][j + 3] == p)
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
for (int j = 6; j > 2; j--) {
|
|
|
|
if (GameBoard[i][j] == p && GameBoard[i + 1][j - 1] == p && GameBoard[i + 2][j - 2] == p
|
|
|
|
&& GameBoard[i + 3][j - 3] == p)
|
|
|
|
finish = true;
|
|
|
|
}
|
2020-02-13 19:24:32 +01:00
|
|
|
|
|
|
|
}*/
|
2020-02-12 15:01:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
/*void switchPlayer() {
|
2020-02-12 15:01:51 +01:00
|
|
|
if (p != 1)
|
|
|
|
p = 1;
|
|
|
|
else
|
|
|
|
p = 1;
|
2020-02-13 19:24:32 +01:00
|
|
|
}*/
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-05 15:01:40 +01:00
|
|
|
GameBoard() {
|
2020-02-13 19:24:32 +01:00
|
|
|
this.initBoard();
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-05 15:01:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|