- minor changes
This commit is contained in:
parent
abd2cf6e86
commit
974c8864b6
1 changed files with 83 additions and 63 deletions
|
@ -23,7 +23,6 @@ import java.awt.*;
|
|||
|
||||
import de.itsblue.ConnectFour.Plate.PlateType;
|
||||
|
||||
|
||||
/**
|
||||
* GameBoard is a fully usable connect4 game board. It can take plates and
|
||||
* insert them into a given column and check if somebody won the game.
|
||||
|
@ -41,17 +40,17 @@ public class GameBoard extends JPanel {
|
|||
/**
|
||||
* The rows of the board
|
||||
*/
|
||||
private int BoardRows = 6;
|
||||
private int boardRows = 6;
|
||||
|
||||
/**
|
||||
* The columns of the board
|
||||
*/
|
||||
private int BoardColumns = 7;
|
||||
private int boardColumns = 7;
|
||||
|
||||
/**
|
||||
* Array containing all plate containers
|
||||
*/
|
||||
private PlateContainer[][] BoardContainers = new PlateContainer[BoardColumns][BoardRows];
|
||||
private PlateContainer[][] BoardContainers = new PlateContainer[boardColumns][boardRows];
|
||||
|
||||
/**
|
||||
* If set to true, the board will not accept any insertions. Will be set to
|
||||
|
@ -64,9 +63,16 @@ public class GameBoard extends JPanel {
|
|||
*/
|
||||
GameBoard() {
|
||||
this.initBoard();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
GameBoard(int rows, int colums) {
|
||||
this.boardRows = rows;
|
||||
this.boardColumns = colums;
|
||||
this.initBoard();
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to insert a plate into a specific column
|
||||
|
@ -79,11 +85,11 @@ public class GameBoard extends JPanel {
|
|||
public String insertPlate(Plate plate, int column) {
|
||||
|
||||
// check if the column is out of range
|
||||
if (column > BoardColumns - 1 || this.boardLocked)
|
||||
if (column > boardColumns - 1 || this.boardLocked)
|
||||
return "err";
|
||||
|
||||
// search for an empty row
|
||||
for (int i = BoardRows - 1; i >= 0; i--) {
|
||||
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);
|
||||
|
@ -105,8 +111,8 @@ public class GameBoard extends JPanel {
|
|||
*/
|
||||
public void clearBoard() {
|
||||
// search for an empty row
|
||||
for (int c = 0; c < BoardColumns; c++) {
|
||||
for (int r = 0; r < BoardRows; r++) {
|
||||
for (int c = 0; c < boardColumns; c++) {
|
||||
for (int r = 0; r < boardRows; r++) {
|
||||
// create the container
|
||||
this.BoardContainers[c][r].removePlate();
|
||||
}
|
||||
|
@ -120,13 +126,12 @@ public class GameBoard extends JPanel {
|
|||
*/
|
||||
private void initBoard() {
|
||||
|
||||
|
||||
// configure the main layout
|
||||
this.setLayout(new GridLayout(this.BoardRows, this.BoardColumns));
|
||||
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++) {
|
||||
for (int i = 0; i < boardRows; i++) {
|
||||
for (int j = 0; j < boardColumns; j++) {
|
||||
// create the container
|
||||
this.BoardContainers[j][i] = new PlateContainer();
|
||||
|
||||
|
@ -137,9 +142,6 @@ public class GameBoard extends JPanel {
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Function to calculate a size for the containers to fit a given dimension
|
||||
*
|
||||
|
@ -149,10 +151,10 @@ public class GameBoard extends JPanel {
|
|||
private int getSuggestedContainerSize(Dimension parentSize) {
|
||||
int containerSize;
|
||||
|
||||
if (parentSize.getWidth() / this.BoardColumns > parentSize.getHeight() / this.BoardRows)
|
||||
containerSize = (int) parentSize.getHeight() / this.BoardRows;
|
||||
if (parentSize.getWidth() / this.boardColumns > parentSize.getHeight() / this.boardRows)
|
||||
containerSize = (int) parentSize.getHeight() / this.boardRows;
|
||||
else
|
||||
containerSize = (int) parentSize.getWidth() / this.BoardColumns;
|
||||
containerSize = (int) parentSize.getWidth() / this.boardColumns;
|
||||
|
||||
return containerSize;
|
||||
}
|
||||
|
@ -283,6 +285,24 @@ public class GameBoard extends JPanel {
|
|||
return containers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get the column count
|
||||
*
|
||||
* @return the column count of the board
|
||||
*/
|
||||
public int getColumns() {
|
||||
return this.boardColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to get the row count
|
||||
*
|
||||
* @return the row count of the board
|
||||
*/
|
||||
public int getRows() {
|
||||
return this.boardRows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override paint function to rescale all containers on every repaint
|
||||
*/
|
||||
|
@ -305,8 +325,8 @@ public class GameBoard extends JPanel {
|
|||
*/
|
||||
@Override
|
||||
public void setPreferredSize(Dimension preferredSize) {
|
||||
Dimension newSize = new Dimension(this.getSuggestedContainerSize(preferredSize) * this.BoardColumns,
|
||||
this.getSuggestedContainerSize(preferredSize) * this.BoardRows);
|
||||
Dimension newSize = new Dimension(this.getSuggestedContainerSize(preferredSize) * this.boardColumns,
|
||||
this.getSuggestedContainerSize(preferredSize) * this.boardRows);
|
||||
super.setPreferredSize(newSize);
|
||||
}
|
||||
}
|
Reference in a new issue