- added plate container
- changed approach of plate
This commit is contained in:
parent
5b09d30d9a
commit
6f4d3eb6ef
4 changed files with 243 additions and 70 deletions
|
@ -19,8 +19,8 @@
|
|||
package de.itsblue.ConnectFour;
|
||||
|
||||
|
||||
import java.awt.Dimension;
|
||||
import javax.swing.JFrame;
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
import de.itsblue.ConnectFour.GameBoard;
|
||||
import de.itsblue.ConnectFour.Plate.PlateType;
|
||||
|
@ -47,11 +47,17 @@ public class ConnectFour extends JFrame {
|
|||
|
||||
this.setPreferredSize(new Dimension(600, 600));
|
||||
|
||||
this.getContentPane().setLayout(new GridBagLayout());
|
||||
|
||||
GameBoard board = new GameBoard();
|
||||
board.setPreferredSize(new Dimension(600,600));
|
||||
//board.setPreferredSize(new Dimension(600,600));
|
||||
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
|
||||
this.pack();
|
||||
|
@ -60,7 +66,5 @@ public class ConnectFour extends JFrame {
|
|||
|
||||
public static void main(final String[] args) {
|
||||
System.out.println(new ConnectFour());
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -21,7 +21,6 @@ package de.itsblue.ConnectFour;
|
|||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
|
||||
public class GameBoard extends JPanel {
|
||||
|
||||
/**
|
||||
|
@ -29,21 +28,49 @@ public class GameBoard extends JPanel {
|
|||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public int BoardRows = 6;
|
||||
public int BoardColumns = 7;
|
||||
JPanel[][] BoardContainers = new JPanel[BoardColumns][BoardRows];
|
||||
private boolean isInited = false;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
// check if the column is out of range
|
||||
if(column > BoardColumns-1)
|
||||
if (column > BoardColumns - 1)
|
||||
return false;
|
||||
|
||||
|
||||
// search for an empty row
|
||||
for(int i = BoardRows-1; i >= 0; i--) {
|
||||
if(this.BoardContainers[column][i].getComponents().length == 0) {
|
||||
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].add(plate);
|
||||
this.BoardContainers[column][i].insertPlate(plate);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -51,68 +78,85 @@ public class GameBoard extends JPanel {
|
|||
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
|
||||
this.setLayout(new GridLayout(this.BoardColumns,this.BoardRows+1));
|
||||
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 JPanel(new GridBagLayout());
|
||||
this.BoardContainers[j][i].setPreferredSize(new Dimension(20, 20));
|
||||
this.BoardContainers[j][i] = new PlateContainer();
|
||||
|
||||
// add the container
|
||||
this.add(this.BoardContainers[j][i]);
|
||||
}
|
||||
}
|
||||
|
||||
this.isInited = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
|
||||
System.out.println("updating sizes");
|
||||
int containerSize;
|
||||
|
||||
/*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;
|
||||
if (this.getSize().getWidth() / this.BoardColumns > this.getSize().getHeight() / this.BoardRows)
|
||||
containerSize = (int) this.getSize().getHeight() / this.BoardRows;
|
||||
else
|
||||
p = 1;
|
||||
}*/
|
||||
containerSize = (int) this.getSize().getWidth() / this.BoardColumns;
|
||||
|
||||
GameBoard() {
|
||||
this.initBoard();
|
||||
containerSize = 30;
|
||||
|
||||
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; }
|
||||
*/
|
||||
}
|
|
@ -18,38 +18,50 @@
|
|||
|
||||
package de.itsblue.ConnectFour;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.*;
|
||||
|
||||
public class Plate extends JPanel {
|
||||
public class Plate {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Enum containing all plate types
|
||||
*/
|
||||
enum PlateType {
|
||||
X, O
|
||||
}
|
||||
|
||||
/**
|
||||
* The tpe of the plate object
|
||||
*/
|
||||
private PlateType type;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param type type of the plate
|
||||
*/
|
||||
Plate(PlateType 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() {
|
||||
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) {
|
||||
this.type = type;
|
||||
}
|
||||
|
|
113
src/de/itsblue/ConnectFour/PlateContainer.java
Normal file
113
src/de/itsblue/ConnectFour/PlateContainer.java
Normal 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));
|
||||
}
|
||||
}
|
Reference in a new issue