From fe3619d3e299b72a122e7f9d2dc398ffee1deb73 Mon Sep 17 00:00:00 2001 From: oliver Date: Tue, 11 Feb 2020 21:46:24 +0100 Subject: [PATCH] added simple gridlayout and added winning Conditions --- src/de/itsblue/ConnectFour/ConnectFour.java | 108 ++++++++++++++++++-- src/de/itsblue/ConnectFour/Plate.java | 7 +- 2 files changed, 103 insertions(+), 12 deletions(-) diff --git a/src/de/itsblue/ConnectFour/ConnectFour.java b/src/de/itsblue/ConnectFour/ConnectFour.java index 7680f2d..c353ea3 100644 --- a/src/de/itsblue/ConnectFour/ConnectFour.java +++ b/src/de/itsblue/ConnectFour/ConnectFour.java @@ -1,26 +1,115 @@ package de.itsblue.ConnectFour; +import javax.swing.*; +import java.awt.*; + import java.awt.Dimension; -import java.awt.GridBagLayout; import javax.swing.JFrame; +import java.awt.Container; +import java.awt.BorderLayout; + 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; + + // 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); - this.setPreferredSize(new Dimension(600,600)); - - this.setLayout(new GridBagLayout()); - this.add(new Plate(Plate.PlateType.X)); - - this.add(new Plate(Plate.PlateType.O)); + // initialize window + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + addComponentsToPane(this.getContentPane()); + this.setPreferredSize(new Dimension(600, 600)); // finish up this.pack(); @@ -29,5 +118,6 @@ public class ConnectFour extends JFrame { public static void main(final String[] args) { System.out.println(new ConnectFour()); + } } \ No newline at end of file diff --git a/src/de/itsblue/ConnectFour/Plate.java b/src/de/itsblue/ConnectFour/Plate.java index 15b5027..93b8670 100644 --- a/src/de/itsblue/ConnectFour/Plate.java +++ b/src/de/itsblue/ConnectFour/Plate.java @@ -21,13 +21,14 @@ public class Plate extends JPanel { Plate(PlateType type) { this.setType(type); - this.setPreferredSize(new Dimension(10, 10)); - + this.setPreferredSize(new Dimension(20, 20)); + this.setBackground(this.type == PlateType.X ? Color.RED:Color.BLACK); } - public PlateType getType() { + + public PlateType getType() { return type; }