moved gamefield and wining conditions to Gameboard

This commit is contained in:
oliver 2020-02-12 15:01:51 +01:00
parent fe3619d3e2
commit e8f533bb99
2 changed files with 103 additions and 80 deletions

View file

@ -9,108 +9,42 @@ import javax.swing.JFrame;
import java.awt.Container;
import java.awt.BorderLayout;
import de.itsblue.ConnectFour.GameBoard;
public class ConnectFour extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
static int r = 6;
static int c = 7;
static int p; // Player
static int[][] Gamefield = new int[c][r];
static int[] filllevel = new int[r];
static boolean finish;
public int r = 6;
public int c = 7;
int[][] Gamefield = new int[c][r];
// start
static GridLayout BoardLayout = new GridLayout(7, 6);
public ConnectFour(final String name) {
super(name);
setResizable(false);
}
public static void addComponentsToPane(final Container pane) {
final JPanel field = new JPanel();
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 = 1; i < c; i++) {
// // first column
field.add(new Plate(Plate.PlateType.O));
for (int j = 0; j < r; j++) {
// field.add(new Plate(Plate.PlateType.X));
if (Gamefield[i][j] == 0) //FIXME
field.add(new Plate(Plate.PlateType.X));
else if (Gamefield[i][j] == 1)
field.add(new Plate(Plate.PlateType.X));
else
field.add(new Plate(Plate.PlateType.O));
}
}
// Add controls to set up horizontal and vertical gaps
pane.add(field, BorderLayout.CENTER);
}
static void winningCondition() {
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 6; j++) {
if (Gamefield[i][j] == p && Gamefield[i + 1][j] == p && Gamefield[i + 2][j] == p
&& Gamefield[i + 3][j] == p) {
finish = true;
}
}
for (int i = 0; i <= 5; i++)
for (int j = 0; j <= 3; j++) {
if (Gamefield[i][j] == p && Gamefield[i][j + 1] == p && Gamefield[i][j + 2] == p
&& Gamefield[i][j + 3] == p)
finish = true;
}
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 3; j++) {
if (Gamefield[i][j] == p && Gamefield[i + 1][j + 1] == p && Gamefield[i + 2][j + 2] == p
&& Gamefield[i + 3][j + 3] == p)
finish = true;
}
for (int i = 0; i < 3; i++)
for (int j = 6; j > 2; j--) {
if (Gamefield[i][j] == p && Gamefield[i + 1][j - 1] == p && Gamefield[i + 2][j - 2] == p
&& Gamefield[i + 3][j - 3] == p)
finish = true;
}
}
static void switchPlayer() {
if (p != 1)
p = 1;
else
p = 1;
}
ConnectFour() {
// Constructor
// initialize window
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
addComponentsToPane(this.getContentPane());
this.setPreferredSize(new Dimension(600, 600));
GameBoard board = new GameBoard();
this.add(board);
// finish up
this.pack();
this.setVisible(true);
@ -118,6 +52,7 @@ public class ConnectFour extends JFrame {
public static void main(final String[] args) {
System.out.println(new ConnectFour());
}
}

View file

@ -1,6 +1,8 @@
package de.itsblue.ConnectFour;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
public class GameBoard extends JPanel {
@ -9,8 +11,94 @@ public class GameBoard extends JPanel {
*/
private static final long serialVersionUID = 1L;
public int r = 6;
public int c = 7;
GridLayout BoardLayout = new GridLayout(7, 6);
int[][] GameBoard = new int[c][r];
int[] filllevel = new int[r];
int p; // Player
boolean finish;
public void addComponentsToPane() {
final JPanel field = this;
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 = 1; i < c; i++) {
// // first column
field.add(new Plate(Plate.PlateType.O));
for (int j = 0; j < r; j++) {
// field.add(new Plate(Plate.PlateType.X));
if (GameBoard[i][j] == 0) // FIXME
field.add(new Plate(Plate.PlateType.X));
else if (GameBoard[i][j] == 1)
field.add(new Plate(Plate.PlateType.X));
else
field.add(new Plate(Plate.PlateType.O));
}
}
// Add controls to set up horizontal and vertical gaps
//pane.add(field, BorderLayout.CENTER);
}
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;
}
GameBoard() {
this.addComponentsToPane();
}
}