162 lines
4.9 KiB
Java
162 lines
4.9 KiB
Java
/*
|
|
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/>.
|
|
*/
|
|
|
|
package de.itsblue.ConnectFour;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
public class GameBoard extends JPanel {
|
|
|
|
/**
|
|
*
|
|
*/
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private boolean isInited = false;
|
|
|
|
/**
|
|
* The rows of the board
|
|
*/
|
|
public int BoardRows = 6;
|
|
|
|
/**
|
|
* The columns of the board
|
|
*/
|
|
public int BoardColumns = 7;
|
|
|
|
/**
|
|
* Array containing all plate containers
|
|
*/
|
|
PlateContainer[][] BoardContainers = new PlateContainer[BoardColumns][BoardRows];
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
GameBoard() {
|
|
this.setPreferredSize(new Dimension(this.BoardColumns * 50, this.BoardRows * 50));
|
|
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
|
|
*/
|
|
public boolean insertPlate(Plate plate, int column) {
|
|
|
|
// 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].containsPlate()) {
|
|
// if the container is empty -> add the plate
|
|
this.BoardContainers[column][i].insertPlate(plate);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Function to fill the board with containers
|
|
*/
|
|
public void initBoard() {
|
|
// configure the main layout
|
|
this.setLayout(new GridLayout(this.BoardRows, this.BoardColumns));
|
|
|
|
// fill the grid with containers
|
|
for (int i = 0; i < BoardRows; i++) {
|
|
for (int j = 0; j < BoardColumns; j++) {
|
|
// create the container
|
|
this.BoardContainers[j][i] = new PlateContainer();
|
|
|
|
// add the container
|
|
this.add(this.BoardContainers[j][i]);
|
|
}
|
|
}
|
|
|
|
this.isInited = true;
|
|
}
|
|
|
|
@Override
|
|
public void paint(Graphics g) {
|
|
|
|
System.out.println("updating sizes");
|
|
int containerSize;
|
|
|
|
if (this.getSize().getWidth() / this.BoardColumns > this.getSize().getHeight() / this.BoardRows)
|
|
containerSize = (int) this.getSize().getHeight() / this.BoardRows;
|
|
else
|
|
containerSize = (int) this.getSize().getWidth() / this.BoardColumns;
|
|
|
|
containerSize = 30;
|
|
|
|
System.out.println(containerSize);
|
|
System.out.println(this.getSize());
|
|
|
|
for (PlateContainer[] plateContainers : BoardContainers) {
|
|
for (PlateContainer plateContainer : plateContainers) {
|
|
//plateContainer.setPreferredSize(new Dimension(containerSize, containerSize));
|
|
}
|
|
}
|
|
|
|
super.paint(g);
|
|
}
|
|
|
|
/*
|
|
* 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 &&
|
|
* GameBoard[i + 3][j - 3] == p) finish = true; }
|
|
*
|
|
* }
|
|
*/
|
|
|
|
/*
|
|
* void switchPlayer() { if (p != 1) p = 1; else p = 1; }
|
|
*/
|
|
} |