2020-02-13 19:46:00 +01:00
|
|
|
/*
|
|
|
|
Connect four - written in java
|
|
|
|
Copyright (C) 2020 Oliver Schappacher and Dorian Zedler
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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-14 19:21:49 +01:00
|
|
|
/**
|
|
|
|
* The rows of the board
|
|
|
|
*/
|
2020-02-13 19:24:32 +01:00
|
|
|
public int BoardRows = 6;
|
2020-02-14 19:21:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The columns of the board
|
|
|
|
*/
|
2020-02-13 19:24:32 +01:00
|
|
|
public int BoardColumns = 7;
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
/**
|
|
|
|
* Array containing all plate containers
|
|
|
|
*/
|
|
|
|
PlateContainer[][] BoardContainers = new PlateContainer[BoardColumns][BoardRows];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
GameBoard() {
|
|
|
|
this.initBoard();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to insert a plate into a specific column
|
|
|
|
*
|
|
|
|
* @param plate Plate object to insert
|
|
|
|
* @param column The column to insert the plate into
|
|
|
|
* @return true if the inserton was successfull, false if the column is full
|
|
|
|
*/
|
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
|
2020-02-14 19:21:49 +01:00
|
|
|
if (column > BoardColumns - 1)
|
2020-02-13 19:24:32 +01:00
|
|
|
return false;
|
2020-02-14 19:21:49 +01:00
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
// search for an empty row
|
2020-02-14 19:21:49 +01:00
|
|
|
for (int i = BoardRows - 1; i >= 0; i--) {
|
|
|
|
if (!this.BoardContainers[column][i].containsPlate()) {
|
2020-02-13 19:24:32 +01:00
|
|
|
// if the container is empty -> add the plate
|
2020-02-14 19:21:49 +01:00
|
|
|
this.BoardContainers[column][i].insertPlate(plate);
|
2020-02-17 08:38:16 +01:00
|
|
|
|
|
|
|
this.checkContainerForWin(column, i);
|
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
/**
|
|
|
|
* Function to clear the board
|
|
|
|
*/
|
|
|
|
public void clearBoard() {
|
|
|
|
// search for an empty row
|
|
|
|
for (int c = 0; c < BoardColumns; c++) {
|
|
|
|
for (int r = 0; r < BoardRows; r++) {
|
|
|
|
// create the container
|
|
|
|
this.BoardContainers[c][r].removePlate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 15:37:51 +01:00
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
/**
|
|
|
|
* Function to fill the board with containers
|
|
|
|
*/
|
|
|
|
public void initBoard() {
|
2020-02-13 19:24:32 +01:00
|
|
|
// configure the main layout
|
2020-02-14 19:21:49 +01:00
|
|
|
this.setLayout(new GridLayout(this.BoardRows, this.BoardColumns));
|
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-14 19:21:49 +01:00
|
|
|
this.BoardContainers[j][i] = new PlateContainer();
|
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-14 19:21:49 +01:00
|
|
|
}
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-16 19:46:57 +01:00
|
|
|
public int getSuggestedContainerSize(Dimension parentSize) {
|
2020-02-14 19:21:49 +01:00
|
|
|
int containerSize;
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-16 19:46:57 +01:00
|
|
|
if (parentSize.getWidth() / this.BoardColumns > parentSize.getHeight() / this.BoardRows)
|
|
|
|
containerSize = (int) parentSize.getHeight() / this.BoardRows;
|
2020-02-14 19:21:49 +01:00
|
|
|
else
|
2020-02-16 19:46:57 +01:00
|
|
|
containerSize = (int) parentSize.getWidth() / this.BoardColumns;
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-16 19:46:57 +01:00
|
|
|
return containerSize;
|
|
|
|
}
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-16 19:46:57 +01:00
|
|
|
/**
|
|
|
|
* Override paint function to rescale all caontainers on every repaint
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void paint(Graphics g) {
|
|
|
|
|
|
|
|
System.out.println("updating sizes");
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
for (PlateContainer[] plateContainers : BoardContainers) {
|
|
|
|
for (PlateContainer plateContainer : plateContainers) {
|
2020-02-16 19:46:57 +01:00
|
|
|
plateContainer.setPreferredSize(new Dimension(this.getSuggestedContainerSize(this.getSize()),
|
|
|
|
this.getSuggestedContainerSize(this.getSize())));
|
2020-02-14 19:21:49 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
super.paint(g);
|
2020-02-05 15:01:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 19:46:57 +01:00
|
|
|
@Override
|
|
|
|
public void setPreferredSize(Dimension preferredSize) {
|
|
|
|
Dimension newSize = new Dimension(this.getSuggestedContainerSize(preferredSize) * this.BoardColumns,
|
|
|
|
this.getSuggestedContainerSize(preferredSize) * this.BoardRows);
|
|
|
|
super.setPreferredSize(newSize);
|
|
|
|
}
|
|
|
|
|
2020-02-17 08:38:16 +01:00
|
|
|
public boolean checkContainerForWin(int c, int r) {
|
|
|
|
|
|
|
|
System.out.println("checking container c=" + c + " r=" + r + " for win");
|
|
|
|
|
|
|
|
// if there is no plate in the container to check
|
|
|
|
// -> return false
|
|
|
|
if (this.getPlateContainer(c, r) == null || !this.getPlateContainer(c, r).containsPlate())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// check all possible winnings clockwise
|
|
|
|
int[] sums = new int[8];
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
PlateContainer currentContainer = null;
|
|
|
|
|
|
|
|
for (int pc = 0; pc < 8; pc++) {
|
|
|
|
currentContainer = null;
|
|
|
|
|
|
|
|
int checkC = 0;
|
|
|
|
int checkR = 0;
|
|
|
|
|
|
|
|
switch (pc) {
|
|
|
|
case 0:
|
|
|
|
// check top
|
|
|
|
checkC = c;
|
|
|
|
checkR = r + i;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// check top right vert
|
|
|
|
checkC = c + i;
|
|
|
|
checkR = r + i;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// check right
|
|
|
|
checkC = c + i;
|
|
|
|
checkR = r;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// check bottom right vert
|
|
|
|
checkC = c + i;
|
|
|
|
checkR = r - i;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
checkC = c;
|
|
|
|
checkR = r - i;
|
|
|
|
// check bottom
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
// check bottom left vert
|
|
|
|
checkC = c - i;
|
|
|
|
checkR = r - i;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
// check left
|
|
|
|
checkC = c - i;
|
|
|
|
checkR = r;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
// check top left vert
|
|
|
|
checkC = c - i;
|
|
|
|
checkR = r + i;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
currentContainer = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentContainer = this.getPlateContainer(checkC, checkR);
|
|
|
|
|
|
|
|
if (currentContainer != null && currentContainer.containsPlate())
|
|
|
|
sums[pc] += currentContainer.getContainedPlate().getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int sum : sums) {
|
|
|
|
if (Math.abs(sum) == 4) {
|
|
|
|
if (sum > 0)
|
|
|
|
System.out.println("negative won");
|
|
|
|
else
|
|
|
|
System.out.println("positive won");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlateContainer getPlateContainer(int containerColumn, int containerRow) {
|
|
|
|
|
|
|
|
if (this.BoardContainers.length > containerColumn && containerColumn >= 0
|
|
|
|
&& this.BoardContainers[containerColumn].length > containerRow && containerRow >= 0)
|
|
|
|
return this.BoardContainers[containerColumn][containerRow];
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
/*
|
|
|
|
* void winningCondition() { 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 &&
|
2020-02-17 08:38:16 +01:00
|
|
|
* GameBoard[i + 3][j - 3] == p) finish = true; } }
|
2020-02-14 19:21:49 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* void switchPlayer() { if (p != 1) p = 1; else p = 1; }
|
|
|
|
*/
|
2020-02-05 15:01:40 +01:00
|
|
|
}
|