- 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;
|
import de.itsblue.ConnectFour.Plate.PlateType;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GameBoard is a fully usable connect4 game board. It can take plates and
|
* 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.
|
* 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
|
* The rows of the board
|
||||||
*/
|
*/
|
||||||
private int BoardRows = 6;
|
private int boardRows = 6;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The columns of the board
|
* The columns of the board
|
||||||
*/
|
*/
|
||||||
private int BoardColumns = 7;
|
private int boardColumns = 7;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Array containing all plate containers
|
* 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
|
* If set to true, the board will not accept any insertions. Will be set to
|
||||||
|
@ -64,10 +63,17 @@ public class GameBoard extends JPanel {
|
||||||
*/
|
*/
|
||||||
GameBoard() {
|
GameBoard() {
|
||||||
this.initBoard();
|
this.initBoard();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
GameBoard(int rows, int colums) {
|
||||||
|
this.boardRows = rows;
|
||||||
|
this.boardColumns = colums;
|
||||||
|
this.initBoard();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to insert a plate into a specific column
|
* 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) {
|
public String insertPlate(Plate plate, int column) {
|
||||||
|
|
||||||
// check if the column is out of range
|
// check if the column is out of range
|
||||||
if (column > BoardColumns - 1 || this.boardLocked)
|
if (column > boardColumns - 1 || this.boardLocked)
|
||||||
return "err";
|
return "err";
|
||||||
|
|
||||||
// search for an empty row
|
// 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 (!this.BoardContainers[column][i].containsPlate()) {
|
||||||
// if the container is empty -> add the plate
|
// if the container is empty -> add the plate
|
||||||
this.BoardContainers[column][i].insertPlate(plate);
|
this.BoardContainers[column][i].insertPlate(plate);
|
||||||
|
@ -105,8 +111,8 @@ public class GameBoard extends JPanel {
|
||||||
*/
|
*/
|
||||||
public void clearBoard() {
|
public void clearBoard() {
|
||||||
// search for an empty row
|
// search for an empty row
|
||||||
for (int c = 0; c < BoardColumns; c++) {
|
for (int c = 0; c < boardColumns; c++) {
|
||||||
for (int r = 0; r < BoardRows; r++) {
|
for (int r = 0; r < boardRows; r++) {
|
||||||
// create the container
|
// create the container
|
||||||
this.BoardContainers[c][r].removePlate();
|
this.BoardContainers[c][r].removePlate();
|
||||||
}
|
}
|
||||||
|
@ -119,14 +125,13 @@ public class GameBoard extends JPanel {
|
||||||
* Function to fill the board with containers
|
* Function to fill the board with containers
|
||||||
*/
|
*/
|
||||||
private void initBoard() {
|
private void initBoard() {
|
||||||
|
|
||||||
|
|
||||||
// configure the main layout
|
// 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
|
// fill the grid with containers
|
||||||
for (int i = 0; i < BoardRows; i++) {
|
for (int i = 0; i < boardRows; i++) {
|
||||||
for (int j = 0; j < BoardColumns; j++) {
|
for (int j = 0; j < boardColumns; j++) {
|
||||||
// create the container
|
// create the container
|
||||||
this.BoardContainers[j][i] = new PlateContainer();
|
this.BoardContainers[j][i] = new PlateContainer();
|
||||||
|
|
||||||
|
@ -134,11 +139,8 @@ public class GameBoard extends JPanel {
|
||||||
this.add(this.BoardContainers[j][i]);
|
this.add(this.BoardContainers[j][i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to calculate a size for the containers to fit a given dimension
|
* 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) {
|
private int getSuggestedContainerSize(Dimension parentSize) {
|
||||||
int containerSize;
|
int containerSize;
|
||||||
|
|
||||||
if (parentSize.getWidth() / this.BoardColumns > parentSize.getHeight() / this.BoardRows)
|
if (parentSize.getWidth() / this.boardColumns > parentSize.getHeight() / this.boardRows)
|
||||||
containerSize = (int) parentSize.getHeight() / this.BoardRows;
|
containerSize = (int) parentSize.getHeight() / this.boardRows;
|
||||||
else
|
else
|
||||||
containerSize = (int) parentSize.getWidth() / this.BoardColumns;
|
containerSize = (int) parentSize.getWidth() / this.boardColumns;
|
||||||
|
|
||||||
return containerSize;
|
return containerSize;
|
||||||
}
|
}
|
||||||
|
@ -235,46 +237,46 @@ public class GameBoard extends JPanel {
|
||||||
int checkR = 0;
|
int checkR = 0;
|
||||||
|
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case 0:
|
case 0:
|
||||||
// check top
|
// check top
|
||||||
checkC = c;
|
checkC = c;
|
||||||
checkR = r + i;
|
checkR = r + i;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
// check top right vert
|
// check top right vert
|
||||||
checkC = c + i;
|
checkC = c + i;
|
||||||
checkR = r + i;
|
checkR = r + i;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
// check right
|
// check right
|
||||||
checkC = c + i;
|
checkC = c + i;
|
||||||
checkR = r;
|
checkR = r;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
// check bottom right vert
|
// check bottom right vert
|
||||||
checkC = c + i;
|
checkC = c + i;
|
||||||
checkR = r - i;
|
checkR = r - i;
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
// check bottom
|
// check bottom
|
||||||
checkC = c;
|
checkC = c;
|
||||||
checkR = r - i;
|
checkR = r - i;
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
// check bottom left vert
|
// check bottom left vert
|
||||||
checkC = c - i;
|
checkC = c - i;
|
||||||
checkR = r - i;
|
checkR = r - i;
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
// check left
|
// check left
|
||||||
checkC = c - i;
|
checkC = c - i;
|
||||||
checkR = r;
|
checkR = r;
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
// check top left vert
|
// check top left vert
|
||||||
checkC = c - i;
|
checkC = c - i;
|
||||||
checkR = r + i;
|
checkR = r + i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
containers[i] = this.getPlateContainer(checkC, checkR);
|
containers[i] = this.getPlateContainer(checkC, checkR);
|
||||||
|
@ -283,6 +285,24 @@ public class GameBoard extends JPanel {
|
||||||
return containers;
|
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
|
* Override paint function to rescale all containers on every repaint
|
||||||
*/
|
*/
|
||||||
|
@ -305,8 +325,8 @@ public class GameBoard extends JPanel {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setPreferredSize(Dimension preferredSize) {
|
public void setPreferredSize(Dimension preferredSize) {
|
||||||
Dimension newSize = new Dimension(this.getSuggestedContainerSize(preferredSize) * this.BoardColumns,
|
Dimension newSize = new Dimension(this.getSuggestedContainerSize(preferredSize) * this.boardColumns,
|
||||||
this.getSuggestedContainerSize(preferredSize) * this.BoardRows);
|
this.getSuggestedContainerSize(preferredSize) * this.boardRows);
|
||||||
super.setPreferredSize(newSize);
|
super.setPreferredSize(newSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue