/* 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 . */ package de.itsblue.ConnectFour; import javax.swing.*; import java.awt.*; public class GameBoard extends JPanel { /** * */ private static final long serialVersionUID = 1L; /** * 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.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); this.checkContainerForWin(column, i); 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]); } } } public int getSuggestedContainerSize(Dimension parentSize) { int containerSize; if (parentSize.getWidth() / this.BoardColumns > parentSize.getHeight() / this.BoardRows) containerSize = (int) parentSize.getHeight() / this.BoardRows; else containerSize = (int) parentSize.getWidth() / this.BoardColumns; return containerSize; } /** * Override paint function to rescale all caontainers on every repaint */ @Override public void paint(Graphics g) { System.out.println("updating sizes"); for (PlateContainer[] plateContainers : BoardContainers) { for (PlateContainer plateContainer : plateContainers) { plateContainer.setPreferredSize(new Dimension(this.getSuggestedContainerSize(this.getSize()), this.getSuggestedContainerSize(this.getSize()))); } } super.paint(g); } @Override public void setPreferredSize(Dimension preferredSize) { Dimension newSize = new Dimension(this.getSuggestedContainerSize(preferredSize) * this.BoardColumns, this.getSuggestedContainerSize(preferredSize) * this.BoardRows); super.setPreferredSize(newSize); } 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; } /* * 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; } */ }