- added plate container

- changed approach of plate
This commit is contained in:
Dorian Zedler 2020-02-14 19:21:49 +01:00
parent 5b09d30d9a
commit 6f4d3eb6ef
4 changed files with 243 additions and 70 deletions

View file

@ -19,8 +19,8 @@
package de.itsblue.ConnectFour; package de.itsblue.ConnectFour;
import java.awt.Dimension; import java.awt.*;
import javax.swing.JFrame; import javax.swing.*;
import de.itsblue.ConnectFour.GameBoard; import de.itsblue.ConnectFour.GameBoard;
import de.itsblue.ConnectFour.Plate.PlateType; import de.itsblue.ConnectFour.Plate.PlateType;
@ -47,11 +47,17 @@ public class ConnectFour extends JFrame {
this.setPreferredSize(new Dimension(600, 600)); this.setPreferredSize(new Dimension(600, 600));
this.getContentPane().setLayout(new GridBagLayout());
GameBoard board = new GameBoard(); GameBoard board = new GameBoard();
board.setPreferredSize(new Dimension(600,600)); //board.setPreferredSize(new Dimension(600,600));
this.add(board); this.add(board);
board.insertPlate(new Plate(PlateType.O), 0); for(int i = 0; i < 7; i++) {
board.insertPlate(new Plate(PlateType.X), i);
}
board.insertPlate(new Plate(PlateType.O), 1);
// finish up // finish up
this.pack(); this.pack();
@ -60,7 +66,5 @@ public class ConnectFour extends JFrame {
public static void main(final String[] args) { public static void main(final String[] args) {
System.out.println(new ConnectFour()); System.out.println(new ConnectFour());
} }
} }

View file

@ -21,7 +21,6 @@ package de.itsblue.ConnectFour;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
public class GameBoard extends JPanel { public class GameBoard extends JPanel {
/** /**
@ -29,21 +28,49 @@ public class GameBoard extends JPanel {
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public int BoardRows = 6; private boolean isInited = false;
public int BoardColumns = 7;
JPanel[][] BoardContainers = new JPanel[BoardColumns][BoardRows];
/**
* 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) { public boolean 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) if (column > BoardColumns - 1)
return false; return false;
// 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].getComponents().length == 0) { 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].add(plate); this.BoardContainers[column][i].insertPlate(plate);
return true; return true;
} }
} }
@ -51,68 +78,85 @@ public class GameBoard extends JPanel {
return false; return false;
} }
public void initBoard() { /**
* 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 // configure the main layout
this.setLayout(new GridLayout(this.BoardColumns,this.BoardRows+1)); 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 JPanel(new GridBagLayout()); this.BoardContainers[j][i] = new PlateContainer();
this.BoardContainers[j][i].setPreferredSize(new Dimension(20, 20));
// add the container // add the container
this.add(this.BoardContainers[j][i]); this.add(this.BoardContainers[j][i]);
} }
} }
this.isInited = true;
} }
@Override
public void paint(Graphics g) {
/*void winningCondition() { System.out.println("updating sizes");
for (int i = 0; i <= 2; i++) int containerSize;
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++) if (this.getSize().getWidth() / this.BoardColumns > this.getSize().getHeight() / this.BoardRows)
for (int j = 0; j <= 3; j++) { containerSize = (int) this.getSize().getHeight() / this.BoardRows;
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 else
p = 1; containerSize = (int) this.getSize().getWidth() / this.BoardColumns;
}*/
GameBoard() { containerSize = 30;
this.initBoard();
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; }
*/
} }

View file

@ -18,38 +18,50 @@
package de.itsblue.ConnectFour; package de.itsblue.ConnectFour;
import javax.swing.JPanel; import javax.swing.*;
import java.awt.Color; import java.awt.*;
import java.awt.Dimension;
public class Plate extends JPanel { public class Plate {
/** /**
* *
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/**
* Enum containing all plate types
*/
enum PlateType { enum PlateType {
X, O X, O
} }
/**
* The tpe of the plate object
*/
private PlateType type; private PlateType type;
/**
* Constructor
* @param type type of the plate
*/
Plate(PlateType type) { Plate(PlateType type) {
this.setType(type); this.setType(type);
this.setPreferredSize(new Dimension(20, 20));
this.setBackground(this.type == PlateType.X ? Color.RED:Color.BLACK);
} }
/**
* Function to get the type of the plate
* @return PlateType
*/
public PlateType getType() { public PlateType getType() {
return type; return this.type;
} }
/**
* Function to change the type of the plate
*
* @param type the type to set the plate to
*/
private void setType(PlateType type) { private void setType(PlateType type) {
this.type = type; this.type = type;
} }

View file

@ -0,0 +1,113 @@
/*
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.JPanel;
import de.itsblue.ConnectFour.Plate.PlateType;
import javax.swing.*;
import java.awt.*;
public class PlateContainer extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* holds the current contained plate object
*/
private Plate containedPlate = null;
/**
* Constructor
*/
PlateContainer() {
}
/**
* Function to insert a plate into the container
*
* @param plate the plate object to insert
* @return true if inserted, false if container is alread occupied
*/
public boolean insertPlate(Plate plate) {
if (this.containsPlate())
return false;
System.out.println("adding plate " + plate);
this.containedPlate = plate;
return true;
}
/**
* Function to get the contained plate
*
* @return null or the contained plate
*/
public Plate getContainedPlate() {
return this.containedPlate;
}
/**
* Function to check if the container is occupied
*
* @return true if occupied, false if not
*/
public boolean containsPlate() {
if (this.containedPlate != null)
return true;
else
return false;
}
/**
* Function to clear the container
*
* @return if it was occupied: contained plate, else: null
*/
public Plate removePlate() {
if (!this.containsPlate())
return null;
this.remove(this.getComponents()[0]);
Plate ret = this.containedPlate;
this.containedPlate = null;
return ret;
}
@Override
public void paint(Graphics g) {
g.setColor(Color.lightGray);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
if (this.containsPlate()) {
g.setColor(this.containedPlate.getType() == PlateType.X ? Color.RED : Color.BLACK);
} else
g.setColor(Color.white);
g.fillOval((int) (this.getWidth() * 0.1), (int) (this.getHeight() * 0.1), (int) (this.getWidth() * 0.8),
(int) (this.getHeight() * 0.8));
}
}