added a method to insert plates into a specific column

This commit is contained in:
Dorian Zedler 2020-02-13 19:24:32 +01:00
parent 4b1b5c0f92
commit 82a7e690cd
2 changed files with 43 additions and 47 deletions

View file

@ -5,6 +5,7 @@ import java.awt.Dimension;
import javax.swing.JFrame;
import de.itsblue.ConnectFour.GameBoard;
import de.itsblue.ConnectFour.Plate.PlateType;
public class ConnectFour extends JFrame {
/**
@ -29,9 +30,11 @@ public class ConnectFour extends JFrame {
this.setPreferredSize(new Dimension(600, 600));
GameBoard board = new GameBoard();
board.setPreferredSize(new Dimension(600,600));
this.add(board);
board.insertPlate(new Plate(PlateType.O), 0);
// finish up
this.pack();
this.setVisible(true);

View file

@ -11,56 +11,48 @@ public class GameBoard extends JPanel {
*/
private static final long serialVersionUID = 1L;
public int r = 6;
public int c = 7;
GridLayout BoardLayout = new GridLayout(c,r+1);
JPanel[][] BoardContainers = new JPanel[c][r];
int[][] GameBoard = new int[c][r];
int[] filllevel = new int[r];
int p; // Player
public int BoardRows = 6;
public int BoardColumns = 7;
JPanel[][] BoardContainers = new JPanel[BoardColumns][BoardRows];
boolean finish;
public boolean insertPlate(Plate plate, int column) {
public void addComponentsToPane() {
final JPanel field = this;
// check if the column is out of range
if(column > BoardColumns-1)
return false;
field.setLayout(BoardLayout);
// Set up components preferred size
JButton b = new JButton(" ");
Dimension buttonSize = b.getPreferredSize();
field.setPreferredSize(new Dimension((int) (buttonSize.getWidth() * 2.5), (int) (buttonSize.getHeight() * 20)));
// Add buttons to experiment with Grid Layout
// field.add(new JButton("Button 2"));
for (int i = 1; i < 8; i++) {
field.add(new JButton("" + i));
}
for (int i = 0; i < r; i++) {
// // first column
for (int j = 0; j < c; j++) {
// field.add(new Plate(Plate.PlateType.X));
this.BoardContainers[j][i] = new JPanel(new GridBagLayout());
this.BoardContainers[j][i].setPreferredSize(new Dimension(20, 20));
if(j == 0)
this.BoardContainers[j][i].add(new Plate(Plate.PlateType.O));
else
this.BoardContainers[j][i].add(new Plate(Plate.PlateType.X));
this.add(this.BoardContainers[j][i]);
// search for an empty row
for(int i = BoardRows-1; i >= 0; i--) {
if(this.BoardContainers[column][i].getComponents().length == 0) {
// if the container is empty -> add the plate
this.BoardContainers[column][i].add(plate);
return true;
}
}
// Add controls to set up horizontal and vertical gaps
//pane.add(field, BorderLayout.CENTER);
return false;
}
void winningCondition() {
public void initBoard() {
// configure the main layout
this.setLayout(new GridLayout(this.BoardColumns,this.BoardRows+1));
// 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 JPanel(new GridBagLayout());
this.BoardContainers[j][i].setPreferredSize(new Dimension(20, 20));
// add the container
this.add(this.BoardContainers[j][i]);
}
}
}
/*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
@ -88,19 +80,20 @@ public class GameBoard extends JPanel {
&& GameBoard[i + 3][j - 3] == p)
finish = true;
}
}
}*/
void switchPlayer() {
/*void switchPlayer() {
if (p != 1)
p = 1;
else
p = 1;
}
}*/
GameBoard() {
this.addComponentsToPane();
this.initBoard();
}